示例#1
0
    def load_file(self, path):
        self.path = path
        if os.access(path, os.R_OK|os.F_OK):
            cfg = Configuration()
            cfg.read(path)

            handler_table = (
                # section name regex       function
                # --------------------------------------------------
                (re.compile('^db\.'),      self.__config_db),
                (re.compile('^driver\.'),  self.__config_driver),
                (re.compile('^settings$'), self.__set_vars),
            )

            for section in cfg.sections:
                for regex, handler in handler_table:
                    if regex.match(section):
                        handler(cfg, section)
示例#2
0
    def load_file(self, path):
        self.path = path
        if os.access(path, os.R_OK | os.F_OK):
            cfg = Configuration()
            cfg.read(path)

            handler_table = (
                # section name regex       function
                # --------------------------------------------------
                (re.compile('^db\.'), self.__config_db),
                (re.compile('^driver\.'), self.__config_driver),
                (re.compile('^settings$'), self.__set_vars),
            )

            for section in cfg.sections:
                for regex, handler in handler_table:
                    if regex.match(section):
                        handler(cfg, section)
示例#3
0
 def testOrdering(self):
     config = Configuration(use_ordered_sections=True)
     config.readfp(StringIO(CONFIG_ORDER_TEST))
     assert config.has_section('a')
     assert config.has_section('y')
     assert config.has_section('z')
     sections = config.sections
     assert len(sections) == 4
     assert sections[0] == 'z'
     assert sections[1] == 'y'
     assert sections[2] == 'a'
     assert sections[3] == 'z2'
示例#4
0
 def testSubstitute2(self):
     os.environ['SOME_ENV_VAR'] = 'test_test_test'
     config = Configuration()
     config.readfp(StringIO(CONFIG2))
     assert config.has_section('section1')
     assert config.has_section('section2')
     assert not config.has_section('foo')
     assert not config.has_section('bar')
     assert not config.has_section('bar2')
     assert config.has_option('section1', 'foo')
     assert config.has_option('section1', 'bar')
     assert not config.has_option('section1', 'bar2')
     assert config.has_option('section2', 'foo')
     assert config.has_option('section2', 'bar')
     assert config.get('section1', 'foo') == 'bar'
     assert config.get('section1', 'bar') == 'bar'
     assert config.get('section2', 'foo') == 'bar'
     assert config.get('section2', 'bar') == os.environ['SOME_ENV_VAR']
示例#5
0
 def testSubstitute1(self):
     config = Configuration()
     config.readfp(StringIO(CONFIG1))
     assert config.has_section('section1')
     assert not config.has_section('section2')
     assert not config.has_section('foo')
     assert not config.has_section('bar')
     assert not config.has_section('bar2')
     assert config.has_option('section1', 'foo')
     assert config.has_option('section1', 'name')
     assert config.get('section1', 'name') == os.path.basename(sys.argv[0])
     assert config.get('section1', 'cwd') == os.getcwd()
     assert config.has_option('section1', 'bar')
     assert config.has_option('section1', 'bar2')
     assert config.get('section1', 'foo') == 'bar'
     assert config.get('section1', 'bar') == 'bar'
     assert config.get('section1', 'bar2') == 'bar'
示例#6
0
    def testBadSubstitution(self):
        cfgString = """
[foo]
var1 = ${bar}
"""
        import sys
        from grizzled.io import AutoFlush
        sys.stdout = AutoFlush(sys.stdout)
        config = Configuration(strict_substitution=False)
        config.readfp(StringIO(cfgString))
        config.write(sys.stdout)

        try:
            var1 = config.get('foo', 'var1', optional=True)
            assert var1 == None, 'Expected empty variable value'
        except:
            raise

        config = Configuration(strict_substitution=True)
        try:
            config.readfp(StringIO(cfgString))
            assert False, 'Expected an exception'
        except NoVariableError:
            pass
        except:
            assert False, 'Unexpected exception'
示例#7
0
    def testInclude(self):
        fd, tempPath = tempfile.mkstemp(suffix='.cfg')

        def unlinkTemp(path):
            try:
                os.unlink(path)
            except:
                pass

        atexit.register(unlinkTemp, tempPath)
        fp = os.fdopen(fd, "w")
        print >> fp, '[section3]\nbaz = somevalue\n'
        fp.close()

        s = '%s\n\n%%include "%s"\n' % (CONFIG2, tempPath)

        os.environ['SOME_ENV_VAR'] = 'test_test_test'
        config = Configuration()
        config.readfp(StringIO(s))
        unlinkTemp(tempPath)
        assert config.has_section('section1')
        assert config.has_section('section2')
        assert config.has_section('section3')
        assert not config.has_section('foo')
        assert not config.has_section('bar')
        assert not config.has_section('bar2')
        assert config.has_option('section1', 'foo')
        assert config.has_option('section1', 'bar')
        assert not config.has_option('section1', 'bar2')
        assert config.has_option('section2', 'foo')
        assert config.has_option('section2', 'bar')
        assert config.has_option('section3', 'baz')
        assert config.get('section1', 'foo') == 'bar'
        assert config.get('section1', 'bar') == 'bar'
        assert config.get('section2', 'foo') == 'bar'
        assert config.get('section2', 'bar') == os.environ['SOME_ENV_VAR']
        assert config.get('section3', 'baz') == 'somevalue'