Simplify(?) ActionsMap initialization by removing some obscure kwargs handling

This commit is contained in:
Alexandre Aubin 2020-05-01 03:07:25 +02:00
parent 57d1b2b6db
commit 1849d1aa3b
2 changed files with 15 additions and 33 deletions

View file

@ -88,7 +88,7 @@ def api(namespaces, host="localhost", port=80, routes={}):
from moulinette.actionsmap import ActionsMap
from moulinette.interfaces.api import Interface, ActionsMapParser
try:
actionsmap = ActionsMap(ActionsMapParser,
actionsmap = ActionsMap(ActionsMapParser(),
namespaces=namespaces)
interface = Interface(actionsmap=actionsmap,
routes=routes)
@ -105,7 +105,7 @@ def api(namespaces, host="localhost", port=80, routes={}):
return 0
def cli(namespaces, args, output_as=None, timeout=None, parser_kwargs={}):
def cli(namespaces, args, top_parser, output_as=None, timeout=None):
"""Command line interface
Execute an action with the moulinette from the CLI and print its
@ -116,16 +116,14 @@ def cli(namespaces, args, output_as=None, timeout=None, parser_kwargs={}):
- args -- A list of argument strings
- output_as -- Output result in another format, see
moulinette.interfaces.cli.Interface for possible values
- parser_kwargs -- A dict of arguments to pass to the parser
class at construction
- top_parser -- The top parser used to build the ActionsMapParser
"""
from moulinette.actionsmap import ActionsMap
from moulinette.interfaces.cli import Interface, ActionsMapParser
try:
actionsmap = ActionsMap(ActionsMapParser,
namespaces=namespaces,
parser_kwargs=parser_kwargs)
actionsmap = ActionsMap(ActionsMapParser(top_parser=top_parser),
namespaces=namespaces)
interface = Interface(actionsmap=actionsmap)
interface.run(args, output_as=output_as, timeout=timeout)
except MoulinetteError as e:

View file

@ -402,18 +402,14 @@ class ActionsMap(object):
all available namespaces.
Keyword arguments:
- parser_class -- The BaseActionsMapParser derived class to use
for parsing the actions map
- top_parser -- A BaseActionsMapParser-derived instance to use for
parsing the actions map
- namespaces -- The list of namespaces to use
- parser_kwargs -- A dict of arguments to pass to the parser
class at construction
"""
def __init__(self, parser_class, namespaces=[], parser_kwargs={}):
if not issubclass(parser_class, BaseActionsMapParser):
raise ValueError("Invalid parser class '%s'" % parser_class.__name__)
self.parser_class = parser_class
def __init__(self, top_parser, namespaces=[]):
assert isinstance(top_parser, BaseActionsMapParser), "Invalid parser class '%s'" % top_parser.__class__.__name__
moulinette_env = init_moulinette_env()
DATA_DIR = moulinette_env["DATA_DIR"]
@ -454,13 +450,8 @@ class ActionsMap(object):
m18n.load_namespace(n)
# Generate parsers
self.extraparser = ExtraArgumentParser(parser_class.interface)
self._parser = self._construct_parser(actionsmaps, **parser_kwargs)
@property
def parser(self):
"""Return the instance of the interface's actions map parser"""
return self._parser
self.extraparser = ExtraArgumentParser(top_parser.interface)
self.parser = self._construct_parser(actionsmaps, top_parser)
def get_authenticator_for_profile(self, auth_profile):
@ -649,15 +640,15 @@ class ActionsMap(object):
# Private methods
def _construct_parser(self, actionsmaps, **kwargs):
def _construct_parser(self, actionsmaps, top_parser):
"""
Construct the parser with the actions map
Keyword arguments:
- actionsmaps -- A dict of multi-level dictionnary of
categories/actions/arguments list for each namespaces
- **kwargs -- Additionnal arguments to pass at the parser
class instantiation
- top_parser -- A BaseActionsMapParser-derived instance to use for
parsing the actions map
Returns:
An interface relevant's parser object
@ -668,13 +659,6 @@ class ActionsMap(object):
# loaded ? Not sure about this ... old code is a bit mysterious...
validate_extra = not self.from_cache
# Instantiate parser
#
# this either returns:
# * moulinette.interfaces.cli.ActionsMapParser
# * moulinette.interfaces.api.ActionsMapParser
top_parser = self.parser_class(**kwargs)
# namespace, actionsmap is a tuple where:
#
# * namespace define the top "name", for us it will always be