Created
January 7, 2013 10:24
-
-
Save bussolon/4473921 to your computer and use it in GitHub Desktop.
Parses a text file containing a tabbed tree structure and creates a graphviz dot file
Based on: http://stackoverflow.com/a/6076548/1042167
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
parseTree.py | |
Created by [Stefano Bussolon ](<http://www.bussolon.it>) | |
Based on: http://stackoverflow.com/a/6076548/1042167 | |
Parses a text file containing a tabbed tree structure and creates a graphviz dot file | |
. Example: | |
Root | |
Branch 1 | |
Branch 1A | |
Leaf 1Aa | |
Leaf 1Ab | |
Branch 1B | |
Leaf 1Ba | |
Branch 2 | |
Leaf 2A | |
""" | |
import re | |
import pydot | |
# *** Tree representation *** | |
class Node(object): | |
def __init__(self, title): | |
self.title = title | |
self.parent = None | |
self.children = [] | |
def add(self, child): | |
self.children.append(child) | |
child.parent = self | |
# *** Node insertion logic *** | |
class Inserter(object): | |
def __init__(self, node, depth = 0): | |
self.node = node | |
self.depth = depth | |
def __call__(self, title, depth): | |
newNode = Node(title) | |
if (depth > self.depth): | |
self.node.add(newNode) | |
self.depth = depth | |
elif (depth == self.depth): | |
self.node.parent.add(newNode) | |
else: | |
parent = self.node.parent | |
for i in xrange(0, self.depth - depth): | |
parent = parent.parent | |
parent.add(newNode) | |
self.depth = depth | |
self.node = newNode | |
def print_tree(node, graph, nodesDnodes, depth = 0): | |
dotnode = pydot.Node(name=node.title, texlbl=node.title, label=node.title) | |
graph.add_node(dotnode) | |
nodesDnodes[node] = dotnode | |
if node.parent != None: | |
dotParent = nodesDnodes[node.parent] | |
graph.add_edge(pydot.Edge(dotParent, dotnode)) | |
print '[%s]%s' % ('\t' * depth, node.title) | |
for child in node.children: | |
print_tree(child, graph, nodesDnodes, depth + 1) | |
# *** File iteration logic *** | |
inputFile = r'yourInputfile.txt' | |
outputFile = r'yourOutputfile.dot' | |
with open(inputFile, 'r') as f: | |
radice = f.readline().rstrip('\n') | |
print (radice) | |
tree = Node(radice) | |
inserter = Inserter(tree) | |
print (inserter) | |
if inserter == None: | |
print ('caxxo') | |
for line in f: | |
line = line.rstrip('\n') | |
print (line) | |
# note there's a bug with your original tab parsing code: | |
# it would count all tabs in the string, not just the ones | |
# at the beginning | |
tabs = re.match('\t*', line).group(0).count('\t') | |
print ('tabs: %i') % tabs | |
title = line[tabs:] | |
inserter(title, tabs) | |
graph = pydot.Dot(graph_name="main_graph",rankdir="LR", labelloc='b', labeljust='r', ranksep=1) | |
#graph.set_node_defaults(shape='circle', fixedsize='true', height=.85, width=.85, fontsize=24) | |
nodesDnodes = {} | |
print_tree(tree,graph, nodesDnodes) | |
graph.write(outputFile, format='raw', prog='dot') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment