comparison piecrust/commands/builtin/scaffolding.py @ 879:58ae026b4c31

chef: Optimize startup time.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 15 Jun 2017 22:38:05 -0700
parents 4850f8c21b6e
children c445a3d5d950
comparison
equal deleted inserted replaced
878:313db67cfc35 879:58ae026b4c31
1 import os 1 import os
2 import os.path 2 import os.path
3 import io
4 import time
5 import glob
6 import logging 3 import logging
7 import textwrap
8 from piecrust import RESOURCES_DIR
9 from piecrust.chefutil import print_help_item
10 from piecrust.commands.base import ExtendableChefCommand, ChefCommandExtension 4 from piecrust.commands.base import ExtendableChefCommand, ChefCommandExtension
11 from piecrust.pathutil import SiteNotFoundError
12 from piecrust.sources.fs import FSContentSourceBase
13 5
14 6
15 logger = logging.getLogger(__name__) 7 logger = logging.getLogger(__name__)
16 8
17 9
54 help="Overwrite any existing content.") 46 help="Overwrite any existing content.")
55 p.set_defaults(source=src) 47 p.set_defaults(source=src)
56 p.set_defaults(sub_func=self._doRun) 48 p.set_defaults(sub_func=self._doRun)
57 49
58 def checkedRun(self, ctx): 50 def checkedRun(self, ctx):
51 from piecrust.pathutil import SiteNotFoundError
52
59 if ctx.app.root_dir is None: 53 if ctx.app.root_dir is None:
60 raise SiteNotFoundError(theme=ctx.app.theme_site) 54 raise SiteNotFoundError(theme=ctx.app.theme_site)
61 55
62 if not hasattr(ctx.args, 'sub_func'): 56 if not hasattr(ctx.args, 'sub_func'):
63 ctx.parser.parse_args(['prepare', '--help']) 57 ctx.parser.parse_args(['prepare', '--help'])
64 return 58 return
65 ctx.args.sub_func(ctx) 59 ctx.args.sub_func(ctx)
66 60
67 def _doRun(self, ctx): 61 def _doRun(self, ctx):
62 import time
68 from piecrust.uriutil import multi_replace 63 from piecrust.uriutil import multi_replace
64 from piecrust.sources.fs import FSContentSourceBase
69 65
70 if not hasattr(ctx.args, 'source'): 66 if not hasattr(ctx.args, 'source'):
71 raise Exception("No source specified. " 67 raise Exception("No source specified. "
72 "Please run `chef prepare -h` for usage.") 68 "Please run `chef prepare -h` for usage.")
73 69
142 'rss': "A fully functional RSS feed.", 138 'rss': "A fully functional RSS feed.",
143 'atom': "A fully functional Atom feed."} 139 'atom': "A fully functional Atom feed."}
144 return descs[name] 140 return descs[name]
145 141
146 def getTemplate(self, app, name): 142 def getTemplate(self, app, name):
143 from piecrust import RESOURCES_DIR
144
147 assert name in ['default', 'rss', 'atom'] 145 assert name in ['default', 'rss', 'atom']
148 src_path = os.path.join(RESOURCES_DIR, 'prepare', '%s.html' % name) 146 src_path = os.path.join(RESOURCES_DIR, 'prepare', '%s.html' % name)
149 with open(src_path, 'r', encoding='utf8') as fp: 147 with open(src_path, 'r', encoding='utf8') as fp:
150 return fp.read() 148 return fp.read()
151 149
172 170
173 def getTemplateDescription(self, app, name): 171 def getTemplateDescription(self, app, name):
174 return "User-defined template." 172 return "User-defined template."
175 173
176 def getTemplate(self, app, name): 174 def getTemplate(self, app, name):
175 import glob
176
177 templates_dir = self._getTemplatesDir(app) 177 templates_dir = self._getTemplatesDir(app)
178 pattern = os.path.join(templates_dir, '%s.*' % name) 178 pattern = os.path.join(templates_dir, '%s.*' % name)
179 matches = glob.glob(pattern) 179 matches = glob.glob(pattern)
180 if not matches: 180 if not matches:
181 raise Exception("No such page scaffolding template: %s" % name) 181 raise Exception("No such page scaffolding template: %s" % name)
194 def getHelpTopics(self): 194 def getHelpTopics(self):
195 return [('scaffolding', 195 return [('scaffolding',
196 "Available templates for the 'prepare' command.")] 196 "Available templates for the 'prepare' command.")]
197 197
198 def getHelpTopic(self, topic, app): 198 def getHelpTopic(self, topic, app):
199 import io
200 import textwrap
201 from piecrust.chefutil import print_help_item
202
199 with io.StringIO() as tplh: 203 with io.StringIO() as tplh:
200 extensions = app.plugin_loader.getCommandExtensions() 204 extensions = app.plugin_loader.getCommandExtensions()
201 for e in extensions: 205 for e in extensions:
202 if e.command_name == 'prepare' and e.supports(app): 206 if e.command_name == 'prepare' and e.supports(app):
203 for n in e.getTemplateNames(app): 207 for n in e.getTemplateNames(app):