##// END OF EJS Templates
expand '~' with path.expanduser in KVLoader...
MinRK -
Show More
@@ -19,6 +19,7 b' Authors'
19 19 #-----------------------------------------------------------------------------
20 20
21 21 import __builtin__ as builtin_mod
22 import os
22 23 import re
23 24 import sys
24 25
@@ -332,6 +333,14 b' class CommandLineConfigLoader(ConfigLoader):'
332 333 """
333 334
334 335 def _exec_config_str(self, lhs, rhs):
336 """execute self.config.<lhs>=<rhs>
337
338 * expands ~ with expanduser
339 * tries to assign with raw exec, otherwise assigns with just the string,
340 allowing `--C.a=foobar` and `--C.a="foobar"` to be equivalent. *Not*
341 equivalent are `--C.a=4` and `--C.a='4'`.
342 """
343 rhs = os.path.expanduser(rhs)
335 344 exec_str = 'self.config.' + lhs + '=' + rhs
336 345 try:
337 346 # Try to see if regular Python syntax will work. This
@@ -343,7 +352,7 b' class CommandLineConfigLoader(ConfigLoader):'
343 352 # the quote marks. Use repr, to get quote marks, and
344 353 # 'u' prefix and see if
345 354 # it succeeds. If it still fails, we let it raise.
346 exec_str = u'self.config.' + lhs + '=' + repr(rhs)
355 exec_str = u'self.config.' + lhs + '= rhs'
347 356 exec exec_str in locals(), globals()
348 357
349 358 def _load_flag(self, cfg):
@@ -131,6 +131,16 b' class TestKeyValueCL(TestCase):'
131 131 self.assertEquals(config.Foo.Bam.value, range(10))
132 132 self.assertEquals(config.D.C.value, 'hi there')
133 133
134 def test_expanduser(self):
135 cl = self.klass()
136 argv = ['--a=~/1/2/3', '--b=~', '--c=~/', '--d="~/"']
137 with mute_warn():
138 config = cl.load_config(argv)
139 self.assertEquals(config.a, os.path.expanduser('~/1/2/3'))
140 self.assertEquals(config.b, os.path.expanduser('~'))
141 self.assertEquals(config.c, os.path.expanduser('~/'))
142 self.assertEquals(config.d, '~/')
143
134 144 def test_extra_args(self):
135 145 cl = self.klass()
136 146 with mute_warn():
@@ -172,6 +182,14 b' class TestKeyValueCL(TestCase):'
172 182 class TestArgParseKVCL(TestKeyValueCL):
173 183 klass = KVArgParseConfigLoader
174 184
185 def test_expanduser2(self):
186 cl = self.klass()
187 argv = ['-a', '~/1/2/3', '--b', "'~/1/2/3'"]
188 with mute_warn():
189 config = cl.load_config(argv, aliases=dict(a='A.a', b='A.b'))
190 self.assertEquals(config.A.a, os.path.expanduser('~/1/2/3'))
191 self.assertEquals(config.A.b, '~/1/2/3')
192
175 193 class TestConfig(TestCase):
176 194
177 195 def test_setget(self):
General Comments 0
You need to be logged in to leave comments. Login now