##// END OF EJS Templates
Added KeyValueConfigLoader with tests.
Brian Granger -
Show More
@@ -307,7 +307,64 b' class CommandLineConfigLoader(ConfigLoader):'
307 307 """
308 308
309 309
310 class KeyValueConfigLoader(CommandLineConfigLoader):
311 """A config loader that loads key value pairs from the command line.
312
313 This allows command line options to be gives in the following form::
314
315 ipython Global.profile="foo" InteractiveShell.autocall=False
316 """
317
318 def __init__(self, argv=None):
319 """Create a key value pair config loader.
320
321 Parameters
322 ----------
323 argv : list
324 A list that has the form of sys.argv[1:] which has unicode
325 elements of the form u"key=value". If this is None (default),
326 then sys.argv[1:] will be used.
327
328 Returns
329 -------
330 config : Config
331 The resulting Config object.
332
333 Examples
334 --------
335
336 >>> from IPython.config.loader import KeyValueConfigLoader
337 >>> cl = KeyValueConfigLoader()
338 >>> cl.load_config(["foo='bar'","A.name='brian'","B.number=0"])
339 {'A': {'name': 'brian'}, 'B': {'number': 0}, 'foo': 'bar'}
340 """
341 if argv == None:
342 argv = sys.argv[1:]
343 self.argv = argv
344
345 def load_config(self, argv=None):
346 """Parse the configuration and generate the Config object.
347
348 Parameters
349 ----------
350 argv : list, optional
351 A list that has the form of sys.argv[1:] which has unicode
352 elements of the form u"key=value". If this is None (default),
353 then self.argv will be used.
354 """
355 self.clear()
356 if argv is None:
357 argv = self.argv
358 for item in argv:
359 pair = tuple(item.split("="))
360 if len(pair) == 2:
361 exec_str = 'self.config.' + pair[0] + '=' + pair[1]
362 exec exec_str in locals(), globals()
363 return self.config
364
365
310 366 class ArgParseConfigLoader(CommandLineConfigLoader):
367 """A loader that uses the argparse module to load from the command line."""
311 368
312 369 def __init__(self, argv=None, *parser_args, **parser_kw):
313 370 """Create a config loader for use with argparse.
@@ -326,6 +383,11 b' class ArgParseConfigLoader(CommandLineConfigLoader):'
326 383 parser_kw : dict
327 384 A tuple of keyword arguments that will be passed to the
328 385 constructor of :class:`argparse.ArgumentParser`.
386
387 Returns
388 -------
389 config : Config
390 The resulting Config object.
329 391 """
330 392 super(CommandLineConfigLoader, self).__init__()
331 393 if argv == None:
@@ -337,8 +399,8 b' class ArgParseConfigLoader(CommandLineConfigLoader):'
337 399 kwargs.update(parser_kw)
338 400 self.parser_kw = kwargs
339 401
340 def load_config(self, args=None):
341 """Parse command line arguments and return as a Struct.
402 def load_config(self, argv=None):
403 """Parse command line arguments and return as a Config object.
342 404
343 405 Parameters
344 406 ----------
@@ -348,10 +410,10 b' class ArgParseConfigLoader(CommandLineConfigLoader):'
348 410 arguments from. If not given, the instance's self.argv attribute
349 411 (given at construction time) is used."""
350 412 self.clear()
351 if args is None:
352 args = self.argv
413 if argv is None:
414 argv = self.argv
353 415 self._create_parser()
354 self._parse_args(args)
416 self._parse_args(argv)
355 417 self._convert_to_config()
356 418 return self.config
357 419
@@ -27,6 +27,7 b' from unittest import TestCase'
27 27 from IPython.config.loader import (
28 28 Config,
29 29 PyFileConfigLoader,
30 KeyValueConfigLoader,
30 31 ArgParseConfigLoader,
31 32 ConfigError
32 33 )
@@ -109,6 +110,19 b' class TestArgParseCL(TestCase):'
109 110 self.assertEquals(config.Global.bam, 'wow')
110 111
111 112
113 class TestKeyValueCL(TestCase):
114
115 def test_basic(self):
116 cl = KeyValueConfigLoader()
117 argv = [s.strip('c.') for s in pyfile.split('\n')[2:-1]]
118 print argv
119 config = cl.load_config(argv)
120 self.assertEquals(config.a, 10)
121 self.assertEquals(config.b, 20)
122 self.assertEquals(config.Foo.Bar.value, 10)
123 self.assertEquals(config.Foo.Bam.value, range(10))
124 self.assertEquals(config.D.C.value, 'hi there')
125
112 126 class TestConfig(TestCase):
113 127
114 128 def test_setget(self):
General Comments 0
You need to be logged in to leave comments. Login now