Show More
@@ -33,7 +33,7 b' from IPython.utils.traitlets import (' | |||||
33 | from IPython.utils.text import indent |
|
33 | from IPython.utils.text import indent | |
34 |
|
34 | |||
35 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
36 | # Descriptions for |
|
36 | # Descriptions for the various sections | |
37 | #----------------------------------------------------------------------------- |
|
37 | #----------------------------------------------------------------------------- | |
38 |
|
38 | |||
39 | flag_description = """ |
|
39 | flag_description = """ | |
@@ -91,7 +91,7 b' class Application(SingletonConfigurable):' | |||||
91 | # The log level for the application |
|
91 | # The log level for the application | |
92 | log_level = Enum((0,10,20,30,40,50), default_value=logging.WARN, |
|
92 | log_level = Enum((0,10,20,30,40,50), default_value=logging.WARN, | |
93 | config=True, |
|
93 | config=True, | |
94 |
help="Set the log level |
|
94 | help="Set the log level.") | |
95 |
|
95 | |||
96 | # the alias map for configurables |
|
96 | # the alias map for configurables | |
97 | aliases = Dict(dict(log_level='Application.log_level')) |
|
97 | aliases = Dict(dict(log_level='Application.log_level')) | |
@@ -144,10 +144,10 b' class Application(SingletonConfigurable):' | |||||
144 | if not self.aliases: |
|
144 | if not self.aliases: | |
145 | return |
|
145 | return | |
146 |
|
|
146 | ||
147 |
|
|
147 | lines = ['Aliases'] | |
148 | print "-------" |
|
148 | lines.append('_'*len(lines[0])) | |
149 |
|
|
149 | lines.append(self.alias_description) | |
150 |
|
150 | lines.append('') | ||
151 |
|
151 | |||
152 | classdict = {} |
|
152 | classdict = {} | |
153 | for c in self.classes: |
|
153 | for c in self.classes: | |
@@ -158,31 +158,38 b' class Application(SingletonConfigurable):' | |||||
158 | cls = classdict[classname] |
|
158 | cls = classdict[classname] | |
159 |
|
159 | |||
160 | trait = cls.class_traits(config=True)[traitname] |
|
160 | trait = cls.class_traits(config=True)[traitname] | |
161 |
help = trait |
|
161 | help = cls.class_get_trait_help(trait) | |
162 | print alias, "(%s)"%longname, ':', trait.__class__.__name__ |
|
162 | help = help.replace(longname, "%s (%s)"%(alias, longname), 1) | |
163 |
|
|
163 | lines.append(help) | |
164 | print indent(help, flatten=True) |
|
164 | # header = "%s (%s) : %s"%(alias, longname, trait.__class__.__name__) | |
165 |
|
165 | # lines.append(header) | ||
|
166 | # help = cls.class_get_trait_help(trait) | |||
|
167 | # if help: | |||
|
168 | # lines.append(indent(help, flatten=True)) | |||
|
169 | lines.append('') | |||
|
170 | print '\n'.join(lines) | |||
166 |
|
171 | |||
167 | def print_flag_help(self): |
|
172 | def print_flag_help(self): | |
168 | """print the flag part of the help""" |
|
173 | """print the flag part of the help""" | |
169 | if not self.flags: |
|
174 | if not self.flags: | |
170 | return |
|
175 | return | |
171 |
|
176 | |||
172 |
|
|
177 | lines = ['Flags'] | |
173 | print "-----" |
|
178 | lines.append('_'*len(lines[0])) | |
174 |
|
|
179 | lines.append(self.flag_description) | |
175 |
|
180 | lines.append('') | ||
176 |
|
181 | |||
177 | for m, (cfg,help) in self.flags.iteritems(): |
|
182 | for m, (cfg,help) in self.flags.iteritems(): | |
178 |
|
|
183 | lines.append('--'+m) | |
179 |
|
|
184 | lines.append(indent(help, flatten=True)) | |
180 |
|
185 | lines.append('') | ||
|
186 | print '\n'.join(lines) | |||
181 |
|
187 | |||
182 | def print_help(self): |
|
188 | def print_help(self): | |
183 | """Print the help for each Configurable class in self.classes.""" |
|
189 | """Print the help for each Configurable class in self.classes.""" | |
184 | self.print_flag_help() |
|
190 | self.print_flag_help() | |
185 | self.print_alias_help() |
|
191 | self.print_alias_help() | |
|
192 | ||||
186 | if self.classes: |
|
193 | if self.classes: | |
187 | print "Class parameters" |
|
194 | print "Class parameters" | |
188 | print "----------------" |
|
195 | print "----------------" | |
@@ -239,3 +246,41 b' class Application(SingletonConfigurable):' | |||||
239 | def exit(self, exit_status=0): |
|
246 | def exit(self, exit_status=0): | |
240 | self.log.debug("Exiting application: %s" % self.name) |
|
247 | self.log.debug("Exiting application: %s" % self.name) | |
241 | sys.exit(exit_status) |
|
248 | sys.exit(exit_status) | |
|
249 | ||||
|
250 | #----------------------------------------------------------------------------- | |||
|
251 | # utility functions, for convenience | |||
|
252 | #----------------------------------------------------------------------------- | |||
|
253 | ||||
|
254 | def boolean_flag(name, configurable, set_help='', unset_help=''): | |||
|
255 | """helper for building basic --trait, --no-trait flags | |||
|
256 | ||||
|
257 | Parameters | |||
|
258 | ---------- | |||
|
259 | ||||
|
260 | name : str | |||
|
261 | The name of the flag. | |||
|
262 | configurable : str | |||
|
263 | The 'Class.trait' string of the trait to be set/unset with the flag | |||
|
264 | set_help : unicode | |||
|
265 | help string for --name flag | |||
|
266 | unset_help : unicode | |||
|
267 | help string for --no-name flag | |||
|
268 | ||||
|
269 | Returns | |||
|
270 | ------- | |||
|
271 | ||||
|
272 | cfg : dict | |||
|
273 | A dict with two keys: 'name', and 'no-name', for setting and unsetting | |||
|
274 | the trait, respectively. | |||
|
275 | """ | |||
|
276 | # default helpstrings | |||
|
277 | set_help = set_help or "set %s=True"%configurable | |||
|
278 | unset_help = unset_help or "set %s=False"%configurable | |||
|
279 | ||||
|
280 | cls,trait = configurable.split('.') | |||
|
281 | ||||
|
282 | setter = Config() | |||
|
283 | setter[cls][trait] = True | |||
|
284 | unsetter = Config() | |||
|
285 | unsetter[cls][trait] = False | |||
|
286 | return {name : (setter, set_help), 'no-'+name : (unsetter, unset_help)} |
@@ -145,15 +145,33 b' class Configurable(HasTraits):' | |||||
145 | final_help = [] |
|
145 | final_help = [] | |
146 | final_help.append(u'%s options' % cls.__name__) |
|
146 | final_help.append(u'%s options' % cls.__name__) | |
147 | final_help.append(len(final_help[0])*u'-') |
|
147 | final_help.append(len(final_help[0])*u'-') | |
148 |
for k, |
|
148 | for k,v in cls.class_traits(config=True).iteritems(): | |
149 |
help = v |
|
149 | help = cls.class_get_trait_help(v) | |
150 | header = "%s.%s : %s" % (cls.__name__, k, v.__class__.__name__) |
|
150 | final_help.append(help) | |
151 | final_help.append(header) |
|
|||
152 | if help is not None: |
|
|||
153 | final_help.append(indent(help, flatten=True)) |
|
|||
154 | return '\n'.join(final_help) |
|
151 | return '\n'.join(final_help) | |
155 |
|
152 | |||
156 | @classmethod |
|
153 | @classmethod | |
|
154 | def class_get_trait_help(cls, trait): | |||
|
155 | """Get the help string for a single """ | |||
|
156 | lines = [] | |||
|
157 | header = "%s.%s : %s" % (cls.__name__, trait.name, trait.__class__.__name__) | |||
|
158 | try: | |||
|
159 | dvr = repr(trait.get_default_value()) | |||
|
160 | except Exception: | |||
|
161 | dvr = None # ignore defaults we can't construct | |||
|
162 | if dvr is not None: | |||
|
163 | header += ' [default: %s]'%dvr | |||
|
164 | lines.append(header) | |||
|
165 | ||||
|
166 | help = trait.get_metadata('help') | |||
|
167 | if help is not None: | |||
|
168 | lines.append(indent(help, flatten=True)) | |||
|
169 | if 'Enum' in trait.__class__.__name__: | |||
|
170 | # include Enum choices | |||
|
171 | lines.append(indent('Choices: %r'%(trait.values,), flatten=True)) | |||
|
172 | return '\n'.join(lines) | |||
|
173 | ||||
|
174 | @classmethod | |||
157 | def class_print_help(cls): |
|
175 | def class_print_help(cls): | |
158 | print cls.class_get_help() |
|
176 | print cls.class_get_help() | |
159 |
|
177 |
General Comments 0
You need to be logged in to leave comments.
Login now