mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
Simplify(?) interface initialization
This commit is contained in:
parent
d2cb802792
commit
eb6d56f7ab
2 changed files with 17 additions and 67 deletions
|
@ -1,7 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from moulinette.core import (
|
||||
init_interface,
|
||||
MoulinetteError,
|
||||
MoulinetteSignals,
|
||||
Moulinette18n,
|
||||
|
@ -73,8 +72,6 @@ def init(logging_config=None, **kwargs):
|
|||
|
||||
|
||||
# Easy access to interfaces
|
||||
|
||||
|
||||
def api(
|
||||
namespaces, host="localhost", port=80, routes={}, use_websocket=True, use_cache=True
|
||||
):
|
||||
|
@ -93,13 +90,16 @@ def api(
|
|||
instead of using the cached one
|
||||
|
||||
"""
|
||||
from moulinette.actionsmap import ActionsMap
|
||||
from moulinette.interfaces.api import Interface, ActionsMapParser
|
||||
try:
|
||||
moulinette = init_interface(
|
||||
"api",
|
||||
kwargs={"routes": routes, "use_websocket": use_websocket},
|
||||
actionsmap={"namespaces": namespaces, "use_cache": use_cache},
|
||||
)
|
||||
moulinette.run(host, port)
|
||||
actionsmap = ActionsMap(ActionsMapParser,
|
||||
namespaces=namespaces,
|
||||
use_cache=use_cache)
|
||||
interface = Interface(actionsmap=actionsmap,
|
||||
routes=routes,
|
||||
use_websocket=use_websocket)
|
||||
interface.run(host, port)
|
||||
except MoulinetteError as e:
|
||||
import logging
|
||||
|
||||
|
@ -138,16 +138,15 @@ def cli(
|
|||
class at construction
|
||||
|
||||
"""
|
||||
from moulinette.actionsmap import ActionsMap
|
||||
from moulinette.interfaces.cli import Interface, ActionsMapParser
|
||||
try:
|
||||
moulinette = init_interface(
|
||||
"cli",
|
||||
actionsmap={
|
||||
"namespaces": namespaces,
|
||||
"use_cache": use_cache,
|
||||
"parser_kwargs": parser_kwargs,
|
||||
},
|
||||
)
|
||||
moulinette.run(args, output_as=output_as, password=password, timeout=timeout)
|
||||
actionsmap = ActionsMap(ActionsMapParser,
|
||||
namespaces=namespaces,
|
||||
use_cache=use_cache,
|
||||
parser_kwargs=parser_kwargs)
|
||||
interface = Interface(actionsmap=actionsmap)
|
||||
interface.run(args, output_as=output_as, password=password, timeout=timeout)
|
||||
except MoulinetteError as e:
|
||||
import logging
|
||||
|
||||
|
|
|
@ -5,8 +5,6 @@ import time
|
|||
import json
|
||||
import logging
|
||||
|
||||
from importlib import import_module
|
||||
|
||||
import moulinette
|
||||
from moulinette.globals import init_moulinette_env
|
||||
|
||||
|
@ -375,53 +373,6 @@ class MoulinetteSignals(object):
|
|||
raise NotImplementedError("this signal is not handled")
|
||||
|
||||
|
||||
# Interfaces & Authenticators management -------------------------------
|
||||
|
||||
|
||||
def init_interface(name, kwargs={}, actionsmap={}):
|
||||
"""Return a new interface instance
|
||||
|
||||
Retrieve the given interface module and return a new instance of its
|
||||
Interface class. It is initialized with arguments 'kwargs' and
|
||||
connected to 'actionsmap' if it's an ActionsMap object, otherwise
|
||||
a new ActionsMap instance will be initialized with arguments
|
||||
'actionsmap'.
|
||||
|
||||
Keyword arguments:
|
||||
- name -- The interface name
|
||||
- kwargs -- A dict of arguments to pass to Interface
|
||||
- actionsmap -- Either an ActionsMap instance or a dict of
|
||||
arguments to pass to ActionsMap
|
||||
|
||||
"""
|
||||
from moulinette.actionsmap import ActionsMap
|
||||
|
||||
try:
|
||||
mod = import_module("moulinette.interfaces.%s" % name)
|
||||
except ImportError as e:
|
||||
logger.exception("unable to load interface '%s' : %s", name, e)
|
||||
raise MoulinetteError("error_see_log")
|
||||
else:
|
||||
try:
|
||||
# Retrieve interface classes
|
||||
parser = mod.ActionsMapParser
|
||||
interface = mod.Interface
|
||||
except AttributeError:
|
||||
logger.exception("unable to retrieve classes of interface '%s'", name)
|
||||
raise MoulinetteError("error_see_log")
|
||||
|
||||
# Instantiate or retrieve ActionsMap
|
||||
if isinstance(actionsmap, dict):
|
||||
amap = ActionsMap(actionsmap.pop("parser", parser), **actionsmap)
|
||||
elif isinstance(actionsmap, ActionsMap):
|
||||
amap = actionsmap
|
||||
else:
|
||||
logger.error("invalid actionsmap value %r", actionsmap)
|
||||
raise MoulinetteError("error_see_log")
|
||||
|
||||
return interface(amap, **kwargs)
|
||||
|
||||
|
||||
# Moulinette core classes ----------------------------------------------
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue