Trying to enrich shlax command in a scalable fashion

This commit is contained in:
jpic 2020-02-16 20:53:49 +01:00
parent 97255866f8
commit f5ab14d383

View File

@ -10,6 +10,7 @@ import asyncio
import cli2
import copy
import inspect
import glob
import os
import sys
@ -22,7 +23,8 @@ 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]):
if shlaxfile:
if os.path.exists(shlaxfile):
self.shlaxfile = Shlaxfile()
self.shlaxfile.parse(shlaxfile)
for name, action in self.shlaxfile.actions.items():
@ -35,6 +37,25 @@ class ConsoleScript(cli2.ConsoleScript):
},
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):