mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
Merge pull request #286 from YunoHost/actions/black
Format Python code with Black
This commit is contained in:
commit
5c982db4b6
5 changed files with 30 additions and 21 deletions
|
@ -26,32 +26,27 @@ __credits__ = """
|
||||||
You should have received a copy of the GNU Affero General Public License
|
You should have received a copy of the GNU Affero General Public License
|
||||||
along with this program; if not, see http://www.gnu.org/licenses
|
along with this program; if not, see http://www.gnu.org/licenses
|
||||||
"""
|
"""
|
||||||
__all__ = [
|
__all__ = ["init", "api", "cli", "m18n", "MoulinetteError", "Moulinette"]
|
||||||
"init",
|
|
||||||
"api",
|
|
||||||
"cli",
|
|
||||||
"m18n",
|
|
||||||
"MoulinetteError",
|
|
||||||
"Moulinette"
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
m18n = Moulinette18n()
|
m18n = Moulinette18n()
|
||||||
|
|
||||||
|
|
||||||
class classproperty(object):
|
class classproperty(object):
|
||||||
def __init__(self, f):
|
def __init__(self, f):
|
||||||
self.f = f
|
self.f = f
|
||||||
|
|
||||||
def __get__(self, obj, owner):
|
def __get__(self, obj, owner):
|
||||||
return self.f(owner)
|
return self.f(owner)
|
||||||
|
|
||||||
class Moulinette():
|
|
||||||
|
class Moulinette:
|
||||||
|
|
||||||
_interface = None
|
_interface = None
|
||||||
|
|
||||||
def prompt(*args, **kwargs):
|
def prompt(*args, **kwargs):
|
||||||
return Moulinette.interface.prompt(*args, **kwargs)
|
return Moulinette.interface.prompt(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def display(*args, **kwargs):
|
def display(*args, **kwargs):
|
||||||
return Moulinette.interface.display(*args, **kwargs)
|
return Moulinette.interface.display(*args, **kwargs)
|
||||||
|
|
||||||
|
@ -133,7 +128,9 @@ def cli(args, top_parser, output_as=None, timeout=None):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
load_only_category = args[0] if args and not args[0].startswith("-") else None
|
load_only_category = args[0] if args and not args[0].startswith("-") else None
|
||||||
Cli(top_parser=top_parser, load_only_category=load_only_category).run(args, output_as=output_as, timeout=timeout)
|
Cli(top_parser=top_parser, load_only_category=load_only_category).run(
|
||||||
|
args, output_as=output_as, timeout=timeout
|
||||||
|
)
|
||||||
except MoulinetteError as e:
|
except MoulinetteError as e:
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
|
|
@ -230,7 +230,7 @@ class _HTTPArgumentParser(object):
|
||||||
raise MoulinetteValidationError(message, raw_msg=True)
|
raise MoulinetteValidationError(message, raw_msg=True)
|
||||||
|
|
||||||
|
|
||||||
class Session():
|
class Session:
|
||||||
|
|
||||||
secret = random_ascii()
|
secret = random_ascii()
|
||||||
actionsmap_name = None # This is later set to the actionsmap name
|
actionsmap_name = None # This is later set to the actionsmap name
|
||||||
|
@ -239,12 +239,19 @@ class Session():
|
||||||
|
|
||||||
assert isinstance(infos, dict)
|
assert isinstance(infos, dict)
|
||||||
|
|
||||||
response.set_cookie(f"session.{Session.actionsmap_name}", infos, secure=True, secret=Session.secret)
|
response.set_cookie(
|
||||||
|
f"session.{Session.actionsmap_name}",
|
||||||
|
infos,
|
||||||
|
secure=True,
|
||||||
|
secret=Session.secret,
|
||||||
|
)
|
||||||
|
|
||||||
def get_infos():
|
def get_infos():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
infos = request.get_cookie(f"session.{Session.actionsmap_name}", secret=Session.secret, default={})
|
infos = request.get_cookie(
|
||||||
|
f"session.{Session.actionsmap_name}", secret=Session.secret, default={}
|
||||||
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
infos = {}
|
infos = {}
|
||||||
|
|
||||||
|
@ -388,7 +395,9 @@ class _ActionsMapPlugin(object):
|
||||||
authenticator = self.actionsmap.get_authenticator(profile)
|
authenticator = self.actionsmap.get_authenticator(profile)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
auth_info = authenticator.authenticate_credentials(credentials, store_session=True)
|
auth_info = authenticator.authenticate_credentials(
|
||||||
|
credentials, store_session=True
|
||||||
|
)
|
||||||
session_infos = Session.get_infos()
|
session_infos = Session.get_infos()
|
||||||
session_infos[profile] = auth_info
|
session_infos[profile] = auth_info
|
||||||
except MoulinetteError as e:
|
except MoulinetteError as e:
|
||||||
|
@ -523,6 +532,7 @@ class _ActionsMapPlugin(object):
|
||||||
def prompt(self, *args, **kwargs):
|
def prompt(self, *args, **kwargs):
|
||||||
raise NotImplementedError("Prompt is not implemented for this interface")
|
raise NotImplementedError("Prompt is not implemented for this interface")
|
||||||
|
|
||||||
|
|
||||||
# HTTP Responses -------------------------------------------------------
|
# HTTP Responses -------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -537,7 +537,9 @@ class Interface:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if not os.isatty(1):
|
if not os.isatty(1):
|
||||||
raise MoulinetteError("Not a tty, can't do interactive prompts", raw_msg=True)
|
raise MoulinetteError(
|
||||||
|
"Not a tty, can't do interactive prompts", raw_msg=True
|
||||||
|
)
|
||||||
|
|
||||||
if is_password:
|
if is_password:
|
||||||
prompt = lambda m: getpass.getpass(colorize(m18n.g("colon", m), color))
|
prompt = lambda m: getpass.getpass(colorize(m18n.g("colon", m), color))
|
||||||
|
@ -553,9 +555,7 @@ class Interface:
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def display(self, message, style="info"):
|
def display(self, message, style="info"):
|
||||||
"""Display a message
|
"""Display a message"""
|
||||||
|
|
||||||
"""
|
|
||||||
if style == "success":
|
if style == "success":
|
||||||
print("{} {}".format(colorize(m18n.g("success"), "green"), message))
|
print("{} {}".format(colorize(m18n.g("success"), "green"), message))
|
||||||
elif style == "warning":
|
elif style == "warning":
|
||||||
|
|
|
@ -17,7 +17,6 @@ from moulinette import m18n, Moulinette
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def iface():
|
def iface():
|
||||||
class DummyInterface:
|
class DummyInterface:
|
||||||
|
|
||||||
def prompt():
|
def prompt():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -164,6 +163,7 @@ 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(ActionsMapParser())
|
amap = ActionsMap(ActionsMapParser())
|
||||||
|
|
||||||
with pytest.raises(MoulinetteError) as exception:
|
with pytest.raises(MoulinetteError) as exception:
|
||||||
|
|
|
@ -87,7 +87,9 @@ class TestAuthAPI:
|
||||||
"CSRF protection"
|
"CSRF protection"
|
||||||
in self.login(moulinette_webapi, csrf=True, status=403).text
|
in self.login(moulinette_webapi, csrf=True, status=403).text
|
||||||
)
|
)
|
||||||
assert not any(c.name == "session.moulitest" for c in moulinette_webapi.cookiejar)
|
assert not any(
|
||||||
|
c.name == "session.moulitest" for c in moulinette_webapi.cookiejar
|
||||||
|
)
|
||||||
|
|
||||||
def test_login_then_legit_request_without_cookies(self, moulinette_webapi):
|
def test_login_then_legit_request_without_cookies(self, moulinette_webapi):
|
||||||
self.login(moulinette_webapi)
|
self.login(moulinette_webapi)
|
||||||
|
|
Loading…
Reference in a new issue