comparison piecrust/commands/base.py @ 1099:07c23be08029

help: Add new help topics on routes.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 17 Feb 2018 11:53:03 -0800
parents 4850f8c21b6e
children
comparison
equal deleted inserted replaced
1098:2323f0788170 1099:07c23be08029
1 import os.path
1 import logging 2 import logging
2 import argparse 3 import argparse
4 import textwrap
3 import functools 5 import functools
6 from piecrust import RESOURCES_DIR
4 from piecrust.pathutil import SiteNotFoundError 7 from piecrust.pathutil import SiteNotFoundError
5 8
6 9
7 logger = logging.getLogger(__name__) 10 logger = logging.getLogger(__name__)
8 11
22 self.requires_website = True 25 self.requires_website = True
23 self.cache_name = 'default' 26 self.cache_name = 'default'
24 27
25 def setupParser(self, parser, app): 28 def setupParser(self, parser, app):
26 raise NotImplementedError() 29 raise NotImplementedError()
30
31 def provideExtensions(self):
32 return None
27 33
28 def run(self, ctx): 34 def run(self, ctx):
29 raise NotImplementedError( 35 raise NotImplementedError(
30 "Command '%s' doesn't implement the `run` " 36 "Command '%s' doesn't implement the `run` "
31 "method." % type(self)) 37 "method." % type(self))
52 p.set_defaults(sub_func=e.checkedRun) 58 p.set_defaults(sub_func=e.checkedRun)
53 59
54 def _loadExtensions(self, app): 60 def _loadExtensions(self, app):
55 if self._extensions is not None: 61 if self._extensions is not None:
56 return 62 return
57 self._extensions = [] 63
64 possible_exts = []
58 for e in app.plugin_loader.getCommandExtensions(): 65 for e in app.plugin_loader.getCommandExtensions():
59 if e.command_name == self.name and e.supports(app): 66 possible_exts.append(e)
60 self._extensions.append(e) 67 for c in app.plugin_loader.getCommands():
68 exts = c.provideExtensions()
69 if exts is not None:
70 possible_exts += exts
71
72 self._extensions = list(filter(
73 lambda e: e.command_name == self.name and e.supports(app),
74 possible_exts))
61 75
62 76
63 class ChefCommandExtension(object): 77 class ChefCommandExtension(object):
64 command_name = '__unknown__' 78 command_name = '__unknown__'
65 79
137 151
138 152
139 def get_func_command(f): 153 def get_func_command(f):
140 return getattr(f, '__command_class__') 154 return getattr(f, '__command_class__')
141 155
156
157 def _get_help_topic_from_resources(command_name, topic):
158 path = os.path.join(RESOURCES_DIR, 'helptopics',
159 '%s_%s.txt' % (command_name, topic))
160 with open(path, 'r', encoding='utf8') as fp:
161 lines = fp.readlines()
162
163 wrapped_lines = []
164 for ln in lines:
165 ln = ln.rstrip('\n')
166 if not ln:
167 wrapped_lines.append('')
168 else:
169 wrapped_lines += textwrap.wrap(ln, width=80)
170 return '\n'.join(wrapped_lines)
171
172
173 class _ResourcesHelpTopics:
174 def getHelpTopic(self, topic, app):
175 category = self.__class__.__name__.lower()
176 if category.endswith('helptopic'):
177 category = category[:-len('helptopic')]
178 return _get_help_topic_from_resources(category, topic)