def test_for_mapping_long_doc_in_write_conf(self): n = self._some_namespaces() n = Namespace(doc='top') n.add_option( 'aaa', 'Default Value Goes In Here', 'This time the documentation string is really long. So long ' 'that we have to write it on multiple lines.', ) cm = ConfigurationManager( n, values_source_list=[], ) out = StringIO() cm.write_conf(for_mapping, opener=stringIO_context_wrapper(out)) received = out.getvalue() out.close() for line in received.splitlines(): self.assertTrue(len(line) < 80, line) expected = """ # This time the documentation string is really long. So long that we have to # write it on multiple lines. (default: 'Default Value Goes In Here') aaa='Default Value Goes In Here' """.strip() self.assertEqual(received.strip(), expected)
def test_for_mapping_nested_namespaces(self): n = self._some_namespaces() cm = ConfigurationManager( n, values_source_list=[], ) out = StringIO() cm.write_conf(for_mapping, opener=stringIO_context_wrapper(out)) received = out.getvalue() out.close() expected = """ # the a (default: '2011-05-04T15:10:00') aaa='2011-05-04T15:10:00' # your uncle (default: 98) c__dwight='98' # husband from Flintstones (default: 'stupid') c__fred='stupid' # wife from Flintstones (default: 'waspish') c__wilma='waspish' # my uncle (default: 97) c__e__dwight='97' # female neighbor from I Love Lucy (default: 'silly') d__ethel='silly' # male neighbor from I Love Lucy (default: 'crabby') d__fred='crabby' # the password (default: 'secret') x__password='******' # how big in tons (default: 100) x__size='100' """.strip() self.assertEqual(received.strip(), expected)
def test_write_skip_aggregations(self): required_config = Namespace() required_config.add_option( 'minimal_version_for_understanding_refusal', doc='ignore the Thottleable protocol', default={'Firefox': '3.5.4'}, ) required_config.add_aggregation('an_agg', lambda x, y, z: 'I refuse') cm = ConfigurationManager( required_config, values_source_list=[], ) config = cm.get_config() s = StringIO() @contextlib.contextmanager def s_opener(): yield s cm.write_conf('py', s_opener) generated_python_module_text = s.getvalue() expected = """# generated Python configman file # ignore the Thottleable protocol minimal_version_for_understanding_refusal = { "Firefox": "3.5.4" } """ self.assertEqual(generated_python_module_text, expected)
def test_write_with_imported_module_with_regex(self): required_config = Namespace() required_config.add_option('identifier', doc='just an identifier re', default=r'[a-zA-Z][a-zA-Z0-9]*', from_string_converter=re.compile) cm = ConfigurationManager( required_config, values_source_list=[], ) config = cm.get_config() s = StringIO() @contextlib.contextmanager def s_opener(): yield s cm.write_conf('py', s_opener) generated_python_module_text = s.getvalue() expected = """# generated Python configman file # just an identifier re identifier = "[a-zA-Z][a-zA-Z0-9]*" """ self.assertEqual(generated_python_module_text, expected)
def test_write_with_imported_module_with_regex(self): required_config = Namespace() required_config.add_option( 'identifier', doc='just an identifier re', default=r'[a-zA-Z][a-zA-Z0-9]*', from_string_converter=re.compile ) cm = ConfigurationManager( required_config, values_source_list=[], ) config = cm.get_config() s = StringIO() @contextlib.contextmanager def s_opener(): yield s cm.write_conf('py', s_opener) generated_python_module_text = s.getvalue() expected = """# generated Python configman file # just an identifier re identifier = "[a-zA-Z][a-zA-Z0-9]*" """ self.assertEqual(generated_python_module_text, expected)
def test_write_simple(self): rc = Namespace() rc.add_option( 'a', default=23 ) rc.add_option( 'b', default='this is b' ) rc.namespace('n') rc.n.add_option( 'x', default=datetime(1999, 12, 31, 11, 59) ) rc.n.add_option( 'y', default=timedelta(3) ) rc.n.add_option( 'z', default=date(1650, 10, 2) ) cm = ConfigurationManager( [rc], values_source_list=[ { 'a': 68, 'n.x': datetime(1960, 5, 4, 15, 10), 'n.y': timedelta(3), 'n.z': date(2001, 1, 1) } ] ) s = StringIO() @contextlib.contextmanager def s_opener(): yield s cm.write_conf('py', s_opener) r = s.getvalue() g = {} l = {} exec r in g, l self.assertEqual(l['a'], 68) self.assertEqual(l['b'], 'this is b') self.assertEqual(l['n'].x, datetime(1960, 5, 4, 15, 10)) self.assertEqual(l['n'].y, timedelta(3)) self.assertEqual(l['n'].z, date(2001, 1, 1))
def test_write_simple(self): rc = Namespace() rc.add_option( 'a', default=23 ) rc.add_option( 'b', default='this is b' ) rc.namespace('n') rc.n.add_option( 'x', default=datetime(1999, 12, 31, 11, 59) ) rc.n.add_option( 'y', default=timedelta(3) ) rc.n.add_option( 'z', default=date(1650, 10, 2) ) cm = ConfigurationManager( [rc], values_source_list=[ { 'a': 68, 'n.x': datetime(1960, 5, 4, 15, 10), 'n.y': timedelta(3), 'n.z': date(2001, 1, 1) } ] ) s = StringIO() @contextlib.contextmanager def s_opener(): yield s cm.write_conf('py', s_opener) r = s.getvalue() g = {} l = {} six.exec_(r, g, l) self.assertEqual(l['a'], 68) self.assertEqual(l['b'], 'this is b') self.assertEqual(l['n'].x, datetime(1960, 5, 4, 15, 10)) self.assertEqual(l['n'].y, timedelta(3)) self.assertEqual(l['n'].z, date(2001, 1, 1))
def donttest_write_flat_with_migration(self): n = Namespace() n.add_option('x', default=13, doc='the x') n.add_option('y', default=-1, doc='the y') n.add_option('z', default='fred', doc='the z') n.namespace('o') n.o.add_option('x', default=13, doc='the x') c = ConfigurationManager( [n], use_admin_controls=True, use_auto_help=False, argv_source=[] ) out = StringIO() c.write_conf(for_conf, opener=stringIO_context_wrapper(out)) result = out.getvalue() expected = ( "# name: x\n" "# doc: the x\n" "# converter: int\n" "# x='13'\n" "\n" "# name: y\n" "# doc: the y\n" "# converter: int\n" "y='-1'\n" "\n" "# name: z\n" "# doc: the z\n" "# converter: str\n" "z='fred'\n" "\n" "#-------------------------------------------------------------------------------\n" "# o - \n" "\n" "# name: o.x\n" "# doc: the x\n" "# converter: int\n" "# o.x='13'\n" "\n" ) self.assertEqual(expected, result, "exepected\n%s\nbut got\n%s" % (expected, result))
def test_for_mapping_nested_namespaces(self): n = self._some_namespaces() cm = ConfigurationManager(n, values_source_list=[]) out = StringIO() cm.write_conf(for_mapping, opener=stringIO_context_wrapper(out)) received = out.getvalue() out.close() expected = """aaa='2011-05-04T15:10:00' c__dwight='98' c__fred='stupid' c__wilma='waspish' c__e__dwight='97' d__ethel='silly' d__fred='crabby' x__password='******' x__size='100'""" self.assertEqual(received.strip(), expected)
def donttest_write_flat_with_migration(self): n = Namespace() n.add_option('x', default=13, doc='the x') n.add_option('y', default=-1, doc='the y') n.add_option('z', default='fred', doc='the z') n.namespace('o') n.o.add_option('x', default=13, doc='the x') c = ConfigurationManager([n], use_admin_controls=True, use_auto_help=False, argv_source=[]) out = StringIO() c.write_conf(for_conf, opener=stringIO_context_wrapper(out)) result = out.getvalue() expected = ( "# name: x\n" "# doc: the x\n" "# converter: int\n" "# x='13'\n" "\n" "# name: y\n" "# doc: the y\n" "# converter: int\n" "y='-1'\n" "\n" "# name: z\n" "# doc: the z\n" "# converter: str\n" "z='fred'\n" "\n" "#-------------------------------------------------------------------------------\n" "# o - \n" "\n" "# name: o.x\n" "# doc: the x\n" "# converter: int\n" "# o.x='13'\n" "\n") self.assertEqual(expected, result, "exepected\n%s\nbut got\n%s" % (expected, result))
def test_write_skip_aggregations(self): required_config = Namespace() required_config.add_option( 'minimal_version_for_understanding_refusal', doc='ignore the Thottleable protocol', default={'Firefox': '3.5.4'}, ) required_config.add_aggregation( 'an_agg', lambda x, y, z: 'I refuse' ) cm = ConfigurationManager( required_config, values_source_list=[], ) config = cm.get_config() s = StringIO() @contextlib.contextmanager def s_opener(): yield s cm.write_conf('py', s_opener) generated_python_module_text = s.getvalue() expected = """# generated Python configman file # ignore the Thottleable protocol minimal_version_for_understanding_refusal = { "Firefox": "3.5.4" } """ self.assertEqual(generated_python_module_text, expected)
def donttest_for_conf_nested_namespaces(self): n = self._some_namespaces() cm = ConfigurationManager(n, values_source_list=[], ) out = StringIO() cm.write_conf(for_conf, opener=stringIO_context_wrapper(out)) received = out.getvalue() out.close() expected = """# name: aaa # doc: the a # converter: configman.datetime_util.datetime_from_ISO_string aaa=2011-05-04T15:10:00 #------------------------------------------------------------------------------- # c - c space # name: c.dwight # doc: your uncle # converter: int c.dwight=98 # name: c.fred # doc: husband from Flintstones # converter: str c.fred=stupid # name: c.wilma # doc: wife from Flintstones # converter: str c.wilma=waspish #------------------------------------------------------------------------------- # e - e space # name: c.e.dwight # doc: my uncle # converter: int c.e.dwight=97 #------------------------------------------------------------------------------- # d - d space # name: d.ethel # doc: female neighbor from I Love Lucy # converter: str d.ethel=silly # name: d.fred # doc: male neighbor from I Love Lucy # converter: str d.fred=crabby #------------------------------------------------------------------------------- # x - x space # name: x.password # doc: the password # converter: str x.password=secret # name: x.size # doc: how big in tons # converter: int x.size=100""" self.assertEqual(received.strip(), expected) strio = StringIO(expected) n.c.dwight.default = 3823 n.c.e.dwight = 'fred' cm2 = ConfigurationManager(n, [stringIO_context_wrapper(strio)], use_admin_controls=False, use_auto_help=False) result = cm2.get_config() self.assertEqual(len(result), 4) self.assertEqual(sorted(result.keys()), ['aaa', 'c', 'd', 'x']) self.assertEqual(len(result.c), 4) self.assertEqual(sorted(result.c.keys()), ['dwight', 'e', 'fred', 'wilma' ]) self.assertEqual(result.c.dwight, 98) self.assertEqual(len(result.c.e), 1) self.assertEqual(result.c.e.dwight, '97')
def test_for_conf_nested_namespaces(self): n = self._some_namespaces() cm = ConfigurationManager( n, values_source_list=[], ) out = StringIO() cm.write_conf(for_conf, opener=stringIO_context_wrapper(out)) received = out.getvalue() out.close() expected = """# name: aaa # doc: the a # converter: configman.datetime_util.datetime_from_ISO_string aaa=2011-05-04T15:10:00 #------------------------------------------------------------------------------- # c - c space # name: c.dwight # doc: your uncle # converter: int c.dwight=98 # name: c.fred # doc: husband from Flintstones # converter: str c.fred=stupid # name: c.wilma # doc: wife from Flintstones # converter: str c.wilma=waspish #------------------------------------------------------------------------------- # e - e space # name: c.e.dwight # doc: my uncle # converter: int c.e.dwight=97 #------------------------------------------------------------------------------- # d - d space # name: d.ethel # doc: female neighbor from I Love Lucy # converter: str d.ethel=silly # name: d.fred # doc: male neighbor from I Love Lucy # converter: str d.fred=crabby #------------------------------------------------------------------------------- # x - x space # name: x.password # doc: the password # converter: str x.password=secret # name: x.size # doc: how big in tons # converter: int x.size=100""" self.assertEqual(received.strip(), expected) strio = StringIO(expected) n.c.dwight.default = 3823 n.c.e.dwight = 'fred' cm2 = ConfigurationManager(n, [stringIO_context_wrapper(strio)], use_admin_controls=False, use_auto_help=False) result = cm2.get_config() self.assertEqual(len(result), 4) self.assertEqual(sorted(result.keys()), ['aaa', 'c', 'd', 'x']) self.assertEqual(len(result.c), 4) self.assertEqual(sorted(result.c.keys()), ['dwight', 'e', 'fred', 'wilma']) self.assertEqual(result.c.dwight, 98) self.assertEqual(len(result.c.e), 1) self.assertEqual(result.c.e.dwight, '97')
def test_write_with_imported_module(self): import os from configman.tests.values_for_module_tests_1 import Alpha, foo, a definitions = { 'os_module': os, 'a': 17, 'imported_class': Alpha, 'imported_function': foo, 'xxx': { 'yyy': a, } } cm = ConfigurationManager( definitions, values_source_list=[], ) s = StringIO() @contextlib.contextmanager def s_opener(): yield s cm.write_conf('py', s_opener) generated_python_module_text = s.getvalue() expected = """# generated Python configman file from configman.dotdict import DotDict from configman.tests.values_for_module_tests_1 import ( foo, Alpha, ) import os # the following symbols will be ignored by configman when # this module is used as a value source. This will # suppress the mismatch warning since these symbols are # values for options, not option names themselves. ignore_symbol_list = [ "Alpha", "DotDict", "foo", "os", ] # a a = 17 # imported_class imported_class = Alpha # imported_function imported_function = foo # os_module os_module = os # Namespace: xxx xxx = DotDict() # yyy xxx.yyy = 18 """ self.assertEqual(generated_python_module_text, expected)
def test_write_with_imported_module_with_internal_mappings(self): import os from configman.tests.values_for_module_tests_1 import Alpha, foo d = { 'a': 18, 'b': 'hello', 'c': [1, 2, 3], 'd': { 'host': 'localhost', 'port': 5432, } } definitions = { 'os_module': os, 'a': 17, 'imported_class': Alpha, 'imported_function': foo, 'xxx': { 'yyy': Option('yyy', default=d) }, 'e': None, } required_config = Namespace() required_config.add_option( 'minimal_version_for_understanding_refusal', doc='ignore the Thottleable protocol', default={'Firefox': '3.5.4'}, ) cm = ConfigurationManager( [definitions, required_config], values_source_list=[], ) config = cm.get_config() s = StringIO() @contextlib.contextmanager def s_opener(): yield s cm.write_conf('py', s_opener) generated_python_module_text = s.getvalue() expected = """# generated Python configman file from configman.dotdict import DotDict from configman.tests.values_for_module_tests_1 import ( foo, Alpha, ) import os # the following symbols will be ignored by configman when # this module is used as a value source. This will # suppress the mismatch warning since these symbols are # values for options, not option names themselves. ignore_symbol_list = [ "Alpha", "DotDict", "foo", "os", ] # a a = 17 # e e = None # imported_class imported_class = Alpha # imported_function imported_function = foo # ignore the Thottleable protocol minimal_version_for_understanding_refusal = { "Firefox": "3.5.4" } # os_module os_module = os # Namespace: xxx xxx = DotDict() xxx.yyy = { "a": 18, "b": "hello", "c": [ 1, 2, 3 ], "d": { "host": "localhost", "port": 5432 } } """ self.assertEqual(generated_python_module_text, expected)