1
0
Fork 0
mirror of https://github.com/YunoHost/moulinette.git synced 2024-09-03 20:06:31 +02:00

Ugly hack to only load 1 category to speed up execution time

This commit is contained in:
Alexandre Aubin 2020-05-01 05:43:32 +02:00
parent 89ad543797
commit 677f4518e4
3 changed files with 18 additions and 4 deletions
moulinette

View file

@ -112,7 +112,8 @@ def cli(args, top_parser, output_as=None, timeout=None):
"""
from moulinette.interfaces.cli import Interface as Cli
try:
Cli(top_parser=top_parser).run(args, output_as=output_as, timeout=timeout)
load_only_category = args[0] if args and not args[0].startswith("-") else None
Cli(top_parser=top_parser, load_only_category=load_only_category).run(args, output_as=output_as, timeout=timeout)
except MoulinetteError as e:
import logging
logging.getLogger().error(e.strerror)

View file

@ -403,9 +403,13 @@ class ActionsMap(object):
Keyword arguments:
- top_parser -- A BaseActionsMapParser-derived instance to use for
parsing the actions map
- load_only_category -- A name of a category that should only be the
one loaded because it's been already determined
that's the only one relevant ... used for optimization
purposes...
"""
def __init__(self, top_parser):
def __init__(self, top_parser, load_only_category=None):
assert isinstance(top_parser, BaseActionsMapParser), "Invalid parser class '%s'" % top_parser.__class__.__name__
@ -442,6 +446,13 @@ class ActionsMap(object):
self.from_cache = False
actionsmaps[n] = self.generate_cache(n)
# If load_only_category is set, and *if* the target category
# is in the actionsmap, we'll load only that one.
# If we filter it even if it doesn't exist, we'll end up with a
# weird help message when we do a typo in the category name..
if load_only_category and load_only_category in actionsmaps[n]:
actionsmaps[n] = {k: v for k, v in actionsmaps[n].items() if k in [load_only_category, "_global"]}
# Load translations
m18n.load_namespace(n)
@ -651,6 +662,7 @@ class ActionsMap(object):
"""
logger.debug("building parser...")
start = time()
# If loading from cache, extra were already checked when cache was
# loaded ? Not sure about this ... old code is a bit mysterious...
@ -756,4 +768,5 @@ class ActionsMap(object):
tid, action_options["configuration"]
)
logger.debug("building parser took %.3fs", time() - start)
return top_parser

View file

@ -420,7 +420,7 @@ class Interface(BaseInterface):
"""
def __init__(self, top_parser=None):
def __init__(self, top_parser=None, load_only_category=None):
# Set user locale
m18n.set_locale(get_locale())
@ -431,7 +431,7 @@ class Interface(BaseInterface):
msignals.set_handler("authenticate", self._do_authenticate)
msignals.set_handler("prompt", self._do_prompt)
self.actionsmap = ActionsMap(ActionsMapParser(top_parser=top_parser))
self.actionsmap = ActionsMap(ActionsMapParser(top_parser=top_parser), load_only_category=load_only_category)
def run(self, args, output_as=None, timeout=None):
"""Run the moulinette