From f5ab14d3835034ac84eed30c7f0c5a64c542937b Mon Sep 17 00:00:00 2001 From: jpic Date: Sun, 16 Feb 2020 20:53:49 +0100 Subject: [PATCH] Trying to enrich shlax command in a scalable fashion --- shlax/cli.py | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/shlax/cli.py b/shlax/cli.py index 0e7d05c..0a0752c 100644 --- a/shlax/cli.py +++ b/shlax/cli.py @@ -10,6 +10,7 @@ import asyncio import cli2 import copy import inspect +import glob import os import sys @@ -22,19 +23,39 @@ class ConsoleScript(cli2.ConsoleScript): def __call__(self, *args, **kwargs): self.shlaxfile = None shlaxfile = sys.argv.pop(1) if len(sys.argv) > 1 else '' - if os.path.exists(shlaxfile.split('::')[0]): - self.shlaxfile = Shlaxfile() - self.shlaxfile.parse(shlaxfile) - for name, action in self.shlaxfile.actions.items(): - self[name] = cli2.Callable( - name, - action.callable(), - options={ - k: cli2.Option(name=k, **v) - for k, v in action.options.items() - }, - color=getattr(action, 'color', cli2.YELLOW), - ) + if shlaxfile: + if os.path.exists(shlaxfile): + self.shlaxfile = Shlaxfile() + self.shlaxfile.parse(shlaxfile) + for name, action in self.shlaxfile.actions.items(): + self[name] = cli2.Callable( + name, + action.callable(), + options={ + k: cli2.Option(name=k, **v) + for k, v in action.options.items() + }, + color=getattr(action, 'color', cli2.YELLOW), + ) + else: + try: + mod = importlib.import_module('shlax.repo.' + shlaxfile) + except ImportError: + print('Could not find ' + shlaxfile) + self.exit_code = 1 + return + else: + from shlax import repo + path = repo.__path__._path[0] + for shlaxfile in glob.glob(os.path.join(path, '*.py')): + name = shlaxfile.split('/')[-1].split('.')[0] + import importlib + mod = importlib.import_module('shlax.repo.' + name) + for k, v in mod.__dict__.items(): + if callable(v): + break + self[name] = cli2.Callable(name, v, doc='lol') + return super().__call__(*args, **kwargs) def call(self, command):