scripts/boxes: Use generator classes instead of main functions

Make Boxes.parseArgs more robust
--list generators by UI group.
Pass arguments explicitly instead of fiddling with sys.argv
This commit is contained in:
Florian Festi 2017-03-17 16:23:25 +01:00
parent f9c0cb8cab
commit bffd732c3f
2 changed files with 28 additions and 21 deletions

View File

@ -349,8 +349,9 @@ class Boxes:
:param args: (Default value = None) parameters, None for using sys.argv
"""
args = args or sys.argv
if args[-1][0] != "-":
if args is None:
args = sys.argv[1:]
if len(args) > 1 and args[-1][0] != "-":
self.inkscapefile = args[-1]
del args[-1]
args = [a for a in args if not a.startswith('--tab=')]

View File

@ -23,31 +23,37 @@ boxes [NAME] [options]
""")
def main():
modules = boxes.generators.getAllGeneratorModules()
del modules['_template']
generators = {name.split('.')[-1].lower() : box for name, box in
boxes.generators.getAllBoxGenerators().items()}
groups = boxes.generators.ui_groups
groups_by_name = boxes.generators.ui_groups_by_name
for name, box in generators.items():
groups_by_name.get(box.ui_group,
groups_by_name["Misc"]).add(box)
if len(sys.argv) > 1 and sys.argv[1].startswith("--id="):
del sys.argv[1]
if len(sys.argv) == 1:
printusage()
elif (sys.argv[1] in modules or
(sys.argv[1].startswith("--generator=") and
sys.argv[1][12:] in modules)):
if sys.argv[1].startswith("--generator="):
sys.argv[1] = sys.argv[1][12:]
name = sys.argv[1]
sys.argv[0] = "%s %s" % (sys.argv[0], sys.argv[1])
sys.argv[1] = '--tab="0"'
del sys.argv[0]
del sys.argv[1]
modules[name].main()
elif sys.argv[1] == '--list':
print("Available generators:")
for name in sorted(modules):
if not hasattr(modules[name], "main"):
continue
print(" * ", name)
for group in groups:
print('\n' + group.title)
if group.description:
print('\n%s\n' % group.description)
else:
print()
for box in group.generators:
print(" * ", box.__name__, ' - ', box.__doc__ or '')
else:
sys.stderr.write(str(sys.argv))
sys.stderr.write("Unknown generator name. Use boxes --list to get a list of available commands.")
name = sys.argv[1].lower()
if name.startswith("--generator="):
name = name[12:]
if (name in generators):
box = generators[name]()
box.parseArgs(sys.argv[2:])
box.render()
else:
sys.stderr.write(str(sys.argv))
sys.stderr.write("Unknown generator name. Use boxes --list to get a list of available commands.")
main()