Show More
@@ -149,34 +149,47 class Configurable(HasTraits): | |||
|
149 | 149 | self.config = newconfig |
|
150 | 150 | |
|
151 | 151 | @classmethod |
|
152 | def class_get_help(cls): | |
|
153 |
"""Get the help string for this class in ReST format. |
|
|
152 | def class_get_help(cls, inst=None): | |
|
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 | 159 | cls_traits = cls.class_traits(config=True) |
|
155 | 160 | final_help = [] |
|
156 | 161 | final_help.append(u'%s options' % cls.__name__) |
|
157 | 162 | final_help.append(len(final_help[0])*u'-') |
|
158 | 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 | 165 | final_help.append(help) |
|
161 | 166 | return '\n'.join(final_help) |
|
162 | 167 | |
|
163 | 168 | @classmethod |
|
164 | def class_get_trait_help(cls, trait): | |
|
165 |
"""Get the help string for a single trait. |
|
|
169 | def class_get_trait_help(cls, trait, inst=None): | |
|
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 | 176 | lines = [] |
|
167 | 177 | header = "--%s.%s=<%s>" % (cls.__name__, trait.name, trait.__class__.__name__) |
|
168 | 178 | lines.append(header) |
|
169 | try: | |
|
170 | dvr = repr(trait.get_default_value()) | |
|
171 | except Exception: | |
|
172 | dvr = None # ignore defaults we can't construct | |
|
173 | if dvr is not None: | |
|
174 | if len(dvr) > 64: | |
|
175 | dvr = dvr[:61]+'...' | |
|
176 | lines.append(indent('Default: %s'%dvr, 4)) | |
|
179 | if inst is not None: | |
|
180 | lines.append(indent('Current: %r' % getattr(inst, trait.name), 4)) | |
|
181 | else: | |
|
182 | try: | |
|
183 | dvr = repr(trait.get_default_value()) | |
|
184 | except Exception: | |
|
185 | dvr = None # ignore defaults we can't construct | |
|
186 | if dvr is not None: | |
|
187 | if len(dvr) > 64: | |
|
188 | dvr = dvr[:61]+'...' | |
|
189 | lines.append(indent('Default: %s' % dvr, 4)) | |
|
177 | 190 | if 'Enum' in trait.__class__.__name__: |
|
178 | 191 | # include Enum choices |
|
179 | lines.append(indent('Choices: %r'%(trait.values,))) | |
|
192 | lines.append(indent('Choices: %r' % (trait.values,))) | |
|
180 | 193 | |
|
181 | 194 | help = trait.get_metadata('help') |
|
182 | 195 | if help is not None: |
@@ -185,9 +198,9 class Configurable(HasTraits): | |||
|
185 | 198 | return '\n'.join(lines) |
|
186 | 199 | |
|
187 | 200 | @classmethod |
|
188 | def class_print_help(cls): | |
|
201 | def class_print_help(cls, inst=None): | |
|
189 | 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 | 205 | @classmethod |
|
193 | 206 | def class_config_section(cls): |
@@ -53,6 +53,15 mc_help=u"""MyConfigurable options | |||
|
53 | 53 | Default: 1.0 |
|
54 | 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 | 65 | class Foo(Configurable): |
|
57 | 66 | a = Int(0, config=True, help="The integer a.") |
|
58 | 67 | b = Unicode('nope', config=True) |
@@ -139,6 +148,10 class TestConfigurable(TestCase): | |||
|
139 | 148 | def test_help(self): |
|
140 | 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 | 156 | class TestSingletonConfigurable(TestCase): |
|
144 | 157 |
General Comments 0
You need to be logged in to leave comments.
Login now