From de7f2adb4c493ad17b1e403db2c1359e4965f042 Mon Sep 17 00:00:00 2001 From: axolotle Date: Tue, 10 Jan 2023 17:39:39 +0100 Subject: [PATCH] separate app factories --- bin/yunohost-fastapi | 2 +- bin/yunohost-typer | 4 ++-- src/__init_.py | 42 +++++++++++++++++++++++++++++++++++++++--- src/interface/api.py | 2 +- src/interface/base.py | 9 ++++++++- 5 files changed, 51 insertions(+), 8 deletions(-) diff --git a/bin/yunohost-fastapi b/bin/yunohost-fastapi index de7c41c14..e6347d49e 100755 --- a/bin/yunohost-fastapi +++ b/bin/yunohost-fastapi @@ -18,7 +18,7 @@ def main( Run the YunoHost API to manage your server. """ uvicorn.run( - "yunohost.__init_:create_interface", + "yunohost.__init_:create_api_interface", factory=True, host=host, port=port, diff --git a/bin/yunohost-typer b/bin/yunohost-typer index d6048d77d..85344efcd 100755 --- a/bin/yunohost-typer +++ b/bin/yunohost-typer @@ -13,7 +13,7 @@ if os.environ["PATH"] != default_path: if __name__ == "__main__": - from yunohost.__init_ import create_interface + from yunohost.__init_ import create_cli_interface if os.geteuid() != 0: sys.stderr.write( @@ -22,5 +22,5 @@ if __name__ == "__main__": ) sys.exit(1) - app = create_interface() + app = create_cli_interface() app() diff --git a/src/__init_.py b/src/__init_.py index 243bf1cac..1ce30edc4 100644 --- a/src/__init_.py +++ b/src/__init_.py @@ -1,10 +1,46 @@ +import logging +from rich.logging import RichHandler from yunohost.interface import Interface +from yunohost.user import user, group, permission -from yunohost.user import app as user_app +from moulinette import m18n +from moulinette.interfaces.cli import get_locale -def create_interface(): +def create_cli_interface(): + init_i18n() + init_logging(interface="cli") + app = Interface(root=True) - app.add(user_app) + + user.add(group) + user.add(permission) + app.add(user) return app.instance + + +def create_api_interface(): + init_i18n() + init_logging(interface="cli") + + app = Interface(root=True) + # Intermediate router to have distincts categories in swagger + user_sub = Interface(prefix="/users") + user_sub.add(user) + user_sub.add(group) + user_sub.add(permission) + app.add(user_sub) + + return app.instance + + +def init_i18n(): + m18n.set_locales_dir("/usr/share/yunohost/locales/") + m18n.set_locale(get_locale()) + + +def init_logging(interface="cli", debug=False, quiet=False, logdir="/var/log/yunohost"): + logging.basicConfig( + level="NOTSET", format="%(message)s", handlers=[RichHandler(show_time=False)] + ) diff --git a/src/interface/api.py b/src/interface/api.py index bf84cff79..b65bebea8 100644 --- a/src/interface/api.py +++ b/src/interface/api.py @@ -67,7 +67,7 @@ class Interface(BaseInterface): def add(self, interface: Interface): assert isinstance(interface.instance, fastapi.APIRouter) self.instance.include_router( - interface.instance, prefix=f"/{interface.name}", tags=[interface.name] + interface.instance, prefix=interface.prefix, tags=[interface.name] if interface.name else [] ) def prepare_params( diff --git a/src/interface/base.py b/src/interface/base.py index e4a01321c..72ecb45a6 100644 --- a/src/interface/base.py +++ b/src/interface/base.py @@ -68,9 +68,16 @@ class BaseInterface: kind: InterfaceKind local_data: dict[str, Any] = {} - def __init__(self, root: bool = False, name: str = "", help: str = ""): + def __init__( + self, + root: bool = False, + name: str = "", + help: str = "", + prefix: str = "", + ): self.name = "root" if root else name or "" self.help = help + self.prefix = prefix def __call__(self, *args, **kwargs): self.local_data = kwargs