Merge pull request #330 from YunoHost/actions/black

Format Python code with Black
This commit is contained in:
Alexandre Aubin 2023-02-01 20:33:26 +01:00 committed by GitHub
commit 1fb77feec3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 1 additions and 61 deletions

View file

@ -39,7 +39,6 @@ class classproperty:
class Moulinette: class Moulinette:
_interface = None _interface = None
def prompt(*args, **kwargs): def prompt(*args, **kwargs):

View file

@ -392,7 +392,6 @@ class ActionsMap:
""" """
def __init__(self, actionsmap_yml, top_parser, load_only_category=None): def __init__(self, actionsmap_yml, top_parser, load_only_category=None):
assert isinstance(top_parser, BaseActionsMapParser), ( assert isinstance(top_parser, BaseActionsMapParser), (
"Invalid parser class '%s'" % top_parser.__class__.__name__ "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" actionsmap_pkl = f"{actionsmap_yml_dir}/.{actionsmap_yml_file}.{actionsmap_yml_stat.st_size}-{actionsmap_yml_stat.st_mtime}.pkl"
def generate_cache(): def generate_cache():
logger.debug("generating cache for actions map") logger.debug("generating cache for actions map")
# Read actions map from yaml file # Read actions map from yaml file
@ -464,7 +462,6 @@ class ActionsMap:
@cache @cache
def get_authenticator(self, auth_method): def get_authenticator(self, auth_method):
if auth_method == "default": if auth_method == "default":
auth_method = self.default_authentication auth_method = self.default_authentication
@ -484,7 +481,6 @@ class ActionsMap:
return mod.Authenticator() return mod.Authenticator()
def check_authentication_if_required(self, *args, **kwargs): def check_authentication_if_required(self, *args, **kwargs):
auth_method = self.parser.auth_method(*args, **kwargs) auth_method = self.parser.auth_method(*args, **kwargs)
if auth_method is None: if auth_method is None:
@ -627,7 +623,6 @@ class ActionsMap:
# category_name is stuff like "user", "domain", "hooks"... # category_name is stuff like "user", "domain", "hooks"...
# category_values is the values of this category (like actions) # category_values is the values of this category (like actions)
for category_name, category_values in actionsmap.items(): for category_name, category_values in actionsmap.items():
actions = category_values.pop("actions", {}) actions = category_values.pop("actions", {})
subcategories = category_values.pop("subcategories", {}) subcategories = category_values.pop("subcategories", {})
@ -675,7 +670,6 @@ class ActionsMap:
# subcategory_name is like "cert" in "domain cert status" # subcategory_name is like "cert" in "domain cert status"
# subcategory_values is the values of this subcategory (like actions) # subcategory_values is the values of this subcategory (like actions)
for subcategory_name, subcategory_values in subcategories.items(): for subcategory_name, subcategory_values in subcategories.items():
actions = subcategory_values.pop("actions") actions = subcategory_values.pop("actions")
# Get subcategory parser # Get subcategory parser

View file

@ -29,7 +29,6 @@ class BaseAuthenticator:
# Each authenticator classes must implement these methods. # Each authenticator classes must implement these methods.
def authenticate_credentials(self, credentials): def authenticate_credentials(self, credentials):
try: try:
# Attempt to authenticate # Attempt to authenticate
auth_info = self._authenticate_credentials(credentials) or {} auth_info = self._authenticate_credentials(credentials) or {}

View file

@ -116,7 +116,6 @@ class Translator:
self.default_locale != self.locale self.default_locale != self.locale
and key in self._translations.get(self.default_locale, {}) and key in self._translations.get(self.default_locale, {})
): ):
try: try:
return self._translations[self.default_locale][key].format( return self._translations[self.default_locale][key].format(
*args, **kwargs *args, **kwargs
@ -246,7 +245,6 @@ class Moulinette18n:
class MoulinetteError(Exception): class MoulinetteError(Exception):
http_code = 500 http_code = 500
"""Moulinette base exception""" """Moulinette base exception"""
@ -264,12 +262,10 @@ class MoulinetteError(Exception):
class MoulinetteValidationError(MoulinetteError): class MoulinetteValidationError(MoulinetteError):
http_code = 400 http_code = 400
class MoulinetteAuthenticationError(MoulinetteError): class MoulinetteAuthenticationError(MoulinetteError):
http_code = 401 http_code = 401
@ -321,7 +317,6 @@ class MoulinetteLock:
logger.debug("acquiring lock...") logger.debug("acquiring lock...")
while True: while True:
lock_pids = self._lock_PIDs() lock_pids = self._lock_PIDs()
if self._is_son_of(lock_pids): if self._is_son_of(lock_pids):
@ -389,7 +384,6 @@ class MoulinetteLock:
raise MoulinetteError("root_required") raise MoulinetteError("root_required")
def _lock_PIDs(self): def _lock_PIDs(self):
if not os.path.isfile(self._lockfile): if not os.path.isfile(self._lockfile):
return [] return []

View file

@ -283,11 +283,9 @@ class ExtendedArgumentParser(argparse.ArgumentParser):
# positionals, optionals and user-defined groups # positionals, optionals and user-defined groups
for action_group in self._action_groups: for action_group in self._action_groups:
# Dirty hack to separate 'subcommands' # Dirty hack to separate 'subcommands'
# into 'actions' and 'subcategories' # into 'actions' and 'subcategories'
if action_group.title == "subcommands": if action_group.title == "subcommands":
# Make a copy of the "action group actions"... # Make a copy of the "action group actions"...
choice_actions = action_group._group_actions[0]._choices_actions choice_actions = action_group._group_actions[0]._choices_actions
actions_subparser = copy.copy(action_group._group_actions[0]) 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 # wrap the usage parts if it's too long
text_width = self._width - self._current_indent text_width = self._width - self._current_indent
if len(prefix) + len(usage) > text_width: if len(prefix) + len(usage) > text_width:
# break usage into wrappable parts # break usage into wrappable parts
part_regexp = r"\(.*?\)+|\[.*?\]+|\S+" part_regexp = r"\(.*?\)+|\[.*?\]+|\S+"
opt_usage = format(optionals, groups) opt_usage = format(optionals, groups)
@ -464,7 +461,6 @@ class JSONExtendedEncoder(JSONEncoder):
""" """
def default(self, o): def default(self, o):
import pytz # Lazy loading, this takes like 3+ sec on a RPi2 ?! import pytz # Lazy loading, this takes like 3+ sec on a RPi2 ?!
"""Return a serializable object""" """Return a serializable object"""

View file

@ -86,7 +86,6 @@ class APIQueueHandler(logging.Handler):
self.actionsmap = None self.actionsmap = None
def emit(self, record): def emit(self, record):
# Prevent triggering this function while moulinette # Prevent triggering this function while moulinette
# is being initialized with --debug # is being initialized with --debug
if not self.actionsmap or len(request.cookies) == 0: if not self.actionsmap or len(request.cookies) == 0:
@ -254,7 +253,6 @@ class _ActionsMapPlugin:
api = 2 api = 2
def __init__(self, actionsmap, log_queues={}): def __init__(self, actionsmap, log_queues={}):
self.actionsmap = actionsmap self.actionsmap = actionsmap
self.log_queues = log_queues self.log_queues = log_queues
@ -293,7 +291,7 @@ class _ActionsMapPlugin:
) )
# Append routes from the actions map # 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) app.route(p, method=m, callback=self.process)
def apply(self, callback, context): def apply(self, callback, context):
@ -376,7 +374,6 @@ class _ActionsMapPlugin:
# This is called before each time a route is going to be processed # This is called before each time a route is going to be processed
def authenticate(self, authenticator): def authenticate(self, authenticator):
try: try:
session_infos = authenticator.get_session_cookie() session_infos = authenticator.get_session_cookie()
except Exception: except Exception:
@ -386,7 +383,6 @@ class _ActionsMapPlugin:
return session_infos return session_infos
def logout(self): def logout(self):
profile = request.params.get("profile", self.actionsmap.default_authentication) profile = request.params.get("profile", self.actionsmap.default_authentication)
authenticator = self.actionsmap.get_authenticator(profile) authenticator = self.actionsmap.get_authenticator(profile)
@ -468,7 +464,6 @@ class _ActionsMapPlugin:
else: else:
return format_for_response(ret) return format_for_response(ret)
finally: finally:
# Clean upload directory # Clean upload directory
# FIXME do that in a better way # FIXME do that in a better way
global UPLOAD_DIR global UPLOAD_DIR
@ -492,7 +487,6 @@ class _ActionsMapPlugin:
queue.put(StopIteration) queue.put(StopIteration)
def display(self, message, style="info"): def display(self, message, style="info"):
profile = request.params.get("profile", self.actionsmap.default_authentication) profile = request.params.get("profile", self.actionsmap.default_authentication)
authenticator = self.actionsmap.get_authenticator(profile) authenticator = self.actionsmap.get_authenticator(profile)
s_id = authenticator.get_session_cookie(raise_if_no_session_exists=False)["id"] 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): def moulinette_error_to_http_response(error):
content = error.content() content = error.content()
if isinstance(content, dict): if isinstance(content, dict):
return HTTPResponse( return HTTPResponse(
@ -634,7 +627,6 @@ class ActionsMapParser(BaseActionsMapParser):
return parser return parser
def auth_method(self, _, route): def auth_method(self, _, route):
try: try:
# Retrieve the tid for the route # Retrieve the tid for the route
_, parser = self._parsers[route] _, parser = self._parsers[route]
@ -648,7 +640,6 @@ class ActionsMapParser(BaseActionsMapParser):
return parser.authentication return parser.authentication
def want_to_take_lock(self, _, route): def want_to_take_lock(self, _, route):
_, parser = self._parsers[route] _, parser = self._parsers[route]
return getattr(parser, "want_to_take_lock", True) return getattr(parser, "want_to_take_lock", True)
@ -717,7 +708,6 @@ class Interface:
type = "api" type = "api"
def __init__(self, routes={}, actionsmap=None): def __init__(self, routes={}, actionsmap=None):
actionsmap = ActionsMap(actionsmap, ActionsMapParser()) actionsmap = ActionsMap(actionsmap, ActionsMapParser())
# Attempt to retrieve log queues from an APIQueueHandler # Attempt to retrieve log queues from an APIQueueHandler

View file

@ -291,7 +291,6 @@ class ActionsMapParser(BaseActionsMapParser):
def __init__( def __init__(
self, parent=None, parser=None, subparser_kwargs=None, top_parser=None self, parent=None, parser=None, subparser_kwargs=None, top_parser=None
): ):
super(ActionsMapParser, self).__init__(parent) super(ActionsMapParser, self).__init__(parent)
if subparser_kwargs is None: if subparser_kwargs is None:
@ -397,7 +396,6 @@ class ActionsMapParser(BaseActionsMapParser):
) )
def auth_method(self, args): def auth_method(self, args):
ret = self.parse_args(args) ret = self.parse_args(args)
tid = getattr(ret, "_tid", []) tid = getattr(ret, "_tid", [])
@ -431,7 +429,6 @@ class ActionsMapParser(BaseActionsMapParser):
raise MoulinetteValidationError(error_message, raw_msg=True) raise MoulinetteValidationError(error_message, raw_msg=True)
def want_to_take_lock(self, args): def want_to_take_lock(self, args):
ret = self.parse_args(args) ret = self.parse_args(args)
tid = getattr(ret, "_tid", []) tid = getattr(ret, "_tid", [])
if len(tid) == 3: if len(tid) == 3:
@ -469,7 +466,6 @@ class Interface:
actionsmap=None, actionsmap=None,
locales_dir=None, locales_dir=None,
): ):
# Set user locale # Set user locale
m18n.set_locale(get_locale()) m18n.set_locale(get_locale())
@ -557,9 +553,7 @@ class Interface:
) )
def _prompt(message): def _prompt(message):
if not is_multiline: if not is_multiline:
import prompt_toolkit import prompt_toolkit
from prompt_toolkit.completion import WordCompleter from prompt_toolkit.completion import WordCompleter
from prompt_toolkit.styles import Style from prompt_toolkit.styles import Style

View file

@ -80,7 +80,6 @@ def call_async_output(args, callback, **kwargs):
p = subprocess.Popen(args, **kwargs) p = subprocess.Popen(args, **kwargs)
while p.poll() is None: while p.poll() is None:
while True: while True:
try: try:
callback, message = log_queue.get(True, 1) 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 # Iterate over commands
error = 0 error = 0
for cmd in cmds: for cmd in cmds:
process = subprocess.Popen( process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=_stderr, shell=shell, **kwargs cmd, stdout=subprocess.PIPE, stderr=_stderr, shell=shell, **kwargs
) )

View file

@ -12,13 +12,11 @@ reference = json.loads(open(locale_folder + "en.json").read())
def fix_locale(locale_file): def fix_locale(locale_file):
this_locale = json.loads(open(locale_folder + locale_file).read()) this_locale = json.loads(open(locale_folder + locale_file).read())
fixed_stuff = False fixed_stuff = False
# We iterate over all keys/string in en.json # We iterate over all keys/string in en.json
for key, string in reference.items(): for key, string in reference.items():
# Ignore check if there's no translation yet for this key # Ignore check if there's no translation yet for this key
if key not in this_locale: if key not in this_locale:
continue continue

View file

@ -76,7 +76,6 @@ def patch_lock(moulinette):
@pytest.fixture(scope="session", autouse=True) @pytest.fixture(scope="session", autouse=True)
def moulinette(tmp_path_factory): def moulinette(tmp_path_factory):
import moulinette import moulinette
import moulinette.core import moulinette.core
from moulinette.utils.log import configure_logging from moulinette.utils.log import configure_logging
@ -104,7 +103,6 @@ def moulinette(tmp_path_factory):
@pytest.fixture @pytest.fixture
def moulinette_webapi(moulinette): def moulinette_webapi(moulinette):
from webtest import TestApp from webtest import TestApp
from webtest.app import CookiePolicy from webtest.app import CookiePolicy

View file

@ -2,7 +2,6 @@ import re
def reformat(lang, transformations): def reformat(lang, transformations):
locale = open(f"locales/{lang}.json").read() locale = open(f"locales/{lang}.json").read()
for pattern, replace in transformations.items(): for pattern, replace in transformations.items():
locale = re.compile(pattern).sub(replace, locale) locale = re.compile(pattern).sub(replace, locale)

View file

@ -10,7 +10,6 @@ locale_files.remove("en.json")
reference = json.loads(open(locale_folder + "en.json").read()) reference = json.loads(open(locale_folder + "en.json").read())
for locale_file in locale_files: for locale_file in locale_files:
print(locale_file) print(locale_file)
this_locale = json.loads( this_locale = json.loads(
open(locale_folder + locale_file).read(), object_pairs_hook=OrderedDict open(locale_folder + locale_file).read(), object_pairs_hook=OrderedDict

View file

@ -22,14 +22,12 @@ class Authenticator(BaseAuthenticator):
pass pass
def _authenticate_credentials(self, credentials=None): def _authenticate_credentials(self, credentials=None):
if not credentials == self.name: if not credentials == self.name:
raise MoulinetteError("invalid_password", raw_msg=True) raise MoulinetteError("invalid_password", raw_msg=True)
return return
def set_session_cookie(self, infos): def set_session_cookie(self, infos):
from bottle import response from bottle import response
assert isinstance(infos, dict) assert isinstance(infos, dict)
@ -49,7 +47,6 @@ class Authenticator(BaseAuthenticator):
) )
def get_session_cookie(self, raise_if_no_session_exists=True): def get_session_cookie(self, raise_if_no_session_exists=True):
from bottle import request from bottle import request
try: try:
@ -68,7 +65,6 @@ class Authenticator(BaseAuthenticator):
return infos return infos
def delete_session_cookie(self): def delete_session_cookie(self):
from bottle import response from bottle import response
response.set_cookie("moulitest", "", max_age=-1) response.set_cookie("moulitest", "", max_age=-1)

View file

@ -22,14 +22,12 @@ class Authenticator(BaseAuthenticator):
pass pass
def _authenticate_credentials(self, credentials=None): def _authenticate_credentials(self, credentials=None):
if not credentials == self.name: if not credentials == self.name:
raise MoulinetteError("invalid_password", raw_msg=True) raise MoulinetteError("invalid_password", raw_msg=True)
return return
def set_session_cookie(self, infos): def set_session_cookie(self, infos):
from bottle import response from bottle import response
assert isinstance(infos, dict) assert isinstance(infos, dict)
@ -49,7 +47,6 @@ class Authenticator(BaseAuthenticator):
) )
def get_session_cookie(self, raise_if_no_session_exists=True): def get_session_cookie(self, raise_if_no_session_exists=True):
from bottle import request from bottle import request
try: try:
@ -68,7 +65,6 @@ class Authenticator(BaseAuthenticator):
return infos return infos
def delete_session_cookie(self): def delete_session_cookie(self):
from bottle import response from bottle import response
response.set_cookie("moulitest", "", max_age=-1) response.set_cookie("moulitest", "", max_age=-1)

View file

@ -161,7 +161,6 @@ def test_required_paremeter_missing_value(iface, caplog):
def test_actions_map_unknown_authenticator(monkeypatch, tmp_path): def test_actions_map_unknown_authenticator(monkeypatch, tmp_path):
from moulinette.interfaces.api import ActionsMapParser from moulinette.interfaces.api import ActionsMapParser
amap = ActionsMap("test/actionsmap/moulitest.yml", ActionsMapParser()) amap = ActionsMap("test/actionsmap/moulitest.yml", ActionsMapParser())

View file

@ -330,7 +330,6 @@ def test_mkdir(tmp_path):
def test_mkdir_with_permission(tmp_path, mocker): def test_mkdir_with_permission(tmp_path, mocker):
# This test only make sense when not being root # This test only make sense when not being root
if os.getuid() == 0: if os.getuid() == 0:
return return

View file

@ -13,12 +13,10 @@ reference = json.loads(open(locale_folder + "en.json").read())
def find_inconsistencies(locale_file): def find_inconsistencies(locale_file):
this_locale = json.loads(open(locale_folder + locale_file).read()) this_locale = json.loads(open(locale_folder + locale_file).read())
# We iterate over all keys/string in en.json # We iterate over all keys/string in en.json
for key, string in reference.items(): for key, string in reference.items():
# Ignore check if there's no translation yet for this key # Ignore check if there's no translation yet for this key
if key not in this_locale: if key not in this_locale:
continue continue

View file

@ -11,7 +11,6 @@ import json
def find_expected_string_keys(): def find_expected_string_keys():
# Try to find : # Try to find :
# m18n.g( "foo" # m18n.g( "foo"
# MoulinetteError("foo" # MoulinetteError("foo"
@ -69,7 +68,6 @@ def test_undefined_i18n_keys():
def test_unused_i18n_keys(): def test_unused_i18n_keys():
unused_keys = keys_defined.difference(expected_string_keys) unused_keys = keys_defined.difference(expected_string_keys)
unused_keys = sorted(unused_keys) unused_keys = sorted(unused_keys)

View file

@ -66,7 +66,6 @@ def test_run_shell_kwargs():
def test_call_async_output(test_file): def test_call_async_output(test_file):
mock_callback_stdout = mock.Mock() mock_callback_stdout = mock.Mock()
mock_callback_stderr = 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): def test_call_async_output_kwargs(test_file, mocker):
mock_callback_stdout = mock.Mock() mock_callback_stdout = mock.Mock()
mock_callback_stdinfo = mock.Mock() mock_callback_stdinfo = mock.Mock()
mock_callback_stderr = mock.Mock() mock_callback_stderr = mock.Mock()

View file

@ -13,12 +13,10 @@ reference = json.loads(open(locale_folder + "en.json").read())
def find_inconsistencies(locale_file): def find_inconsistencies(locale_file):
this_locale = json.loads(open(locale_folder + locale_file).read()) this_locale = json.loads(open(locale_folder + locale_file).read())
# We iterate over all keys/string in en.json # We iterate over all keys/string in en.json
for key, string in reference.items(): for key, string in reference.items():
# Ignore check if there's no translation yet for this key # Ignore check if there's no translation yet for this key
if key not in this_locale: if key not in this_locale:
continue continue