From e0d64a0e2c9bbf2a1530ad9516c4a77e8e26d947 Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Thu, 20 May 2021 17:04:52 +0200 Subject: [PATCH] [pylint] searx/utils.py - activated pylint activated pylint and fix messages from pylint:: searx/utils.py:44:0: R0903: Too few public methods (0/2) (too-few-public-methods) searx/utils.py:58:0: C0103: Argument name "os" doesn't conform to '(([a-z][a-zA-Z0-9_]{2,30})|(_[a-z0-9_]*))$' pattern (invalid-name) searx/utils.py:151:4: R1705: Unnecessary "elif" after "return" (no-else-return) searx/utils.py:263:0: C0103: Argument name "d" doesn't conform to '(([a-z][a-zA-Z0-9_]{2,30})|(_[a-z0-9_]*))$' pattern (invalid-name) searx/utils.py:322:4: R1705: Unnecessary "else" after "return" (no-else-return) searx/utils.py:361:4: R1705: Unnecessary "else" after "return" (no-else-return) searx/utils.py:378:12: C0103: Variable name "lc" doesn't conform to '(([a-z][a-zA-Z0-9_]{2,30})|(_[a-z0-9_]*)|([a-z]))$' pattern (invalid-name) searx/utils.py:452:0: R1710: Either all return statements in a function should return an expression, or none of them should. (inconsistent-return-statements) searx/utils.py:464:0: C0103: Argument name "s" doesn't conform to '(([a-z][a-zA-Z0-9_]{2,30})|(_[a-z0-9_]*))$' pattern (invalid-name) searx/utils.py:489:4: C0103: Function name "f" doesn't conform to '(([a-z][a-zA-Z0-9_]{2,30})|(_[a-z0-9_]*))$' pattern (invalid-name) searx/utils.py:610:7: R1716: Simplify chained comparison between the operands (chained-comparison) Signed-off-by: Markus Heiser --- searx/utils.py | 61 +++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/searx/utils.py b/searx/utils.py index 55a386bd5..c28243e37 100644 --- a/searx/utils.py +++ b/searx/utils.py @@ -1,4 +1,11 @@ # -*- coding: utf-8 -*- +# SPDX-License-Identifier: AGPL-3.0-or-later +# lint: pylint +# pylint: disable=missing-class-docstring, missing-function-docstring +"""Common utilities + +""" + import sys import re import importlib @@ -33,13 +40,7 @@ ecma_unescape2_re = re.compile(r'%([0-9a-fA-F]{2})', re.UNICODE) xpath_cache = dict() lang_to_lc_cache = dict() - -class NotSetClass: - pass - - -NOTSET = NotSetClass() - +NOTSET = object() def searx_useragent(): """Return the searx User Agent""" @@ -48,7 +49,7 @@ def searx_useragent(): suffix=settings['outgoing'].get('useragent_suffix', '')).strip() -def gen_useragent(os=None): +def gen_useragent(os=None): # pylint: disable=invalid-name """Return a random browser User Agent See searx/data/useragents.json @@ -147,21 +148,25 @@ def extract_text(xpath_results, allow_none=False): for e in xpath_results: result = result + extract_text(e) return result.strip() - elif isinstance(xpath_results, ElementBase): + + if isinstance(xpath_results, ElementBase): # it's a element text = html.tostring( xpath_results, encoding='unicode', method='text', with_tail=False ) text = text.strip().replace('\n', ' ') return ' '.join(text.split()) - elif isinstance(xpath_results, (_ElementStringResult, _ElementUnicodeResult, str, Number, bool)): + + if isinstance(xpath_results, (_ElementStringResult, _ElementUnicodeResult, str, Number, bool)): return str(xpath_results) - elif xpath_results is None and allow_none: + + if xpath_results is None and allow_none: return None - elif xpath_results is None and not allow_none: + + if xpath_results is None and not allow_none: raise ValueError('extract_text(None, allow_none=False)') - else: - raise ValueError('unsupported type') + + raise ValueError('unsupported type') def normalize_url(url, base_url): @@ -253,7 +258,7 @@ def extract_url(xpath_results, base_url): return normalize_url(url, base_url) -def dict_subset(d, properties): +def dict_subset(d, properties): # pylint: disable=invalid-name """Extract a subset of a dict Examples: @@ -314,8 +319,8 @@ def convert_str_to_int(number_str): """Convert number_str to int or 0 if number_str is not a number.""" if number_str.isdigit(): return int(number_str) - else: - return 0 + + return 0 def int_or_zero(num): @@ -356,11 +361,11 @@ def is_valid_lang(lang): if l[0][:2] == lang: return (True, l[0][:2], l[3].lower()) return False - else: - for l in language_codes: - if l[1].lower() == lang or l[3].lower() == lang: - return (True, l[0][:2], l[3].lower()) - return False + + for l in language_codes: + if l[1].lower() == lang or l[3].lower() == lang: + return (True, l[0][:2], l[3].lower()) + return False def _get_lang_to_lc_dict(lang_list): @@ -368,7 +373,7 @@ def _get_lang_to_lc_dict(lang_list): value = lang_to_lc_cache.get(key, None) if value is None: value = dict() - for lc in lang_list: + for lc in lang_list: # pylint: disable=invalid-name value.setdefault(lc.split('-')[0], lc) lang_to_lc_cache[key] = value return value @@ -452,9 +457,9 @@ def to_string(obj): return obj.__str__() if hasattr(obj, '__repr__'): return obj.__repr__() + return None - -def ecma_unescape(s): +def ecma_unescape(s): # pylint: disable=invalid-name """Python implementation of the unescape javascript function https://www.ecma-international.org/ecma-262/6.0/#sec-unescape-string @@ -479,10 +484,10 @@ def get_string_replaces_function(replaces): rep = {re.escape(k): v for k, v in replaces.items()} pattern = re.compile("|".join(rep.keys())) - def f(text): + def func(text): return pattern.sub(lambda m: rep[re.escape(m.group(0))], text) - return f + return func def get_engine_from_settings(name): @@ -600,7 +605,7 @@ def eval_xpath_getindex(elements, xpath_spec, index, default=NOTSET): * result (bool, float, list, str): Results. """ result = eval_xpath_list(elements, xpath_spec) - if index >= -len(result) and index < len(result): + if index >= -len(result) and index < len(result): # pylint: disable=chained-comparison return result[index] if default == NOTSET: # raise an SearxEngineXPathException instead of IndexError