2016-07-11 23:01:57 +02:00
|
|
|
#!/usr/bin/env python3
|
2017-05-05 08:04:09 +02:00
|
|
|
"""boxes.py
|
|
|
|
|
|
|
|
Generate stencils for wooden boxes.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
boxes <generator> [<args>...]
|
|
|
|
boxes --list
|
|
|
|
boxes (-h | --help)
|
|
|
|
boxes --version
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-h --help Show this screen.
|
|
|
|
--version Show version.
|
|
|
|
--list List available generators.
|
|
|
|
"""
|
2016-03-23 22:15:34 +01:00
|
|
|
|
2017-05-05 08:04:09 +02:00
|
|
|
import os
|
2022-12-31 15:52:55 +01:00
|
|
|
import sys
|
2022-06-24 11:54:40 +02:00
|
|
|
import gettext
|
2017-02-23 17:20:50 +01:00
|
|
|
|
2016-03-25 17:49:33 +01:00
|
|
|
try:
|
|
|
|
import boxes
|
|
|
|
except ImportError:
|
2018-08-28 07:32:39 +02:00
|
|
|
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..'))
|
2016-03-25 17:49:33 +01:00
|
|
|
import boxes
|
|
|
|
|
|
|
|
import boxes.generators
|
2016-03-23 22:15:34 +01:00
|
|
|
|
2023-04-27 23:45:06 +02:00
|
|
|
|
2017-05-05 08:04:09 +02:00
|
|
|
def print_usage():
|
|
|
|
print(__doc__)
|
|
|
|
|
2023-04-27 23:45:06 +02:00
|
|
|
|
2017-05-05 08:04:09 +02:00
|
|
|
def list_grouped_generators():
|
|
|
|
print('Available generators:')
|
|
|
|
for group in generator_groups():
|
|
|
|
print('\n' + group.title)
|
|
|
|
if group.description:
|
|
|
|
print('\n%s\n' % group.description)
|
2017-03-17 16:23:25 +01:00
|
|
|
else:
|
2017-05-05 08:04:09 +02:00
|
|
|
print()
|
|
|
|
for box in group.generators:
|
|
|
|
print(' * {} - {}'.format(box.__name__, box.__doc__ or ''))
|
|
|
|
|
2023-04-27 23:45:06 +02:00
|
|
|
|
2022-06-24 11:54:40 +02:00
|
|
|
def get_translation():
|
|
|
|
try:
|
|
|
|
return gettext.translation('boxes.py', localedir='locale')
|
|
|
|
except OSError:
|
|
|
|
return gettext.translation('boxes.py', fallback=True)
|
2023-04-27 23:45:06 +02:00
|
|
|
|
|
|
|
|
2017-05-05 08:04:09 +02:00
|
|
|
def run_generator(name, args):
|
|
|
|
generators = generators_by_name()
|
|
|
|
lower_name = name.lower()
|
|
|
|
|
|
|
|
if lower_name in generators.keys():
|
|
|
|
box = generators[lower_name]()
|
2022-06-24 11:54:40 +02:00
|
|
|
box.translations = get_translation()
|
2017-05-05 08:04:09 +02:00
|
|
|
box.parseArgs(args)
|
2019-02-08 17:43:15 +01:00
|
|
|
box.open()
|
2017-05-05 08:04:09 +02:00
|
|
|
box.render()
|
2019-02-08 17:32:04 +01:00
|
|
|
box.close()
|
2017-05-05 08:04:09 +02:00
|
|
|
else:
|
|
|
|
msg = ('Unknown generator \'{}\'. Use boxes --list to get a list of '
|
|
|
|
'available commands.\n').format(name)
|
|
|
|
sys.stderr.write(msg)
|
|
|
|
|
|
|
|
|
|
|
|
def generator_groups():
|
|
|
|
generators = generators_by_name()
|
|
|
|
return group_generators(generators)
|
|
|
|
|
|
|
|
|
|
|
|
def group_generators(generators):
|
|
|
|
groups = boxes.generators.ui_groups
|
|
|
|
groups_by_name = boxes.generators.ui_groups_by_name
|
|
|
|
|
|
|
|
for name, generator in generators.items():
|
|
|
|
group_for_generator = groups_by_name.get(generator.ui_group,
|
|
|
|
groups_by_name['Misc'])
|
|
|
|
group_for_generator.add(generator)
|
|
|
|
|
|
|
|
return groups
|
|
|
|
|
|
|
|
|
|
|
|
def generators_by_name():
|
|
|
|
all_generators = boxes.generators.getAllBoxGenerators()
|
|
|
|
|
|
|
|
return {
|
|
|
|
name.split('.')[-1].lower(): generator
|
|
|
|
for name, generator in all_generators.items()
|
|
|
|
}
|
|
|
|
|
2016-03-23 22:15:34 +01:00
|
|
|
|
2023-04-27 23:48:36 +02:00
|
|
|
def main() -> None:
|
|
|
|
if len(sys.argv) > 1 and sys.argv[1].startswith("--id="):
|
|
|
|
del sys.argv[1]
|
|
|
|
if len(sys.argv) == 1:
|
|
|
|
print_usage()
|
|
|
|
elif sys.argv[1] == '--list':
|
|
|
|
list_grouped_generators()
|
|
|
|
else:
|
|
|
|
name = sys.argv[1].lower()
|
|
|
|
if name.startswith("--generator="):
|
|
|
|
name = name[12:]
|
|
|
|
run_generator(name, sys.argv[2:])
|
|
|
|
|
|
|
|
|
2017-05-05 08:04:09 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|