Exemple #1
0
 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
Exemple #2
0
    def _rename_type(self, root, lines, action):
        """Rename type transformation"""
        try:
            node = root.get_node_at_path(action['parameters']['path'])
        except:
            return False
        old = action['parameters']['old_name'] 
        new = action['parameters']['new_name']
 
        StructureChanger.change_tag(lines, node, old,  new)
        return True