Mercurial > piecrust2
annotate piecrust/commands/builtin/info.py @ 1188:a7c43131d871
bake: Fix file write flushing problem with Python 3.8+
Writing the cache files fails in Python 3.8 because it looks like flushing
behaviour has changed. We need to explicitly flush. And even then, in very
rare occurrences, it looks like it can still run into racing conditions,
so we do a very hacky and ugly "retry" loop when fetching cached data :(
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 15 Jun 2021 22:36:23 -0700 |
parents | 986ecdaa2a36 |
children |
rev | line source |
---|---|
1134
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
1 import os |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
2 import os.path |
1
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 import logging |
1099
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
4 from piecrust.commands.base import ( |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
5 ChefCommand, ChefCommandExtension, _ResourcesHelpTopics) |
1
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 |
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 |
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 logger = logging.getLogger(__name__) |
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 |
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 |
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 class RootCommand(ChefCommand): |
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 def __init__(self): |
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 super(RootCommand, self).__init__() |
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 self.name = 'root' |
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 self.description = "Gets the root directory of the current website." |
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
17 def setupParser(self, parser, app): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
18 pass |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
19 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
20 def run(self, ctx): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
21 logger.info(ctx.app.root_dir) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
22 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
23 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
24 class ShowConfigCommand(ChefCommand): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
25 def __init__(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
26 super(ShowConfigCommand, self).__init__() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
27 self.name = 'showconfig' |
755
f9d926669d7a
chef: Make all the commands descriptions fit in one line.
Ludovic Chabant <ludovic@chabant.com>
parents:
731
diff
changeset
|
28 self.description = ("Shows the website's configuration.") |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
29 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
30 def setupParser(self, parser, app): |
161
516db87a04d4
cosmetic: pep8 compliance.
Ludovic Chabant <ludovic@chabant.com>
parents:
107
diff
changeset
|
31 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
32 'path', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
33 help="The path to a config section or value", |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
34 nargs='?') |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
35 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
36 def run(self, ctx): |
879
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
37 import yaml |
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
38 from piecrust.configuration import ConfigurationDumper |
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
39 |
567
a65f04ddbea2
showconfig: Don't crash when the whole config should be shown.
Ludovic Chabant <ludovic@chabant.com>
parents:
340
diff
changeset
|
40 if ctx.args.path: |
a65f04ddbea2
showconfig: Don't crash when the whole config should be shown.
Ludovic Chabant <ludovic@chabant.com>
parents:
340
diff
changeset
|
41 show = ctx.app.config.get(ctx.args.path) |
a65f04ddbea2
showconfig: Don't crash when the whole config should be shown.
Ludovic Chabant <ludovic@chabant.com>
parents:
340
diff
changeset
|
42 else: |
a65f04ddbea2
showconfig: Don't crash when the whole config should be shown.
Ludovic Chabant <ludovic@chabant.com>
parents:
340
diff
changeset
|
43 show = ctx.app.config.getAll() |
a65f04ddbea2
showconfig: Don't crash when the whole config should be shown.
Ludovic Chabant <ludovic@chabant.com>
parents:
340
diff
changeset
|
44 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
45 if show is not None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
46 if isinstance(show, (dict, list)): |
107
10fc9c8bf682
Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
47 out = yaml.dump(show, default_flow_style=False, |
10fc9c8bf682
Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
48 Dumper=ConfigurationDumper) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
49 logger.info(out) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
50 else: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
51 logger.info(show) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
52 elif ctx.args.path: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
53 logger.error("No such configuration path: %s" % ctx.args.path) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
54 ctx.result = 1 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
55 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
56 |
163
6d23473fab41
sources: Add `chef sources` command to list page sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
162
diff
changeset
|
57 class ShowSourcesCommand(ChefCommand): |
6d23473fab41
sources: Add `chef sources` command to list page sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
162
diff
changeset
|
58 def __init__(self): |
6d23473fab41
sources: Add `chef sources` command to list page sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
162
diff
changeset
|
59 super(ShowSourcesCommand, self).__init__() |
6d23473fab41
sources: Add `chef sources` command to list page sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
162
diff
changeset
|
60 self.name = 'sources' |
6d23473fab41
sources: Add `chef sources` command to list page sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
162
diff
changeset
|
61 self.description = "Shows the sources defined for this website." |
6d23473fab41
sources: Add `chef sources` command to list page sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
162
diff
changeset
|
62 |
6d23473fab41
sources: Add `chef sources` command to list page sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
162
diff
changeset
|
63 def setupParser(self, parser, app): |
6d23473fab41
sources: Add `chef sources` command to list page sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
162
diff
changeset
|
64 pass |
6d23473fab41
sources: Add `chef sources` command to list page sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
162
diff
changeset
|
65 |
6d23473fab41
sources: Add `chef sources` command to list page sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
162
diff
changeset
|
66 def run(self, ctx): |
6d23473fab41
sources: Add `chef sources` command to list page sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
162
diff
changeset
|
67 for src in ctx.app.sources: |
6d23473fab41
sources: Add `chef sources` command to list page sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
162
diff
changeset
|
68 logger.info("%s:" % src.name) |
6d23473fab41
sources: Add `chef sources` command to list page sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
162
diff
changeset
|
69 logger.info(" type: %s" % src.config.get('type')) |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
70 logger.debug(" class: %s" % type(src)) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
71 desc = src.describe() |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
72 if isinstance(desc, dict): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
73 for k, v in desc.items(): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
74 logger.info(" %s: %s" % (k, v)) |
163
6d23473fab41
sources: Add `chef sources` command to list page sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
162
diff
changeset
|
75 |
6d23473fab41
sources: Add `chef sources` command to list page sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
162
diff
changeset
|
76 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
77 class ShowRoutesCommand(ChefCommand): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
78 def __init__(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
79 super(ShowRoutesCommand, self).__init__() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
80 self.name = 'routes' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
81 self.description = "Shows the routes defined for this website." |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
82 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
83 def setupParser(self, parser, app): |
1
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
84 pass |
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
85 |
1099
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
86 def provideExtensions(self): |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
87 return [RoutesHelpTopic()] |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
88 |
1
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
89 def run(self, ctx): |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
90 for route in ctx.app.routes: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
91 logger.info("%s:" % route.uri_pattern) |
711
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
567
diff
changeset
|
92 logger.info(" source: %s" % (route.source_name or '')) |
168
56d6b17e057b
routes: Show regex patterns for routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
164
diff
changeset
|
93 logger.info(" regex: %s" % route.uri_re.pattern) |
731
dafb7d76c2ba
routes: Show the route template function.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
94 logger.info(" function: %s(%s)" % ( |
787
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
755
diff
changeset
|
95 route.func_name, |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
96 ', '.join(route.uri_params))) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
97 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
98 |
1099
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
99 class RoutesHelpTopic(ChefCommandExtension, _ResourcesHelpTopics): |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
100 command_name = 'help' |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
101 |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
102 def getHelpTopics(self): |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
103 return [('routes_config', |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
104 "Specifying URL routes for your site's content."), |
1103
6462c4a87532
routes: Make help topic names consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
1099
diff
changeset
|
105 ('routes_params', |
1099
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
106 "Show the available route parameters.")] |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
107 |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
108 def getHelpTopic(self, topic, app): |
1103
6462c4a87532
routes: Make help topic names consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
1099
diff
changeset
|
109 if topic != 'routes_params': |
1099
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
110 return _ResourcesHelpTopics.getHelpTopic(self, topic, app) |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
111 |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
112 import textwrap |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
113 |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
114 help_txt = ( |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
115 textwrap.fill( |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
116 "Route parameters can be used as placeholders when specifying " |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
117 "route URL patterns in your site configuration. See " |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
118 "`chef help routes_config` for more information.") + |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
119 "\n\n") |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
120 if app.root_dir is None: |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
121 help_txt += textwrap.fill( |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
122 "Running this help command in a PieCrust website would show " |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
123 "the route parameters available for your site's sources. " |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
124 "However, no PieCrust website has been found in the current " |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
125 "working directory. ") |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
126 return help_txt |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
127 |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
128 srcs_by_types = {} |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
129 for src in app.sources: |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
130 srcs_by_types.setdefault(src.SOURCE_NAME, []).append(src) |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
131 |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
132 for type_name, srcs in srcs_by_types.items(): |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
133 help_txt += textwrap.fill( |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
134 "Route parameters for '%s' sources (%s):" % ( |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
135 type_name, ', '.join([s.name for s in srcs]))) |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
136 help_txt += "\n" |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
137 for rp in srcs[0].getSupportedRouteParameters(): |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
138 help_txt += " - %s\n" % rp.param_name |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
139 help_txt += "\n" |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
140 return help_txt |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
141 |
07c23be08029
help: Add new help topics on routes.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
142 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
143 class ShowPathsCommand(ChefCommand): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
144 def __init__(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
145 super(ShowPathsCommand, self).__init__() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
146 self.name = 'paths' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
147 self.description = "Shows the paths that this website is using." |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
148 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
149 def setupParser(self, parser, app): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
150 pass |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
151 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
152 def run(self, ctx): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
153 app = ctx.app |
307
869a206facd5
internal: Remove mentions of plugins directories and sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
168
diff
changeset
|
154 paths = ['theme_dir', 'templates_dirs', 'cache_dir'] |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
155 for p in paths: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
156 value = getattr(app, p) |
162
c4b155b08b52
paths: properly format lists of paths.
Ludovic Chabant <ludovic@chabant.com>
parents:
161
diff
changeset
|
157 if isinstance(value, list): |
c4b155b08b52
paths: properly format lists of paths.
Ludovic Chabant <ludovic@chabant.com>
parents:
161
diff
changeset
|
158 logging.info("%s:" % p) |
c4b155b08b52
paths: properly format lists of paths.
Ludovic Chabant <ludovic@chabant.com>
parents:
161
diff
changeset
|
159 for v in value: |
c4b155b08b52
paths: properly format lists of paths.
Ludovic Chabant <ludovic@chabant.com>
parents:
161
diff
changeset
|
160 logging.info(" - %s" % v) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
161 else: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
162 logging.info("%s: %s" % (p, value)) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
163 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
164 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
165 class FindCommand(ChefCommand): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
166 def __init__(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
167 super(FindCommand, self).__init__() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
168 self.name = 'find' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
169 self.description = "Find pages in the website." |
1
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
170 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
171 def setupParser(self, parser, app): |
161
516db87a04d4
cosmetic: pep8 compliance.
Ludovic Chabant <ludovic@chabant.com>
parents:
107
diff
changeset
|
172 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
173 'pattern', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
174 help="The pattern to match with page filenames", |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
175 nargs='?') |
161
516db87a04d4
cosmetic: pep8 compliance.
Ludovic Chabant <ludovic@chabant.com>
parents:
107
diff
changeset
|
176 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
177 '-n', '--name', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
178 help="Limit the search to sources matching this name") |
161
516db87a04d4
cosmetic: pep8 compliance.
Ludovic Chabant <ludovic@chabant.com>
parents:
107
diff
changeset
|
179 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
180 '--full-path', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
181 help="Return full paths instead of root-relative paths", |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
182 action='store_true') |
161
516db87a04d4
cosmetic: pep8 compliance.
Ludovic Chabant <ludovic@chabant.com>
parents:
107
diff
changeset
|
183 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
184 '--metadata', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
185 help="Return metadata about the page instead of just the path", |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
186 action='store_true') |
164
4534ccbdd2a3
find: Fix the `find` command, add more options.
Ludovic Chabant <ludovic@chabant.com>
parents:
163
diff
changeset
|
187 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
188 '--include-theme', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
189 help="Include theme pages to the search", |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
190 action='store_true') |
164
4534ccbdd2a3
find: Fix the `find` command, add more options.
Ludovic Chabant <ludovic@chabant.com>
parents:
163
diff
changeset
|
191 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
192 '--exact', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
193 help=("Match the exact given pattern, instead of any page " |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
194 "containing the pattern"), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
195 action='store_true') |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
196 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
197 def run(self, ctx): |
879
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
198 import fnmatch |
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
199 from piecrust.sources.fs import FSContentSourceBase |
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
200 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
201 pattern = ctx.args.pattern |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
202 sources = list(ctx.app.sources) |
340
794b047c9726
find: Don't change the pattern when there's none.
Ludovic Chabant <ludovic@chabant.com>
parents:
334
diff
changeset
|
203 if not ctx.args.exact and pattern is not None: |
164
4534ccbdd2a3
find: Fix the `find` command, add more options.
Ludovic Chabant <ludovic@chabant.com>
parents:
163
diff
changeset
|
204 pattern = '*%s*' % pattern |
4534ccbdd2a3
find: Fix the `find` command, add more options.
Ludovic Chabant <ludovic@chabant.com>
parents:
163
diff
changeset
|
205 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
206 for src in sources: |
164
4534ccbdd2a3
find: Fix the `find` command, add more options.
Ludovic Chabant <ludovic@chabant.com>
parents:
163
diff
changeset
|
207 if not ctx.args.include_theme and src.is_theme_source: |
4534ccbdd2a3
find: Fix the `find` command, add more options.
Ludovic Chabant <ludovic@chabant.com>
parents:
163
diff
changeset
|
208 continue |
4534ccbdd2a3
find: Fix the `find` command, add more options.
Ludovic Chabant <ludovic@chabant.com>
parents:
163
diff
changeset
|
209 if ctx.args.name and not fnmatch.fnmatch(src.name, ctx.args.name): |
4534ccbdd2a3
find: Fix the `find` command, add more options.
Ludovic Chabant <ludovic@chabant.com>
parents:
163
diff
changeset
|
210 continue |
4534ccbdd2a3
find: Fix the `find` command, add more options.
Ludovic Chabant <ludovic@chabant.com>
parents:
163
diff
changeset
|
211 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
212 is_fs_src = isinstance(src, FSContentSourceBase) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
213 items = src.getAllContents() |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
214 for item in items: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
215 if ctx.args.metadata: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
216 logger.info("spec:%s" % item.spec) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
217 for key, val in item.metadata.items(): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
218 logger.info("%s:%s" % (key, val)) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
219 logger.info("---") |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
220 else: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
221 if is_fs_src: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
222 name = os.path.relpath(item.spec, ctx.app.root_dir) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
223 if pattern is None or fnmatch.fnmatch(name, pattern): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
224 if ctx.args.metadata: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
225 logger.info("path:%s" % item.spec) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
226 for key, val in item.metadata.items(): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
227 logger.info("%s:%s" % (key, val)) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
228 logger.info("---") |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
229 else: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
230 if ctx.args.full_path: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
231 name = item.spec |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
232 logger.info(name) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
233 else: |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
234 if pattern is None or fnmatch.fnmatch(name, pattern): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
235 logger.info(item.spec) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
236 |
1134
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
237 |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
238 class UrlCommand(ChefCommand): |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
239 def __init__(self): |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
240 super().__init__() |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
241 self.name = 'url' |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
242 self.description = "Gets the URL to a given page." |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
243 |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
244 def setupParser(self, parser, app): |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
245 parser.add_argument( |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
246 'path', |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
247 help="The path to the page.") |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
248 parser.add_argument( |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
249 '-f', '--func', |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
250 dest='tpl_func', |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
251 action='store_true', |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
252 help="Return the template function call instead of the URL.") |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
253 |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
254 def run(self, ctx): |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
255 from piecrust.sources.fs import FSContentSourceBase |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
256 from piecrust.routing import RouteParameter |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
257 |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
258 # Find which source this page might belong to. |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
259 full_path = os.path.join(ctx.app.root_dir, ctx.args.path) |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
260 for src in ctx.app.sources: |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
261 if not isinstance(src, FSContentSourceBase): |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
262 continue |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
263 |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
264 if full_path.startswith(src.fs_endpoint_path + os.sep): |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
265 parent_src = src |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
266 break |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
267 else: |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
268 raise Exception("Can't find which source this page belongs to.") |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
269 |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
270 route = ctx.app.getSourceRoute(parent_src.name) |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
271 content_item = parent_src.findContentFromSpec(full_path) |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
272 route_params = content_item.metadata['route_params'] |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
273 |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
274 if ctx.args.tpl_func: |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
275 if not route.func_name: |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
276 raise Exception("Source '%s' doesn't have a route with " |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
277 "a template function name defined." % |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
278 parent_src.name) |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
279 |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
280 url = '%s(' % route.func_name |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
281 for i, p in enumerate(route.uri_params): |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
282 if i > 0: |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
283 url += ', ' |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
284 pinfo = route.getParameter(p) |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
285 if pinfo.param_type == RouteParameter.TYPE_INT2: |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
286 url += '%02d' % route_params[p] |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
287 elif pinfo.param_type == RouteParameter.TYPE_INT4: |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
288 url += '%04d' % route_params[p] |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
289 else: |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
290 url += str(route_params[p]) |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
291 url += ')' |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
292 logger.info(url) |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
293 else: |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
294 url = route.getUri(route_params) |
986ecdaa2a36
url: New `url` command to get the URL of a page from its path.
Ludovic Chabant <ludovic@chabant.com>
parents:
1103
diff
changeset
|
295 logger.info(url) |