-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlonsdaleite
executable file
·136 lines (109 loc) · 3.18 KB
/
lonsdaleite
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/python
# vim: ft=python
import sys, getopt, webbrowser, re
def usage():
print("""
-d dump graph after parsing
-c expect input piped from Clasp's output
-u construct URL for Google's GraphViz Charts API
-b open that URL in the default web browser
""")
def main():
expectClasp = False
constructUrl = False
showInBrowser = False
dumpGraph = False
try:
opts, args = getopt.getopt(sys.argv[1:], "dcub")
except getopt.GetoptError, err:
print str(err)
usage()
sys.exit(2)
for o, a in opts:
if o == "-d":
dumpGraph = True
elif o == "-c":
expectClasp = True
elif o == "-u":
constructUrl = True
elif o == "-b":
showInBrowser = True
patternSpecs = {
"type": 1,
"engine": 1,
"node": 1,
"edge": 2,
"graph_attr": 2,
"node_attr": 3,
"edge_attr": 4,
"global_node_attr": 2,
"global_edge_attr": 2}
patterns = {}
for k, v in patternSpecs.items():
functor = "graphviz_" + k
args = "\(" + ",".join(["(.*?)"] * v) + "\)"
patterns[k] = re.compile(functor + args)
graph = {}
for k in patternSpecs.keys():
graph[k] = []
graph['type'].append(('graph',))
graph['engine'].append(('dot',))
def consider(term):
for k, v in patterns.items():
match = v.match(term)
if match:
graph[k].append(match.groups())
break
if expectClasp:
# look for all of the facts on a single line joined by spaces
for line in sys.stdin.readlines():
line = line.strip()
if line and line[0].islower():
terms = line.split(" ") # TODO: handle embedded spaces
for t in terms:
consider(t)
else:
# look for one fact per line
for line in sys.stdin.readlines():
consider(line.strip())
if dumpGraph:
print(graph)
a_list = lambda seq: "[" + (",".join("%s=%s" % kv for kv in seq)) + "]"
graphType = graph['type'][-1][0]
connector = "->" if graphType == "digraph" else "--"
graphvizCode = graphType
graphvizCode += '{'
if graph['graph_attr']:
graphvizCode += "graph" + a_list(graph['graph_attr'])
graphvizCode += ';'
if graph['global_node_attr']:
graphvizCode += "node" + a_list(graph['global_node_attr'])
graphvizCode += ';'
if graph['global_edge_attr']:
graphvizCode += "edge" + a_list(graph['global_edge_attr'])
graphvizCode += ';'
nodes = []
for (n1,) in graph['node']:
attrs = [(k,v) for n1t,k,v in graph['node_attr'] if n1t == n1]
nodes.append(n1 + a_list(attrs) if attrs else n1)
graphvizCode += ';'.join(nodes)
graphvizCode += ';'
edges = []
for (n1,n2) in graph['edge']:
attrs = [(k,v) for n1t,n2t,k,v in graph['edge_attr'] \
if n1t == n1 and n2t == n2]
edges.append(n1 + connector + n2 + (a_list(attrs) if attrs else ""))
graphvizCode += ';'.join(edges)
graphvizCode += '}'
if constructUrl:
cht = "gv:%s" % graph['engine'][-1][0]
chl = graphvizCode
url = "http://chart.googleapis.com/chart?chl=" + chl + "&cht=" + cht
if showInBrowser:
webbrowser.open(url)
else:
print(url)
else:
print(graphvizCode)
if __name__ == "__main__":
main()