mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
Get rid of msettings, replace with a convenient singleton class that also wraps prompt and display
This commit is contained in:
parent
ebdb1e22ee
commit
67d9c9cdd1
10 changed files with 35 additions and 38 deletions
|
@ -1,5 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from os import environ
|
||||||
from moulinette.core import (
|
from moulinette.core import (
|
||||||
MoulinetteError,
|
MoulinetteError,
|
||||||
Moulinette18n,
|
Moulinette18n,
|
||||||
|
@ -30,22 +31,34 @@ __all__ = [
|
||||||
"api",
|
"api",
|
||||||
"cli",
|
"cli",
|
||||||
"m18n",
|
"m18n",
|
||||||
"env",
|
|
||||||
"init_interface",
|
|
||||||
"MoulinetteError",
|
"MoulinetteError",
|
||||||
|
"Moulinette"
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
msettings = dict()
|
|
||||||
m18n = Moulinette18n()
|
m18n = Moulinette18n()
|
||||||
|
|
||||||
|
class classproperty(object):
|
||||||
|
def __init__(self, f):
|
||||||
|
self.f = f
|
||||||
|
def __get__(self, obj, owner):
|
||||||
|
return self.f(owner)
|
||||||
|
|
||||||
def prompt(**kwargs):
|
class Moulinette():
|
||||||
return msettings["interface"].prompt(**kwargs)
|
|
||||||
|
_interface = None
|
||||||
|
|
||||||
|
def prompt(*args, **kwargs):
|
||||||
|
return Moulinette.interface.prompt(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def display(**kwargs):
|
def display(*args, **kwargs):
|
||||||
return msettings["interface"].display(**kwargs)
|
return Moulinette.interface.display(*args, **kwargs)
|
||||||
|
|
||||||
|
@classproperty
|
||||||
|
def interface(cls):
|
||||||
|
return cls._interface
|
||||||
|
|
||||||
|
|
||||||
# Package functions
|
# Package functions
|
||||||
|
|
||||||
|
@ -127,8 +140,3 @@ def cli(args, top_parser, output_as=None, timeout=None):
|
||||||
logging.getLogger("moulinette").error(e.strerror)
|
logging.getLogger("moulinette").error(e.strerror)
|
||||||
return 1
|
return 1
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def env():
|
|
||||||
"""Initialise moulinette specific configuration."""
|
|
||||||
return init_moulinette_env()
|
|
||||||
|
|
|
@ -11,9 +11,9 @@ from time import time
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
|
|
||||||
from moulinette import m18n, msettings
|
from moulinette import m18n, Moulinette
|
||||||
from moulinette.cache import open_cachefile
|
|
||||||
from moulinette.globals import init_moulinette_env
|
from moulinette.globals import init_moulinette_env
|
||||||
|
from moulinette.cache import open_cachefile
|
||||||
from moulinette.core import (
|
from moulinette.core import (
|
||||||
MoulinetteError,
|
MoulinetteError,
|
||||||
MoulinetteLock,
|
MoulinetteLock,
|
||||||
|
@ -97,7 +97,7 @@ class CommentParameter(_ExtraParameter):
|
||||||
def __call__(self, message, arg_name, arg_value):
|
def __call__(self, message, arg_name, arg_value):
|
||||||
if arg_value is None:
|
if arg_value is None:
|
||||||
return
|
return
|
||||||
return msettings['interface'].display(m18n.n(message))
|
return Moulinette.display(m18n.n(message))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate(klass, value, arg_name):
|
def validate(klass, value, arg_name):
|
||||||
|
@ -134,7 +134,7 @@ class AskParameter(_ExtraParameter):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Ask for the argument value
|
# Ask for the argument value
|
||||||
return msettings['interface'].prompt(m18n.n(message))
|
return Moulinette.prompt(m18n.n(message))
|
||||||
except NotImplementedError:
|
except NotImplementedError:
|
||||||
return arg_value
|
return arg_value
|
||||||
|
|
||||||
|
@ -172,7 +172,7 @@ class PasswordParameter(AskParameter):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Ask for the password
|
# Ask for the password
|
||||||
return msettings['interface'].prompt(m18n.n(message), True, True)
|
return Moulinette.prompt(m18n.n(message), True, True)
|
||||||
except NotImplementedError:
|
except NotImplementedError:
|
||||||
return arg_value
|
return arg_value
|
||||||
|
|
||||||
|
@ -499,7 +499,7 @@ class ActionsMap(object):
|
||||||
return
|
return
|
||||||
|
|
||||||
authenticator = self.get_authenticator(auth_method)
|
authenticator = self.get_authenticator(auth_method)
|
||||||
msettings['interface'].authenticate(authenticator)
|
Moulinette.interface.authenticate(authenticator)
|
||||||
|
|
||||||
def process(self, args, timeout=None, **kwargs):
|
def process(self, args, timeout=None, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -6,7 +6,6 @@ import hashlib
|
||||||
import hmac
|
import hmac
|
||||||
|
|
||||||
from moulinette.utils.text import random_ascii
|
from moulinette.utils.text import random_ascii
|
||||||
from moulinette.cache import open_cachefile, get_cachedir, cachefile_exists
|
|
||||||
from moulinette.core import MoulinetteError, MoulinetteAuthenticationError
|
from moulinette.core import MoulinetteError, MoulinetteAuthenticationError
|
||||||
|
|
||||||
logger = logging.getLogger("moulinette.authenticator")
|
logger = logging.getLogger("moulinette.authenticator")
|
||||||
|
|
|
@ -42,10 +42,3 @@ def open_cachefile(filename, mode="r", subdir=""):
|
||||||
cache_dir = get_cachedir(subdir, make_dir=True if mode[0] == "w" else False)
|
cache_dir = get_cachedir(subdir, make_dir=True if mode[0] == "w" else False)
|
||||||
file_path = os.path.join(cache_dir, filename)
|
file_path = os.path.join(cache_dir, filename)
|
||||||
return open(file_path, mode)
|
return open(file_path, mode)
|
||||||
|
|
||||||
|
|
||||||
def cachefile_exists(filename, subdir=""):
|
|
||||||
|
|
||||||
cache_dir = get_cachedir(subdir, make_dir=False)
|
|
||||||
file_path = os.path.join(cache_dir, filename)
|
|
||||||
return os.path.exists(file_path)
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ import argparse
|
||||||
import copy
|
import copy
|
||||||
from collections import deque, OrderedDict
|
from collections import deque, OrderedDict
|
||||||
|
|
||||||
from moulinette import msettings, m18n
|
from moulinette import Moulinette, m18n
|
||||||
from moulinette.core import MoulinetteError
|
from moulinette.core import MoulinetteError
|
||||||
|
|
||||||
logger = logging.getLogger("moulinette.interface")
|
logger = logging.getLogger("moulinette.interface")
|
||||||
|
|
|
@ -13,7 +13,7 @@ from geventwebsocket import WebSocketError
|
||||||
from bottle import request, response, Bottle, HTTPResponse
|
from bottle import request, response, Bottle, HTTPResponse
|
||||||
from bottle import abort
|
from bottle import abort
|
||||||
|
|
||||||
from moulinette import m18n, env, msettings
|
from moulinette import m18n, Moulinette
|
||||||
from moulinette.actionsmap import ActionsMap
|
from moulinette.actionsmap import ActionsMap
|
||||||
from moulinette.core import MoulinetteError, MoulinetteValidationError
|
from moulinette.core import MoulinetteError, MoulinetteValidationError
|
||||||
from moulinette.interfaces import (
|
from moulinette.interfaces import (
|
||||||
|
@ -734,7 +734,7 @@ class Interface:
|
||||||
|
|
||||||
self._app = app
|
self._app = app
|
||||||
|
|
||||||
msettings["interface"] = self
|
Moulinette._interface = self
|
||||||
|
|
||||||
def run(self, host="localhost", port=80):
|
def run(self, host="localhost", port=80):
|
||||||
"""Run the moulinette
|
"""Run the moulinette
|
||||||
|
|
|
@ -11,7 +11,7 @@ from datetime import date, datetime
|
||||||
|
|
||||||
import argcomplete
|
import argcomplete
|
||||||
|
|
||||||
from moulinette import m18n, msettings
|
from moulinette import m18n, Moulinette
|
||||||
from moulinette.actionsmap import ActionsMap
|
from moulinette.actionsmap import ActionsMap
|
||||||
from moulinette.core import MoulinetteError, MoulinetteValidationError
|
from moulinette.core import MoulinetteError, MoulinetteValidationError
|
||||||
from moulinette.interfaces import (
|
from moulinette.interfaces import (
|
||||||
|
@ -473,7 +473,7 @@ class Interface:
|
||||||
load_only_category=load_only_category,
|
load_only_category=load_only_category,
|
||||||
)
|
)
|
||||||
|
|
||||||
msettings["interface"] = self
|
Moulinette._interface = self
|
||||||
|
|
||||||
def run(self, args, output_as=None, timeout=None):
|
def run(self, args, output_as=None, timeout=None):
|
||||||
"""Run the moulinette
|
"""Run the moulinette
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -3,7 +3,7 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from setuptools import setup, find_packages
|
from setuptools import setup, find_packages
|
||||||
from moulinette.globals import init_moulinette_env
|
from moulinette import init_moulinette_env
|
||||||
|
|
||||||
|
|
||||||
LOCALES_DIR = init_moulinette_env()['LOCALES_DIR']
|
LOCALES_DIR = init_moulinette_env()['LOCALES_DIR']
|
||||||
|
|
|
@ -11,7 +11,7 @@ from moulinette.actionsmap import (
|
||||||
)
|
)
|
||||||
|
|
||||||
from moulinette.core import MoulinetteError
|
from moulinette.core import MoulinetteError
|
||||||
from moulinette import m18n, msettings
|
from moulinette import m18n, Moulinette
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
@ -73,7 +73,7 @@ def test_ask_parameter(iface, mocker):
|
||||||
|
|
||||||
from moulinette.core import Moulinette18n
|
from moulinette.core import Moulinette18n
|
||||||
|
|
||||||
msettings["interface"] = iface
|
Moulinette._interface = iface
|
||||||
mocker.patch.object(Moulinette18n, "n", return_value="awesome_test")
|
mocker.patch.object(Moulinette18n, "n", return_value="awesome_test")
|
||||||
mocker.patch.object(iface, "prompt", return_value="awesome_test")
|
mocker.patch.object(iface, "prompt", return_value="awesome_test")
|
||||||
arg = ask("foobar", "a", None)
|
arg = ask("foobar", "a", None)
|
||||||
|
@ -87,7 +87,7 @@ def test_password_parameter(iface, mocker):
|
||||||
|
|
||||||
from moulinette.core import Moulinette18n
|
from moulinette.core import Moulinette18n
|
||||||
|
|
||||||
msettings["interface"] = iface
|
Moulinette._interface = iface
|
||||||
mocker.patch.object(Moulinette18n, "n", return_value="awesome_test")
|
mocker.patch.object(Moulinette18n, "n", return_value="awesome_test")
|
||||||
mocker.patch.object(iface, "prompt", return_value="awesome_test")
|
mocker.patch.object(iface, "prompt", return_value="awesome_test")
|
||||||
arg = ask("foobar", "a", None)
|
arg = ask("foobar", "a", None)
|
||||||
|
|
|
@ -100,9 +100,6 @@ class TestAuthAPI:
|
||||||
self.login(moulinette_webapi)
|
self.login(moulinette_webapi)
|
||||||
|
|
||||||
assert "session.moulitest" in moulinette_webapi.cookies
|
assert "session.moulitest" in moulinette_webapi.cookies
|
||||||
print("====================cookie")
|
|
||||||
print(moulinette_webapi.cookiejar)
|
|
||||||
print(moulinette_webapi.cookies)
|
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
moulinette_webapi.get("/test-auth/default", status=200).text
|
moulinette_webapi.get("/test-auth/default", status=200).text
|
||||||
|
|
Loading…
Add table
Reference in a new issue