annotate piecrust/commands/builtin/scaffolding.py @ 1145:e94737572542

serve: Fix an issue where false positive matches were rendered as the requested page. Now we try to render the page, but also try to detect for the most common "empty" pages.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 05 Jun 2018 22:08:51 -0700
parents a85b2827ba1a
children 74c0c7483986
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import os
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import os.path
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import logging
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 from piecrust.commands.base import ExtendableChefCommand, ChefCommandExtension
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 logger = logging.getLogger(__name__)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 class PrepareCommand(ExtendableChefCommand):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 """ Chef command for creating pages with some default content.
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 """
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 def __init__(self):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 super(PrepareCommand, self).__init__()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 self.name = 'prepare'
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 self.description = "Prepares new content for your website."
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 def setupParser(self, parser, app):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 # Don't setup anything if this is a null app
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 # (for when `chef` is run from outside a website)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 if app.root_dir is None:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 return
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
24 from piecrust.sources.interfaces import IPreparingSource
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
25
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 subparsers = parser.add_subparsers()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 for src in app.sources:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 if not isinstance(src, IPreparingSource):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 logger.debug("Skipping source '%s' because it's not "
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 "preparable." % src.name)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 continue
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 if src.is_theme_source:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 logger.debug("Skipping source '%s' because it's a theme "
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 "source." % src.name)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 continue
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 p = subparsers.add_parser(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
37 src.config['item_name'],
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
38 help=("Creates an empty page in the '%s' source." %
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
39 src.name))
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 src.setupPrepareParser(p, app)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 p.add_argument('-t', '--template', default='default',
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 help="The template to use, which will change the "
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
43 "generated text and header. Run `chef help "
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
44 "scaffolding` for more information.")
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
45 p.add_argument('-f', '--force', action='store_true',
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
46 help="Overwrite any existing content.")
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 p.set_defaults(source=src)
773
87f1e79d3fbe prepare: Use the same convention as other commands with sub-commands.
Ludovic Chabant <ludovic@chabant.com>
parents: 535
diff changeset
48 p.set_defaults(sub_func=self._doRun)
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49
773
87f1e79d3fbe prepare: Use the same convention as other commands with sub-commands.
Ludovic Chabant <ludovic@chabant.com>
parents: 535
diff changeset
50 def checkedRun(self, ctx):
879
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
51 from piecrust.pathutil import SiteNotFoundError
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
52
773
87f1e79d3fbe prepare: Use the same convention as other commands with sub-commands.
Ludovic Chabant <ludovic@chabant.com>
parents: 535
diff changeset
53 if ctx.app.root_dir is None:
87f1e79d3fbe prepare: Use the same convention as other commands with sub-commands.
Ludovic Chabant <ludovic@chabant.com>
parents: 535
diff changeset
54 raise SiteNotFoundError(theme=ctx.app.theme_site)
87f1e79d3fbe prepare: Use the same convention as other commands with sub-commands.
Ludovic Chabant <ludovic@chabant.com>
parents: 535
diff changeset
55
87f1e79d3fbe prepare: Use the same convention as other commands with sub-commands.
Ludovic Chabant <ludovic@chabant.com>
parents: 535
diff changeset
56 if not hasattr(ctx.args, 'sub_func'):
87f1e79d3fbe prepare: Use the same convention as other commands with sub-commands.
Ludovic Chabant <ludovic@chabant.com>
parents: 535
diff changeset
57 ctx.parser.parse_args(['prepare', '--help'])
87f1e79d3fbe prepare: Use the same convention as other commands with sub-commands.
Ludovic Chabant <ludovic@chabant.com>
parents: 535
diff changeset
58 return
87f1e79d3fbe prepare: Use the same convention as other commands with sub-commands.
Ludovic Chabant <ludovic@chabant.com>
parents: 535
diff changeset
59 ctx.args.sub_func(ctx)
87f1e79d3fbe prepare: Use the same convention as other commands with sub-commands.
Ludovic Chabant <ludovic@chabant.com>
parents: 535
diff changeset
60
87f1e79d3fbe prepare: Use the same convention as other commands with sub-commands.
Ludovic Chabant <ludovic@chabant.com>
parents: 535
diff changeset
61 def _doRun(self, ctx):
879
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
62 import time
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
63 from piecrust.uriutil import multi_replace
879
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
64 from piecrust.sources.fs import FSContentSourceBase
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
65
165
8355eb9dd8fe prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
66 if not hasattr(ctx.args, 'source'):
8355eb9dd8fe prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
67 raise Exception("No source specified. "
8355eb9dd8fe prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
68 "Please run `chef prepare -h` for usage.")
8355eb9dd8fe prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
69
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 app = ctx.app
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 tpl_name = ctx.args.template
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 extensions = self.getExtensions(app)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 ext = next(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
74 filter(
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
75 lambda e: tpl_name in e.getTemplateNames(app),
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
76 extensions),
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
77 None)
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 if ext is None:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 raise Exception("No such page template: %s" % tpl_name)
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
80 tpl_text = ext.getTemplate(app, tpl_name)
187
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
81 if tpl_text is None:
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
82 raise Exception("Error loading template: %s" % tpl_name)
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
83
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
84 source = ctx.args.source
892
c445a3d5d950 internal: Make `createContent` use a dictionary-like object.
Ludovic Chabant <ludovic@chabant.com>
parents: 879
diff changeset
85 content_item = source.createContent(vars(ctx.args))
947
a85b2827ba1a prepare: Fix old API calls.
Ludovic Chabant <ludovic@chabant.com>
parents: 892
diff changeset
86 if content_item is None:
a85b2827ba1a prepare: Fix old API calls.
Ludovic Chabant <ludovic@chabant.com>
parents: 892
diff changeset
87 raise Exception("Can't create item.")
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
89 config_tokens = {
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
90 '%title%': "Untitled Content",
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
91 '%time.today%': time.strftime('%Y/%m/%d'),
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
92 '%time.now%': time.strftime('%H:%M:%S')
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
93 }
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
94 config = content_item.metadata.get('config')
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
95 if config:
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
96 for k, v in config.items():
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
97 config_tokens['%%%s%%' % k] = v
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
98 tpl_text = multi_replace(tpl_text, config_tokens)
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
100 logger.info("Creating content: %s" % content_item.spec)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
101 mode = 'w' if ctx.args.force else 'x'
947
a85b2827ba1a prepare: Fix old API calls.
Ludovic Chabant <ludovic@chabant.com>
parents: 892
diff changeset
102 with source.openItem(content_item, mode) as f:
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 f.write(tpl_text)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
105 # If this was a file-system content item, see if we need to auto-open
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
106 # an editor on it.
774
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
107 editor = ctx.app.config.get('prepare/editor')
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
108 editor_type = ctx.app.config.get('prepare/editor_type', 'exe')
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
109 if editor and isinstance(source, FSContentSourceBase):
774
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
110 import shlex
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
111 shell = False
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
112 args = '%s "%s"' % (editor, content_item.spec)
774
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
113 if '%path%' in editor:
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
114 args = editor.replace('%path%', content_item.spec)
774
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
115
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
116 if editor_type.lower() == 'shell':
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
117 shell = True
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
118 else:
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
119 args = shlex.split(args)
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
120
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
121 import subprocess
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
122 logger.info("Running: %s" % args)
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
123 subprocess.Popen(args, shell=shell)
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
124
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
125
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
126 class DefaultPrepareTemplatesCommandExtension(ChefCommandExtension):
187
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
127 """ Provides the default scaffolding templates to the `prepare`
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
128 command.
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
129 """
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
130 def __init__(self):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
131 super(DefaultPrepareTemplatesCommandExtension, self).__init__()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
132 self.command_name = 'prepare'
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
133
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
134 def getTemplateNames(self, app):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
135 return ['default', 'rss', 'atom']
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
136
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
137 def getTemplateDescription(self, app, name):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
138 descs = {
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
139 'default': "The default template, for a simple page.",
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
140 'rss': "A fully functional RSS feed.",
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
141 'atom': "A fully functional Atom feed."}
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
142 return descs[name]
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
143
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
144 def getTemplate(self, app, name):
879
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
145 from piecrust import RESOURCES_DIR
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
146
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
147 assert name in ['default', 'rss', 'atom']
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
148 src_path = os.path.join(RESOURCES_DIR, 'prepare', '%s.html' % name)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
149 with open(src_path, 'r', encoding='utf8') as fp:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
150 return fp.read()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
151
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
152
187
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
153 class UserDefinedPrepareTemplatesCommandExtension(ChefCommandExtension):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
154 """ Provides user-defined scaffolding templates to the `prepare`
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
155 command.
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
156 """
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
157 def __init__(self):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
158 super(UserDefinedPrepareTemplatesCommandExtension, self).__init__()
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
159 self.command_name = 'prepare'
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
160
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
161 def _getTemplatesDir(self, app):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
162 return os.path.join(app.root_dir, 'scaffold/pages')
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
163
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
164 def supports(self, app):
534
5bbeb11fe8d9 bug: Fix crash running `chef help scaffolding` outside of a website.
Ludovic Chabant <ludovic@chabant.com>
parents: 363
diff changeset
165 if not app.root_dir:
5bbeb11fe8d9 bug: Fix crash running `chef help scaffolding` outside of a website.
Ludovic Chabant <ludovic@chabant.com>
parents: 363
diff changeset
166 return False
187
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
167 return os.path.isdir(self._getTemplatesDir(app))
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
168
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
169 def getTemplateNames(self, app):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
170 names = os.listdir(self._getTemplatesDir(app))
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
171 return map(lambda n: os.path.splitext(n)[0], names)
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
172
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
173 def getTemplateDescription(self, app, name):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
174 return "User-defined template."
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
175
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
176 def getTemplate(self, app, name):
879
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
177 import glob
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
178
187
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
179 templates_dir = self._getTemplatesDir(app)
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
180 pattern = os.path.join(templates_dir, '%s.*' % name)
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
181 matches = glob.glob(pattern)
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
182 if not matches:
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
183 raise Exception("No such page scaffolding template: %s" % name)
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
184 if len(matches) > 1:
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
185 raise Exception(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
186 "More than one scaffolding template has name: %s" % name)
187
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
187 with open(matches[0], 'r', encoding='utf8') as fp:
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
188 return fp.read()
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
189
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
190
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
191 class DefaultPrepareTemplatesHelpTopic(ChefCommandExtension):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
192 """ Provides help topics for the `prepare` command.
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
193 """
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
194 command_name = 'help'
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
195
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
196 def getHelpTopics(self):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
197 return [('scaffolding',
165
8355eb9dd8fe prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
198 "Available templates for the 'prepare' command.")]
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
199
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
200 def getHelpTopic(self, topic, app):
879
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
201 import io
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
202 import textwrap
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
203 from piecrust.chefutil import print_help_item
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
204
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
205 with io.StringIO() as tplh:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
206 extensions = app.plugin_loader.getCommandExtensions()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
207 for e in extensions:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
208 if e.command_name == 'prepare' and e.supports(app):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
209 for n in e.getTemplateNames(app):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
210 d = e.getTemplateDescription(app, n)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
211 print_help_item(tplh, n, d)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
212 help_list = tplh.getvalue()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
213
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
214 help_txt = (
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
215 textwrap.fill(
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
216 "Running the 'prepare' command will let "
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
217 "PieCrust setup a page for you in the correct place, with "
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
218 "some hopefully useful default text.") +
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
219 "\n\n" +
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
220 textwrap.fill("The following templates are available:") +
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
221 "\n\n" +
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
222 help_list +
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
223 "\n" +
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
224 "You can add user-defined templates by creating pages in a "
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
225 "`scaffold/pages` sub-directory in your website.")
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
226 return help_txt
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
227