From 52bbab7df17b14d82e3cd8f5f0f5e09b2d95c720 Mon Sep 17 00:00:00 2001 From: alexAubin Date: Wed, 1 Feb 2023 19:30:09 +0000 Subject: [PATCH] :art: Format Python code with Black --- moulinette/__init__.py | 1 - moulinette/actionsmap.py | 6 ------ moulinette/authentication.py | 1 - moulinette/core.py | 6 ------ moulinette/interfaces/__init__.py | 4 ---- moulinette/interfaces/api.py | 12 +----------- moulinette/interfaces/cli.py | 6 ------ moulinette/utils/process.py | 2 -- test/autofix_locale_format.py | 2 -- test/conftest.py | 2 -- test/reformat_locales.py | 1 - test/remove_stale_i18n_strings.py | 1 - test/src/authenticators/dummy.py | 4 ---- test/src/authenticators/yoloswag.py | 4 ---- test/test_actionsmap.py | 1 - test/test_filesystem.py | 1 - test/test_i18n_format_consistency.py | 2 -- test/test_i18n_keys.py | 2 -- test/test_process.py | 2 -- test/test_translation_format_consistency.py | 2 -- 20 files changed, 1 insertion(+), 61 deletions(-) diff --git a/moulinette/__init__.py b/moulinette/__init__.py index b4135d9a..6df5abef 100755 --- a/moulinette/__init__.py +++ b/moulinette/__init__.py @@ -39,7 +39,6 @@ class classproperty: class Moulinette: - _interface = None def prompt(*args, **kwargs): diff --git a/moulinette/actionsmap.py b/moulinette/actionsmap.py index 7e51deb0..e5e9547b 100644 --- a/moulinette/actionsmap.py +++ b/moulinette/actionsmap.py @@ -392,7 +392,6 @@ class ActionsMap: """ def __init__(self, actionsmap_yml, top_parser, load_only_category=None): - assert isinstance(top_parser, BaseActionsMapParser), ( "Invalid parser class '%s'" % top_parser.__class__.__name__ ) @@ -408,7 +407,6 @@ class ActionsMap: actionsmap_pkl = f"{actionsmap_yml_dir}/.{actionsmap_yml_file}.{actionsmap_yml_stat.st_size}-{actionsmap_yml_stat.st_mtime}.pkl" def generate_cache(): - logger.debug("generating cache for actions map") # Read actions map from yaml file @@ -464,7 +462,6 @@ class ActionsMap: @cache def get_authenticator(self, auth_method): - if auth_method == "default": auth_method = self.default_authentication @@ -484,7 +481,6 @@ class ActionsMap: return mod.Authenticator() def check_authentication_if_required(self, *args, **kwargs): - auth_method = self.parser.auth_method(*args, **kwargs) if auth_method is None: @@ -627,7 +623,6 @@ class ActionsMap: # category_name is stuff like "user", "domain", "hooks"... # category_values is the values of this category (like actions) for category_name, category_values in actionsmap.items(): - actions = category_values.pop("actions", {}) subcategories = category_values.pop("subcategories", {}) @@ -675,7 +670,6 @@ class ActionsMap: # subcategory_name is like "cert" in "domain cert status" # subcategory_values is the values of this subcategory (like actions) for subcategory_name, subcategory_values in subcategories.items(): - actions = subcategory_values.pop("actions") # Get subcategory parser diff --git a/moulinette/authentication.py b/moulinette/authentication.py index 4db1acc5..bf2f76a3 100644 --- a/moulinette/authentication.py +++ b/moulinette/authentication.py @@ -29,7 +29,6 @@ class BaseAuthenticator: # Each authenticator classes must implement these methods. def authenticate_credentials(self, credentials): - try: # Attempt to authenticate auth_info = self._authenticate_credentials(credentials) or {} diff --git a/moulinette/core.py b/moulinette/core.py index aaf23978..e83f716b 100644 --- a/moulinette/core.py +++ b/moulinette/core.py @@ -116,7 +116,6 @@ class Translator: self.default_locale != self.locale and key in self._translations.get(self.default_locale, {}) ): - try: return self._translations[self.default_locale][key].format( *args, **kwargs @@ -246,7 +245,6 @@ class Moulinette18n: class MoulinetteError(Exception): - http_code = 500 """Moulinette base exception""" @@ -264,12 +262,10 @@ class MoulinetteError(Exception): class MoulinetteValidationError(MoulinetteError): - http_code = 400 class MoulinetteAuthenticationError(MoulinetteError): - http_code = 401 @@ -321,7 +317,6 @@ class MoulinetteLock: logger.debug("acquiring lock...") while True: - lock_pids = self._lock_PIDs() if self._is_son_of(lock_pids): @@ -389,7 +384,6 @@ class MoulinetteLock: raise MoulinetteError("root_required") def _lock_PIDs(self): - if not os.path.isfile(self._lockfile): return [] diff --git a/moulinette/interfaces/__init__.py b/moulinette/interfaces/__init__.py index d816b5c7..54776552 100644 --- a/moulinette/interfaces/__init__.py +++ b/moulinette/interfaces/__init__.py @@ -283,11 +283,9 @@ class ExtendedArgumentParser(argparse.ArgumentParser): # positionals, optionals and user-defined groups for action_group in self._action_groups: - # Dirty hack to separate 'subcommands' # into 'actions' and 'subcategories' if action_group.title == "subcommands": - # Make a copy of the "action group actions"... choice_actions = action_group._group_actions[0]._choices_actions actions_subparser = copy.copy(action_group._group_actions[0]) @@ -387,7 +385,6 @@ class PositionalsFirstHelpFormatter(argparse.HelpFormatter): # wrap the usage parts if it's too long text_width = self._width - self._current_indent if len(prefix) + len(usage) > text_width: - # break usage into wrappable parts part_regexp = r"\(.*?\)+|\[.*?\]+|\S+" opt_usage = format(optionals, groups) @@ -464,7 +461,6 @@ class JSONExtendedEncoder(JSONEncoder): """ def default(self, o): - import pytz # Lazy loading, this takes like 3+ sec on a RPi2 ?! """Return a serializable object""" diff --git a/moulinette/interfaces/api.py b/moulinette/interfaces/api.py index 9007d773..14ba0b54 100644 --- a/moulinette/interfaces/api.py +++ b/moulinette/interfaces/api.py @@ -86,7 +86,6 @@ class APIQueueHandler(logging.Handler): self.actionsmap = None def emit(self, record): - # Prevent triggering this function while moulinette # is being initialized with --debug if not self.actionsmap or len(request.cookies) == 0: @@ -254,7 +253,6 @@ class _ActionsMapPlugin: api = 2 def __init__(self, actionsmap, log_queues={}): - self.actionsmap = actionsmap self.log_queues = log_queues @@ -293,7 +291,7 @@ class _ActionsMapPlugin: ) # Append routes from the actions map - for (m, p) in self.actionsmap.parser.routes: + for m, p in self.actionsmap.parser.routes: app.route(p, method=m, callback=self.process) def apply(self, callback, context): @@ -376,7 +374,6 @@ class _ActionsMapPlugin: # This is called before each time a route is going to be processed def authenticate(self, authenticator): - try: session_infos = authenticator.get_session_cookie() except Exception: @@ -386,7 +383,6 @@ class _ActionsMapPlugin: return session_infos def logout(self): - profile = request.params.get("profile", self.actionsmap.default_authentication) authenticator = self.actionsmap.get_authenticator(profile) @@ -468,7 +464,6 @@ class _ActionsMapPlugin: else: return format_for_response(ret) finally: - # Clean upload directory # FIXME do that in a better way global UPLOAD_DIR @@ -492,7 +487,6 @@ class _ActionsMapPlugin: queue.put(StopIteration) def display(self, message, style="info"): - profile = request.params.get("profile", self.actionsmap.default_authentication) authenticator = self.actionsmap.get_authenticator(profile) s_id = authenticator.get_session_cookie(raise_if_no_session_exists=False)["id"] @@ -517,7 +511,6 @@ class _ActionsMapPlugin: def moulinette_error_to_http_response(error): - content = error.content() if isinstance(content, dict): return HTTPResponse( @@ -634,7 +627,6 @@ class ActionsMapParser(BaseActionsMapParser): return parser def auth_method(self, _, route): - try: # Retrieve the tid for the route _, parser = self._parsers[route] @@ -648,7 +640,6 @@ class ActionsMapParser(BaseActionsMapParser): return parser.authentication def want_to_take_lock(self, _, route): - _, parser = self._parsers[route] return getattr(parser, "want_to_take_lock", True) @@ -717,7 +708,6 @@ class Interface: type = "api" def __init__(self, routes={}, actionsmap=None): - actionsmap = ActionsMap(actionsmap, ActionsMapParser()) # Attempt to retrieve log queues from an APIQueueHandler diff --git a/moulinette/interfaces/cli.py b/moulinette/interfaces/cli.py index 54216894..84e72185 100644 --- a/moulinette/interfaces/cli.py +++ b/moulinette/interfaces/cli.py @@ -291,7 +291,6 @@ class ActionsMapParser(BaseActionsMapParser): def __init__( self, parent=None, parser=None, subparser_kwargs=None, top_parser=None ): - super(ActionsMapParser, self).__init__(parent) if subparser_kwargs is None: @@ -397,7 +396,6 @@ class ActionsMapParser(BaseActionsMapParser): ) def auth_method(self, args): - ret = self.parse_args(args) tid = getattr(ret, "_tid", []) @@ -431,7 +429,6 @@ class ActionsMapParser(BaseActionsMapParser): raise MoulinetteValidationError(error_message, raw_msg=True) def want_to_take_lock(self, args): - ret = self.parse_args(args) tid = getattr(ret, "_tid", []) if len(tid) == 3: @@ -469,7 +466,6 @@ class Interface: actionsmap=None, locales_dir=None, ): - # Set user locale m18n.set_locale(get_locale()) @@ -557,9 +553,7 @@ class Interface: ) def _prompt(message): - if not is_multiline: - import prompt_toolkit from prompt_toolkit.completion import WordCompleter from prompt_toolkit.styles import Style diff --git a/moulinette/utils/process.py b/moulinette/utils/process.py index e4f2cf31..99945428 100644 --- a/moulinette/utils/process.py +++ b/moulinette/utils/process.py @@ -80,7 +80,6 @@ def call_async_output(args, callback, **kwargs): p = subprocess.Popen(args, **kwargs) while p.poll() is None: - while True: try: callback, message = log_queue.get(True, 1) @@ -201,7 +200,6 @@ def run_commands(cmds, callback=None, separate_stderr=False, shell=True, **kwarg # Iterate over commands error = 0 for cmd in cmds: - process = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=_stderr, shell=shell, **kwargs ) diff --git a/test/autofix_locale_format.py b/test/autofix_locale_format.py index dd781263..ad21fea0 100644 --- a/test/autofix_locale_format.py +++ b/test/autofix_locale_format.py @@ -12,13 +12,11 @@ reference = json.loads(open(locale_folder + "en.json").read()) def fix_locale(locale_file): - this_locale = json.loads(open(locale_folder + locale_file).read()) fixed_stuff = False # We iterate over all keys/string in en.json for key, string in reference.items(): - # Ignore check if there's no translation yet for this key if key not in this_locale: continue diff --git a/test/conftest.py b/test/conftest.py index 45147ebb..50b73580 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -76,7 +76,6 @@ def patch_lock(moulinette): @pytest.fixture(scope="session", autouse=True) def moulinette(tmp_path_factory): - import moulinette import moulinette.core from moulinette.utils.log import configure_logging @@ -104,7 +103,6 @@ def moulinette(tmp_path_factory): @pytest.fixture def moulinette_webapi(moulinette): - from webtest import TestApp from webtest.app import CookiePolicy diff --git a/test/reformat_locales.py b/test/reformat_locales.py index 9119c728..1807e756 100644 --- a/test/reformat_locales.py +++ b/test/reformat_locales.py @@ -2,7 +2,6 @@ import re def reformat(lang, transformations): - locale = open(f"locales/{lang}.json").read() for pattern, replace in transformations.items(): locale = re.compile(pattern).sub(replace, locale) diff --git a/test/remove_stale_i18n_strings.py b/test/remove_stale_i18n_strings.py index d3a4fe2a..8c680aa2 100644 --- a/test/remove_stale_i18n_strings.py +++ b/test/remove_stale_i18n_strings.py @@ -10,7 +10,6 @@ locale_files.remove("en.json") reference = json.loads(open(locale_folder + "en.json").read()) for locale_file in locale_files: - print(locale_file) this_locale = json.loads( open(locale_folder + locale_file).read(), object_pairs_hook=OrderedDict diff --git a/test/src/authenticators/dummy.py b/test/src/authenticators/dummy.py index fde2a0c6..1d11a609 100644 --- a/test/src/authenticators/dummy.py +++ b/test/src/authenticators/dummy.py @@ -22,14 +22,12 @@ class Authenticator(BaseAuthenticator): pass def _authenticate_credentials(self, credentials=None): - if not credentials == self.name: raise MoulinetteError("invalid_password", raw_msg=True) return def set_session_cookie(self, infos): - from bottle import response assert isinstance(infos, dict) @@ -49,7 +47,6 @@ class Authenticator(BaseAuthenticator): ) def get_session_cookie(self, raise_if_no_session_exists=True): - from bottle import request try: @@ -68,7 +65,6 @@ class Authenticator(BaseAuthenticator): return infos def delete_session_cookie(self): - from bottle import response response.set_cookie("moulitest", "", max_age=-1) diff --git a/test/src/authenticators/yoloswag.py b/test/src/authenticators/yoloswag.py index 40dffd12..7dc41fdc 100644 --- a/test/src/authenticators/yoloswag.py +++ b/test/src/authenticators/yoloswag.py @@ -22,14 +22,12 @@ class Authenticator(BaseAuthenticator): pass def _authenticate_credentials(self, credentials=None): - if not credentials == self.name: raise MoulinetteError("invalid_password", raw_msg=True) return def set_session_cookie(self, infos): - from bottle import response assert isinstance(infos, dict) @@ -49,7 +47,6 @@ class Authenticator(BaseAuthenticator): ) def get_session_cookie(self, raise_if_no_session_exists=True): - from bottle import request try: @@ -68,7 +65,6 @@ class Authenticator(BaseAuthenticator): return infos def delete_session_cookie(self): - from bottle import response response.set_cookie("moulitest", "", max_age=-1) diff --git a/test/test_actionsmap.py b/test/test_actionsmap.py index eda49198..9fb6108a 100644 --- a/test/test_actionsmap.py +++ b/test/test_actionsmap.py @@ -161,7 +161,6 @@ def test_required_paremeter_missing_value(iface, caplog): def test_actions_map_unknown_authenticator(monkeypatch, tmp_path): - from moulinette.interfaces.api import ActionsMapParser amap = ActionsMap("test/actionsmap/moulitest.yml", ActionsMapParser()) diff --git a/test/test_filesystem.py b/test/test_filesystem.py index 87a940d2..1949c185 100644 --- a/test/test_filesystem.py +++ b/test/test_filesystem.py @@ -330,7 +330,6 @@ def test_mkdir(tmp_path): def test_mkdir_with_permission(tmp_path, mocker): - # This test only make sense when not being root if os.getuid() == 0: return diff --git a/test/test_i18n_format_consistency.py b/test/test_i18n_format_consistency.py index bfd0e3ae..45d2c7f6 100644 --- a/test/test_i18n_format_consistency.py +++ b/test/test_i18n_format_consistency.py @@ -13,12 +13,10 @@ reference = json.loads(open(locale_folder + "en.json").read()) def find_inconsistencies(locale_file): - this_locale = json.loads(open(locale_folder + locale_file).read()) # We iterate over all keys/string in en.json for key, string in reference.items(): - # Ignore check if there's no translation yet for this key if key not in this_locale: continue diff --git a/test/test_i18n_keys.py b/test/test_i18n_keys.py index 912cf448..d3cca7ce 100644 --- a/test/test_i18n_keys.py +++ b/test/test_i18n_keys.py @@ -11,7 +11,6 @@ import json def find_expected_string_keys(): - # Try to find : # m18n.g( "foo" # MoulinetteError("foo" @@ -69,7 +68,6 @@ def test_undefined_i18n_keys(): def test_unused_i18n_keys(): - unused_keys = keys_defined.difference(expected_string_keys) unused_keys = sorted(unused_keys) diff --git a/test/test_process.py b/test/test_process.py index 2484101a..5a241698 100644 --- a/test/test_process.py +++ b/test/test_process.py @@ -66,7 +66,6 @@ def test_run_shell_kwargs(): def test_call_async_output(test_file): - mock_callback_stdout = mock.Mock() mock_callback_stderr = mock.Mock() @@ -118,7 +117,6 @@ def test_call_async_output(test_file): def test_call_async_output_kwargs(test_file, mocker): - mock_callback_stdout = mock.Mock() mock_callback_stdinfo = mock.Mock() mock_callback_stderr = mock.Mock() diff --git a/test/test_translation_format_consistency.py b/test/test_translation_format_consistency.py index bfd0e3ae..45d2c7f6 100644 --- a/test/test_translation_format_consistency.py +++ b/test/test_translation_format_consistency.py @@ -13,12 +13,10 @@ reference = json.loads(open(locale_folder + "en.json").read()) def find_inconsistencies(locale_file): - this_locale = json.loads(open(locale_folder + locale_file).read()) # We iterate over all keys/string in en.json for key, string in reference.items(): - # Ignore check if there's no translation yet for this key if key not in this_locale: continue