class ObjectPostprocessing(unittest.TestCase):
    def setUp(self) -> None:
        self.testDict = {
            "a": {
                "b": {
                    "c": "ahoj"
                }
            },
            "int": 2,
            "str": "anystr",
            "bool": False,
            "list": [1, 2, {
                "a": "y"
            }],
            "list_inside": [{
                "m": {
                    "n": "o"
                }
            }],
            "dict": {
                "a": "x"
            },
        }
        self.dp = DictProcessing(self.testDict)

    def testMatchNoSelector(self):
        self.assertEqual(1, len(list(self.dp.match([]))))

    def testMatchMultiSelector(self):
        self.assertEqual(3, len(list(self.dp.match(["a"]))))

    def testMatchDeep(self):
        output = list(self.dp.match(["list_inside", "m"]))
        self.assertEqual(1, len(output))
        self.assertEqual({"n": "o"}, output[0])

        output = list(self.dp.match(["n"]))
        self.assertEqual(1, len(output))
        self.assertEqual("o", output[0])

    def testMatchNoMatch(self):
        self.assertEqual(0, len(list(self.dp.match(["non_sense"]))))

    def testReplaceMulti(self):
        tmp_dict = deepcopy(self.testDict)
        DictProcessing.replace(self.testDict, "a", "some")
        self.assertEqual(self.testDict["a"], "some")
        self.assertEqual(self.testDict["dict"]["a"], "some")
        self.assertEqual(self.testDict["list"][2]["a"], "some")
        self.assertNotEqual(tmp_dict, self.testDict)

    def testReplaceNone(self):
        tmp_dict = deepcopy(self.testDict)
        DictProcessing.replace(self.testDict, "nonsense", "some")
        self.assertEqual(tmp_dict, self.testDict)
Пример #2
0
def purge(replaces, files, dry_run, simplify):
    for one_file in files:
        click.echo(f"Processing file: {one_file.name}")
        object_representation = yaml.safe_load(one_file)
        processor = DictProcessing(object_representation)
        for item in replaces:
            click.echo(f"\tTry to apply: {item}")
            selector_str, key, type_of_value, value = item.split(":", 3)
            selector_list = [] if not selector_str else selector_str.split("%")
            # retype the output object to proper type ()
            value = getattr(builtins, type_of_value)(value)
            for matched in processor.match(selector=selector_list):
                click.echo(f"\t\tMatched {selector_list}")
                processor.replace(obj=matched, key=key, value=value)
        if simplify:
            processor.simplify()
        if not dry_run:
            click.echo(f"Writing content back to file: {one_file.name}")
            with open(one_file.name, mode="w") as outfile:
                outfile.write(yaml.safe_dump(object_representation))