def transform(self, yaml, cfg): """transform yaml file""" self.err=[] notification_handler = NotificationHandler() loader = Loader(notification_handler) root = loader.load(yaml) lines = yaml.splitlines(False) changes = False text = cfg.get_curr_format_text() root_input_type = get_root_input_type_from_json(text) for aaction in self._transformation['actions']: if changes: root, lines = self.refresh(root_input_type, yaml, notification_handler, loader) changes=False for action in self._replace_wildchars(root, aaction): if changes: root, lines = self.refresh(root_input_type, yaml, notification_handler, loader) if action['action'] == "delete-key": changes = self._delete_key(root, lines, action) elif action['action'] == "move-key": changes = self._move_key(root, lines, action) if changes and "set_type_path" in action['parameters']: yaml = "\n".join(lines) root, lines = self.refresh(root_input_type, yaml, notification_handler, loader) changes = False try: node = root.get_node_at_path(action['parameters']['set_type_path']) if node.type is not None: StructureChanger.change_tag(lines, node, node.type.value, action['parameters']['new_type'].strip()) else: StructureChanger.add_tag(lines, node, action['parameters']['new_type'].strip()) changes = True except: pass elif action['action'] == "rename-type": changes = self._rename_type(root, lines, action) elif action['action'] == "move-key-forward": changes = self._move_key_forward(root, lines, action) elif action['action'] == "change-value": changes = self._change_value(root, lines, action) elif action['action'] == "replace-value": changes = self._replace_value(root, lines, action) elif action['action'] == "merge-arrays": changes = self._add_array(root, lines, action) if 'destination_path' in action['parameters']: if changes: yaml = "\n".join(lines) root, lines = self.refresh(root_input_type, yaml, notification_handler, loader) action['parameters']['keep-source'] = False changes = self._move_key(root, lines, action) elif action['action'] == "add-key": changes = self._add_key(root, lines, action) elif action['action'] == "scale-value": changes = self._scale_value(root, lines, action) if changes: yaml = "\n".join(lines) yaml = "\n".join(lines) return yaml
def update_format(cls): """reread json format file and update node tree""" if cls.curr_format_file is None: return text = cls.get_curr_format_text() try: cls.root_input_type = get_root_input_type_from_json(text) except Exception as e: from os.path import join file_name = join(cls.format_dir, cls.curr_format_file + ".json") cls._report_error("Can't parse format file: " + file_name , e) else: InfoTextGenerator.init(text) cls.autocomplete_helper.create_options(cls.root_input_type) cls.update()