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
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
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)
def test_parse_spec(self): x = parse(self.fd("""!spec\ntype: !!int "0"\ndescription:\nvalue: 1""")) assert isinstance(x, ValueSpec) assert x.obj_type == int assert x.value == 1 assert not x.description assert not x.examples assert not x.deprecated x = parse(self.fd("""!spec\ntype: !!int "0"\ndescription:\nvalue: 1\ndeprecated: true""")) assert x.deprecated repr(x)
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
def test_parse_uri(self): x = parse(self.fd("""!url "smtp://host:25/" """)) assert isinstance(x, URL), type(x) assert str(x) == "smtp://host:25/" x = x.parse() assert x.hostname == "host" assert x.port == 25 assert x.scheme == "smtp"
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)
def sources_main(): usage = "usage: %prog [options] yaml [yaml ...]" parser = OptionParser(usage=usage) #parser.add_option("-L", "--output-layers", dest="layers", action="store_true", # default=False, # help="Display where values are sourced from.") (options, yamls) = parser.parse_args() if not yamls: parser.print_usage() exit(-1) descriptor = parse(open(yamls[0])) parsed = [parse(open(f)) for f in yamls[1:]] params = OrderedDict() for section in descriptor.keys(): params[section] = {} for key in descriptor[section]: for yam, yam_d in zip(yamls, [descriptor] + parsed): if key in yam_d.get(section, {}): params[section][key] = os.path.basename(yam) unparse(sys.stdout, dict(params), default_flow_style=False)
def test1(self): v = parse(self.open("spec.yaml")) assert isinstance(v, ConfigurationDescriptor) c = ConfigurationBuilder(v).build() f = ConfigParserFacade(c) def compare(method, section, key, type_): assert f.has_section(section) assert f.has_option(section, key) assert key in f.options(section) assert key in dict(f.items(section)) assert method(section, key) == c.value(section, key), (section, key) assert isinstance(method(section, key), type_) compare(f.get, "email", "from_address", basestring) compare(f.getint, "tests", "int", int) compare(f.getboolean, "tests", "bool", bool) compare(f.getfloat, "tests", "float", float) compare(f.getlist, "tests", "list", list) assert f.getlist('tests', 'list') == [1] assert v['tests']['list'].obj_type == list assert v['tests']['optlist'].obj_type == (list, type(None)) assert v['tests']['optlist'].value == None
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)
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']
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
def test_parse_optional(self): x = parse(self.fd("""!optional "abc" """)) assert isinstance(x, Optional) repr(x)
def test_parse_requirement(self): x = parse(self.fd("""!required "abc" """)) assert isinstance(x, Requirement) repr(x)
def test_parse_bad(self): with assert_raises(ScannerError): parse(self.open("bad.yaml"))
def test_parse_doc(self): v = parse(self.open("spec.yaml")) assert isinstance(v, ConfigurationDescriptor)