Skip to content

Instantly share code, notes, and snippets.

@dougcooper
Last active March 17, 2019 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dougcooper/67191942c63f55ebd0fc6bbc58dd1ef1 to your computer and use it in GitHub Desktop.
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
#!/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