Mercurial > piecrust2
annotate piecrust/commands/builtin/scaffolding.py @ 852:4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
* Everything is a `ContentSource`, including assets directories.
* Most content sources are subclasses of the base file-system source.
* A source is processed by a "pipeline", and there are 2 built-in pipelines,
one for assets and one for pages. The asset pipeline is vaguely functional,
but the page pipeline is completely broken right now.
* Rewrite the baking process as just running appropriate pipelines on each
content item. This should allow for better parallelization.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 17 May 2017 00:11:48 -0700 |
parents | 2bb3c1a04e98 |
children | 58ae026b4c31 |
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 io |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 import time |
187
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
5 import glob |
100
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 import logging |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 import textwrap |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 from piecrust import RESOURCES_DIR |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 from piecrust.chefutil import print_help_item |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 from piecrust.commands.base import ExtendableChefCommand, ChefCommandExtension |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
11 from piecrust.pathutil import SiteNotFoundError |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
12 from piecrust.sources.fs import FSContentSourceBase |
100
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 logger = logging.getLogger(__name__) |
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 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 class PrepareCommand(ExtendableChefCommand): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 """ 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
|
20 """ |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 def __init__(self): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 super(PrepareCommand, self).__init__() |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 self.name = 'prepare' |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 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
|
25 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 def setupParser(self, parser, app): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 # 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
|
28 # (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
|
29 if app.root_dir is None: |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 return |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
32 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
|
33 |
100
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 subparsers = parser.add_subparsers() |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 for src in app.sources: |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 if not isinstance(src, IPreparingSource): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 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
|
38 "preparable." % src.name) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 continue |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 if src.is_theme_source: |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 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
|
42 "source." % src.name) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 continue |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 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
|
45 src.config['item_name'], |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
46 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
|
47 src.name)) |
100
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 src.setupPrepareParser(p, app) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 p.add_argument('-t', '--template', default='default', |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 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
|
51 "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
|
52 "scaffolding` for more information.") |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
53 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
|
54 help="Overwrite any existing content.") |
100
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
55 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
|
56 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
|
57 |
773
87f1e79d3fbe
prepare: Use the same convention as other commands with sub-commands.
Ludovic Chabant <ludovic@chabant.com>
parents:
535
diff
changeset
|
58 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
|
59 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
|
60 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
|
61 |
87f1e79d3fbe
prepare: Use the same convention as other commands with sub-commands.
Ludovic Chabant <ludovic@chabant.com>
parents:
535
diff
changeset
|
62 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
|
63 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
|
64 return |
87f1e79d3fbe
prepare: Use the same convention as other commands with sub-commands.
Ludovic Chabant <ludovic@chabant.com>
parents:
535
diff
changeset
|
65 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
|
66 |
87f1e79d3fbe
prepare: Use the same convention as other commands with sub-commands.
Ludovic Chabant <ludovic@chabant.com>
parents:
535
diff
changeset
|
67 def _doRun(self, ctx): |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
68 from piecrust.uriutil import multi_replace |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
69 |
165
8355eb9dd8fe
prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents:
100
diff
changeset
|
70 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
|
71 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
|
72 "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
|
73 |
100
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
74 app = ctx.app |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
75 tpl_name = ctx.args.template |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
76 extensions = self.getExtensions(app) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
77 ext = next( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
78 filter( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
79 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
|
80 extensions), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
81 None) |
100
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
82 if ext is None: |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
83 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
|
84 tpl_text = ext.getTemplate(app, tpl_name) |
187
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
85 if tpl_text is None: |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
86 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
|
87 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
88 source = ctx.args.source |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
89 content_item = source.createContent(ctx.args) |
100
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
90 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
91 config_tokens = { |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
92 '%title%': "Untitled Content", |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
93 '%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
|
94 '%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
|
95 } |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
96 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
|
97 if config: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
98 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
|
99 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
|
100 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
|
101 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
102 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
|
103 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
|
104 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
|
105 f.write(tpl_text) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
106 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
107 # 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
|
108 # 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
|
109 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
|
110 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
|
111 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
|
112 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
|
113 shell = False |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
114 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
|
115 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
|
116 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
|
117 |
2bb3c1a04e98
prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents:
773
diff
changeset
|
118 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
|
119 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
|
120 else: |
2bb3c1a04e98
prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents:
773
diff
changeset
|
121 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
|
122 |
2bb3c1a04e98
prepare: Add ablity to run an editor program after creating the page file.
Ludovic Chabant <ludovic@chabant.com>
parents:
773
diff
changeset
|
123 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
|
124 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
|
125 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
|
126 |
100
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 class DefaultPrepareTemplatesCommandExtension(ChefCommandExtension): |
187
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
129 """ 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
|
130 command. |
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 __init__(self): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
133 super(DefaultPrepareTemplatesCommandExtension, self).__init__() |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
134 self.command_name = 'prepare' |
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 def getTemplateNames(self, app): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
137 return ['default', 'rss', 'atom'] |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
138 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
139 def getTemplateDescription(self, app, name): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
140 descs = { |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
141 '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
|
142 '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
|
143 'atom': "A fully functional Atom feed."} |
100
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
144 return descs[name] |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
145 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
146 def getTemplate(self, app, name): |
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): |
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): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
199 with io.StringIO() as tplh: |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
200 extensions = app.plugin_loader.getCommandExtensions() |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
201 for e in extensions: |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
202 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
|
203 for n in e.getTemplateNames(app): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
204 d = e.getTemplateDescription(app, n) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
205 print_help_item(tplh, n, d) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
206 help_list = tplh.getvalue() |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
207 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
208 help_txt = ( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
209 textwrap.fill( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
210 "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
|
211 "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
|
212 "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
|
213 "\n\n" + |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
214 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
|
215 "\n\n" + |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
216 help_list + |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
217 "\n" + |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
774
diff
changeset
|
218 "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
|
219 "`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
|
220 return help_txt |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
221 |