forked from hippysurfer/revj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
executable file
·212 lines (172 loc) · 7.14 KB
/
gui.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""Reverse Snowflake Joins
Copyright (c) 2008, Alexandru Toth
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY Alexandru Toth ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."""
import os, platform, sys, traceback
from tkinter import *
from revj import *
from constants import *
def deleteFiles():
for x in [DOTFILE, GIFFILE]:
try:
os.remove(x)
except:
pass
def dotRunner(algo='one', dist=1):
""" PhotoImage supports only GIF format"""
cmd = ''
if 'one' in algo:
# if there is some problem in the script for gvpr
cmd = 'dot -Grankdir=LR -Granksep=%s -Edir=none -T gif -o "%s" "%s"' % \
(dist, GIFFILE, DOTFILE)
else:
cmd = ('%s "%s" | gvpr -f"%s" | dot -Grankdir=LR -Granksep=%s -Edir=none '
'-T gif -o "%s"') % (algo, DOTFILE, DIRG, dist, GIFFILE)
print(cmd)
os.system(cmd)
def exportRunner(algo='one', dist=1):
""" PhotoImage supports only GIF format"""
cmd = ''
if 'one' in algo:
# if there is some problem in the script for gvpr
cmd = 'dot -Grankdir=LR -Granksep=%s -Edir=none -T png -o "%s" "%s"' % \
(dist, PNGFILE, DOTFILE)
else:
cmd = ('%s "%s" | gvpr -f"%s" | dot -Grankdir=LR -Granksep=%s -Edir=none '
'-T gif -o "%s"') % (algo, DOTFILE, DIRG, dist, PNGFILE)
print(cmd)
os.system(cmd)
class GUIApp:
def __init__(self, master):
pan = PanedWindow(orient=VERTICAL)
pan.pack(fill=BOTH, expand=1)
# resizable frame for scrollable input
sqlFrame = PanedWindow(pan)
pan.add(sqlFrame)
sqlDummyFrame = Frame(sqlFrame)
sqlDummyFrame.pack()
sqlFrame.add(sqlDummyFrame)
self.sqlInput = Text(sqlDummyFrame)
self.sqlInput.insert(INSERT, DEFSQL)
self.sqlInput.pack(expand=True, fill=BOTH, side=LEFT)
sqlScrol = Scrollbar(sqlDummyFrame)
sqlScrol.pack(expand=False, fill=Y, side=RIGHT)
self.sqlInput.config(yscrollcommand=sqlScrol.set)
sqlScrol.config(command=self.sqlInput.yview)
# resizable frame with fixed size buttons and menus
decor = PanedWindow(pan)
middleDummy = Frame(decor)
middleDummy.pack()
label = Label(middleDummy, text="Graph algorithm=")
label.pack(side=LEFT)
self.varAlgo = Variable(master)
self.varAlgo.set("neato")
self.lstAlgo = OptionMenu(middleDummy, self.varAlgo,
'neato', 'fdp', 'twopi', 'circo')
# one pass not functioning
# , 'one-pass (in case of trouble)')
self.lstAlgo.pack(expand=False, side=LEFT)
labelDist = Label(middleDummy, text="Distance=")
labelDist.pack(side=LEFT)
self.varDist = Variable(master)
self.varDist.set("1")
self.lstDist = OptionMenu(middleDummy, self.varDist, '1', '2', '3')
self.lstDist.pack(expand=False, side=LEFT)
self.btn = Button(middleDummy, text="Generate Diagram",
command=self.gen)
self.btn.pack(expand=False, side=LEFT)
self.exbtn = Button(middleDummy, text="Export Diagram",
command=self.export)
self.exbtn.pack(expand=False, side=LEFT)
pan.add(decor)
# resizable frame for scrollable diagram
diagramFrame = PanedWindow(pan)
pan.add(diagramFrame)
diagramDummyFrame = Frame(diagramFrame)
diagramDummyFrame.pack()
diagramFrame.add(diagramDummyFrame)
diagramDummyFrame2 = Frame(diagramDummyFrame)
diagramDummyFrame2.pack(expand=True, fill=BOTH, side=TOP)
self.canvas = Canvas(diagramDummyFrame2,
scrollregion=(0, 0, 2000, 2000))
self.canvas.pack(expand=True, fill=BOTH, side=LEFT)
# don't forget to load the image !!!!
self.load('')
self.gen(WELLCOME)
diagramVScrol = Scrollbar(diagramDummyFrame2)
diagramVScrol.pack(expand=False, fill=Y, side=RIGHT)
self.canvas.config(yscrollcommand=diagramVScrol.set)
diagramVScrol.config(command=self.canvas.yview)
diagramHScrol = Scrollbar(diagramDummyFrame, orient=HORIZONTAL)
diagramHScrol.pack(expand=False, fill=X, side=BOTTOM)
self.canvas.config(xscrollcommand=diagramHScrol.set)
diagramHScrol.config(command=self.canvas.xview)
def load(self, fname=GIFFILE):
try:
if fname == '':
raise Exception('except will make an empty image')
self.graph = PhotoImage(file=fname)
except:
self.graph = PhotoImage()
self.canvas.create_image(0, 0, image=self.graph, anchor=NW)
def gen_dot(self, wellcome=''):
deleteFiles()
f = open('current.dot', 'w')
try:
if wellcome == '':
s = self.sqlInput.get(1.0, END)
a = []
for l in s.splitlines():
if l[0:3].lower() == 'sql':
a.append(l[3:])
else:
a.append(l)
s = "\n".join(a)
s = query2Dot(s, self.varAlgo.get())
else:
s = """graph
{
node [shape=record, fontsize=12];
ERROR [label="%s" color=white];
}""" % wellcome
except:
exc = sys.exc_info()[2]
readableExc = traceback.extract_tb(exc)[-1][-1]
s = """graph
{
node [shape=record, fontsize=12];
ERROR [label="%s" fontcolor=red color=white];
}""" % sys.exc_info()[1] # + ',' + readableExc
f.write(s)
f.close()
# algo moved to subGraphDotRunner()
# a = self.varAlgo.get()
def gen(self, wellcome=''):
self.gen_dot(wellcome)
s = dotRunner('one-pass', self.varDist.get())
self.load()
def export(self, wellcome=''):
exportRunner('one-pass', self.varDist.get())
# this is needed for revj initialization
initGlobalGrammar()
deleteFiles()
root = Tk()
app = GUIApp(root)
root.title('Reverse Snowflake Joins')
root.mainloop()