##// END OF EJS Templates
allow using instance values in place of defaults in Configurable.class_get_help
MinRK -
Show More
@@ -149,34 +149,47 b' class Configurable(HasTraits):'
149 self.config = newconfig
149 self.config = newconfig
150
150
151 @classmethod
151 @classmethod
152 def class_get_help(cls):
152 def class_get_help(cls, inst=None):
153 """Get the help string for this class in ReST format."""
153 """Get the help string for this class in ReST format.
154
155 If `inst` is given, it's current trait values will be used in place of
156 class defaults.
157 """
158 assert inst is None or isinstance(inst, cls)
154 cls_traits = cls.class_traits(config=True)
159 cls_traits = cls.class_traits(config=True)
155 final_help = []
160 final_help = []
156 final_help.append(u'%s options' % cls.__name__)
161 final_help.append(u'%s options' % cls.__name__)
157 final_help.append(len(final_help[0])*u'-')
162 final_help.append(len(final_help[0])*u'-')
158 for k,v in cls.class_traits(config=True).iteritems():
163 for k,v in cls.class_traits(config=True).iteritems():
159 help = cls.class_get_trait_help(v)
164 help = cls.class_get_trait_help(v, inst)
160 final_help.append(help)
165 final_help.append(help)
161 return '\n'.join(final_help)
166 return '\n'.join(final_help)
162
167
163 @classmethod
168 @classmethod
164 def class_get_trait_help(cls, trait):
169 def class_get_trait_help(cls, trait, inst=None):
165 """Get the help string for a single trait."""
170 """Get the help string for a single trait.
171
172 If `inst` is given, it's current trait values will be used in place of
173 the class default.
174 """
175 assert inst is None or isinstance(inst, cls)
166 lines = []
176 lines = []
167 header = "--%s.%s=<%s>" % (cls.__name__, trait.name, trait.__class__.__name__)
177 header = "--%s.%s=<%s>" % (cls.__name__, trait.name, trait.__class__.__name__)
168 lines.append(header)
178 lines.append(header)
169 try:
179 if inst is not None:
170 dvr = repr(trait.get_default_value())
180 lines.append(indent('Current: %r' % getattr(inst, trait.name), 4))
171 except Exception:
181 else:
172 dvr = None # ignore defaults we can't construct
182 try:
173 if dvr is not None:
183 dvr = repr(trait.get_default_value())
174 if len(dvr) > 64:
184 except Exception:
175 dvr = dvr[:61]+'...'
185 dvr = None # ignore defaults we can't construct
176 lines.append(indent('Default: %s'%dvr, 4))
186 if dvr is not None:
187 if len(dvr) > 64:
188 dvr = dvr[:61]+'...'
189 lines.append(indent('Default: %s' % dvr, 4))
177 if 'Enum' in trait.__class__.__name__:
190 if 'Enum' in trait.__class__.__name__:
178 # include Enum choices
191 # include Enum choices
179 lines.append(indent('Choices: %r'%(trait.values,)))
192 lines.append(indent('Choices: %r' % (trait.values,)))
180
193
181 help = trait.get_metadata('help')
194 help = trait.get_metadata('help')
182 if help is not None:
195 if help is not None:
@@ -185,9 +198,9 b' class Configurable(HasTraits):'
185 return '\n'.join(lines)
198 return '\n'.join(lines)
186
199
187 @classmethod
200 @classmethod
188 def class_print_help(cls):
201 def class_print_help(cls, inst=None):
189 """Get the help string for a single trait and print it."""
202 """Get the help string for a single trait and print it."""
190 print cls.class_get_help()
203 print cls.class_get_help(inst)
191
204
192 @classmethod
205 @classmethod
193 def class_config_section(cls):
206 def class_config_section(cls):
@@ -53,6 +53,15 b' mc_help=u"""MyConfigurable options'
53 Default: 1.0
53 Default: 1.0
54 The integer b."""
54 The integer b."""
55
55
56 mc_help_inst=u"""MyConfigurable options
57 ----------------------
58 --MyConfigurable.a=<Int>
59 Current: 5
60 The integer a.
61 --MyConfigurable.b=<Float>
62 Current: 4.0
63 The integer b."""
64
56 class Foo(Configurable):
65 class Foo(Configurable):
57 a = Int(0, config=True, help="The integer a.")
66 a = Int(0, config=True, help="The integer a.")
58 b = Unicode('nope', config=True)
67 b = Unicode('nope', config=True)
@@ -139,6 +148,10 b' class TestConfigurable(TestCase):'
139 def test_help(self):
148 def test_help(self):
140 self.assertEquals(MyConfigurable.class_get_help(), mc_help)
149 self.assertEquals(MyConfigurable.class_get_help(), mc_help)
141
150
151 def test_help_inst(self):
152 inst = MyConfigurable(a=5, b=4)
153 self.assertEquals(MyConfigurable.class_get_help(inst), mc_help_inst)
154
142
155
143 class TestSingletonConfigurable(TestCase):
156 class TestSingletonConfigurable(TestCase):
144
157
General Comments 0
You need to be logged in to leave comments. Login now