def test_10_dump_and_multi_load__default_merge_strategy(self): a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) a_path = os.path.join(self.workdir, "a.json") b_path = os.path.join(self.workdir, "b.json") g_path = os.path.join(self.workdir, "*.json") TT.dump(a, a_path) TT.dump(b, b_path) a0 = TT.multi_load(g_path) a02 = TT.multi_load([g_path, b_path]) self.assertEqual(a0["name"], a["name"]) self.assertEqual(a0["a"], b["a"]) self.assertEqual(a0["b"]["b"], b["b"]["b"]) self.assertEqual(a0["b"]["c"], a["b"]["c"]) self.assertEqual(a0["b"]["d"], b["b"]["d"]) self.assertEqual(a02["name"], a["name"]) self.assertEqual(a02["a"], b["a"]) self.assertEqual(a02["b"]["b"], b["b"]["b"]) self.assertEqual(a02["b"]["c"], a["b"]["c"]) self.assertEqual(a02["b"]["d"], b["b"]["d"]) a1 = TT.multi_load([a_path, b_path], ac_merge=TT.MS_DICTS) self.assertEqual(a1["name"], a["name"]) self.assertEqual(a1["a"], b["a"]) self.assertEqual(a1["b"]["b"], b["b"]["b"]) self.assertEqual(a1["b"]["c"], a["b"]["c"]) self.assertEqual(a1["b"]["d"], b["b"]["d"])
def test_60_multi_load__w_ac_dict_option(self): TT.dump(self.dic, self.a_path) TT.dump(self.upd, self.b_path) res = TT.multi_load(self.g_path, ac_dict=MyODict) self.assert_dicts_equal(res, self.exp) self.assertTrue(isinstance(res, MyODict))
def test_30_dump_and_load(self): a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) a_path = os.path.join(self.workdir, "a.json") b_path = os.path.join(self.workdir, "b.json") A.dump(a, a_path) self.assertTrue(os.path.exists(a_path)) A.dump(b, b_path) self.assertTrue(os.path.exists(b_path)) a1 = A.load(a_path) 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"]) a2 = A.load(os.path.join(self.workdir, '*.json')) self.assertEquals(a2["name"], a["name"]) self.assertEquals(a2["a"], b["a"]) self.assertEquals(a2["b"]["b"], [1, 2, 3, 4, 5]) self.assertEquals(a2["b"]["c"], a["b"]["c"]) self.assertEquals(a2["b"]["d"], b["b"]["d"]) a3 = A.load([a_path, b_path]) self.assertEquals(a3["name"], a["name"]) self.assertEquals(a3["a"], b["a"]) self.assertEquals(a3["b"]["b"], [1, 2, 3, 4, 5]) self.assertEquals(a3["b"]["c"], a["b"]["c"]) self.assertEquals(a3["b"]["d"], b["b"]["d"])
def test_32_dump_and_load__w_options(self): a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) a_path = os.path.join(self.workdir, "a.json") b_path = os.path.join(self.workdir, "b.json") TT.dump(a, a_path, indent=2) self.assertTrue(os.path.exists(a_path)) TT.dump(b, b_path, indent=2) self.assertTrue(os.path.exists(b_path)) a1 = TT.load(a_path, parse_int=int) 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"]) a2 = TT.load(os.path.join(self.workdir, '*.json'), parse_int=int) self.assertEquals(a2["name"], a["name"]) self.assertEquals(a2["a"], b["a"]) self.assertEquals(a2["b"]["b"], [1, 2, 3, 4, 5]) self.assertEquals(a2["b"]["c"], a["b"]["c"]) self.assertEquals(a2["b"]["d"], b["b"]["d"]) a3 = TT.load([a_path, b_path], parse_int=int) self.assertEquals(a3["name"], a["name"]) self.assertEquals(a3["a"], b["a"]) self.assertEquals(a3["b"]["b"], [1, 2, 3, 4, 5]) self.assertEquals(a3["b"]["c"], a["b"]["c"]) self.assertEquals(a3["b"]["d"], b["b"]["d"])
def test_22_dump_and_single_load__w_ac_dict_option(self): TT.dump(self.cnf, self.a_path) self.assertTrue(os.path.exists(self.a_path)) res = TT.single_load(self.a_path, ac_dict=MyODict) self.assert_dicts_equal(res, self.cnf, ordered=True) self.assertTrue(isinstance(res, MyODict))
def test_32_dump_and_load__w_options(self): a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) a_path = os.path.join(self.workdir, "a.json") b_path = os.path.join(self.workdir, "b.json") A.dump(a, a_path, indent=2) self.assertTrue(os.path.exists(a_path)) A.dump(b, b_path, indent=2) self.assertTrue(os.path.exists(b_path)) a1 = A.load(a_path, parse_int=int) 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"]) a2 = A.load(os.path.join(self.workdir, '*.json'), parse_int=int) self.assertEquals(a2["name"], a["name"]) self.assertEquals(a2["a"], b["a"]) self.assertEquals(a2["b"]["b"], [1, 2, 3, 4, 5]) self.assertEquals(a2["b"]["c"], a["b"]["c"]) self.assertEquals(a2["b"]["d"], b["b"]["d"]) a3 = A.load([a_path, b_path], parse_int=int) self.assertEquals(a3["name"], a["name"]) self.assertEquals(a3["a"], b["a"]) self.assertEquals(a3["b"]["b"], [1, 2, 3, 4, 5]) self.assertEquals(a3["b"]["c"], a["b"]["c"]) self.assertEquals(a3["b"]["d"], b["b"]["d"])
def _output_result(cnf, outpath, otype, inpath, itype): """ :param cnf: Configuration object to print out :param outpath: Output file path or None :param otype: Output type or None :param inpath: Input file path :param itype: Input type or None """ if not outpath or outpath == "-": outpath = sys.stdout if otype is None: if itype is None: try: otype = API.find_loader(inpath).type() except AttributeError: _exit_with_output("Specify inpath and/or outpath type[s] " "with -I/--itype or -O/--otype option " "explicitly", 1) else: otype = itype if anyconfig.mdicts.is_dict_like(cnf): API.dump(cnf, outpath, otype) else: _exit_with_output(str(cnf)) # Output primitive types as it is.
def test_30_dump_and_load(self): a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) a_path = os.path.join(self.workdir, "a.json") b_path = os.path.join(self.workdir, "b.json") TT.dump(a, a_path) self.assertTrue(os.path.exists(a_path)) TT.dump(b, b_path) self.assertTrue(os.path.exists(b_path)) a1 = TT.load(a_path) self.assertEqual(a1["name"], a["name"]) self.assertEqual(a1["a"], a["a"]) self.assertEqual(a1["b"]["b"], a["b"]["b"]) self.assertEqual(a1["b"]["c"], a["b"]["c"]) a2 = TT.load(os.path.join(self.workdir, '*.json')) self.assertEqual(a2["name"], a["name"]) self.assertEqual(a2["a"], b["a"]) self.assertEqual(a2["b"]["b"], [1, 2, 3, 4, 5]) self.assertEqual(a2["b"]["c"], a["b"]["c"]) self.assertEqual(a2["b"]["d"], b["b"]["d"]) a3 = TT.load([a_path, b_path]) self.assertEqual(a3["name"], a["name"]) self.assertEqual(a3["a"], b["a"]) self.assertEqual(a3["b"]["b"], [1, 2, 3, 4, 5]) self.assertEqual(a3["b"]["c"], a["b"]["c"]) self.assertEqual(a3["b"]["d"], b["b"]["d"])
def test_22_dump_and_single_load__w_ac_dict_option(self): TT.dump(self.cnf, self.a_path) self.assertTrue(self.a_path.exists()) res = TT.single_load(self.a_path, ac_dict=MyODict) self.assert_dicts_equal(res, self.cnf, ordered=True) self.assertTrue(isinstance(res, MyODict))
def test_70_multi_inputs__w_template(self): if not anyconfig.template.SUPPORTED: return a = dict(name="a", a=1, b=dict(b=[1, 2], c="C")) inputsdir = os.path.join(self.workdir, "in") os.makedirs(inputsdir) A.dump(a, os.path.join(inputsdir, "a0.yml")) open(os.path.join(inputsdir, "a1.yml"), "w").write( """\ name: {{ name }} a: {{ a }} b: b: {% for x in b.b -%} - {{ x }} {% endfor %} c: {{ b.c }} """ ) output = os.path.join(self.workdir, "b.json") TT.main(["dummy", "--template", "-o", output, os.path.join(inputsdir, "*.yml")]) self.assertTrue(os.path.exists(output))
def _output_result(cnf, outpath, otype, inpath, itype): """ :param cnf: Configuration object to print out :param outpath: Output file path or None :param otype: Output type or None :param inpath: Input file path :param itype: Input type or None """ if not outpath or outpath == "-": outpath = sys.stdout if otype is None: if itype is None: try: otype = API.find_loader(inpath).type() except AttributeError: _exit_with_output( "Specify inpath and/or outpath type[s] " "with -I/--itype or -O/--otype option " "explicitly", 1) else: otype = itype if anyconfig.mdicts.is_dict_like(cnf): API.dump(cnf, outpath, otype) else: _exit_with_output(str(cnf)) # Output primitive types as it is.
def test_10_dump_and_single_load(self): cpath = os.path.join(self.workdir, "a.json") TT.dump(self.cnf, cpath) self.assertTrue(os.path.exists(cpath)) cnf1 = TT.single_load(cpath) self.assertTrue(dicts_equal(self.cnf, cnf1), str(cnf1))
def test_11_dump_and_single_load__to_from_stream(self): cpath = os.path.join(self.workdir, "a.json") TT.dump(self.cnf, open(cpath, 'w')) self.assertTrue(os.path.exists(cpath)) cnf1 = TT.single_load(open(cpath)) self.assertTrue(dicts_equal(self.cnf, cnf1), str(cnf1))
def test_60_wrong_merge_strategy(self): cpath = self.workdir / "a.json" TT.dump(dict(a=1, b=2), cpath) try: TT.multi_load([cpath, cpath], ac_merge="merge_st_not_exist") raise RuntimeError("Wrong merge strategy was not handled!") except ValueError: self.assertTrue(bool(1)) # To suppress warn of pylint.
def test_60_wrong_merge_strategy(self): cpath = os.path.join(self.workdir, "a.json") TT.dump(dict(a=1, b=2), cpath) try: TT.multi_load([cpath, cpath], ac_merge="merge_st_not_exist") raise RuntimeError("Wrong merge strategy was not handled!") except ValueError: self.assertTrue(bool(1)) # To suppress warn of pylint.
def test_20_dump_and_single_load__w_ordered_option(self): TT.dump(self.cnf, self.a_path) self.assertTrue(os.path.exists(self.a_path)) # It works w/ JSON backend but some backend cannot keep the order of # items and the tests might fail. res = TT.single_load(self.a_path, ac_ordered=True) self.assert_dicts_equal(res, self.cnf, ordered=True) self.assertTrue(isinstance(res, anyconfig.compat.OrderedDict))
def test_80_no_out_dumper(self): a = dict(name="a", a=1, b=dict(b=[1, 2], c="C"), d=[1, 2]) infile = os.path.join(self.workdir, "a.json") A.dump(a, infile) exited = False outfile = os.path.join(self.workdir, "out.conf") _run("-o", outfile, infile) self.assertFalse(exited)
def test_38_single_input__gen_schema_and_validate_with_it(self): cnf = dict(name="a", a=1, b=dict(b=[1, 2], c="C")) infile = os.path.join(self.workdir, "cnf.json") output = os.path.join(self.workdir, "out.yaml") A.dump(cnf, infile) self.run_and_check_exit_code(["--gen-schema", "-o", output, infile], 0) self.assertTrue(os.path.exists(output)) self.run_and_check_exit_code(["--schema", output, "--validate", infile], 0)
def test_31_dump_and_load__to_from_stream(self): with TT.open(self.a_path, mode='w') as strm: TT.dump(self.dic, strm) self.assertTrue(os.path.exists(self.a_path)) with TT.open(self.a_path) as strm: res = TT.load(strm, ac_parser="json") self.assert_dicts_equal(res, self.dic)
def test_20_dump_and_single_load__w_ordered_option(self): TT.dump(self.cnf, self.a_path) self.assertTrue(self.a_path.exists()) # It works w/ JSON backend but some backend cannot keep the order of # items and the tests might fail. res = TT.single_load(self.a_path, ac_ordered=True) self.assert_dicts_equal(res, self.cnf, ordered=True) self.assertTrue(isinstance(res, collections.OrderedDict))
def test_31_dump_and_load__to_from_stream(self): with TT.open(self.a_path, mode='w') as strm: TT.dump(self.dic, strm) self.assertTrue(self.a_path.exists()) with TT.open(self.a_path) as strm: res = TT.load(strm, ac_parser="json") self.assert_dicts_equal(res, self.dic)
def test_80_no_out_dumper(self): a = dict(name="a", a=1, b=dict(b=[1, 2], c="C"), d=[1, 2]) input = os.path.join(self.workdir, "a.json") A.dump(a, input) try: TT.main(["dummy", "-o", "out.txt", input]) sys.exit(-1) except RuntimeError: pass
def test_39_single_load__w_validation(self): (cnf, scm) = (CNF_0, SCM_0) cpath = os.path.join(self.workdir, "cnf.json") spath = os.path.join(self.workdir, "scm.json") TT.dump(cnf, cpath) TT.dump(scm, spath) cnf1 = TT.single_load(cpath, ac_schema=spath) self.assert_dicts_equal(cnf, cnf1)
def test_39_single_load__w_validation(self): (cnf, scm) = (CNF_0, SCM_0) cpath = self.workdir / "cnf.json" spath = self.workdir / "scm.json" TT.dump(cnf, cpath) TT.dump(scm, spath) cnf1 = TT.single_load(cpath, ac_schema=spath) self.assert_dicts_equal(cnf, cnf1)
def test_39_single_load__w_validation(self): (cnf, scm) = (CNF_0, SCM_0) cpath = os.path.join(self.workdir, "cnf.json") spath = os.path.join(self.workdir, "scm.json") TT.dump(cnf, cpath) TT.dump(scm, spath) cnf1 = TT.single_load(cpath, ac_schema=spath) self.assertTrue(dicts_equal(cnf, cnf1), str(cnf1))
def test_37_single_input_w_schema(self): (infile, scmfile) = (CNF_0_PATH, SCM_0_PATH) output = os.path.join(self.workdir, "output.json") self.run_and_check_exit_code(["--schema", scmfile, "--validate", infile], 0) self.run_and_check_exit_code(["--schema", scmfile, "-o", output, infile], 0) infile2 = os.path.join(self.workdir, "input.yml") cnf = CNF_0.copy() cnf["a"] = "aaa" # Validation should fail. A.dump(cnf, infile2) self.run_and_check_exit_code(["--schema", scmfile, "--validate", infile2], 1)
def test_13_dump_and_single_load__namedtuple(self): if not IS_PYTHON_3: # TODO: it does not work with python3. cpath = os.path.join(self.workdir, "a.json") cnf = OrderedDict(sorted(self.cnf.items())) cnf0 = convert_to(cnf, ac_namedtuple=True) TT.dump(cnf0, cpath) self.assertTrue(os.path.exists(cpath)) cnf1 = TT.single_load(cpath, ac_namedtuple=True) self.assertTrue(cnf0 == cnf1, "\n%r ->\n%r" % (cnf0, cnf1))
def test_72_single_input__no_template(self): a = dict(name="a", a=1, b=dict(b=[1, 2], c="C")) infile = os.path.join(self.workdir, "a.json") output = os.path.join(self.workdir, "b.json") A.dump(a, infile) self.assertTrue(os.path.exists(infile)) TT.main(["dummy", "-o", output, infile]) self.assertTrue(os.path.exists(output))
def test_30_single_input(self): a = dict(name="a", a=1, b=dict(b=[1, 2], c="C")) input = os.path.join(self.workdir, "a.json") output = os.path.join(self.workdir, "b.json") A.dump(a, input) self.assertTrue(os.path.exists(input)) T.main(["dummy", "-o", output, input]) self.assertTrue(os.path.exists(output))
def test_36_load_w_validation(self): cnf_path = os.path.join(self.workdir, "cnf.json") scm_path = os.path.join(self.workdir, "scm.json") TT.dump(CNF_0, cnf_path) TT.dump(SCM_0, scm_path) cnf_2 = TT.load(cnf_path, ac_context={}, ac_schema=scm_path) 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"])
def test_10_single_load_w_validation(self): cnf_path = self.workdir / "cnf.json" scm_path = self.workdir / "scm.json" TT.dump(CNF_0, cnf_path) TT.dump(SCM_0, scm_path) cnf_2 = TT.single_load(cnf_path, ac_context={}, ac_schema=scm_path) 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"])
def test_38_load_w_validation_yaml(self): cnf_path = os.path.join(self.workdir, "cnf.yml") scm_path = os.path.join(self.workdir, "scm.yml") TT.dump(CNF_0, cnf_path) TT.dump(SCM_0, scm_path) cnf_2 = TT.load(cnf_path, ac_context={}, ac_schema=scm_path) 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"])
def test_38_load_w_validation_yaml(self): cnf_path = self.workdir / "cnf.yml" scm_path = self.workdir / "scm.yml" TT.dump(CNF_0, cnf_path) TT.dump(SCM_0, scm_path) cnf_2 = TT.load(cnf_path, ac_context={}, ac_schema=scm_path) 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"])
def test_20_dump_and_multi_load(self): a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) a_path = os.path.join(self.workdir, "a.json") b_path = os.path.join(self.workdir, "b.json") A.dump(a, a_path) self.assertTrue(os.path.exists(a_path)) A.dump(b, b_path) self.assertTrue(os.path.exists(b_path)) a1 = A.multi_load([a_path, b_path], merge=A.MS_DICTS) self.assertEquals(a1["name"], a["name"]) self.assertEquals(a1["a"], b["a"]) self.assertEquals(a1["b"]["b"], b["b"]["b"]) self.assertEquals(a1["b"]["c"], a["b"]["c"]) self.assertEquals(a1["b"]["d"], b["b"]["d"]) a2 = A.multi_load([a_path, b_path], merge=A.MS_DICTS_AND_LISTS) self.assertEquals(a2["name"], a["name"]) self.assertEquals(a2["a"], b["a"]) self.assertEquals(a2["b"]["b"], [0, 1, 2, 3, 4, 5]) self.assertEquals(a2["b"]["c"], a["b"]["c"]) self.assertEquals(a2["b"]["d"], b["b"]["d"]) a3 = A.multi_load(os.path.join(self.workdir, "*.json")) self.assertEquals(a1["name"], a["name"]) self.assertEquals(a1["a"], b["a"]) self.assertEquals(a1["b"]["b"], b["b"]["b"]) self.assertEquals(a1["b"]["c"], a["b"]["c"]) self.assertEquals(a1["b"]["d"], b["b"]["d"]) a4 = A.multi_load([a_path, b_path], merge=A.MS_REPLACE) self.assertEquals(a4["name"], a["name"]) self.assertEquals(a4["a"], b["a"]) self.assertEquals(a4["b"]["b"], b["b"]["b"]) self.assertFalse("c" in a4["b"]) self.assertEquals(a4["b"]["d"], b["b"]["d"]) a5 = A.multi_load([a_path, b_path], merge=A.MS_NO_REPLACE) self.assertEquals(a5["name"], a["name"]) self.assertEquals(a5["a"], a["a"]) self.assertEquals(a5["b"]["b"], a["b"]["b"]) self.assertEquals(a5["b"]["c"], a["b"]["c"]) self.assertFalse("d" in a5["b"])
def _try_dump(cnf, outpath, otype, fmsg): """ :param cnf: Configuration object to print out :param outpath: Output file path or None :param otype: Output type or None :param fmsg: message if it cannot detect otype by `inpath` """ try: API.dump(cnf, outpath, otype) except API.UnknownFileTypeError: _exit_with_output(fmsg % outpath, 1) except API.UnknownProcessorTypeError: _exit_with_output("Invalid output type '%s'" % otype, 1)
def test_31_dump_and_load__to_from_stream(self): cnf = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") cpath = os.path.join(self.workdir, "a.json") with open(cpath, 'w') as strm: TT.dump(cnf, strm) self.assertTrue(os.path.exists(cpath)) with open(cpath, 'r') as strm: cnf1 = TT.load(strm, ac_parser="json") self.assertTrue(dicts_equal(cnf, cnf1), "cnf vs. cnf1: %s\n\n%s" % (str(cnf), str(cnf1)))
def test_20_dump_and_multi_load__mixed_file_types(self): c_path = self.workdir / "c.yml" TT.dump(self.dic, self.a_path) # JSON try: TT.dump(self.upd, c_path) # YAML except (TT.UnknownProcessorTypeError, TT.UnknownFileTypeError): return # YAML backend is not available in this env. self.assertTrue(self.a_path.exists()) self.assertTrue(c_path.exists()) res = TT.multi_load([self.a_path, c_path]) self.assert_dicts_equal(res, self.exp)
def test_32_single_input_w_get_option(self): d = dict(name="a", a=dict(b=dict(c=[1, 2], d="C"))) input = os.path.join(self.workdir, "a.json") output = os.path.join(self.workdir, "b.json") A.dump(d, input) self.assertTrue(os.path.exists(input)) T.main(["dummy", "-o", output, "--get", "a.b", input]) self.assertTrue(os.path.exists(output)) x = A.load(output) self.assertEquals(x, d['a']['b'])
def test_10_dump_and_single_load(self): a = dict(name="a", a=1, b=dict(b=[1, 2], c="C")) a_path = os.path.join(self.workdir, "a.json") A.dump(a, a_path) self.assertTrue(os.path.exists(a_path)) a1 = A.single_load(a_path) 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"])
def test_20_dump_and_multi_load__mixed_file_types(self): c_path = os.path.join(self.workdir, "c.yml") TT.dump(self.dic, self.a_path) # JSON try: TT.dump(self.upd, c_path) # YAML except (TT.UnknownProcessorTypeError, TT.UnknownFileTypeError): return # YAML backend is not available in this env. self.assertTrue(os.path.exists(self.a_path)) self.assertTrue(os.path.exists(c_path)) res = TT.multi_load([self.a_path, c_path]) self.assert_dicts_equal(res, self.exp)
def test_38_load_w_validation_yaml(self): if "yml" not in TT.list_types(): skip_test() cnf_path = os.path.join(self.workdir, "cnf.yml") scm_path = os.path.join(self.workdir, "scm.yml") TT.dump(CNF_0, cnf_path) TT.dump(SCM_0, scm_path) cnf_2 = TT.load(cnf_path, ac_context={}, ac_schema=scm_path) 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"])
def _check_multi_load_with_strategy(self, exp, merge=TT.MS_DICTS): TT.dump(self.dic, self.a_path) TT.dump(self.upd, self.b_path) self.assertTrue(os.path.exists(self.a_path)) self.assertTrue(os.path.exists(self.b_path)) res0 = TT.multi_load(self.g_path, ac_merge=merge) res1 = TT.multi_load([self.g_path, self.b_path], ac_merge=merge) self.assertTrue(res0) self.assertTrue(res1) self.assert_dicts_equal(res0, exp) self.assert_dicts_equal(res1, exp)
def test_40_load_w_query(self): cnf_path = self.workdir / "cnf.json" TT.dump(CNF_0, cnf_path) try: if TT.query.jmespath: self.assertEqual(TT.load(cnf_path, ac_query="a"), 1) self.assertEqual(TT.load(cnf_path, ac_query="b.b"), [1, 2]) self.assertEqual(TT.load(cnf_path, ac_query="b.b[1]"), 2) self.assertEqual(TT.load(cnf_path, ac_query="b.b[1:]"), [2]) self.assertEqual(TT.load(cnf_path, ac_query="b.b[::-1]"), [2, 1]) self.assertEqual(TT.load(cnf_path, ac_query="length(b.b)"), 2) except (NameError, AttributeError): pass # jmespath is not available.
def test_30_dump_and_load(self): TT.dump(self.dic, self.a_path) TT.dump(self.upd, self.b_path) self.assertTrue(os.path.exists(self.a_path)) self.assertTrue(os.path.exists(self.b_path)) res = TT.load(self.a_path) self.assert_dicts_equal(res, self.dic) res = TT.load(self.g_path) self.assert_dicts_equal(res, self.exp) res = TT.load([self.a_path, self.b_path]) self.assert_dicts_equal(res, self.exp)
def test_30_dump_and_load(self): TT.dump(self.dic, self.a_path) TT.dump(self.upd, self.b_path) self.assertTrue(self.a_path.exists()) self.assertTrue(self.b_path.exists()) res = TT.load(self.a_path) self.assert_dicts_equal(res, self.dic) res = TT.load(self.g_path) self.assert_dicts_equal(res, self.exp) res = TT.load([self.a_path, self.b_path]) self.assert_dicts_equal(res, self.exp)
def _load_and_dump_with_opened_files(self, filename, rmode='r', wmode='w', **oopts): cpath = os.path.join(self.workdir, filename) with TT.open(cpath, 'w', **oopts) as out: TT.dump(self.cnf, out) self.assertTrue(_is_file_object(out)) self.assertEqual(out.mode, wmode) with TT.open(cpath, 'rb', **oopts) as inp: cnf1 = TT.single_load(inp) self.assertTrue(_is_file_object(inp)) self.assertEqual(inp.mode, rmode) cpair = (self.cnf, cnf1) self.assertTrue(dicts_equal(*cpair), "%r vs. %r" % cpair)
def test_40_load_w_query(self): cnf_path = os.path.join(self.workdir, "cnf.json") TT.dump(CNF_0, cnf_path) try: if TT.query.jmespath: self.assertEqual(TT.load(cnf_path, ac_query="a"), 1) self.assertEqual(TT.load(cnf_path, ac_query="b.b"), [1, 2]) self.assertEqual(TT.load(cnf_path, ac_query="b.b[1]"), 2) self.assertEqual(TT.load(cnf_path, ac_query="b.b[1:]"), [2]) self.assertEqual(TT.load(cnf_path, ac_query="b.b[::-1]"), [2, 1]) self.assertEqual(TT.load(cnf_path, ac_query="length(b.b)"), 2) except (NameError, AttributeError): pass # jmespath is not available.
def _check_multi_load_with_strategy(self, exp, merge=TT.MS_DICTS): TT.dump(self.dic, self.a_path) TT.dump(self.upd, self.b_path) self.assertTrue(self.a_path.exists()) self.assertTrue(self.b_path.exists()) res0 = TT.multi_load(self.g_path, ac_merge=merge) res1 = TT.multi_load([self.g_path, self.b_path], ac_merge=merge) self.assertTrue(res0) self.assertTrue(res1) self.assert_dicts_equal(res0, exp) self.assert_dicts_equal(res1, exp)
def _load_and_dump_with_opened_files(self, filename, rmode='r', wmode='w', **oopts): cpath = self.workdir / filename with TT.open(cpath, 'w', **oopts) as out: TT.dump(self.cnf, out) self.assertTrue(_is_file_object(out)) self.assertEqual(out.mode, wmode) with TT.open(cpath, 'rb', **oopts) as inp: cnf1 = TT.single_load(inp) self.assertTrue(_is_file_object(inp)) self.assertEqual(inp.mode, rmode) cpair = (self.cnf, cnf1) self.assertEqual(*cpair)
def test_16_single_load__template(self): if not anyconfig.template.SUPPORTED: return cpath = os.path.join(self.workdir, "a.yaml") TT.open(cpath, mode='w').write(CNF_TMPL_0) cnf = TT.single_load(cpath, ac_template=True, ac_context=self.cnf) self.assert_dicts_equal(cnf, self.cnf) spath = os.path.join(self.workdir, "scm.json") TT.dump(dict(type="integer"), spath) # Validation should fail. cnf2 = TT.single_load(cpath, ac_template=True, ac_context=self.cnf, ac_schema=spath) self.assertTrue(cnf2 is None)
def _try_dump(cnf, outpath, otype, fmsg, extra_opts=None): """ :param cnf: Configuration object to print out :param outpath: Output file path or None :param otype: Output type or None :param fmsg: message if it cannot detect otype by 'inpath' :param extra_opts: Map object will be given to API.dump as extra options """ if extra_opts is None: extra_opts = {} try: API.dump(cnf, outpath, otype, **extra_opts) except API.UnknownFileTypeError: _exit_with_output(fmsg % outpath, 1) except API.UnknownProcessorTypeError: _exit_with_output("Invalid output type '%s'" % otype, 1)