annotate piecrust/commands/builtin/scaffolding.py @ 886:dcdec4b951a1

admin: Get the admin panel working again.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 20 Jun 2017 21:13:08 -0700
parents 58ae026b4c31
children c445a3d5d950
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
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
85 content_item = source.createContent(ctx.args)
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
87 config_tokens = {
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
88 '%title%': "Untitled Content",
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
89 '%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
90 '%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
91 }
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
92 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
93 if config:
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
94 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
95 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
96 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
97
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
98 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
99 mode = 'w' if ctx.args.force else 'x'
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
100 with content_item.open(mode) as f:
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101 f.write(tpl_text)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
103 # 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
104 # 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
105 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
106 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
107 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
108 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
109 shell = False
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
110 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
111 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
112 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
113
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
114 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
115 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
116 else:
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
117 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
118
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
119 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
120 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
121 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
122
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
123
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
124 class DefaultPrepareTemplatesCommandExtension(ChefCommandExtension):
187
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
125 """ 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
126 command.
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
127 """
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
128 def __init__(self):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
129 super(DefaultPrepareTemplatesCommandExtension, self).__init__()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
130 self.command_name = 'prepare'
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
131
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
132 def getTemplateNames(self, app):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
133 return ['default', 'rss', 'atom']
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
134
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
135 def getTemplateDescription(self, app, name):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
136 descs = {
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
137 '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
138 '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
139 'atom': "A fully functional Atom feed."}
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
140 return descs[name]
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
141
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
142 def getTemplate(self, app, name):
879
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
143 from piecrust import RESOURCES_DIR
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
144
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
145 assert name in ['default', 'rss', 'atom']
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
146 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
147 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
148 return fp.read()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
149
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
150
187
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
151 class UserDefinedPrepareTemplatesCommandExtension(ChefCommandExtension):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
152 """ Provides user-defined scaffolding templates to the `prepare`
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
153 command.
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
154 """
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
155 def __init__(self):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
156 super(UserDefinedPrepareTemplatesCommandExtension, self).__init__()
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
157 self.command_name = 'prepare'
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
158
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
159 def _getTemplatesDir(self, app):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
160 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
161
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
162 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
163 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
164 return False
187
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
165 return os.path.isdir(self._getTemplatesDir(app))
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
166
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
167 def getTemplateNames(self, app):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
168 names = os.listdir(self._getTemplatesDir(app))
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
169 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
170
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
171 def getTemplateDescription(self, app, name):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
172 return "User-defined template."
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
173
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
174 def getTemplate(self, app, name):
879
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
175 import glob
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
176
187
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
177 templates_dir = self._getTemplatesDir(app)
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
178 pattern = os.path.join(templates_dir, '%s.*' % name)
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
179 matches = glob.glob(pattern)
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
180 if not matches:
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
181 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
182 if len(matches) > 1:
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
183 raise Exception(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
184 "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
185 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
186 return fp.read()
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
187
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
188
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
189 class DefaultPrepareTemplatesHelpTopic(ChefCommandExtension):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
190 """ Provides help topics for the `prepare` command.
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
191 """
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
192 command_name = 'help'
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 def getHelpTopics(self):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
195 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
196 "Available templates for the 'prepare' command.")]
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
197
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
198 def getHelpTopic(self, topic, app):
879
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
199 import io
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
200 import textwrap
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
201 from piecrust.chefutil import print_help_item
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
202
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
203 with io.StringIO() as tplh:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
204 extensions = app.plugin_loader.getCommandExtensions()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
205 for e in extensions:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
206 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
207 for n in e.getTemplateNames(app):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
208 d = e.getTemplateDescription(app, n)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
209 print_help_item(tplh, n, d)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
210 help_list = tplh.getvalue()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
211
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
212 help_txt = (
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
213 textwrap.fill(
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
214 "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
215 "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
216 "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
217 "\n\n" +
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
218 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
219 "\n\n" +
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
220 help_list +
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
221 "\n" +
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 774
diff changeset
222 "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
223 "`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
224 return help_txt
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
225