Show More
@@ -334,27 +334,24 b' class CommandLineConfigLoader(ConfigLoader):' | |||||
334 | """ |
|
334 | """ | |
335 |
|
335 | |||
336 | def _exec_config_str(self, lhs, rhs): |
|
336 | def _exec_config_str(self, lhs, rhs): | |
337 | """execute self.config.<lhs>=<rhs> |
|
337 | """execute self.config.<lhs> = <rhs> | |
338 |
|
338 | |||
339 | * expands ~ with expanduser |
|
339 | * expands ~ with expanduser | |
340 |
* tries to assign with raw e |
|
340 | * tries to assign with raw eval, otherwise assigns with just the string, | |
341 | allowing `--C.a=foobar` and `--C.a="foobar"` to be equivalent. *Not* |
|
341 | allowing `--C.a=foobar` and `--C.a="foobar"` to be equivalent. *Not* | |
342 | equivalent are `--C.a=4` and `--C.a='4'`. |
|
342 | equivalent are `--C.a=4` and `--C.a='4'`. | |
343 | """ |
|
343 | """ | |
344 | rhs = os.path.expanduser(rhs) |
|
344 | rhs = os.path.expanduser(rhs) | |
345 | exec_str = 'self.config.' + lhs + '=' + rhs |
|
|||
346 | try: |
|
345 | try: | |
347 | # Try to see if regular Python syntax will work. This |
|
346 | # Try to see if regular Python syntax will work. This | |
348 | # won't handle strings as the quote marks are removed |
|
347 | # won't handle strings as the quote marks are removed | |
349 | # by the system shell. |
|
348 | # by the system shell. | |
350 | exec exec_str in locals(), globals() |
|
349 | value = eval(rhs) | |
351 | except (NameError, SyntaxError): |
|
350 | except (NameError, SyntaxError): | |
352 |
# This case happens if the rhs is a string |
|
351 | # This case happens if the rhs is a string. | |
353 | # the quote marks. Use repr, to get quote marks, and |
|
352 | value = rhs | |
354 | # 'u' prefix and see if |
|
353 | ||
355 | # it succeeds. If it still fails, we let it raise. |
|
354 | exec u'self.config.%s = value' % lhs | |
356 | exec_str = u'self.config.' + lhs + '= rhs' |
|
|||
357 | exec exec_str in locals(), globals() |
|
|||
358 |
|
355 | |||
359 | def _load_flag(self, cfg): |
|
356 | def _load_flag(self, cfg): | |
360 | """update self.config from a flag, which can be a dict or Config""" |
|
357 | """update self.config from a flag, which can be a dict or Config""" |
@@ -190,6 +190,14 b' class TestArgParseKVCL(TestKeyValueCL):' | |||||
190 | self.assertEquals(config.A.a, os.path.expanduser('~/1/2/3')) |
|
190 | self.assertEquals(config.A.a, os.path.expanduser('~/1/2/3')) | |
191 | self.assertEquals(config.A.b, '~/1/2/3') |
|
191 | self.assertEquals(config.A.b, '~/1/2/3') | |
192 |
|
192 | |||
|
193 | def test_eval(self): | |||
|
194 | cl = self.klass() | |||
|
195 | argv = ['-c', 'a=5'] | |||
|
196 | with mute_warn(): | |||
|
197 | config = cl.load_config(argv, aliases=dict(c='A.c')) | |||
|
198 | self.assertEquals(config.A.c, u"a=5") | |||
|
199 | ||||
|
200 | ||||
193 | class TestConfig(TestCase): |
|
201 | class TestConfig(TestCase): | |
194 |
|
202 | |||
195 | def test_setget(self): |
|
203 | def test_setget(self): |
General Comments 0
You need to be logged in to leave comments.
Login now