Last active
March 17, 2019 23:45
-
-
Save dougcooper/67191942c63f55ebd0fc6bbc58dd1ef1 to your computer and use it in GitHub Desktop.
Converts a tab indented outline to an edge based graph for use with text import feature of Draw.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python2 | |
import sys | |
class Node(object): | |
def __init__(self,parent,data): | |
self.parent = parent | |
self.data = data | |
self.children = [] | |
def getParent(self): | |
return self.parent | |
def getData(self): | |
return self.data | |
def addChild(self,data): | |
node = Node(self,data) | |
self.children.append(node) | |
return node | |
def getChildren(self): | |
return self.children | |
def count_tabs(message): | |
cnt = 0 | |
for char in message: | |
if char == '\t': | |
cnt+=1 | |
continue | |
else: | |
return cnt | |
def print_tree(file,node): | |
for child in node.getChildren(): | |
file.write(str(child.getParent().getData())+"->"+str(child.getData())+"\n") | |
print_tree(file,child) | |
def cleanup_line(curr_cnt,line): | |
return line[curr_cnt:].rstrip("\n").rstrip(" ") | |
def main(argv): | |
if len(argv) != 2: | |
assert("Not enough argments") | |
tabbed_file = open(str(argv[0]), "r") | |
csv_file = open(str(argv[1]),"w+") | |
lines = tabbed_file.readlines() | |
prev_cnt = count_tabs(lines[0]) | |
master_node = Node(None,cleanup_line(prev_cnt,lines[0])) | |
prev_node = master_node | |
for line in lines[1:]: | |
curr_cnt = count_tabs(line) | |
line = cleanup_line(curr_cnt,line) | |
if (curr_cnt > prev_cnt): | |
prev_node = prev_node.addChild(line) | |
elif (curr_cnt == prev_cnt): | |
prev_node = prev_node.getParent().addChild(line) | |
else: | |
for i in range(prev_cnt-curr_cnt+1): | |
prev_node = prev_node.getParent() | |
prev_node = prev_node.addChild(line) | |
prev_cnt = curr_cnt | |
print_tree(csv_file,master_node) | |
tabbed_file.close() | |
csv_file.close() | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment