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 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from moulinette.core import (
|
from moulinette.core import (
|
||||||
init_interface,
|
|
||||||
MoulinetteError,
|
MoulinetteError,
|
||||||
MoulinetteSignals,
|
MoulinetteSignals,
|
||||||
Moulinette18n,
|
Moulinette18n,
|
||||||
|
@ -73,8 +72,6 @@ def init(logging_config=None, **kwargs):
|
||||||
|
|
||||||
|
|
||||||
# Easy access to interfaces
|
# Easy access to interfaces
|
||||||
|
|
||||||
|
|
||||||
def api(
|
def api(
|
||||||
namespaces, host="localhost", port=80, routes={}, use_websocket=True, use_cache=True
|
namespaces, host="localhost", port=80, routes={}, use_websocket=True, use_cache=True
|
||||||
):
|
):
|
||||||
|
@ -93,13 +90,16 @@ def api(
|
||||||
instead of using the cached one
|
instead of using the cached one
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
from moulinette.actionsmap import ActionsMap
|
||||||
|
from moulinette.interfaces.api import Interface, ActionsMapParser
|
||||||
try:
|
try:
|
||||||
moulinette = init_interface(
|
actionsmap = ActionsMap(ActionsMapParser,
|
||||||
"api",
|
namespaces=namespaces,
|
||||||
kwargs={"routes": routes, "use_websocket": use_websocket},
|
use_cache=use_cache)
|
||||||
actionsmap={"namespaces": namespaces, "use_cache": use_cache},
|
interface = Interface(actionsmap=actionsmap,
|
||||||
)
|
routes=routes,
|
||||||
moulinette.run(host, port)
|
use_websocket=use_websocket)
|
||||||
|
interface.run(host, port)
|
||||||
except MoulinetteError as e:
|
except MoulinetteError as e:
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
@ -138,16 +138,15 @@ def cli(
|
||||||
class at construction
|
class at construction
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
from moulinette.actionsmap import ActionsMap
|
||||||
|
from moulinette.interfaces.cli import Interface, ActionsMapParser
|
||||||
try:
|
try:
|
||||||
moulinette = init_interface(
|
actionsmap = ActionsMap(ActionsMapParser,
|
||||||
"cli",
|
namespaces=namespaces,
|
||||||
actionsmap={
|
use_cache=use_cache,
|
||||||
"namespaces": namespaces,
|
parser_kwargs=parser_kwargs)
|
||||||
"use_cache": use_cache,
|
interface = Interface(actionsmap=actionsmap)
|
||||||
"parser_kwargs": parser_kwargs,
|
interface.run(args, output_as=output_as, password=password, timeout=timeout)
|
||||||
},
|
|
||||||
)
|
|
||||||
moulinette.run(args, output_as=output_as, password=password, timeout=timeout)
|
|
||||||
except MoulinetteError as e:
|
except MoulinetteError as e:
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
|
|
@ -5,8 +5,6 @@ import time
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from importlib import import_module
|
|
||||||
|
|
||||||
import moulinette
|
import moulinette
|
||||||
from moulinette.globals import init_moulinette_env
|
from moulinette.globals import init_moulinette_env
|
||||||
|
|
||||||
|
@ -375,53 +373,6 @@ class MoulinetteSignals(object):
|
||||||
raise NotImplementedError("this signal is not handled")
|
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 ----------------------------------------------
|
# Moulinette core classes ----------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue