mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
Merge pull request #330 from YunoHost/actions/black
Format Python code with Black
This commit is contained in:
commit
1fb77feec3
20 changed files with 1 additions and 61 deletions
|
@ -39,7 +39,6 @@ class classproperty:
|
|||
|
||||
|
||||
class Moulinette:
|
||||
|
||||
_interface = None
|
||||
|
||||
def prompt(*args, **kwargs):
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 {}
|
||||
|
|
|
@ -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 []
|
||||
|
||||
|
|
|
@ -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"""
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue