summaryrefslogtreecommitdiff
path: root/bin/eo_graph_gen.py
diff options
context:
space:
mode:
Diffstat (limited to 'bin/eo_graph_gen.py')
-rwxr-xr-xbin/eo_graph_gen.py189
1 files changed, 189 insertions, 0 deletions
diff --git a/bin/eo_graph_gen.py b/bin/eo_graph_gen.py
new file mode 100755
index 0000000..d7b2342
--- /dev/null
+++ b/bin/eo_graph_gen.py
@@ -0,0 +1,189 @@
1#!/usr/bin/env python
2
3from eoparser.helper import isXML, dir_files_get, abs_path_get
4from eoparser.xmlparser import XMLparser
5from argparse import ArgumentParser
6import os, sys
7
8def verbose_true(mes):
9 print mes
10
11def verbose_false(mes):
12 pass
13
14def main():
15 parser = ArgumentParser()
16 parser.add_argument("-d", "--dir", dest="directory",
17 action="append", help="Path to XML descriptions", required=True)
18
19 parser.add_argument("-o", "--outfile", dest="outfile",
20 action="store", help="Out file in png format. Default: \"out.png\"", default="out.png")
21
22 parser.add_argument("-v", "--verbose",
23 action="store_true", dest="verbose", default=False,
24 help="Verbose output")
25 """
26 parser.add_argument("--graphstyle", action="store", dest="graphstyle", default="digraph",
27 help="Set graph style. Default: \"digraph\"")
28 """
29
30 parser.add_argument("--extedge", action="store", dest="extedge", default="dashed, dashed, dotted, dashed",
31 help="Set line style for extension edges depending on extension type(REGULAR, MIXIN, INTERFACE, NO_INSTANT).[\"solid, dotted, dashed, bold\"]. Default: \"dashed, dashed, dotted, dashed\"")
32
33 parser.add_argument("--rankdir", action="store", dest="rankdir", default="BT",
34 help="Direction of directed graph[BT, TB, LR, RL]. Default: \"BT\"")
35
36 parser.add_argument("--graphfontsize", action="store", dest="graphfontsize", default="15",
37 help="Graph font size. Default: \"15\"")
38
39 parser.add_argument("--graphlabel", action="store", dest="graphlabel", default="",
40 help="Graph label")
41
42 parser.add_argument("--nodefontsize", action="store", dest="nodefontsize", default="10",
43 help="Node font size. Default: \"10\"")
44
45 parser.add_argument("--nodeshape", action="store", dest="nodeshape", default="polygon",
46 help="Node shape.[\"polygon\", \"circle\", \"ellipse\"] Default: \"polygon\"")
47
48 parser.add_argument("--long-names", action="store", dest="short_long", default="short",
49 help="Node shape.[\"short\", \"long\"] Default: \"short\"")
50
51 styles = {}
52
53 args = parser.parse_args()
54 styles["extedge"] = args.extedge.replace(" ", "").split(",")
55 if len (styles["extedge"]) < 4:
56 print "--extedge option is not correct"
57 exit(1)
58 styles["graphstyle"] = "digraph"# args.graphstyle
59 styles["rankdir"] = args.rankdir
60 styles["graphfontsize"] = args.graphfontsize
61 styles["graphlabel"] = args.graphlabel
62 styles["nodefontsize"] = args.nodefontsize
63 styles["nodeshape"] = args.nodeshape
64 SHORT_LONG = 0 if args.short_long == "short" else 1
65
66 TYPE_REGULAR = "EO_CLASS_TYPE_REGULAR"
67 TYPE_MIXIN = "EO_CLASS_TYPE_MIXIN"
68 TYPE_INTERFACE = "EO_CLASS_TYPE_INTERFACE"
69 TYPE_NO_INSTANT = "EO_CLASS_TYPE_REGULAR_NO_INSTANT"
70
71 types_list = (TYPE_REGULAR, TYPE_MIXIN, TYPE_INTERFACE, TYPE_NO_INSTANT)
72
73 type_names = {
74 TYPE_REGULAR : ("REGULAR", "EO_CLASS_TYPE_REGULAR"),
75 TYPE_MIXIN : ("MIXIN", "EO_CLASS_TYPE_MIXIN"),
76 TYPE_INTERFACE : ("INTERFACE", "EO_CLASS" ),
77 TYPE_NO_INSTANT : ("NO INSTANT", "EO_CLASS_TYPE_REGULAR_NO_INSTANT")
78 }
79
80 #node colors
81 n_color = {
82 TYPE_REGULAR :"green",
83 TYPE_MIXIN : "blue",
84 TYPE_INTERFACE :"red",
85 TYPE_NO_INSTANT :"darkviolet"
86 }
87
88 #ext edge line style
89 p_edge = {}
90 p_edge[TYPE_REGULAR] = styles["extedge"][0]
91 p_edge[TYPE_MIXIN] = styles["extedge"][1]
92 p_edge[TYPE_INTERFACE] = styles["extedge"][2]
93 p_edge[TYPE_NO_INSTANT] = styles["extedge"][3]
94
95 verbose_print = None
96 if args.verbose is True:
97 verbose_print = verbose_true
98 else:
99 verbose_print = verbose_false
100
101 directories = args.directory + sys.path
102 directories = abs_path_get(directories, False)
103
104 outfile = args.outfile
105 outfile = os.path.expanduser(outfile)
106 outfile = os.path.abspath(outfile)
107
108 outdir = os.path.split(outfile)[0]
109 if not os.path.exists(outdir):
110 print "ERROR: output directory %s doesn't exists... Aborting..."%outdir
111 exit(1)
112
113 xml_files = dir_files_get(directories)
114 xml_files = filter(isXML, xml_files)
115
116 graph = {}
117
118 if len(xml_files) == 0:
119 print "ERROR: no xml files were found"
120 exit(1)
121
122 xp = XMLparser()
123 for f in xml_files:
124 xp.parse(f)
125
126 graph = dict(xp.objects)
127
128 del xp
129
130 if len(graph) == 0:
131 print "ERROR: no data was found in source files"
132 exit(1)
133
134 lines = []
135
136 lines.append("%s G{"%(styles["graphstyle"]))
137 lines.append(" graph [rankdir = \"%s\", fontsize = %s, label = \"%s\"];"%(styles["rankdir"], styles["graphfontsize"], styles["graphlabel"]))
138 lines.append(" node [shape = \"%s\" fontsize = %s];"%(styles["nodeshape"], styles["nodefontsize"]))
139
140 #generating dot node description for node, for each class
141 for n, o in graph.items():
142 if o.eo_type in types_list:
143 line = " \"%s\" [label = \"%s \\n %s\", color = \"%s\"];"%(n, o.c_name, type_names[o.eo_type][SHORT_LONG], n_color[o.eo_type])
144 else:
145 line = " \"%s\" [label = \"%s \\n %s\"];"%(n, o.c_name, o.eo_type)
146
147 lines.append(line)
148
149 #generating dot edge description for each parent of each class
150 for n, o in graph.items():
151 parent = o.parents[0]
152 ext = o.parents[1:]
153 ext = filter(len, ext)
154 if len(parent) != 0:
155 if parent in graph:
156 p_type = graph[parent].eo_type
157 else:
158 p_type = TYPE_REGULAR
159 line = " \"%s\" -> \"%s\" [style = \"%s\", color=\"%s\"];"%(n, parent, "solid", n_color[p_type])
160 lines.append(line)
161 for l in ext:
162 eo_type_tmp = graph[l].eo_type
163 if eo_type_tmp in types_list:
164 line = " \"%s\" -> \"%s\" [style=\"%s\", color=\"%s\"] ;"%(n, l, p_edge[eo_type_tmp], n_color[eo_type_tmp])
165 else:
166 line = " \"%s\" -> \"%s\" [style=\"%s\", color=\"%s\"] ;"%(k, l, "dashed", "black")
167
168 lines.append(line)
169 lines.append("}")
170
171 tmp_dot_file = "tmp.dot"
172 f = open(tmp_dot_file, 'w')
173 for l in lines:
174 f.write(l+'\n')
175
176 f.close()
177
178 verbose_print("Dot file: '%s' was generated"%(tmp_dot_file))
179 verbose_print("Graph file: '%s was generated"%(outfile))
180 os.system("dot -Tpng %s -o %s"%(tmp_dot_file, outfile))
181 #os.system("neato -Tpng %s -o %s_neato"%(tmp_dot_file, outfile))
182 #os.system("twopi -Tpng %s -o %s_twopi"%(tmp_dot_file, outfile))
183 #os.system("circo -Tpng %s -o %s_circo"%(tmp_dot_file, outfile))
184 #os.system("fdp -Tpng %s -o %s_fdp"%(tmp_dot_file, outfile))
185 #os.system("sfdp -Tpng %s -o %s_sfdp"%(tmp_dot_file, outfile))
186
187if __name__ == "__main__":
188 main()
189