comparison piecrust/commands/builtin/scaffolding.py @ 165:8355eb9dd8fe

prepare: Show a more friendly user message when no arguments are given. cosmetic: pep8 compliance.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 01 Jan 2015 23:01:55 -0800
parents 69d5eecfa449
children d5b7c2a4ec9d
comparison
equal deleted inserted replaced
164:4534ccbdd2a3 165:8355eb9dd8fe
44 logger.debug("Skipping source '%s' because it's a theme " 44 logger.debug("Skipping source '%s' because it's a theme "
45 "source." % src.name) 45 "source." % src.name)
46 continue 46 continue
47 p = subparsers.add_parser( 47 p = subparsers.add_parser(
48 src.item_name, 48 src.item_name,
49 help="Creates an empty page in the '%s' source." % src.name) 49 help=("Creates an empty page in the '%s' source." %
50 src.name))
50 src.setupPrepareParser(p, app) 51 src.setupPrepareParser(p, app)
51 p.add_argument('-t', '--template', default='default', 52 p.add_argument('-t', '--template', default='default',
52 help="The template to use, which will change the " 53 help="The template to use, which will change the "
53 "generated text and header.") 54 "generated text and header.")
54 p.set_defaults(source=src) 55 p.set_defaults(source=src)
55 56
56 def run(self, ctx): 57 def run(self, ctx):
58 if not hasattr(ctx.args, 'source'):
59 raise Exception("No source specified. "
60 "Please run `chef prepare -h` for usage.")
61
57 app = ctx.app 62 app = ctx.app
58 source = ctx.args.source 63 source = ctx.args.source
59 metadata = source.buildMetadata(ctx.args) 64 metadata = source.buildMetadata(ctx.args)
60 rel_path, metadata = source.findPagePath(metadata, MODE_CREATING) 65 rel_path, metadata = source.findPagePath(metadata, MODE_CREATING)
61 path = source.resolveRef(rel_path) 66 path = source.resolveRef(rel_path)
62 name, ext = os.path.splitext(path) 67 name, ext = os.path.splitext(path)
63 if ext == '.*': 68 if ext == '.*':
64 path = '%s.%s' % (name, 69 path = '%s.%s' % (
70 name,
65 app.config.get('site/default_auto_format')) 71 app.config.get('site/default_auto_format'))
66 if os.path.exists(path): 72 if os.path.exists(path):
67 raise Exception("'%s' already exists." % path) 73 raise Exception("'%s' already exists." % path)
68 74
69 tpl_name = ctx.args.template 75 tpl_name = ctx.args.template
76 if ext is None: 82 if ext is None:
77 raise Exception("No such page template: %s" % tpl_name) 83 raise Exception("No such page template: %s" % tpl_name)
78 84
79 tpl_text = ext.getTemplate(ctx.app, tpl_name) 85 tpl_text = ext.getTemplate(ctx.app, tpl_name)
80 title = (metadata.get('slug') or metadata.get('path') or 86 title = (metadata.get('slug') or metadata.get('path') or
81 'Untitled page') 87 'Untitled page')
82 title = make_title(title) 88 title = make_title(title)
83 tokens = { 89 tokens = {
84 '%title%': title, 90 '%title%': title,
85 '%time.today%': time.strftime('%Y/%m/%d'), 91 '%time.today%': time.strftime('%Y/%m/%d'),
86 '%time.now%': time.strftime('%H:%M:%S')} 92 '%time.now%': time.strftime('%H:%M:%S')}
124 """ 130 """
125 command_name = 'help' 131 command_name = 'help'
126 132
127 def getHelpTopics(self): 133 def getHelpTopics(self):
128 return [('scaffolding', 134 return [('scaffolding',
129 "Available templates for the 'prepare' command.")] 135 "Available templates for the 'prepare' command.")]
130 136
131 def getHelpTopic(self, topic, app): 137 def getHelpTopic(self, topic, app):
132 with io.StringIO() as tplh: 138 with io.StringIO() as tplh:
133 extensions = app.plugin_loader.getCommandExtensions() 139 extensions = app.plugin_loader.getCommandExtensions()
134 for e in extensions: 140 for e in extensions:
137 d = e.getTemplateDescription(app, n) 143 d = e.getTemplateDescription(app, n)
138 print_help_item(tplh, n, d) 144 print_help_item(tplh, n, d)
139 help_list = tplh.getvalue() 145 help_list = tplh.getvalue()
140 146
141 help_txt = ( 147 help_txt = (
142 textwrap.fill("Running the 'prepare' command will let " 148 textwrap.fill(
143 "PieCrust setup a page for you in the correct place, with " 149 "Running the 'prepare' command will let "
144 "some hopefully useful default text.") + 150 "PieCrust setup a page for you in the correct place, with "
151 "some hopefully useful default text.") +
145 "\n\n" + 152 "\n\n" +
146 textwrap.fill("The following templates are available:") + 153 textwrap.fill("The following templates are available:") +
147 "\n\n" + 154 "\n\n" +
148 help_list) 155 help_list)
149 return help_txt 156 return help_txt