示例#1
0
 def test_overlay_2(self):
     x = parse(self.fd("""!spec\ntype: !!int "0"\ndescription:\nvalue: 1\n"""))
     y = parse(self.fd("""section:\n param: 2"""))
     z = parse(self.fd("""section:\n param: 3"""))
     b = ConfigurationBuilder(dict(section=dict(param=x)))
     params = b.build(y, z)
     assert params['section']['param'] == 3
示例#2
0
 def test_build_2(self):
     x = parse(self.fd("""!spec\ntype: !!int "0"\ndescription:\nvalue: 1\n"""))
     b = ConfigurationBuilder(dict(section=dict(param=x),
                                   section_1=dict(param=x)))
     params = b.build()
     assert params['section']['param'] == 1
     assert params['section_1']['param'] == 1
示例#3
0
 def test_validator_required(self):
     x = parse(self.fd("""!spec\ntype: !!int "0"\ndescription:\nvalue: !required\n"""))
     b = ConfigurationBuilder(dict(section=dict(param=x)))
     params = b.build()
     assert params['section']['param'] != 1
     errors = b.validate(params)
     assert ('section', 'param') in errors
     assert isinstance(errors[('section', 'param')], Requirement)
示例#4
0
    def test_validator_type_list(self):
        x = parse(self.fd("""!spec\ntype: [ !!int "0", !!null ]\ndescription:\nvalue: !required\n"""))
        y = parse(self.fd("""section:\n param: !!null"""))
        b = ConfigurationBuilder(dict(section=dict(param=x)))
        params = b.build(y)
        assert params['section']['param'] == None

        errors = b.validate(params)
        assert not errors
示例#5
0
    def test_validator_type_error(self):
        x = parse(self.fd("""!spec\ntype: !!int "0"\ndescription:\nvalue: !required\n"""))
        y = parse(self.fd("""section:\n param: foo"""))
        b = ConfigurationBuilder(dict(section=dict(param=x)))
        params = b.build(y)
        assert params['section']['param'] == 'foo'

        errors = b.validate(params)
        assert errors
        assert ('section', 'param') in errors
        assert isinstance(errors[('section', 'param')], TypeError)
示例#6
0
        return self._params[section].keys()

    def items(self, section):
        return self._params[section].items()

    def sections(self):
        return self._params.keys()

    def to_dict(self):
        return self._params

    def set(self, section, key, value):
        sect = self._params.setdefault(section, {})
        sect[key] = value

if __name__ == '__main__':
    import sys
    from pyyacc.parser import parse
    from pyyacc.parser import ConfigurationBuilder

    descriptor = parse(open(sys.argv[1]))
    yamls = [parse(open(f)) for f in sys.argv[2:]]
    b = ConfigurationBuilder(descriptor)
    params = b.build(*yamls)
    cfg = RawConfigParser()
    for section in params.keys():
        cfg.add_section(section)
        for key, value in params[section].iteritems():
            cfg.set(section, key, value)
    cfg.write(sys.stdout)
示例#7
0
 def test_overlay_3(self):
     x = parse(self.fd("""!spec\ntype: !!int "0"\ndescription:\nvalue: !optional\n"""))
     b = ConfigurationBuilder(dict(section=dict(param=x)))
     params = b.build()
     assert 'param' not in params['section']
示例#8
0
 def test_build_1(self):
     x = parse(self.fd("""!spec\ntype: !!int "0"\ndescription:\nvalue: 1\ndeprecated: true"""))
     b = ConfigurationBuilder(dict(section=dict(param=x)))
     params = b.build()
     assert isinstance(params, ConfigSet)
     assert params['section']['param'] == 1