示例#1
0
def test_real_duty2():
    initial_configs_dir = os.path.join(
        os.path.dirname(__file__), "files", "configs"
    )
    with tempfile.TemporaryDirectory(
        dir=os.path.join(os.path.dirname(__file__), "files")
    ) as temp:
        make_configs(temp, initial_configs_dir)

        with open(os.path.join(temp, "master.json")) as mf, open(
            os.path.join(temp, "big1.json")
        ) as b1f, open(os.path.join(temp, "big2.json")) as b2f, open(
            os.path.join(initial_configs_dir, "big1.json")
        ) as ib1, open(
            os.path.join(initial_configs_dir, "big2.json")
        ) as ib2:
            mc = json.loads(mf.read())
            b1 = json.loads(b1f.read())
            b2 = json.loads(b2f.read())

            ib1 = json.loads(ib1.read())
            ib2 = json.loads(ib2.read())

        restored1 = Splitter(mc) + Splitter(b1)
        restored1 = restored1.as_dict()

        restored2 = Splitter(mc) + Splitter(b2)
        restored2 = restored2.as_dict()

        assert all([ib1[k] == restored1[k] for k in restored1.keys()])
        assert all([ib2[k] == restored2[k] for k in restored2.keys()])
示例#2
0
def make_configs(target_dir: str, scan_dir: str):
    master_conf = Splitter(dict_={})
    for file in os.listdir(scan_dir):
        path = os.path.join(scan_dir, file)
        if os.path.isdir(path):
            continue
        ext = Path(path).suffix
        with open(path, "r") as f:
            if ext == ".json":
                conf = Splitter(dict_=json.loads(f.read()))

        if not master_conf:
            master_conf = conf
        else:
            master_conf = master_conf ^ conf

    # save master config
    with open(os.path.join(target_dir, "master.json"), "w") as mf:

        mf.write(json.dumps(master_conf.as_dict()))

    for file in os.listdir(scan_dir):
        path = os.path.join(scan_dir, file)
        if os.path.isdir(path):
            continue
        ext = Path(path).suffix

        with open(path, "r") as f:
            if ext == ".json":
                conf = Splitter(dict_=json.loads(f.read()))

        conf = conf - master_conf
        with open(os.path.join(target_dir, file), "w") as cf:
            cf.write(json.dumps(conf.as_dict()))