Mercurial > piecrust2
comparison piecrust/commands/builtin/util.py @ 100:69d5eecfa449
Better `prepare` command, with templates and help topics.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 13 Sep 2014 23:31:21 -0700 |
parents | 28958565a17b |
children | 2823ea40cfac |
comparison
equal
deleted
inserted
replaced
99:8703be118430 | 100:69d5eecfa449 |
---|---|
4 import codecs | 4 import codecs |
5 import logging | 5 import logging |
6 import yaml | 6 import yaml |
7 from piecrust.app import CONFIG_PATH | 7 from piecrust.app import CONFIG_PATH |
8 from piecrust.commands.base import ChefCommand | 8 from piecrust.commands.base import ChefCommand |
9 from piecrust.sources.base import IPreparingSource, MODE_CREATING | |
10 | 9 |
11 | 10 |
12 logger = logging.getLogger(__name__) | 11 logger = logging.getLogger(__name__) |
13 | 12 |
14 | 13 |
64 if os.path.isdir(cache_dir): | 63 if os.path.isdir(cache_dir): |
65 logger.info("Purging cache: %s" % cache_dir) | 64 logger.info("Purging cache: %s" % cache_dir) |
66 shutil.rmtree(cache_dir) | 65 shutil.rmtree(cache_dir) |
67 | 66 |
68 | 67 |
69 class PrepareCommand(ChefCommand): | |
70 def __init__(self): | |
71 super(PrepareCommand, self).__init__() | |
72 self.name = 'prepare' | |
73 self.description = "Prepares new content for your website." | |
74 | |
75 def setupParser(self, parser, app): | |
76 # Don't setup anything if this is a null app | |
77 # (for when `chef` is run from outside a website) | |
78 if app.root_dir is None: | |
79 return | |
80 | |
81 subparsers = parser.add_subparsers() | |
82 for src in app.sources: | |
83 if not isinstance(src, IPreparingSource): | |
84 logger.debug("Skipping source '%s' because it's not " | |
85 "preparable." % src.name) | |
86 continue | |
87 if src.is_theme_source: | |
88 logger.debug("Skipping source '%s' because it's a theme " | |
89 "source." % src.name) | |
90 continue | |
91 p = subparsers.add_parser(src.item_name) | |
92 src.setupPrepareParser(p, app) | |
93 p.set_defaults(source=src) | |
94 | |
95 def run(self, ctx): | |
96 app = ctx.app | |
97 source = ctx.args.source | |
98 metadata = source.buildMetadata(ctx.args) | |
99 rel_path, metadata = source.findPagePath(metadata, MODE_CREATING) | |
100 path = source.resolveRef(rel_path) | |
101 name, ext = os.path.splitext(path) | |
102 if ext == '.*': | |
103 path = '%s.%s' % (name, | |
104 app.config.get('site/default_auto_format')) | |
105 if os.path.exists(path): | |
106 raise Exception("'%s' already exists." % path) | |
107 | |
108 logger.info("Creating page: %s" % os.path.relpath(path, app.root_dir)) | |
109 if not os.path.exists(os.path.dirname(path)): | |
110 os.makedirs(os.path.dirname(path), 0o755) | |
111 with open(path, 'w') as f: | |
112 f.write('---\n') | |
113 f.write('title: %s\n' % 'Unknown title') | |
114 f.write('---\n') | |
115 f.write("This is a new page!\n") | |
116 | |
117 | |
118 class ImportCommand(ChefCommand): | 68 class ImportCommand(ChefCommand): |
119 def __init__(self): | 69 def __init__(self): |
120 super(ImportCommand, self).__init__() | 70 super(ImportCommand, self).__init__() |
121 self.name = 'import' | 71 self.name = 'import' |
122 self.description = "Imports content from another CMS into PieCrust." | 72 self.description = "Imports content from another CMS into PieCrust." |