def write_to_yaml(self, yaml): """return yaml text with comments""" lines = yaml.splitlines(False) for comment in self.comments: intend = re.search(r'^(\s*)(\S.*)$', lines[comment.inden_line]) if comment.after: if (len(lines[comment.pos.line - 1]) - 1) <= comment.pos.column: if len(lines) <= comment.pos.line: lines.append(intend.group(1) + "# " + comment.rows[0]) else: lines.insert(comment.pos.line, intend.group(1) + "# " + comment.rows[0]) else: if len(lines) <= comment.pos.line - 1: lines.append(intend.group(1) + "# " + comment.rows[0]) else: lines.insert(comment.pos.line - 1, intend.group(1) + "# " + comment.rows[0]) else: lines.insert(comment.pos.line - 1, intend.group(1) + "# " + comment.rows[0]) for i in range(1, len(comment.rows)): if comment.after: lines.insert(comment.pos.line + i, intend.group(1) + "# " + comment.rows[i]) else: lines.insert(comment.pos.line + i - 1, intend.group(1) + "# " + comment.rows[i]) return "\n".join(lines)
def fix_intendation(yaml, root): """ Move intendation for array This method suppose that that yaml is clean text created by yaml.dump method with parameters defaultlt_flow_style=False and indent=2. """ lines = yaml.splitlines(False) lines = _traverse_nodes_intendation(root, lines) return "\n".join(lines)
def write_to_yaml(self, yaml): """return yaml text with comments""" lines = yaml.splitlines(False) for comment in self.comments: intend = re.search(r'^(\s*)(\S.*)$', lines[comment.inden_line]) if comment.after: if (len(lines[comment.pos.line-1])-1) <= comment.pos.column: if len(lines) <= comment.pos.line: lines.append(intend.group(1) + "# " + comment.rows[0]) else: lines.insert(comment.pos.line, intend.group(1) + "# " + comment.rows[0]) else: if len(lines) <= comment.pos.line-1: lines.append(intend.group(1) + "# " + comment.rows[0]) else: lines.insert(comment.pos.line-1, intend.group(1) + "# " + comment.rows[0]) else: lines.insert(comment.pos.line-1, intend.group(1) + "# " + comment.rows[0]) for i in range(1, len(comment.rows)): if comment.after: lines.insert(comment.pos.line+i, intend.group(1) + "# " + comment.rows[i]) else: lines.insert(comment.pos.line+i-1, intend.group(1) + "# " + comment.rows[i]) return "\n".join(lines)
def __init__(self, message, yaml): yaml_lines = yaml.splitlines() if len(yaml_lines) > 10: yaml = "\n".join(yaml_lines[:10]) + "\n..." self.message = "%s in rule:\n\n%s" % (message, yaml)
def upgrade(): for filename in sorted(glob.glob('tests/*.yaml')): continue print(filename) with open(filename) as f: yaml = f.read() nyaml = '' cur_type = '' cur_name = '' materials = {} shapes = {} for line in yaml.splitlines(): line = line.rstrip() if not line: nyaml += line + '\n' elif line in ['materials:', 'shapes:', 'instances:']: cur_type = line if line == 'instances:': nyaml += 'shapes:\n' elif not line.startswith(' '): cur_type = '' nyaml += line + '\n' elif line.startswith(' ') and not cur_type: nyaml += line + '\n' elif line.startswith(' ') and cur_type == 'materials:': if line.startswith(' - '): cur_name = line.partition(':')[2].strip() materials[cur_name] = '' else: materials[cur_name] += line + '\n' elif line.startswith(' ') and cur_type == 'shapes:': if line.startswith(' - '): cur_name = line.partition(':')[2].strip() shapes[cur_name] = '' else: shapes[cur_name] += line.replace('filename:', 'shape:') + '\n' elif line.startswith(' ') and cur_type == 'instances:': if line.startswith(' - '): nyaml += ' - name: ' + line.partition( ':')[2].strip().replace('_', '-') + '\n' elif line.startswith(' material:'): nyaml += materials[line.partition(':')[2].strip()] elif line.startswith(' shape:'): nyaml += shapes[line.partition(':')[2].strip()] else: nyaml += line + '\n' with open(filename, 'wt') as f: f.write(nyaml) for filename in sorted(glob.glob('tests/*.yaml')): print(filename) with open(filename) as f: yaml = f.read() nyaml = '' cur_type = '' cur_name = '' textures = {} for line in yaml.splitlines(): line = line.rstrip() if not line: nyaml += line + '\n' elif line in ['textures:']: cur_type = line elif not line.startswith(' '): cur_type = '' nyaml += line + '\n' elif line.startswith(' ') and not cur_type: if '_tex:' in line: nyaml += line.partition(':')[0] + ': ' + textures[ line.partition(':')[2].strip()] + '\n' else: nyaml += line + '\n' elif line.startswith(' ') and cur_type == 'textures:': if line.startswith(' - '): cur_name = line.partition(':')[2].strip() textures[cur_name] = '' else: textures[cur_name] = line.partition(':')[2].strip() with open(filename, 'wt') as f: f.write(nyaml)
def fix_tags(yaml, root): """Replase TYPE and refferences by tags""" lines = yaml.splitlines(False) add_anchor = {} anchor_idx = {} del_lines = [] _traverse_nodes(root, lines, add_anchor, anchor_idx, del_lines) new_lines = [] add_lines = {} need_move_forward = [] for i in add_anchor: try: anchor = root.get_node_at_path(i) col = anchor.span.start.column-1 line = anchor.span.start.line-1 if anchor.key.span is not None: col = anchor.key.span.end.column-1 line = anchor.key.span.end.line-1 if len(lines[line]) > col+1: text = lines[line][col+1:] tag = re.search(r'^(\s+!\S+)', text) if tag is not None: col += len(tag.group(1)) if len(lines[line]) > col+1: if col > 1 and lines[line][col-2:col] == "- ": add_lines[line+1] = col * " " + lines[line][col:] lines[line] = lines[line][:col] + "&anchor" + str(anchor_idx[i]) else: lines[line] = (lines[line][:col] + " &anchor" + str(anchor_idx[i]) + ' ' + lines[line][col:]) else: lines[line] += " &anchor" + str(anchor_idx[i]) # if anchor is in same level as ref and ref is before anchor, rename first_ref = None for ref_node in add_anchor[i]: if ref_node.span.start < anchor.span.start: if first_ref is None or first_ref.span.start > ref_node.span.start: first_ref = ref_node if (first_ref is not None and first_ref.parent is not None and first_ref.parent.parent is not None and anchor.parent is not None): anchor_path = anchor.parent.absolute_path ref_path = first_ref.parent.parent.absolute_path if len(anchor_path) <= len(ref_path) and ref_path[:len(anchor_path)] == anchor_path: need_move_forward.append(anchor.absolute_path) else: if (first_ref.parent.parent.parent is not None and anchor.parent.parent is not None): anchor_path = anchor.parent.parent.absolute_path ref_path = first_ref.parent.parent.parent.absolute_path if (len(anchor_path) <= len(ref_path) and ref_path[:len(anchor_path)] == anchor_path): need_move_forward.append(anchor.parent.absolute_path) except: continue for i in range(0, len(lines)): if i in add_lines: new_lines.append(add_lines[i]) if i not in del_lines: new_lines.append(lines[i]) return "\n".join(new_lines), need_move_forward
def fix_tags(yaml, root): """Replase TYPE and refferences by tags""" lines = yaml.splitlines(False) add_anchor = {} anchor_idx = {} del_lines = [] _traverse_nodes(root, lines, add_anchor, anchor_idx, del_lines) new_lines = [] add_lines = {} need_move_forward = [] for i in add_anchor: try: anchor = root.get_node_at_path(i) col = anchor.span.start.column - 1 line = anchor.span.start.line - 1 if anchor.key.span is not None: col = anchor.key.span.end.column - 1 line = anchor.key.span.end.line - 1 if len(lines[line]) > col + 1: text = lines[line][col + 1:] tag = re.search(r'^(\s+!\S+)', text) if tag is not None: col += len(tag.group(1)) if len(lines[line]) > col + 1: if col > 1 and lines[line][col - 2:col] == "- ": add_lines[line + 1] = col * " " + lines[line][col:] lines[line] = lines[line][:col] + "&anchor" + str( anchor_idx[i]) else: lines[line] = (lines[line][:col] + " &anchor" + str(anchor_idx[i]) + ' ' + lines[line][col:]) else: lines[line] += " &anchor" + str(anchor_idx[i]) # if anchor is in same level as ref and ref is before anchor, rename first_ref = None for ref_node in add_anchor[i]: if ref_node.span.start < anchor.span.start: if first_ref is None or first_ref.span.start > ref_node.span.start: first_ref = ref_node if (first_ref is not None and first_ref.parent is not None and first_ref.parent.parent is not None and anchor.parent is not None): anchor_path = anchor.parent.absolute_path ref_path = first_ref.parent.parent.absolute_path if len(anchor_path) <= len(ref_path) and ref_path[:len( anchor_path)] == anchor_path: need_move_forward.append(anchor.absolute_path) else: if (first_ref.parent.parent.parent is not None and anchor.parent.parent is not None): anchor_path = anchor.parent.parent.absolute_path ref_path = first_ref.parent.parent.parent.absolute_path if (len(anchor_path) <= len(ref_path) and ref_path[:len(anchor_path)] == anchor_path): need_move_forward.append( anchor.parent.absolute_path) except: continue for i in range(0, len(lines)): if i in add_lines: new_lines.append(add_lines[i]) if i not in del_lines: new_lines.append(lines[i]) return "\n".join(new_lines), need_move_forward