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 testDictWithList(self):
     DataMiner().data_type = DataTypes.DictWithList
     self.store_key()
     print(PersistentObjectStorage().storage_object)
     processor = DictProcessing(PersistentObjectStorage().storage_object)
     processor.simplify()
     self.assertIn(
         "'a': {'d': {'e': {'%s': [" % DataMiner().key,
         str(PersistentObjectStorage().storage_object),
     )
 def testValue(self):
     DataMiner().data_type = DataTypes.Value
     self.store_key()
     print(PersistentObjectStorage().storage_object)
     processor = DictProcessing(PersistentObjectStorage().storage_object)
     processor.simplify()
     self.assertIn(
         "'a': {'d': {'e': {'metadata'",
         str(PersistentObjectStorage().storage_object),
     )
 def testValue(self):
     self.cassette.data_miner.data_type = DataTypes.Value
     self.store_key()
     print(self.cassette.storage_object)
     processor = DictProcessing(self.cassette.storage_object)
     processor.simplify()
     self.assertIn(
         "'a': {'d': {'e': {'metadata'",
         str(self.cassette.storage_object),
     )
 def testDictWithList(self):
     self.cassette.data_miner.data_type = DataTypes.DictWithList
     self.store_key()
     print(self.cassette.storage_object)
     processor = DictProcessing(self.cassette.storage_object)
     processor.simplify()
     self.assertIn(
         "'a': {'d': {'e': {'%s': [" % self.cassette.data_miner.key,
         str(self.cassette.storage_object),
     )
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)
 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)
示例#8
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)
 def testDefault(self):
     self.store_key()
     processor = DictProcessing(PersistentObjectStorage().storage_object)
     processor.simplify()
     self.assertIn("'a': {'d': {'e': [",
                   str(PersistentObjectStorage().storage_object))
 def testDefault(self):
     self.store_key()
     processor = DictProcessing(self.cassette.storage_object)
     processor.simplify()
     self.assertIn("'a': {'d': {'e': [", str(self.cassette.storage_object))