Commit 6e676cc0 authored by Nicolas Gelot's avatar Nicolas Gelot

Introduce flake8 check for main module

parent a9e2a3a0
...@@ -38,6 +38,7 @@ pep8_check() { ...@@ -38,6 +38,7 @@ pep8_check() {
# W503 line break before binary operator # W503 line break before binary operator
# E722 do not use bare 'except' # E722 do not use bare 'except'
pycodestyle --exclude=searx/static --max-line-length=120 --ignore "E402,W503,E722" "$SEARX_DIR" "$BASE_DIR/tests" pycodestyle --exclude=searx/static --max-line-length=120 --ignore "E402,W503,E722" "$SEARX_DIR" "$BASE_DIR/tests"
flake8 --ignore=E722 $SEARX_DIR/*.py
} }
unit_tests() { unit_tests() {
......
babel==2.3.4 babel==2.3.4
mock==2.0.0 mock==2.0.0
pycodestyle==2.4.0 pycodestyle==2.5.0
flake8==3.7.7
mockredispy==2.9.3 mockredispy==2.9.3
pytest==4.1.0 pytest==4.1.0
pytest-cov==2.6.1 pytest-cov==2.6.1
......
...@@ -61,7 +61,7 @@ def response(resp): ...@@ -61,7 +61,7 @@ def response(resp):
content = content_string.format(doi_content="", abstract_content=abstract) content = content_string.format(doi_content="", abstract_content=abstract)
if len(content) > 300: if len(content) > 300:
content = content[0:300] + "..." content = content[0:300] + "..."
# TODO: center snippet on query term # TODO: center snippet on query term
publishedDate = datetime.strptime(entry.xpath('.//published')[0].text, '%Y-%m-%dT%H:%M:%SZ') publishedDate = datetime.strptime(entry.xpath('.//published')[0].text, '%Y-%m-%dT%H:%M:%SZ')
......
...@@ -81,7 +81,7 @@ def response(resp): ...@@ -81,7 +81,7 @@ def response(resp):
pass pass
if len(content) > 300: if len(content) > 300:
content = content[0:300] + "..." content = content[0:300] + "..."
# TODO: center snippet on query term # TODO: center snippet on query term
res_dict = {'url': url, res_dict = {'url': url,
......
...@@ -374,7 +374,7 @@ def add_url(urls, result, property_id=None, default_label=None, url_prefix=None, ...@@ -374,7 +374,7 @@ def add_url(urls, result, property_id=None, default_label=None, url_prefix=None,
# wiki links don't have property in wikidata page # wiki links don't have property in wikidata page
if link_type and 'wiki' in link_type: if link_type and 'wiki' in link_type:
links.append(get_wikilink(result, link_type)) links.append(get_wikilink(result, link_type))
else: else:
dom_element = result.xpath(property_xpath.replace('{propertyid}', property_id)) dom_element = result.xpath(property_xpath.replace('{propertyid}', property_id))
if dom_element: if dom_element:
......
...@@ -22,7 +22,6 @@ from searx.engines import ( ...@@ -22,7 +22,6 @@ from searx.engines import (
categories, engines, engine_shortcuts categories, engines, engine_shortcuts
) )
import re import re
import sys
VALID_LANGUAGE_CODE = re.compile(r'^[a-z]{2,3}(-[a-zA-Z]{2})?$') VALID_LANGUAGE_CODE = re.compile(r'^[a-z]{2,3}(-[a-zA-Z]{2})?$')
...@@ -78,20 +77,21 @@ class RawTextQuery(object): ...@@ -78,20 +77,21 @@ class RawTextQuery(object):
# if correct language-code is found # if correct language-code is found
# set it as new search-language # set it as new search-language
if (lang == lang_id if (
or lang == lang_name lang in [lang_id, lang_name, english_name]
or lang == english_name or lang.replace("-", " ") == country # noqa
or lang.replace('-', ' ') == country)\ ) and lang not in self.languages:
and lang not in self.languages: parse_next = True
parse_next = True lang_parts = lang_id.split("-")
lang_parts = lang_id.split('-') if len(lang_parts) == 2:
if len(lang_parts) == 2: self.languages.append(
self.languages.append(lang_parts[0] + '-' + lang_parts[1].upper()) lang_parts[0] + "-" + lang_parts[1].upper()
else: )
self.languages.append(lang_id) else:
# to ensure best match (first match is not necessarily the best one) self.languages.append(lang_id)
if lang == lang_id: # to ensure best match (first match is not necessarily the best one)
break if lang == lang_id:
break
# user may set a valid, yet not selectable language # user may set a valid, yet not selectable language
if VALID_LANGUAGE_CODE.match(lang): if VALID_LANGUAGE_CODE.match(lang):
......
...@@ -11,7 +11,7 @@ from codecs import getincrementalencoder ...@@ -11,7 +11,7 @@ from codecs import getincrementalencoder
from imp import load_source from imp import load_source
from numbers import Number from numbers import Number
from os.path import splitext, join from os.path import splitext, join
from io import open from pathlib import Path
from random import choice from random import choice
import sys import sys
import json import json
...@@ -19,7 +19,6 @@ import json ...@@ -19,7 +19,6 @@ import json
from searx import settings from searx import settings
from searx.version import VERSION_STRING from searx.version import VERSION_STRING
from searx.languages import language_codes from searx.languages import language_codes
from searx import settings
from searx import logger from searx import logger
from io import StringIO from io import StringIO
...@@ -31,8 +30,10 @@ logger = logger.getChild('utils') ...@@ -31,8 +30,10 @@ logger = logger.getChild('utils')
blocked_tags = ('script', blocked_tags = ('script',
'style') 'style')
useragents = json.loads(open(os.path.dirname(os.path.realpath(__file__)) useragents = json.load(open(
+ "/data/useragents.json", 'r', encoding='utf-8').read()) Path(__file__).parent / "data" / "useragents.json",
encoding='utf-8')
)
def searx_useragent(): def searx_useragent():
...@@ -161,7 +162,7 @@ def get_resources_directory(searx_directory, subdirectory, resources_directory): ...@@ -161,7 +162,7 @@ def get_resources_directory(searx_directory, subdirectory, resources_directory):
if not resources_directory: if not resources_directory:
resources_directory = os.path.join(searx_directory, subdirectory) resources_directory = os.path.join(searx_directory, subdirectory)
if not os.path.isdir(resources_directory): if not os.path.isdir(resources_directory):
raise Exception(directory + " is not a directory") raise Exception(resources_directory + " is not a directory")
return resources_directory return resources_directory
......
...@@ -17,17 +17,10 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >. ...@@ -17,17 +17,10 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >.
(C) 2013- by Adam Tauber, <asciimoo@gmail.com> (C) 2013- by Adam Tauber, <asciimoo@gmail.com>
''' '''
if __name__ == '__main__':
from sys import path
from os.path import realpath, dirname
path.append(realpath(dirname(realpath(__file__)) + '/../'))
import hashlib import hashlib
import hmac import hmac
import json import json
import os import os
import sys
import time import time
import copy import copy
...@@ -35,7 +28,6 @@ import requests ...@@ -35,7 +28,6 @@ import requests
from searx import logger from searx import logger
logger = logger.getChild('webapp')
from pygments import highlight from pygments import highlight
from pygments.lexers import get_lexer_by_name from pygments.lexers import get_lexer_by_name
...@@ -75,11 +67,11 @@ from searx.url_utils import urlencode, urlparse, urljoin ...@@ -75,11 +67,11 @@ from searx.url_utils import urlencode, urlparse, urljoin
from searx.utils import new_hmac from searx.utils import new_hmac
import threading import threading
from io import StringIO
# serve pages with HTTP/1.1 # serve pages with HTTP/1.1
from werkzeug.serving import WSGIRequestHandler from werkzeug.serving import WSGIRequestHandler
logger = logger.getChild('webapp')
WSGIRequestHandler.protocol_version = "HTTP/{}".format(settings['server'].get('http_protocol_version', '1.0')) WSGIRequestHandler.protocol_version = "HTTP/{}".format(settings['server'].get('http_protocol_version', '1.0'))
# about static # about static
...@@ -306,10 +298,11 @@ def render(template_name, override_theme=None, **kwargs): ...@@ -306,10 +298,11 @@ def render(template_name, override_theme=None, **kwargs):
if 'categories' not in kwargs: if 'categories' not in kwargs:
kwargs['categories'] = ['general'] kwargs['categories'] = ['general']
kwargs['categories'].extend(x for x in kwargs["categories"].extend(
sorted(categories.keys()) x
if x != 'general' for x in sorted(categories.keys())
and x in enabled_categories) if x != "general" and x in enabled_categories
)
if 'all_categories' not in kwargs: if 'all_categories' not in kwargs:
kwargs['all_categories'] = ['general'] kwargs['all_categories'] = ['general']
...@@ -415,7 +408,7 @@ def pre_request(): ...@@ -415,7 +408,7 @@ def pre_request():
else: else:
try: try:
preferences.parse_dict(request.form) preferences.parse_dict(request.form)
except Exception as e: except Exception:
logger.exception('invalid settings') logger.exception('invalid settings')
request.errors.append(gettext('Invalid settings')) request.errors.append(gettext('Invalid settings'))
...@@ -431,8 +424,9 @@ def pre_request(): ...@@ -431,8 +424,9 @@ def pre_request():
allowed_plugins = preferences.plugins.get_enabled() allowed_plugins = preferences.plugins.get_enabled()
disabled_plugins = preferences.plugins.get_disabled() disabled_plugins = preferences.plugins.get_disabled()
for plugin in plugins: for plugin in plugins:
if ((plugin.default_on and plugin.id not in disabled_plugins) if (
or plugin.id in allowed_plugins): plugin.default_on and plugin.id not in disabled_plugins
) or plugin.id in allowed_plugins:
request.user_plugins.append(plugin) request.user_plugins.append(plugin)
...@@ -631,7 +625,6 @@ def preferences(): ...@@ -631,7 +625,6 @@ def preferences():
# render preferences # render preferences
image_proxy = request.preferences.get_value('image_proxy') image_proxy = request.preferences.get_value('image_proxy')
lang = request.preferences.get_value('language')
disabled_engines = request.preferences.engines.get_disabled() disabled_engines = request.preferences.engines.get_disabled()
allowed_plugins = request.preferences.plugins.get_enabled() allowed_plugins = request.preferences.plugins.get_enabled()
...@@ -677,11 +670,13 @@ def preferences(): ...@@ -677,11 +670,13 @@ def preferences():
def _is_selected_language_supported(engine, preferences): def _is_selected_language_supported(engine, preferences):
language = preferences.get_value('language') language = preferences.get_value("language")
return (language == 'all' return language == "all" or match_language(
or match_language(language, language,
getattr(engine, 'supported_languages', []), getattr(engine, "supported_languages", []),
getattr(engine, 'language_aliases', {}), None)) getattr(engine, "language_aliases", {}),
None,
)
@app.route('/image_proxy', methods=['GET']) @app.route('/image_proxy', methods=['GET'])
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment