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

View file

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