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)
Example #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))
 def testReplaceNone(self):
     tmp_dict = deepcopy(self.testDict)
     DictProcessing.replace(self.testDict, "nonsense", "some")
     self.assertEqual(tmp_dict, self.testDict)