def test_write_json(self): n = config_manager.Namespace(doc='top') n.add_option('aaa', '2011-05-04T15:10:00', 'the a', short_form='a', from_string_converter=dtu.datetime_from_ISO_string ) c = config_manager.ConfigurationManager( [n], use_admin_controls=True, use_auto_help=False, argv_source=[] ) out = StringIO() c.write_conf(for_json, opener=stringIO_context_wrapper(out)) received = out.getvalue() out.close() jrec = json.loads(received) expect_to_find = { "short_form": "a", "default": "2011-05-04T15:10:00", "doc": "the a", "value": "2011-05-04T15:10:00", "from_string_converter": "configman.datetime_util.datetime_from_ISO_string", "name": "aaa" } for key, value in expect_to_find.items(): self.assertEqual(jrec['aaa'][key], value)
def test_namespace_from_json_with_default_datetime_date(self): """fix that verifies this bug https://github.com/twobraids/configman/issues/7 """ j = ( u'{"bday": {"default": "1979-12-13", "name": "bday",' u' "from_string_converter": "configman.datetime_util.date_from_ISO' u'_string", "doc": null, "value": "1979-12-13", ' u'"short_form": null}}' ) config = config_manager.ConfigurationManager( [j], [], use_auto_help=False, use_admin_controls=True, argv_source=[] ) option = config_manager.Option( 'bday', default="1979-12-13", ) self.assertEqual( config.option_definitions.bday.default, option.default )
def test_walk_expanding_class_options(self): class A(config_manager.RequiredConfig): required_config = { 'a': config_manager.Option('a', 1, 'the a'), 'b': 17, } n = config_manager.Namespace() n.source = config_manager.Namespace() n.source.add_option('c', A, 'the A class') assert n.source.c.doc == 'the A class' n.dest = config_manager.Namespace() n.dest.add_option('c', A, doc='the A class') assert n.dest.c.doc == 'the A class' c = config_manager.ConfigurationManager( [n], use_admin_controls=True, #use_config_files=False, use_auto_help=False, argv_source=[]) e = config_manager.Namespace() e.s = config_manager.Namespace() e.s.add_option('c', A, doc='the A class') e.s.add_option('a', 1, 'the a') e.s.add_option('b', default=17) e.d = config_manager.Namespace() e.d.add_option('c', A, doc='the A class') e.d.add_option('a', 1, 'the a') e.d.add_option('b', default=17) def namespace_test(val): self.assertEqual(type(val), config_manager.Namespace) def option_test(val, expected=None): self.assertEqual(val.name, expected.name) self.assertEqual(val.default, expected.default) self.assertEqual(val.doc, expected.doc) e = [ ('dest', 'dest', namespace_test), ('dest.a', 'a', functools.partial(option_test, expected=e.d.a)), ('dest.b', 'b', functools.partial(option_test, expected=e.d.b)), ('dest.c', 'c', functools.partial(option_test, expected=e.d.c)), ('source', 'source', namespace_test), ('source.a', 'a', functools.partial(option_test, expected=e.s.a)), ('source.b', 'b', functools.partial(option_test, expected=e.s.b)), ('source.c', 'c', functools.partial(option_test, expected=e.s.c)), ] c_contents = [(qkey, key, val) for qkey, key, val in c._walk_config()] c_contents.sort() e.sort() for c_tuple, e_tuple in zip(c_contents, e): qkey, key, val = c_tuple e_qkey, e_key, e_fn = e_tuple self.assertEqual(qkey, e_qkey) self.assertEqual(key, e_key) e_fn(val)
def test_write_ini(self): n = self._some_namespaces() c = config_manager.ConfigurationManager( [n], [ConfigParser], use_admin_controls=False, #use_config_files=False, use_auto_help=False, argv_source=[]) out = StringIO() c.write_conf(for_configparse, opener=stringIO_context_wrapper(out)) received = out.getvalue() out.close() expected = """[top_level] # name: aaa # doc: the a # converter: configman.datetime_util.datetime_from_ISO_string aaa=2011-05-04T15:10:00 [c] # c space # name: c.fred # doc: husband from Flintstones # converter: str fred=stupid # name: c.wilma # doc: wife from Flintstones # converter: str wilma=waspish [d] # d space # name: d.ethel # doc: female neighbor from I Love Lucy # converter: str ethel=silly # name: d.fred # doc: male neighbor from I Love Lucy # converter: str fred=crabby [x] # x space # name: x.password # doc: the password # converter: str password=secret # name: x.size # doc: how big in tons # converter: int size=100 """ self.assertEqual(expected.strip(), received.strip())
def test_write_ini(self): n = self._some_namespaces() c = config_manager.ConfigurationManager( [n], use_admin_controls=True, #use_config_files=False, use_auto_help=False, argv_source=[]) expected = \ """# name: aaa # doc: the a # converter: configman.datetime_util.datetime_from_ISO_string # Inspect the automatically written value below to make sure it is valid # as a Python object for its intended converter function. aaa='2011-05-04T15:10:00' [c] # name: fred # doc: husband from Flintstones # converter: str fred='stupid, deadly' # name: wilma # doc: wife from Flintstones # converter: str wilma=waspish's [d] # name: ethel # doc: female neighbor from I Love Lucy # converter: str ethel=silly # name: fred # doc: male neighbor from I Love Lucy # converter: str fred=crabby [x] # name: password # doc: the password # converter: str password=secret "message" # name: size # doc: how big in tons # converter: int size=100 """ out = StringIO() c.write_conf(for_configobj, opener=stringIO_context_wrapper(out)) received = out.getvalue() out.close() self.assertEqual(expected.strip(), received.strip())
def test_json_round_trip(self): n = config_manager.Namespace(doc='top') n.add_option('aaa', '2011-05-04T15:10:00', 'the a', short_form='a', from_string_converter=dtu.datetime_from_ISO_string) expected_date = dtu.datetime_from_ISO_string('2011-05-04T15:10:00') n.add_option('bbb', '37', 'the a', short_form='a', from_string_converter=int) n.add_option('write', 'json') n.add_aggregation('bbb_minus_one', bbb_minus_one) #t = tempfile.NamedTemporaryFile('w', suffix='.json', delete=False) name = '/tmp/test.json' import functools opener = functools.partial(open, name, 'w') c1 = config_manager.ConfigurationManager([n], [], use_admin_controls=True, use_auto_help=False, app_name='/tmp/test', app_version='0', app_description='', argv_source=[]) c1.write_conf('json', opener) d1 = {'bbb': 88} d2 = {'bbb': '-99'} try: with open(name) as jfp: j = json.load(jfp) c2 = config_manager.ConfigurationManager((j, ), (d1, d2), use_admin_controls=True, use_auto_help=False, argv_source=[]) config = c2.get_config() self.assertEqual(config.aaa, expected_date) self.assertEqual(config.bbb, -99) self.assertEqual(config.bbb_minus_one, -100) finally: os.unlink(name)
def test_for_getopt_get_values_with_short_form(self): c = config_manager.ConfigurationManager( use_admin_controls=True, #use_config_files=False, use_auto_help=False, argv_source=[]) source = ['-l', '10'] o = ValueSource(source) c.option_definitions.add_option('limit', default=0, short_form='l') self.assertEqual(o.get_values(c, False), {'limit': '10'}) self.assertEqual(o.get_values(c, True), {'limit': '10'})
def test_configuration_with_namespace(self): namespace = config_manager.Namespace() namespace.add_option('a') namespace.a.default = 1 namespace.a.doc = 'the a' namespace.b = 17 config = config_manager.ConfigurationManager([namespace], argv_source=[]) self.assertTrue( isinstance(config.option_definitions.b, config_manager.Option)) self.assertEqual(config.option_definitions.b.value, 17) self.assertEqual(config.option_definitions.b.default, 17) self.assertEqual(config.option_definitions.b.name, 'b')
def test_namespace_constructor_3(self): """test json definition""" j = '{ "a": {"name": "a", "default": 1, "doc": "the a"}, "b": 17}' config = config_manager.ConfigurationManager([j], argv_source=[]) self.assertTrue( isinstance(config.option_definitions.a, config_manager.Option)) self.assertEqual(config.option_definitions.a.value, 1) self.assertEqual(config.option_definitions.a.default, 1) self.assertEqual(config.option_definitions.a.name, 'a') self.assertTrue( isinstance(config.option_definitions.b, config_manager.Option)) self.assertEqual(config.option_definitions.b.value, 17) self.assertEqual(config.option_definitions.b.default, 17) self.assertEqual(config.option_definitions.b.name, 'b')
def test_for_getopt_get_values(self): c = config_manager.ConfigurationManager( use_admin_controls=True, #use_config_files=False, use_auto_help=False, argv_source=[]) source = ['--limit', '10'] o = ValueSource(source) self.assertEqual(o.get_values(c, True), {}) self.assertRaises(NotAnOptionError, o.get_values, c, False) c.option_definitions.add_option('limit', default=0) self.assertEqual(o.get_values(c, False), {'limit': '10'}) self.assertEqual(o.get_values(c, True), {'limit': '10'})
def test_overlay_config_5(self): """test namespace definition w/getopt""" n = config_manager.Namespace() n.add_option('a', 1, doc='the a') n.b = 17 n.add_option('c', False, doc='the c') c = config_manager.ConfigurationManager([n], [['--a', '2', '--c']], use_admin_controls=True, use_auto_help=False, argv_source=[]) self.assertTrue( isinstance(c.option_definitions.b, config_manager.Option)) self.assertEqual(c.option_definitions.a.value, 2) self.assertEqual(c.option_definitions.b.value, 17) self.assertEqual(c.option_definitions.b.default, 17) self.assertEqual(c.option_definitions.b.name, 'b') self.assertEqual(c.option_definitions.c.name, 'c') self.assertEqual(c.option_definitions.c.value, True)
def test_overlay_config_6(self): """test namespace definition w/getopt""" n = config_manager.Namespace() n.add_option('a', doc='the a', default=1) n.b = 17 n.c = config_manager.Namespace() n.c.add_option('extra', doc='the x', default=3.14159, short_form='e') c = config_manager.ConfigurationManager( [n], [['--a', '2', '--c.extra', '11.0']], use_admin_controls=True, use_auto_help=False) self.assertEqual(type(c.option_definitions.b), config_manager.Option) self.assertEqual(c.option_definitions.a.value, 2) self.assertEqual(c.option_definitions.b.value, 17) self.assertEqual(c.option_definitions.b.default, 17) self.assertEqual(c.option_definitions.b.name, 'b') self.assertEqual(c.option_definitions.c.extra.name, 'extra') self.assertEqual(c.option_definitions.c.extra.doc, 'the x') self.assertEqual(c.option_definitions.c.extra.default, '11.0') self.assertEqual(c.option_definitions.c.extra.value, 11.0)
def test_overlay_config_6a(self): """test namespace w/getopt w/short form""" n = config_manager.Namespace() n.add_option('a', 1, doc='the a') n.b = 17 n.c = config_manager.Namespace() n.c.add_option('extra', 3.14159, 'the x', short_form='e') c = config_manager.ConfigurationManager( [n], [getopt], use_admin_controls=True, argv_source=['--a', '2', '-e', '11.0'], use_auto_help=False) self.assertEqual(c.option_definitions.a, n.a) self.assertEqual(type(c.option_definitions.b), config_manager.Option) self.assertEqual(c.option_definitions.a.value, 2) self.assertEqual(c.option_definitions.b.value, 17) self.assertEqual(c.option_definitions.b.default, 17) self.assertEqual(c.option_definitions.b.name, 'b') self.assertEqual(c.option_definitions.c.extra.name, 'extra') self.assertEqual(c.option_definitions.c.extra.doc, 'the x') self.assertEqual(c.option_definitions.c.extra.default, 3.14159) self.assertEqual(c.option_definitions.c.extra.value, 11.0)
def test_for_configobj_basics_2(self): tmp_filename = os.path.join(tempfile.gettempdir(), 'test.ini') open(tmp_filename, 'w').write(""" # comment name=Peter awesome= # comment [othersection] foo=bar # other comment """) try: o = for_configobj.ValueSource(tmp_filename) c = config_manager.ConfigurationManager( [], use_admin_controls=True, #use_config_files=False, use_auto_help=False, argv_source=[]) self.assertEqual(o.get_values(c, False), { 'othersection': { 'foo': 'bar' }, 'name': 'Peter', 'awesome': '' }) self.assertEqual(o.get_values(c, True), { 'othersection': { 'foo': 'bar' }, 'name': 'Peter', 'awesome': '' }) finally: if os.path.isfile(tmp_filename): os.remove(tmp_filename)
def test_write_ini(self): n = self._some_namespaces() c = config_manager.ConfigurationManager( [n], use_admin_controls=True, #use_config_files=False, use_auto_help=False, argv_source=[]) expected = """aaa = 2011-05-04T15:10:00 [x] password = secret size = 100 [c] wilma = waspish fred = stupid [d] ethel = silly fred = crabby """ out = StringIO() c.write_conf(for_configobj, opener=stringIO_context_wrapper(out)) received = out.getvalue() out.close() self.assertEqual(expected.strip(), received.strip())
def test_for_configparse_basics_2(self): tmp_filename = os.path.join( tempfile.gettempdir(), 'test.%s' % for_configparse.file_name_extension) open(tmp_filename, 'w').write(""" ; comment [top_level] name=Peter awesome: ; comment [othersection] foo=bar ; other comment """) try: o = ValueSource(tmp_filename) c = config_manager.ConfigurationManager( [], use_admin_controls=True, #use_config_files=False, use_auto_help=False, argv_source=[]) self.assertEqual(o.get_values(c, False), { 'othersection.foo': 'bar', 'name': 'Peter', 'awesome': '' }) self.assertEqual(o.get_values(c, True), { 'othersection.foo': 'bar', 'name': 'Peter', 'awesome': '' }) finally: if os.path.isfile(tmp_filename): os.remove(tmp_filename)
def donttest_write_ini_with_migration(self): n = self._some_namespaces() n.namespace('o') n.o.add_option('password', 'secret "message"', 'the password') n.o.namespace('beyond_o') n.o.beyond_o.add_option('password', 'secret "message"', 'the password') c = config_manager.ConfigurationManager([n], [ConfigParser], use_admin_controls=False, use_auto_help=False, argv_source=[]) out = StringIO() c.write_conf(for_configparse, opener=stringIO_context_wrapper(out)) received = out.getvalue() out.close() expected = \ """[top_level] # name: aaa # doc: the a # converter: configman.datetime_util.datetime_from_ISO_string aaa='2011-05-04T15:10:00' # name: password # doc: the password # converter: str password='******' [c] # name: fred # doc: husband from Flintstones # converter: str fred='stupid' # name: wilma # doc: wife from Flintstones # converter: str wilma=\'waspish\' [d] # name: ethel # doc: female neighbor from I Love Lucy # converter: str ethel='silly' # name: fred # doc: male neighbor from I Love Lucy # converter: str fred='crabby' [o] # name: password # doc: the password # converter: str # password='******' [o.beyond_o] # name: password # doc: the password # converter: str # password='******' [x] # name: password # doc: the password # converter: str password='******' # name: size # doc: how big in tons # converter: int size='100' """ self.assertEqual(expected.strip(), received.strip())
def test_write_ini_with_migration(self): n = self._some_namespaces() n.namespace('o') n.o.add_option('password', 'secret "message"', 'the password') c = config_manager.ConfigurationManager([n], [{ 'migration': True }], use_admin_controls=True, use_auto_help=False, argv_source=[]) expected = \ """# name: aaa # doc: the a # converter: configman.datetime_util.datetime_from_ISO_string # Inspect the automatically written value below to make sure it is valid # as a Python object for its intended converter function. aaa='2011-05-04T15:10:00' # name: password # doc: the password # converter: str # The following value is common for more than one section below. Its value # may be set here for all or it can be overridden in its original section password=secret "message" [c] # name: fred # doc: husband from Flintstones # converter: str fred='stupid, deadly' # name: wilma # doc: wife from Flintstones # converter: str wilma=waspish's [d] # name: ethel # doc: female neighbor from I Love Lucy # converter: str ethel=silly # name: fred # doc: male neighbor from I Love Lucy # converter: str fred=crabby [o] # name: password # doc: the password # converter: str # The following value has been automatically commented out because # the option is found in other sections and the defaults are the same. # The common value can be found in the lowest level section. Uncomment # to override that lower level value #password=secret "message" [x] # name: password # doc: the password # converter: str # The following value has been automatically commented out because # the option is found in other sections and the defaults are the same. # The common value can be found in the lowest level section. Uncomment # to override that lower level value #password=secret "message" # name: size # doc: how big in tons # converter: int size=100 """ out = StringIO() c.write_conf(for_configobj, opener=stringIO_context_wrapper(out)) received = out.getvalue() out.close() self.assertEqual(expected.strip(), received.strip()) values = c.get_config() self.assertEqual(values.x.size, 100) self.assertEqual(values.x.password, 'secret "message"') self.assertEqual(values.o.password, 'secret "message"') self.assertRaises(KeyError, lambda: values.d.password == 'secret "message"') self.assertRaises(KeyError, lambda: values.password == 'secret "message"') # try the round trip tmp_filename = os.path.join(tempfile.gettempdir(), 'test.ini') open(tmp_filename, 'w').write(received) try: c = config_manager.ConfigurationManager( [n], [tmp_filename], use_admin_controls=True, use_auto_help=False, argv_source=[]) values = c.get_config() self.assertEqual(values.x.size, 100) self.assertEqual(values.x.password, 'secret "message"') self.assertEqual(values.o.password, 'secret "message"') self.assertRaises( KeyError, lambda: values.d.password == 'secret "message"') self.assertRaises( KeyError, lambda: values.password == 'secret "message"') finally: if os.path.isfile(tmp_filename): os.remove(tmp_filename)