Ejemplo n.º 1
0
    def test3(self):

        data = r''' "a b c" : 'a b c' '''

        parser = ConfigParser(StringIO.StringIO(data))
        options = parser.parse()

        self.assertEqual(len(options), 1)
        self.assertEqual(options, [('a b c', 'a b c')])
Ejemplo n.º 2
0
    def testComments(self):

        data = '''#comment\na = b  # comment\n\n  #comment\n'''

        parser = ConfigParser(StringIO.StringIO(data))
        options = parser.parse()

        self.assertEqual(len(options), 1)
        self.assertEqual(options, [('a', 'b')])
Ejemplo n.º 3
0
    def testComments(self):

        data = '''#comment\na = b  # comment\n\n  #comment\n'''

        parser = ConfigParser(io.StringIO(data))
        options = parser.parse()

        self.assertEqual(len(options), 1)
        self.assertEqual(options, [('a', 'b')])
Ejemplo n.º 4
0
    def test2(self):

        data = "a = { b = [c, d, e] }"

        parser = ConfigParser(StringIO.StringIO(data))
        options = parser.parse()

        self.assertEqual(len(options), 3)
        self.assertEqual(options, [('a.b', 'c'), ('a.b', 'd'), ('a.b', 'e')])
Ejemplo n.º 5
0
    def test3(self):

        data = r''' "a b c" : 'a b c' '''

        parser = ConfigParser(io.StringIO(data))
        options = parser.parse()

        self.assertEqual(len(options), 1)
        self.assertEqual(options, [('a b c', 'a b c')])
Ejemplo n.º 6
0
    def test3(self):

        data = r'''a = { b = "a b 'c", c: '\t\n\b\r\f\u1AX' }'''

        parser = ConfigParser(io.StringIO(data))
        options = parser.parse()

        self.assertEqual(len(options), 2)
        self.assertEqual(options, [('a.b', 'a b \'c'), ('a.c', '\t\n\b\r\f\x1AX')])
Ejemplo n.º 7
0
    def test2(self):

        data = "a = { b = [c, d, e] }"

        parser = ConfigParser(io.StringIO(data))
        options = parser.parse()

        self.assertEqual(len(options), 3)
        self.assertEqual(options, [('a.b', 'c'), ('a.b', 'd'), ('a.b', 'e')])
Ejemplo n.º 8
0
    def test1(self):

        data = "a, b: c\n[d,e]"

        parser = ConfigParser(io.StringIO(data))
        options = parser.parse()

        self.assertEqual(len(options), 4)
        self.assertEqual(options, [('a', None), ('b', 'c'), ('d', None), ('e', None)])
Ejemplo n.º 9
0
    def __init__(self, files):
        """
        Process all config files, throws on error
        """

        UserDict.UserDict.__init__(self)

        def _options(group):
            '''massage options list, takes list of (key, opt) pairs'''
            options = list(opt for k, opt in group)
            if len(options) == 1:
                options = options[0]
            return options

        for config in files:

            # parse whole thing
            try:
                cfgParser = ConfigParser(open(config))
                options = cfgParser.parse()
            except Exception as ex:
                logging.error('Failed to parse configuration file: %r', ex)
                raise

            # options are returned as a list of (key, value) pairs, there will be
            # more than one key appearance for some options, merge this together and
            # make a dict out of it
            options.sort()
            options = dict((key, _options(group))
                           for key, group in itertools.groupby(
                               options, lambda pair: pair[0]))

            # in partitioner config files loaded earlier have higher priority
            # (options are not overwritten by later configs), do the same here
            options.update(self.data)
            self.data = options

        # check that we have a set of required options defined
        missing = [
            key for key in self.requiredConfigKeys if key not in self.data
        ]
        if self.isRefMatch:
            missing += [
                key for key in self.requiredRefMatchKeys
                if key not in self.data
            ]
        elif self.partitioned:
            missing += [
                key for key in self.requiredPartKeys if key not in self.data
            ]
        if missing:
            logging.error(
                'Required options are missing from configuration files: %r',
                ' '.join(missing))
            raise KeyError('required options are missing: ' +
                           ' '.join(missing))
Ejemplo n.º 10
0
    def __init__(self, files):
        """
        Process all config files, throws on error
        """

        dict.__init__(self)

        def _options(group):
            '''massage options list, takes list of (key, opt) pairs'''
            options = list(opt for k, opt in group)
            if len(options) == 1:
                options = options[0]
            return options

        for config in files:

            # parse whole thing
            try:
                cfgParser = ConfigParser(open(config))
                options = cfgParser.parse()
            except Exception as ex:
                logging.error('Failed to parse configuration file: %r', ex)
                raise

            # options are returned as a list of (key, value) pairs, there will be
            # more than one key appearance for some options, merge this together and
            # make a dict out of it
            options.sort()
            options = dict((key, _options(group)) for key, group
                           in itertools.groupby(options, lambda pair: pair[0]))

            # in partitioner config files loaded earlier have higher priority
            # (options are not overwritten by later configs), do the same here
            for k, v in options.items():
                if k not in self:
                    self[k] = v

        # check that we have a set of required options defined
        missing = [key for key in self.requiredConfigKeys if key not in self]
        if self.isRefMatch:
            missing += [key for key in self.requiredRefMatchKeys if key not in self]
        elif self.partitioned:
            missing += [key for key in self.requiredPartKeys if key not in self]
        if missing:
            logging.error('Required options are missing from configuration files: %r',
                          ' '.join(missing))
            raise KeyError('required options are missing: ' + ' '.join(missing))
Ejemplo n.º 11
0
    def testExcept2(self):

        data = "a = 'b"

        parser = ConfigParser(StringIO.StringIO(data))
        self.assertRaises(ValueError, parser.parse)
Ejemplo n.º 12
0
    def testExcept4(self):

        data = """ a = x\x13y """

        parser = ConfigParser(StringIO.StringIO(data))
        self.assertRaises(ValueError, parser.parse)