Esempio n. 1
0
    def update_data(file_path_to_write: str, file_path_to_read: str, file_ending: str) -> None:
        """
        Collects special chosen fields from the file_path_to_read and writes them into the file_path_to_write.
        :param file_path_to_write: The output file path to add the special fields to.
        :param file_path_to_read: The input file path to read the special fields from.
        :param file_ending: The files ending
        :return: None
        """

        pack_obj_data, _ = get_dict_from_file(file_path_to_read)
        fields: list = DELETED_YML_FIELDS_BY_DEMISTO if file_ending == 'yml' else DELETED_JSON_FIELDS_BY_DEMISTO
        # Creates a nested-complex dict of all fields to be deleted by Demisto.
        # We need the dict to be nested, to easily merge it later to the file data.
        preserved_data: dict = unflatten({field: dictor(pack_obj_data, field) for field in fields if
                                          dictor(pack_obj_data, field)}, splitter='dot')

        if file_ending == 'yml':
            ryaml = YAML()
            ryaml.preserve_quotes = True  # type: ignore
            with open(file_path_to_write, 'r') as yf:
                file_yaml_object = ryaml.load(yf)
            if pack_obj_data:
                merge(file_yaml_object, preserved_data)
            with open(file_path_to_write, 'w') as yf:
                ryaml.dump(file_yaml_object, yf)

        elif file_ending == 'json':
            file_data: dict = get_json(file_path_to_write)
            if pack_obj_data:
                merge(file_data, preserved_data)
            json_depth: int = get_depth(file_data)
            with open(file_path_to_write, 'w') as jf:
                json.dump(obj=file_data, fp=jf, indent=json_depth)
Esempio n. 2
0
 def test_get_depth(self, data, output):
     assert get_depth(data) == output