mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
grammar.py et al: Centralize more code
More code is removed from the special parser directories and centralized into grammar.py, Cmd.py, and generate-flex-bison.mk. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
fee94deb48
commit
6297f10f55
6 changed files with 210 additions and 177 deletions
|
|
@ -4,6 +4,7 @@ import os
|
|||
import sys
|
||||
import inspect
|
||||
import re
|
||||
import importlib
|
||||
|
||||
import Object
|
||||
import log
|
||||
|
|
@ -29,23 +30,27 @@ class Cmd(Object.Object): # export
|
|||
r.set_defaults(func=self.run)
|
||||
return r
|
||||
|
||||
def run_sub_commands(description = '', prefix = 'Cmd'): # export
|
||||
def run_sub_commands(description = '', filter = '^Cmd.*', modules=None): # export
|
||||
|
||||
classes = inspect.getmembers(sys.modules["__main__"], inspect.isclass)
|
||||
parser = argparse.ArgumentParser(usage=os.path.basename(sys.argv[0]) + ' [command] [options]',
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter, description=description)
|
||||
parser.add_argument('--log-level', help='Log level', default='notice')
|
||||
parser.add_argument('--log-flags', help='Log flags', default='stderr,position,prio,color')
|
||||
subparsers = parser.add_subparsers(title='Available commands', metavar='')
|
||||
|
||||
for (name, cls) in classes:
|
||||
if not re.match('^Cmd.', name):
|
||||
continue
|
||||
if inspect.isabstract(cls):
|
||||
continue
|
||||
cls().add_parser(subparsers)
|
||||
|
||||
args=parser.parse_args()
|
||||
if modules == None:
|
||||
modules = [ '__main__' ]
|
||||
for m in modules:
|
||||
if m != '__main__':
|
||||
importlib.import_module(m)
|
||||
for name, c in inspect.getmembers(sys.modules[m], inspect.isclass):
|
||||
if not re.match(filter, name):
|
||||
continue
|
||||
if inspect.isabstract(c):
|
||||
continue
|
||||
c().add_parser(subparsers)
|
||||
args = parser.parse_args()
|
||||
log.set_level(args.log_level)
|
||||
log.set_flags(args.log_flags)
|
||||
|
||||
args.func(args)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue