Mercurial > piecrust2
comparison piecrust/commands/builtin/info.py @ 1099:07c23be08029
help: Add new help topics on routes.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 17 Feb 2018 11:53:03 -0800 |
parents | 58ae026b4c31 |
children | 6462c4a87532 |
comparison
equal
deleted
inserted
replaced
1098:2323f0788170 | 1099:07c23be08029 |
---|---|
1 import os.path | 1 import os.path |
2 import logging | 2 import logging |
3 from piecrust.commands.base import ChefCommand | 3 from piecrust.commands.base import ( |
4 ChefCommand, ChefCommandExtension, _ResourcesHelpTopics) | |
4 | 5 |
5 | 6 |
6 logger = logging.getLogger(__name__) | 7 logger = logging.getLogger(__name__) |
7 | 8 |
8 | 9 |
79 self.description = "Shows the routes defined for this website." | 80 self.description = "Shows the routes defined for this website." |
80 | 81 |
81 def setupParser(self, parser, app): | 82 def setupParser(self, parser, app): |
82 pass | 83 pass |
83 | 84 |
85 def provideExtensions(self): | |
86 return [RoutesHelpTopic()] | |
87 | |
84 def run(self, ctx): | 88 def run(self, ctx): |
85 for route in ctx.app.routes: | 89 for route in ctx.app.routes: |
86 logger.info("%s:" % route.uri_pattern) | 90 logger.info("%s:" % route.uri_pattern) |
87 logger.info(" source: %s" % (route.source_name or '')) | 91 logger.info(" source: %s" % (route.source_name or '')) |
88 logger.info(" regex: %s" % route.uri_re.pattern) | 92 logger.info(" regex: %s" % route.uri_re.pattern) |
89 logger.info(" function: %s(%s)" % ( | 93 logger.info(" function: %s(%s)" % ( |
90 route.func_name, | 94 route.func_name, |
91 ', '.join(route.uri_params))) | 95 ', '.join(route.uri_params))) |
96 | |
97 | |
98 class RoutesHelpTopic(ChefCommandExtension, _ResourcesHelpTopics): | |
99 command_name = 'help' | |
100 | |
101 def getHelpTopics(self): | |
102 return [('routes_config', | |
103 "Specifying URL routes for your site's content."), | |
104 ('route_params', | |
105 "Show the available route parameters.")] | |
106 | |
107 def getHelpTopic(self, topic, app): | |
108 if topic != 'route_params': | |
109 return _ResourcesHelpTopics.getHelpTopic(self, topic, app) | |
110 | |
111 import textwrap | |
112 | |
113 help_txt = ( | |
114 textwrap.fill( | |
115 "Route parameters can be used as placeholders when specifying " | |
116 "route URL patterns in your site configuration. See " | |
117 "`chef help routes_config` for more information.") + | |
118 "\n\n") | |
119 if app.root_dir is None: | |
120 help_txt += textwrap.fill( | |
121 "Running this help command in a PieCrust website would show " | |
122 "the route parameters available for your site's sources. " | |
123 "However, no PieCrust website has been found in the current " | |
124 "working directory. ") | |
125 return help_txt | |
126 | |
127 srcs_by_types = {} | |
128 for src in app.sources: | |
129 srcs_by_types.setdefault(src.SOURCE_NAME, []).append(src) | |
130 | |
131 for type_name, srcs in srcs_by_types.items(): | |
132 help_txt += textwrap.fill( | |
133 "Route parameters for '%s' sources (%s):" % ( | |
134 type_name, ', '.join([s.name for s in srcs]))) | |
135 help_txt += "\n" | |
136 for rp in srcs[0].getSupportedRouteParameters(): | |
137 help_txt += " - %s\n" % rp.param_name | |
138 help_txt += "\n" | |
139 return help_txt | |
92 | 140 |
93 | 141 |
94 class ShowPathsCommand(ChefCommand): | 142 class ShowPathsCommand(ChefCommand): |
95 def __init__(self): | 143 def __init__(self): |
96 super(ShowPathsCommand, self).__init__() | 144 super(ShowPathsCommand, self).__init__() |