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:
parent
f9c0cb8cab
commit
bffd732c3f
|
@ -349,8 +349,9 @@ class Boxes:
|
||||||
:param args: (Default value = None) parameters, None for using sys.argv
|
:param args: (Default value = None) parameters, None for using sys.argv
|
||||||
|
|
||||||
"""
|
"""
|
||||||
args = args or sys.argv
|
if args is None:
|
||||||
if args[-1][0] != "-":
|
args = sys.argv[1:]
|
||||||
|
if len(args) > 1 and args[-1][0] != "-":
|
||||||
self.inkscapefile = args[-1]
|
self.inkscapefile = args[-1]
|
||||||
del args[-1]
|
del args[-1]
|
||||||
args = [a for a in args if not a.startswith('--tab=')]
|
args = [a for a in args if not a.startswith('--tab=')]
|
||||||
|
|
|
@ -23,31 +23,37 @@ boxes [NAME] [options]
|
||||||
""")
|
""")
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
modules = boxes.generators.getAllGeneratorModules()
|
generators = {name.split('.')[-1].lower() : box for name, box in
|
||||||
del modules['_template']
|
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="):
|
if len(sys.argv) > 1 and sys.argv[1].startswith("--id="):
|
||||||
del sys.argv[1]
|
del sys.argv[1]
|
||||||
if len(sys.argv) == 1:
|
if len(sys.argv) == 1:
|
||||||
printusage()
|
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':
|
elif sys.argv[1] == '--list':
|
||||||
print("Available generators:")
|
print("Available generators:")
|
||||||
for name in sorted(modules):
|
for group in groups:
|
||||||
if not hasattr(modules[name], "main"):
|
print('\n' + group.title)
|
||||||
continue
|
if group.description:
|
||||||
print(" * ", name)
|
print('\n%s\n' % group.description)
|
||||||
|
else:
|
||||||
|
print()
|
||||||
|
for box in group.generators:
|
||||||
|
print(" * ", box.__name__, ' - ', box.__doc__ or '')
|
||||||
else:
|
else:
|
||||||
sys.stderr.write(str(sys.argv))
|
name = sys.argv[1].lower()
|
||||||
sys.stderr.write("Unknown generator name. Use boxes --list to get a list of available commands.")
|
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()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue