Mercurial > p > roundup > code
comparison roundup/admin.py @ 6957:f924af12ef50
issue2551233 - create new roundup-admin command "templates"
add a command to list all templates, the directory where they are
defined, and a description.
If called as templates trace_dir also list all directores that are
search for templates even if none are found.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Mon, 12 Sep 2022 18:29:50 -0400 |
| parents | b5e465646a7c |
| children | 3211745e8d7c |
comparison
equal
deleted
inserted
replaced
| 6956:ca6b056b79a4 | 6957:f924af12ef50 |
|---|---|
| 287 print(line[indent:]) | 287 print(line[indent:]) |
| 288 else: | 288 else: |
| 289 print(line) | 289 print(line) |
| 290 return 0 | 290 return 0 |
| 291 | 291 |
| 292 def listTemplates(self): | 292 def listTemplates(self, trace_search=False): |
| 293 """ List all the available templates. | 293 """ List all the available templates. |
| 294 | 294 |
| 295 Look in the following places, where the later rules take precedence: | 295 Look in the following places, where the later rules take precedence: |
| 296 | 296 |
| 297 1. <roundup.admin.__file__>/../../share/roundup/templates/* | 297 1. <roundup.admin.__file__>/../../share/roundup/templates/* |
| 325 path = __file__ | 325 path = __file__ |
| 326 # move up N elements in the path | 326 # move up N elements in the path |
| 327 for _i in range(N): | 327 for _i in range(N): |
| 328 path = os.path.dirname(path) | 328 path = os.path.dirname(path) |
| 329 tdir = os.path.join(path, 'share', 'roundup', 'templates') | 329 tdir = os.path.join(path, 'share', 'roundup', 'templates') |
| 330 if debug: print(tdir) | 330 if debug or trace_search: print(tdir) |
| 331 if os.path.isdir(tdir): | 331 if os.path.isdir(tdir): |
| 332 templates = init.listTemplates(tdir) | 332 templates = init.listTemplates(tdir) |
| 333 if debug: print(" Found templates breaking loop") | 333 if debug: print(" Found templates breaking loop") |
| 334 break | 334 break |
| 335 | 335 |
| 347 for _N in 1, 2: | 347 for _N in 1, 2: |
| 348 path = os.path.dirname(path) | 348 path = os.path.dirname(path) |
| 349 # path is /usr/local/lib/python3.10/site-packages | 349 # path is /usr/local/lib/python3.10/site-packages |
| 350 tdir = os.path.join(path, sys.prefix[1:], 'share', | 350 tdir = os.path.join(path, sys.prefix[1:], 'share', |
| 351 'roundup', 'templates') | 351 'roundup', 'templates') |
| 352 if debug: print(tdir) | 352 if debug or trace_search: print(tdir) |
| 353 if os.path.isdir(tdir): | 353 if os.path.isdir(tdir): |
| 354 templates.update(init.listTemplates(tdir)) | 354 templates.update(init.listTemplates(tdir)) |
| 355 | 355 |
| 356 try: | 356 try: |
| 357 # sigh pip 3.10 in virtual env finds another place to bury them. | 357 # sigh pip 3.10 in virtual env finds another place to bury them. |
| 358 # why local and sys.base_prefix are in path I do not know. | 358 # why local and sys.base_prefix are in path I do not know. |
| 359 # path is /usr/local/lib/python3.10/site-packages | 359 # path is /usr/local/lib/python3.10/site-packages |
| 360 tdir = os.path.join(path, sys.base_prefix[1:], 'local', 'share', | 360 tdir = os.path.join(path, sys.base_prefix[1:], 'local', 'share', |
| 361 'roundup', 'templates') | 361 'roundup', 'templates') |
| 362 if debug: print(tdir) | 362 if debug or trace_search: print(tdir) |
| 363 if os.path.isdir(tdir): | 363 if os.path.isdir(tdir): |
| 364 templates.update(init.listTemplates(tdir)) | 364 templates.update(init.listTemplates(tdir)) |
| 365 # path is /usr/local/lib/python3.10/site-packages | 365 # path is /usr/local/lib/python3.10/site-packages |
| 366 | 366 |
| 367 | 367 |
| 368 tdir = os.path.join(path, sys.base_prefix[1:], 'share', | 368 tdir = os.path.join(path, sys.base_prefix[1:], 'share', |
| 369 'roundup', 'templates') | 369 'roundup', 'templates') |
| 370 if debug: print(tdir) | 370 if debug or trace_search: print(tdir) |
| 371 if os.path.isdir(tdir): | 371 if os.path.isdir(tdir): |
| 372 templates.update(init.listTemplates(tdir)) | 372 templates.update(init.listTemplates(tdir)) |
| 373 except AttributeError: | 373 except AttributeError: |
| 374 pass # sys.base_prefix doesn't work under python2 | 374 pass # sys.base_prefix doesn't work under python2 |
| 375 | 375 |
| 376 # Try subdirs of the current dir | 376 # Try subdirs of the current dir |
| 377 templates.update(init.listTemplates(os.getcwd())) | 377 templates.update(init.listTemplates(os.getcwd())) |
| 378 if debug: print(os.getcwd() + '/*') | 378 if debug or trace_search: print(os.getcwd() + '/*') |
| 379 | 379 |
| 380 # Finally, try the current directory as a template | 380 # Finally, try the current directory as a template |
| 381 template = init.loadTemplateInfo(os.getcwd()) | 381 template = init.loadTemplateInfo(os.getcwd()) |
| 382 if debug: print(os.getcwd() + '/*') | 382 if debug or trace_search: print(os.getcwd()) |
| 383 if template: | 383 if template: |
| 384 if debug: print(" Found template %s"%template['name']) | 384 if debug: print(" Found template %s"%template['name']) |
| 385 templates[template['name']] = template | 385 templates[template['name']] = template |
| 386 | 386 |
| 387 return templates | 387 return templates |
| 1078 raise UsageError(_('%(classname)s has no property ' | 1078 raise UsageError(_('%(classname)s has no property ' |
| 1079 '"%(propname)s"') % locals()) | 1079 '"%(propname)s"') % locals()) |
| 1080 print(_('%(nodeid)4s: %(value)s') % locals()) | 1080 print(_('%(nodeid)4s: %(value)s') % locals()) |
| 1081 return 0 | 1081 return 0 |
| 1082 | 1082 |
| 1083 def do_templates(self, args): | |
| 1084 ''"""Usage: templates [trace_search] | |
| 1085 List templates and their installed directories. | |
| 1086 | |
| 1087 With trace_search also list all directories that are | |
| 1088 searched for templates. | |
| 1089 """ | |
| 1090 import textwrap | |
| 1091 | |
| 1092 trace_search = False | |
| 1093 if args and args[0] == "trace_search": | |
| 1094 trace_search = True | |
| 1095 | |
| 1096 templates = self.listTemplates(trace_search=trace_search) | |
| 1097 | |
| 1098 for name in sorted(list(templates.keys())): | |
| 1099 templates[name]['description'] = textwrap.fill( | |
| 1100 "\n".join([ line.lstrip() for line in | |
| 1101 templates[name]['description'].split("\n")]), | |
| 1102 70, | |
| 1103 subsequent_indent=" " | |
| 1104 ) | |
| 1105 print(""" | |
| 1106 Name: %(name)s | |
| 1107 Path: %(path)s | |
| 1108 Desc: %(description)s | |
| 1109 """%templates[name]) | |
| 1110 | |
| 1083 def do_table(self, args): | 1111 def do_table(self, args): |
| 1084 ''"""Usage: table classname [property[,property]*] | 1112 ''"""Usage: table classname [property[,property]*] |
| 1085 List the instances of a class in tabular form. | 1113 List the instances of a class in tabular form. |
| 1086 | 1114 |
| 1087 Lists all instances of the given class. If the properties are not | 1115 Lists all instances of the given class. If the properties are not |
| 1711 try: | 1739 try: |
| 1712 return self.do_install(self.tracker_home, args) | 1740 return self.do_install(self.tracker_home, args) |
| 1713 except UsageError as message: # noqa: F841 | 1741 except UsageError as message: # noqa: F841 |
| 1714 print(_('Error: %(message)s') % locals()) | 1742 print(_('Error: %(message)s') % locals()) |
| 1715 return 1 | 1743 return 1 |
| 1744 elif command == "templates": | |
| 1745 return self.do_templates(args[1:]) | |
| 1716 | 1746 |
| 1717 # get the tracker | 1747 # get the tracker |
| 1718 try: | 1748 try: |
| 1719 tracker = roundup.instance.open(self.tracker_home) | 1749 tracker = roundup.instance.open(self.tracker_home) |
| 1720 except ValueError as message: # noqa: F841 | 1750 except ValueError as message: # noqa: F841 |
