Trying to enrich shlax command in a scalable fashion
This commit is contained in:
parent
97255866f8
commit
f5ab14d383
47
shlax/cli.py
47
shlax/cli.py
@ -10,6 +10,7 @@ import asyncio
|
|||||||
import cli2
|
import cli2
|
||||||
import copy
|
import copy
|
||||||
import inspect
|
import inspect
|
||||||
|
import glob
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
@ -22,19 +23,39 @@ class ConsoleScript(cli2.ConsoleScript):
|
|||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
self.shlaxfile = None
|
self.shlaxfile = None
|
||||||
shlaxfile = sys.argv.pop(1) if len(sys.argv) > 1 else ''
|
shlaxfile = sys.argv.pop(1) if len(sys.argv) > 1 else ''
|
||||||
if os.path.exists(shlaxfile.split('::')[0]):
|
if shlaxfile:
|
||||||
self.shlaxfile = Shlaxfile()
|
if os.path.exists(shlaxfile):
|
||||||
self.shlaxfile.parse(shlaxfile)
|
self.shlaxfile = Shlaxfile()
|
||||||
for name, action in self.shlaxfile.actions.items():
|
self.shlaxfile.parse(shlaxfile)
|
||||||
self[name] = cli2.Callable(
|
for name, action in self.shlaxfile.actions.items():
|
||||||
name,
|
self[name] = cli2.Callable(
|
||||||
action.callable(),
|
name,
|
||||||
options={
|
action.callable(),
|
||||||
k: cli2.Option(name=k, **v)
|
options={
|
||||||
for k, v in action.options.items()
|
k: cli2.Option(name=k, **v)
|
||||||
},
|
for k, v in action.options.items()
|
||||||
color=getattr(action, 'color', cli2.YELLOW),
|
},
|
||||||
)
|
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)
|
return super().__call__(*args, **kwargs)
|
||||||
|
|
||||||
def call(self, command):
|
def call(self, command):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user