Пример #1
0
        def test_configobj_relative_includes(self):
            include_file_name = ''
            ini_file_name = ''
            try:
                db_creds_dir = tempfile.mkdtemp()
                db_creds_basename = os.path.basename(db_creds_dir)
                ini_repo_dir = tempfile.mkdtemp()
                with tempfile.NamedTemporaryFile('w',
                                                 suffix='ini',
                                                 dir=db_creds_dir,
                                                 delete=False) as f:
                    include_file_name = f.name
                    include_file_basename = os.path.basename(f.name)
                    contents = ('dbhostname=myserver\n'
                                'dbname=some_database\n'
                                'dbuser=dwight\n'
                                'dbpassword=secrets\n')
                    f.write(contents)

                with tempfile.NamedTemporaryFile('w',
                                                 suffix='ini',
                                                 dir=ini_repo_dir,
                                                 delete=False) as f:
                    ini_file_name = f.name
                    contents = ('+include ../%s/%s\n'
                                '\n'
                                '[destination]\n'
                                '+include ../%s/%s\n' %
                                (db_creds_basename, include_file_basename,
                                 db_creds_basename, include_file_basename))
                    f.write(contents)
                o = for_configobj.ValueSource(ini_file_name)
                expected_dict = {
                    'dbhostname': 'myserver',
                    'dbname': 'some_database',
                    'dbuser': '******',
                    'dbpassword': '******',
                    'destination': {
                        'dbhostname': 'myserver',
                        'dbname': 'some_database',
                        'dbuser': '******',
                        'dbpassword': '******',
                    }
                }
                self.assertEqual(o.get_values(1, True), expected_dict)
            finally:
                if os.path.isfile(include_file_name):
                    os.remove(include_file_name)
                if os.path.isfile(ini_file_name):
                    os.remove(ini_file_name)
                if os.path.isdir(db_creds_dir):
                    os.rmdir(db_creds_dir)
                if os.path.isdir(ini_repo_dir):
                    os.rmdir(ini_repo_dir)
Пример #2
0
        def test_configobj_includes_inside_sections(self):
            include_file_name = ''
            ini_file_name = ''
            try:
                with tempfile.NamedTemporaryFile('w',
                                                 suffix='ini',
                                                 delete=False) as f:
                    include_file_name = f.name
                    contents = ('dbhostname=myserver\n'
                                'dbname=some_database\n'
                                'dbuser=dwight\n'
                                'dbpassword=secrets\n')
                    f.write(contents)

                with tempfile.NamedTemporaryFile('w',
                                                 suffix='ini',
                                                 delete=False) as f:
                    ini_file_name = f.name
                    contents = ('[source]\n'
                                '+include %s\n'
                                '\n'
                                '[destination]\n'
                                '+include %s\n' %
                                (include_file_name, include_file_name))
                    f.write(contents)
                o = for_configobj.ValueSource(ini_file_name)
                expected_dict = {
                    'source': {
                        'dbhostname': 'myserver',
                        'dbname': 'some_database',
                        'dbuser': '******',
                        'dbpassword': '******'
                    },
                    'destination': {
                        'dbhostname': 'myserver',
                        'dbname': 'some_database',
                        'dbuser': '******',
                        'dbpassword': '******'
                    }
                }
                self.assertEqual(o.get_values(1, True), expected_dict)
            finally:
                if os.path.isfile(include_file_name):
                    os.remove(include_file_name)
                if os.path.isfile(ini_file_name):
                    os.remove(ini_file_name)
Пример #3
0
        def test_for_configobj_basics(self):
            """test basic use of for_configobj"""
            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)
                r = {
                    'othersection': {
                        'foo': 'bar'
                    },
                    'name': 'Peter',
                    'awesome': ''
                }
                assert o.get_values(None, None) == r
                # in the case of this implementation of a ValueSource,
                # the two parameters to get_values are dummies.  That may
                # not be true for all ValueSource implementations
                self.assertEqual(o.get_values(0, False), r)
                self.assertEqual(o.get_values(1, True), r)
                self.assertEqual(o.get_values(2, False), r)
                self.assertEqual(o.get_values(3, True), r)

                v = o.get_values(None, True, DotDict)
                self.assertTrue(isinstance(v, DotDict))
                v = o.get_values(None, None, obj_hook=DotDictWithAcquisition)
                self.assertTrue(isinstance(v, DotDictWithAcquisition))

            finally:
                if os.path.isfile(tmp_filename):
                    os.remove(tmp_filename)
Пример #4
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 = 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)