def _add_array(self, root, lines, action): """Move key transformation""" try: parent2 = root.get_node_at_path(action['parameters']['addition_path']) except: return False if parent2.implementation != DataNode.Implementation.sequence: raise TransformationFileFormatError( "Specified addition path (" + self._get_paths_str(action, 'addition_path') + \ ") is not array type node." ) if parent2.parent is None: raise TransformationFileFormatError( "Specified addition path can't be in root node" ) try: parent1 = root.get_node_at_path(action['parameters']['source_path']) except: return False if parent1.implementation != DataNode.Implementation.sequence: raise TransformationFileFormatError( "Specified source path (" + self._get_paths_str(action, 'path') + \ ") is not array type node." ) if parent2 == parent1: raise TransformationFileFormatError( "Source and addition paths must be different (" + self._get_paths_str(action, 'source_path') + ")") sl1, sc1, sl2, sc2 = StructureChanger.value_pos(parent1) sl2, sc2 = self._fix_end(lines, sl1, sc1, sl2, sc2 ) al1, ac1, al2, ac2 = StructureChanger.value_pos(parent2) al2, ac2 = self._fix_end(lines, al1, ac1, al2, ac2 ) if (al1>=sl1 and sl2>=al2) or (sl1>=al1 and al2>=sl2): raise TransformationFileFormatError( "Source and addition nodes can't contains each other (" + self._get_paths_str(action, 'source_path') + ")") indentation1 = StructureChanger.get_indentation(lines, sl1) al1, ac1, al2, ac2 = StructureChanger.add_comments(lines, al1, ac1, al2, ac2) add = StructureChanger.copy_structure(lines, al1, ac1, al2, ac2, indentation1, False) if sl2 < al1 or (sl2 == al1 and sl2 < ac1): # addition after source, first delete action['parameters']['path'] = action['parameters']['addition_path'] action['parameters']['deep'] = True self._delete_key(root, lines, action) self._add_comma(lines, sl1, sl2, sc2 ) StructureChanger.paste_structure(lines, sl2, add, True) if not(sl2 < al1 or (sl2 == al1 and sl2 < ac1)): action['parameters']['path'] = action['parameters']['addition_path'] action['parameters']['deep'] = True self._delete_key(root, lines, action) return True
def _change_value(self, root, lines, action): """Change value to speciffied""" try: node = root.get_node_at_path(action['parameters']['path']) except: return False if node.implementation != DataNode.Implementation.scalar: self._add_notification( "Specified path '{0}', is not scalar type node, action is ignored".format( self._get_paths_str(action, 'path')), self._Severity.warning , action, node ) return False old = action['parameters']['old_value'] new = action['parameters']['new_value'] l1, c1, l2, c2 = StructureChanger.value_pos(node) return StructureChanger.replace(lines, new, old, l1, c1, l2, c2 )
def _replace_value(self, root, lines, action): """Rename type transformation""" try: node = root.get_node_at_path(action['parameters']['path']) except: return False if node.implementation != DataNode.Implementation.scalar: self._add_notification( "Specified path '{0}', is not scalar type node, action is ignored".format( self._get_paths_str(action, 'path')), self._Severity.warning , action, node ) return False pattern = action['parameters']['pattern'] replacement = action['parameters']['replacement'] old = str(node.value) new = re.sub( pattern, replacement, old) if new != old: l1, c1, l2, c2 = StructureChanger.value_pos(node) return StructureChanger.replace(lines, new, old, l1, c1, l2, c2 ) return False
def _scale_value(self, root, lines, action): """Multiply value by set scale""" try: node = root.get_node_at_path(action['parameters']['path']) scale = float(action['parameters']['scale']) except: return False if node.implementation != DataNode.Implementation.scalar: self._add_notification( "Specified path '{0}', is not scalar type node, action is ignored".format( self._get_paths_str(action, 'path')), self._Severity.warning , action, node ) return False try: value = float(node.value) except ValueError: self._add_notification( "Type of value in specified path '{0}', is not numeric, action is ignored".format( self._get_paths_str(action, 'path')), self._Severity.warning , action, node ) return False l1, c1, l2, c2 = StructureChanger.value_pos(node) return StructureChanger.replace(lines, str(scale*value), lines[l1][c1:c2], l1, c1, l2, c2 )