annotate piecrust/commands/builtin/scaffolding.py @ 778:5e91bc0e3b4d

internal: Move admin panel code into the piecrust package.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 16 Jul 2016 15:02:24 +0200
parents 2bb3c1a04e98
children 4850f8c21b6e
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 re
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 import io
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 import time
187
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
6 import glob
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 import logging
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 import textwrap
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 from piecrust import RESOURCES_DIR
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 from piecrust.chefutil import print_help_item
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 from piecrust.commands.base import ExtendableChefCommand, ChefCommandExtension
242
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 187
diff changeset
12 from piecrust.sources.base import MODE_CREATING
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 187
diff changeset
13 from piecrust.sources.interfaces import IPreparingSource
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 from piecrust.uriutil import multi_replace
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 logger = logging.getLogger(__name__)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 def make_title(slug):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 slug = re.sub(r'[\-_]', ' ', slug)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 return slug.title()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 class PrepareCommand(ExtendableChefCommand):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 """ 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
27 """
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 def __init__(self):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 super(PrepareCommand, self).__init__()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 self.name = 'prepare'
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 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
32
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 def setupParser(self, parser, app):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 # 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
35 # (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
36 if app.root_dir is None:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 return
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 subparsers = parser.add_subparsers()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 for src in app.sources:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 if not isinstance(src, IPreparingSource):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 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
43 "preparable." % src.name)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 continue
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 if src.is_theme_source:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 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
47 "source." % src.name)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 continue
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 p = subparsers.add_parser(
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50 src.item_name,
165
8355eb9dd8fe prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
51 help=("Creates an empty page in the '%s' source." %
8355eb9dd8fe prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
52 src.name))
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 src.setupPrepareParser(p, app)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 p.add_argument('-t', '--template', default='default',
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 help="The template to use, which will change the "
535
03c3a77fda60 prepare: More help about scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents: 534
diff changeset
56 "generated text and header. Run `chef help "
03c3a77fda60 prepare: More help about scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents: 534
diff changeset
57 "scaffolding` for more information.")
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 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
59 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
60
773
87f1e79d3fbe prepare: Use the same convention as other commands with sub-commands.
Ludovic Chabant <ludovic@chabant.com>
parents: 535
diff changeset
61 def checkedRun(self, ctx):
87f1e79d3fbe prepare: Use the same convention as other commands with sub-commands.
Ludovic Chabant <ludovic@chabant.com>
parents: 535
diff changeset
62 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
63 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
64
87f1e79d3fbe prepare: Use the same convention as other commands with sub-commands.
Ludovic Chabant <ludovic@chabant.com>
parents: 535
diff changeset
65 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
66 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
67 return
87f1e79d3fbe prepare: Use the same convention as other commands with sub-commands.
Ludovic Chabant <ludovic@chabant.com>
parents: 535
diff changeset
68 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
69
87f1e79d3fbe prepare: Use the same convention as other commands with sub-commands.
Ludovic Chabant <ludovic@chabant.com>
parents: 535
diff changeset
70 def _doRun(self, ctx):
165
8355eb9dd8fe prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
71 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
72 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
73 "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
74
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 app = ctx.app
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 source = ctx.args.source
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77 metadata = source.buildMetadata(ctx.args)
363
dd25bd3ce1f9 serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 242
diff changeset
78 factory = source.findPageFactory(metadata, MODE_CREATING)
dd25bd3ce1f9 serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 242
diff changeset
79 path = factory.path
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 name, ext = os.path.splitext(path)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81 if ext == '.*':
165
8355eb9dd8fe prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
82 path = '%s.%s' % (
8355eb9dd8fe prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
83 name,
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 app.config.get('site/default_auto_format'))
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 if os.path.exists(path):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 raise Exception("'%s' already exists." % path)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 tpl_name = ctx.args.template
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 extensions = self.getExtensions(app)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90 ext = next(
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 filter(
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 lambda e: tpl_name in e.getTemplateNames(ctx.app),
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 extensions),
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94 None)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 if ext is None:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 raise Exception("No such page template: %s" % tpl_name)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98 tpl_text = ext.getTemplate(ctx.app, tpl_name)
187
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
99 if tpl_text is None:
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
100 raise Exception("Error loading template: %s" % tpl_name)
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101 title = (metadata.get('slug') or metadata.get('path') or
165
8355eb9dd8fe prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
102 'Untitled page')
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 title = make_title(title)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 tokens = {
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105 '%title%': title,
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106 '%time.today%': time.strftime('%Y/%m/%d'),
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107 '%time.now%': time.strftime('%H:%M:%S')}
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108 tpl_text = multi_replace(tpl_text, tokens)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 logger.info("Creating page: %s" % os.path.relpath(path, app.root_dir))
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111 if not os.path.exists(os.path.dirname(path)):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
112 os.makedirs(os.path.dirname(path), 0o755)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
113
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114 with open(path, 'w') as f:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
115 f.write(tpl_text)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
116
774
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
117 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
118 editor_type = ctx.app.config.get('prepare/editor_type', 'exe')
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
119 if editor:
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
120 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
121 shell = False
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
122 args = '%s "%s"' % (editor, path)
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
123 if '%path%' in editor:
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
124 args = editor.replace('%path%', path)
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
125
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
126 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
127 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
128 else:
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
129 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
130
2bb3c1a04e98 prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents: 773
diff changeset
131 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
132 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
133 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
134
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
135
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
136 class DefaultPrepareTemplatesCommandExtension(ChefCommandExtension):
187
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
137 """ 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
138 command.
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
139 """
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
140 def __init__(self):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
141 super(DefaultPrepareTemplatesCommandExtension, self).__init__()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
142 self.command_name = 'prepare'
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 getTemplateNames(self, app):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
145 return ['default', 'rss', 'atom']
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
146
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
147 def getTemplateDescription(self, app, name):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
148 descs = {
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
149 'default': "The default template, for a simple page.",
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
150 'rss': "A fully functional RSS feed.",
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
151 'atom': "A fully functional Atom feed."}
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
152 return descs[name]
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
153
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
154 def getTemplate(self, app, name):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
155 assert name in ['default', 'rss', 'atom']
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
156 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
157 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
158 return fp.read()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
159
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
160
187
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
161 class UserDefinedPrepareTemplatesCommandExtension(ChefCommandExtension):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
162 """ Provides user-defined scaffolding templates to the `prepare`
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
163 command.
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
164 """
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
165 def __init__(self):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
166 super(UserDefinedPrepareTemplatesCommandExtension, self).__init__()
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
167 self.command_name = 'prepare'
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 _getTemplatesDir(self, app):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
170 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
171
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
172 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
173 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
174 return False
187
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
175 return os.path.isdir(self._getTemplatesDir(app))
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
176
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
177 def getTemplateNames(self, app):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
178 names = os.listdir(self._getTemplatesDir(app))
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
179 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
180
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
181 def getTemplateDescription(self, app, name):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
182 return "User-defined template."
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
183
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
184 def getTemplate(self, app, name):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
185 templates_dir = self._getTemplatesDir(app)
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
186 pattern = os.path.join(templates_dir, '%s.*' % name)
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
187 matches = glob.glob(pattern)
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
188 if not matches:
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
189 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
190 if len(matches) > 1:
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
191 raise Exception(
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
192 "More than one scaffolding template has name: %s" % name)
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
193 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
194 return fp.read()
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
195
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
196
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
197 class DefaultPrepareTemplatesHelpTopic(ChefCommandExtension):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
198 """ Provides help topics for the `prepare` command.
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 command_name = 'help'
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
201
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
202 def getHelpTopics(self):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
203 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
204 "Available templates for the 'prepare' command.")]
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
205
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
206 def getHelpTopic(self, topic, app):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
207 with io.StringIO() as tplh:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
208 extensions = app.plugin_loader.getCommandExtensions()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
209 for e in extensions:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
210 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
211 for n in e.getTemplateNames(app):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
212 d = e.getTemplateDescription(app, n)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
213 print_help_item(tplh, n, d)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
214 help_list = tplh.getvalue()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
215
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
216 help_txt = (
165
8355eb9dd8fe prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
217 textwrap.fill(
8355eb9dd8fe prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
218 "Running the 'prepare' command will let "
8355eb9dd8fe prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
219 "PieCrust setup a page for you in the correct place, with "
8355eb9dd8fe prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
220 "some hopefully useful default text.") +
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
221 "\n\n" +
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
222 textwrap.fill("The following templates are available:") +
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
223 "\n\n" +
535
03c3a77fda60 prepare: More help about scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents: 534
diff changeset
224 help_list +
03c3a77fda60 prepare: More help about scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents: 534
diff changeset
225 "\n" +
03c3a77fda60 prepare: More help about scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents: 534
diff changeset
226 "You can add user-defined templates by creating pages in a "
03c3a77fda60 prepare: More help about scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents: 534
diff changeset
227 "`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
228 return help_txt
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
229