Esempio n. 1
0
    def test_40_loads_wo_type(self):
        a = dict(requires=["bash", "zsh"])
        a_s = "requires:bash,zsh"

        a1 = A.loads(a_s)

        self.assertEquals(a1["requires"],   a["requires"])
Esempio n. 2
0
    def test_42_loads_w_type_not_exist(self):
        a = dict(requires=["bash", "zsh"])
        a_s = "requires:bash,zsh"

        a1 = A.loads(a_s, "type_not_exist")

        self.assertEquals(a1["requires"], a["requires"])
Esempio n. 3
0
    def test_42_loads_w_type_not_exist(self):
        a_s = "requires:bash,zsh"

        a1 = TT.loads(a_s, "type_not_exist")
        # a = dict(requires=["bash", "zsh"])
        # self.assertEqual(a1["requires"], a["requires"])
        self.assertTrue(a1 is None)
Esempio n. 4
0
    def test_40_loads_wo_type(self):
        a = dict(requires=["bash", "zsh"])
        a_s = "requires:bash,zsh"

        a1 = A.loads(a_s)

        self.assertEquals(a1["requires"], a["requires"])
Esempio n. 5
0
    def test_40_loads_wo_type(self):
        a_s = "requires:bash,zsh"

        a1 = TT.loads(a_s)
        # a = dict(requires=["bash", "zsh"])
        # self.assertEqual(a1["requires"], a["requires"])
        self.assertTrue(a1 is None)
Esempio n. 6
0
    def test_42_loads_w_type_not_exist(self):
        a_s = "requires:bash,zsh"

        a1 = TT.loads(a_s, "type_not_exist")
        # a = dict(requires=["bash", "zsh"])
        # self.assertEqual(a1["requires"], a["requires"])
        self.assertTrue(a1 is None)
Esempio n. 7
0
def main(argv=sys.argv):
    p = option_parser()
    (options, args) = p.parse_args(argv[1:])

    llvl = logging.DEBUG if options.debug else logging.INFO
    logging.basicConfig(level=llvl)

    if not args:
        if options.list:
            sys.stdout.write(
                "Supported config types: " + ", ".join(A.list_types()) + "\n"
            )
            sys.exit(0)
        else:
            p.print_usage()
            sys.exit(-1)

    data = A.load(args, options.itype, options.merge)

    if options.args:
        diff = A.loads(options.args, options.atype)
        data.update(diff, options.merge)

    if options.output:
        cp = A.find_loader(options.output, options.otype)
        cp.dump(data, options.output)
    else:
        assert options.otype is not None, \
            "Please specify Output type w/ -O/--otype option"

        cp = A.find_loader(None, options.otype)
        sys.stdout.write(cp.dumps(data))
Esempio n. 8
0
    def test_42_loads_w_type_not_exist(self):
        a = dict(requires=["bash", "zsh"])
        a_s = "requires:bash,zsh"

        a1 = A.loads(a_s, "type_not_exist")

        self.assertEquals(a1["requires"],   a["requires"])
Esempio n. 9
0
    def test_40_loads_wo_type(self):
        a_s = "requires:bash,zsh"

        a1 = TT.loads(a_s)
        # a = dict(requires=["bash", "zsh"])
        # self.assertEqual(a1["requires"], a["requires"])
        self.assertTrue(a1 is None)
Esempio n. 10
0
    def test_30_dumps_and_loads(self):
        a = dict(name="a", a=1, b=dict(b=[1, 2], c="C"))
        a1 = A.loads(A.dumps(a, "json"), "json")

        self.assertEquals(a1["name"], a["name"])
        self.assertEquals(a1["a"], a["a"])
        self.assertEquals(a1["b"]["b"], a["b"]["b"])
        self.assertEquals(a1["b"]["c"], a["b"]["c"])
Esempio n. 11
0
    def test_30_dumps_and_loads__w_options(self):
        a = dict(name="a", a=1, b=dict(b=[1, 2], c="C"))
        a1 = A.loads(A.dumps(a, "json", indent=2), "json", ensure_ascii=False)

        self.assertEquals(a1["name"], a["name"])
        self.assertEquals(a1["a"], a["a"])
        self.assertEquals(a1["b"]["b"], a["b"]["b"])
        self.assertEquals(a1["b"]["c"], a["b"]["c"])
Esempio n. 12
0
    def test_32_dumps_and_loads__w_options__no_dumper(self):
        a = dict(name="a", a=1, b=dict(b=[1, 2], c="C"))
        a1 = A.loads(A.dumps(a, "type_not_exist"), "json")

        self.assertEquals(a1["name"], a["name"])
        self.assertEquals(a1["a"], a["a"])
        self.assertEquals(a1["b"]["b"], a["b"]["b"])
        self.assertEquals(a1["b"]["c"], a["b"]["c"])
Esempio n. 13
0
    def test_49_loads_w_validation_error(self):
        if not anyconfig.schema.JSONSCHEMA_IS_AVAIL:
            skip_test()

        cnf_s = """{"a": "aaa"}"""
        scm_s = TT.dumps(SCM_0, "json")
        cnf_2 = TT.loads(cnf_s, ac_parser="json", ac_schema=scm_s)
        self.assertTrue(cnf_2 is None, cnf_2)
Esempio n. 14
0
    def test_30_dumps_and_loads__w_options(self):
        a = dict(name="a", a=1, b=dict(b=[1, 2], c="C"))
        a1 = A.loads(A.dumps(a, "json", indent=2), "json", ensure_ascii=False)

        self.assertEquals(a1["name"],   a["name"])
        self.assertEquals(a1["a"],      a["a"])
        self.assertEquals(a1["b"]["b"], a["b"]["b"])
        self.assertEquals(a1["b"]["c"], a["b"]["c"])
Esempio n. 15
0
    def test_32_dumps_and_loads__w_options__no_dumper(self):
        a = dict(name="a", a=1, b=dict(b=[1, 2], c="C"))
        a1 = A.loads(A.dumps(a, "type_not_exist"), "json")

        self.assertEquals(a1["name"],   a["name"])
        self.assertEquals(a1["a"],      a["a"])
        self.assertEquals(a1["b"]["b"], a["b"]["b"])
        self.assertEquals(a1["b"]["c"], a["b"]["c"])
Esempio n. 16
0
    def test_30_dumps_and_loads(self):
        a = dict(name="a", a=1, b=dict(b=[1, 2], c="C"))
        a1 = A.loads(A.dumps(a, "json"), "json")

        self.assertEquals(a1["name"],   a["name"])
        self.assertEquals(a1["a"],      a["a"])
        self.assertEquals(a1["b"]["b"], a["b"]["b"])
        self.assertEquals(a1["b"]["c"], a["b"]["c"])
Esempio n. 17
0
    def test_46_loads_w_type__broken_template(self):
        if not anyconfig.template.SUPPORTED:
            return

        a = dict(requires="{% }}", )
        a_s = 'requires: "{% }}"'
        a1 = TT.loads(a_s, ac_parser="yaml", ac_template=True, ac_context={})

        self.assertEqual(a1["requires"], a["requires"])
Esempio n. 18
0
    def test_44_loads_w_type__template(self):
        if not anyconfig.template.SUPPORTED:
            return

        a_s = "requires: [{{ requires|join(', ') }}]"
        reqs = dict(requires=["bash", "zsh"])

        a1 = TT.loads(a_s, ac_parser="yaml", ac_template=True, ac_context=reqs)

        self.assertEqual(a1["requires"], reqs["requires"])
Esempio n. 19
0
    def test_48_loads_w_validation(self):
        cnf_s = TT.dumps(CNF_0, "json")
        scm_s = TT.dumps(SCM_0, "json")
        cnf_2 = TT.loads(cnf_s, ac_parser="json", ac_context={},
                         ac_schema=scm_s)

        self.assertEqual(cnf_2["name"], CNF_0["name"])
        self.assertEqual(cnf_2["a"], CNF_0["a"])
        self.assertEqual(cnf_2["b"]["b"], CNF_0["b"]["b"])
        self.assertEqual(cnf_2["b"]["c"], CNF_0["b"]["c"])
Esempio n. 20
0
    def test_46_loads_w_type__broken_template(self):
        if not AT.SUPPORTED:
            return

        a = dict(requires="{% }}", )
        a_s = 'requires: "{% }}"'
        a1 = TT.loads(a_s, forced_type="yaml", ac_template=True,
                      ac_context={})

        self.assertEquals(a1["requires"],   a["requires"])
Esempio n. 21
0
    def test_48_loads_w_validation(self):
        cnf_s = TT.dumps(CNF_0, "json")
        scm_s = TT.dumps(SCM_0, "json")
        cnf_2 = TT.loads(cnf_s, ac_parser="json", ac_context={},
                         ac_schema=scm_s)

        self.assertEqual(cnf_2["name"], CNF_0["name"])
        self.assertEqual(cnf_2["a"], CNF_0["a"])
        self.assertEqual(cnf_2["b"]["b"], CNF_0["b"]["b"])
        self.assertEqual(cnf_2["b"]["c"], CNF_0["b"]["c"])
Esempio n. 22
0
def main(argv=sys.argv):
    """
    :param argv: Argument list to parse [sys.argv]
    """
    parser = option_parser()
    (options, args) = parser.parse_args(argv[1:])

    A.set_loglevel(to_log_level(options.loglevel))

    if not args:
        if options.list:
            tlist = ", ".join(A.list_types()) + "\n"
            sys.stdout.write("Supported config types: " + tlist)
            sys.exit(0)
        else:
            parser.print_usage()
            sys.exit(-1)

    data = data = os.environ.copy() if options.env else A.container()
    diff = A.load(args, options.itype, ignore_missing=options.ignore_missing,
                  merge=options.merge, ac_template=options.template)
    data.update(diff)

    if options.args:
        diff = A.loads(options.args, options.atype,
                       ac_template=options.template, ac_context=data)
        data.update(diff, options.merge)

    if options.get:
        (data, err) = A.get(data, options.get)
        if err:
            raise RuntimeError(err)

    if options.set:
        A.set_(data, *(options.set.split('=')))

    if options.output:
        cparser = A.find_loader(options.output, options.otype)
        if cparser is None:
            raise RuntimeError("No suitable dumper was found for %s",
                               options.output)

        cparser.dump(data, options.output)
    else:
        # TODO: Reuse input type automatically detected as it's impossible to
        # detect output type w/o options.output.
        if options.otype is None:
            if options.itype is None:
                raise RuntimeError("Please specify input and/or output type "
                                   "with -I (--itype) or -O (--otype) option")
            else:
                options.otype = options.itype

        cparser = A.find_loader(None, options.otype)
        sys.stdout.write(cparser.dumps(data))
Esempio n. 23
0
    def test_44_loads_w_type__template(self):
        if not anyconfig.template.SUPPORTED:
            return

        a_s = "requires: [{{ requires|join(', ') }}]"
        reqs = dict(requires=["bash", "zsh"])

        a1 = TT.loads(a_s, ac_parser="yaml", ac_template=True,
                      ac_context=reqs)

        self.assertEqual(a1["requires"], reqs["requires"])
Esempio n. 24
0
    def test_44_loads_w_type__template(self):
        if not AT.SUPPORTED:
            return

        a = dict(requires=["bash", "zsh"])
        a_s = "requires: [{{ requires|join(', ') }}]"
        context = dict(requires=["bash", "zsh"], )

        a1 = TT.loads(a_s, forced_type="yaml", ac_template=True,
                      ac_context=context)

        self.assertEquals(a1["requires"],   a["requires"])
Esempio n. 25
0
def main(argv=sys.argv):
    """
    :param argv: Argument list to parse [sys.argv]
    """
    parser = option_parser()
    (options, args) = parser.parse_args(argv[1:])

    A.set_loglevel(to_log_level(options.loglevel))

    if not args:
        if options.list:
            tlist = ", ".join(A.list_types()) + "\n"
            sys.stdout.write("Supported config types: " + tlist)
            sys.exit(0)
        else:
            parser.print_usage()
            sys.exit(-1)

    data = A.load(args,
                  options.itype,
                  options.merge,
                  ignore_missing=options.ignore_missing)

    if options.args:
        diff = A.loads(options.args, options.atype)
        data.update(diff, options.merge)

    if options.get:
        (data, err) = A.get(data, options.get)
        if err:
            raise RuntimeError(err)

    if options.set:
        A.set_(data, *(options.set.split('=')))

    if options.output:
        cparser = A.find_loader(options.output, options.otype)
        cparser.dump(data, options.output)
    else:
        # TODO: Reuse input type automatically detected as it's impossible to
        # detect output type w/o options.output.
        if options.otype is None:
            if options.itype is None:
                raise RuntimeError("Please specify input and/or output type "
                                   "with -I (--itype) or -O (--otype) option")
            else:
                options.otype = options.itype

        cparser = A.find_loader(None, options.otype)
        sys.stdout.write(cparser.dumps(data))
Esempio n. 26
0
def main(argv=None):
    """
    :param argv: Argument list to parse or None (sys.argv will be set).
    """
    (parser, options, args) = parse_args(argv=argv)
    API.LOGGER.setLevel(to_log_level(options.loglevel))

    _check_options_and_args(parser, options, args)

    cnf = API.container(os.environ.copy() if options.env else {})
    diff = API.load(args, options.itype,
                    ignore_missing=options.ignore_missing,
                    merge=options.merge, ac_template=options.template,
                    ac_schema=options.schema)

    _exit_if_load_failure(diff, "Failed to load: args=%s" % ", ".join(args))
    cnf.update(diff)

    if options.args:
        diff = API.loads(options.args, options.atype,
                         ac_template=options.template, ac_context=cnf)
        cnf.update(diff, options.merge)

    _exit_if_only_to_validate(options.validate)

    if options.gen_schema:
        cnf = API.gen_schema(cnf)

    if options.get:
        cnf = _do_get(cnf, options.get)

    if options.set:
        (key, val) = options.set.split('=')
        API.set_(cnf, key, anyconfig.parser.parse(val))

    _output_result(cnf, options.output, options.otype, args[0], options.itype)
Esempio n. 27
0
 def test_30_dumps_and_loads(self):
     cnf = TT.loads(TT.dumps(self.cnf, "json"), "json")
     self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
Esempio n. 28
0
 def test_30_dumps_and_loads__w_options(self):
     cnf = TT.loads(TT.dumps(self.cnf, "json", indent=2), "json",
                    ensure_ascii=False)
     self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
Esempio n. 29
0
 def test_32_dumps_and_loads__w_options__no_dumper(self):
     cnf = TT.loads(TT.dumps(self.cnf, "type_not_exist"), "json")
     self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
Esempio n. 30
0
 def test_40_loads_wo_type(self):
     cnf_s = "requires:bash,zsh"
     self.assertTrue(TT.loads(cnf_s) is None)
Esempio n. 31
0
 def test_30_dumps_and_loads__w_options(self):
     res = TT.loads(TT.dumps(self.cnf, "json", indent=2), "json",
                    ensure_ascii=False)
     self.assert_dicts_equal(res, self.cnf)
Esempio n. 32
0
 def test_30_dumps_and_loads(self):
     res = TT.loads(TT.dumps(self.cnf, "json"), "json")
     self.assert_dicts_equal(res, self.cnf)
Esempio n. 33
0
 def test_49_loads_w_validation_error(self):
     cnf_s = """{"a": "aaa"}"""
     scm_s = TT.dumps(SCM_0, "json")
     cnf_2 = TT.loads(cnf_s, ac_parser="json", ac_schema=scm_s)
     self.assertTrue(cnf_2 is None, cnf_2)
Esempio n. 34
0
 def test_49_loads_w_validation_error(self):
     cnf_s = """{"a": "aaa"}"""
     scm_s = TT.dumps(SCM_0, "json")
     cnf_2 = TT.loads(cnf_s, ac_parser="json", ac_schema=scm_s)
     self.assertTrue(cnf_2 is None, cnf_2)
Esempio n. 35
0
 def test_40_loads_wo_type(self):
     cnf_s = "requires:bash,zsh"
     self.assertTrue(TT.loads(cnf_s) is None)
Esempio n. 36
0
 def test_30_dumps_and_loads(self):
     cnf = TT.loads(TT.dumps(self.cnf, "json"), "json")
     self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
Esempio n. 37
0
 def test_30_dumps_and_loads__w_options(self):
     res = TT.loads(TT.dumps(self.cnf, "json", indent=2), "json",
                    ensure_ascii=False)
     self.assert_dicts_equal(res, self.cnf)
Esempio n. 38
0
 def test_30_dumps_and_loads(self):
     res = TT.loads(TT.dumps(self.cnf, "json"), "json")
     self.assert_dicts_equal(res, self.cnf)
Esempio n. 39
0
 def test_30_dumps_and_loads__w_options(self):
     cnf = TT.loads(TT.dumps(self.cnf, "json", indent=2),
                    "json",
                    ensure_ascii=False)
     self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
Esempio n. 40
0
 def test_32_dumps_and_loads__w_options__no_dumper(self):
     cnf = TT.loads(TT.dumps(self.cnf, "type_not_exist"), "json")
     self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
Esempio n. 41
0
def main(argv=None):
    """
    :param argv: Argument list to parse or None (sys.argv will be set).
    """
    if argv is None:
        argv = sys.argv

    parser = option_parser()
    (options, args) = parser.parse_args(argv[1:])

    A.set_loglevel(to_log_level(options.loglevel))

    if not args:
        if options.list:
            tlist = ", ".join(A.list_types()) + "\n"
            sys.stdout.write("Supported config types: " + tlist)
            sys.exit(0)
        else:
            parser.print_usage()
            sys.exit(1)

    if options.validate and options.schema is None:
        sys.stderr.write("--validate option requires --scheme option")
        sys.exit(1)

    data = data = os.environ.copy() if options.env else A.container()
    diff = A.load(args, options.itype,
                  ignore_missing=options.ignore_missing,
                  merge=options.merge, ac_template=options.template,
                  ac_schema=options.schema)

    if diff is None:
        sys.stderr.write("Validation failed")
        sys.exit(1)

    data.update(diff)

    if options.args:
        diff = A.loads(options.args, options.atype,
                       ac_template=options.template, ac_context=data)
        data.update(diff, options.merge)

    if options.validate:
        A.LOGGER.info("Validation succeeds")
        sys.exit(0)

    if options.gen_schema:
        data = A.gen_schema(data)

    if options.get:
        (data, err) = A.get(data, options.get)
        if err:
            raise RuntimeError(err)

        # Output primitive types as it is.
        if not anyconfig.mergeabledict.is_mergeabledict_or_dict(data):
            sys.stdout.write(str(data) + '\n')
            return

    if options.set:
        (key, val) = options.set.split('=')
        A.set_(data, key, anyconfig.parser.parse(val))

    if options.output:
        cparser = A.find_loader(options.output, options.otype)
        if cparser is None:
            raise RuntimeError("No suitable dumper was found for %s",
                               options.output)

        cparser.dump(data, options.output)
    else:
        if options.otype is None:
            if options.itype is None:
                psr = A.find_loader(args[0])
                if psr is not None:
                    options.otype = psr.type()  # Reuse detected input type
                else:
                    raise RuntimeError("Please specify input and/or output "
                                       "type with -I (--itype) or -O "
                                       "(--otype) option")
            else:
                options.otype = options.itype

        cparser = A.find_loader(None, options.otype)
        sys.stdout.write(cparser.dumps(data) + '\n')