From eb6d56f7ab6ffc3c06a199fb77b9924f5443c9de Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Fri, 1 May 2020 01:08:10 +0200 Subject: [PATCH] Simplify(?) interface initialization --- moulinette/__init__.py | 35 +++++++++++++++--------------- moulinette/core.py | 49 ------------------------------------------ 2 files changed, 17 insertions(+), 67 deletions(-) diff --git a/moulinette/__init__.py b/moulinette/__init__.py index 4aae1be0..d9df19b1 100755 --- a/moulinette/__init__.py +++ b/moulinette/__init__.py @@ -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 diff --git a/moulinette/core.py b/moulinette/core.py index d3a2299c..f7ef4876 100644 --- a/moulinette/core.py +++ b/moulinette/core.py @@ -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 ----------------------------------------------