-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht-time-gui.pyw
210 lines (204 loc) · 11.1 KB
/
t-time-gui.pyw
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
from zipfile import ZipFile
import importlib,threading,tkinter,tkinter.messagebox,tkinter.filedialog,tkinter.ttk
t_time = importlib.import_module("t-time")
"""t-time-ui
author: Andrew Bailey
A tk-based GUI for t-time, to easily select routes and stops
class exports:
GtfsProcessorGui
RouteSelecter
StopSelecter
LabeledMultiSelecter
UnlabeledMultiSelecter
"""
class GtfsProcessorGui(tkinter.Tk):
"""subclasses tkinter.Tk for main window"""
# title of window
_baseTitle="t-time"
# how much spacing around widgets
_padding=6
# how wide are buttons
_buttonWidth=16
def __init__(self):
"""extends tkinter.Tk.__init__"""
tkinter.Tk.__init__(self)
self.buttonrow=tkinter.ttk.Frame(self)
self.bNext=tkinter.ttk.Button(self.buttonrow,text="Next",command=self.next,width=self._buttonWidth)
self.bExit=tkinter.ttk.Button(self.buttonrow,text="Exit",command=self.close,width=self._buttonWidth)
self.bNext.pack(anchor=tkinter.S,side=tkinter.RIGHT,padx=GtfsProcessorGui._padding,pady=GtfsProcessorGui._padding)
self.bExit.pack(anchor=tkinter.S,side=tkinter.RIGHT,padx=GtfsProcessorGui._padding/2,pady=GtfsProcessorGui._padding)
self.buttonrow.pack(anchor=tkinter.SE,side=tkinter.BOTTOM,fill=tkinter.X,expand=False)
self.geometry("900x900")
def next(self):
"""delegates to the internal frame"""
self.mainframe.next()
def close(self):
"""close main window"""
self.destroy()
class RouteSelecter(tkinter.ttk.Frame):
"""subclasses tkinter.ttk.Frame for route selection"""
def __init__(self,master,inputZip):
"""extends tkinter.ttk.Frame.__init__"""
tkinter.ttk.Frame.__init__(self,master)
self.inputZip=inputZip
self.master=master
self.master.wm_title("Select routes - "+GtfsProcessorGui._baseTitle)
self.selectedRoutes,self.master.gtfs.selectedRoutes=self.master.gtfs.selectedRoutes,()
self.master.gtfs.readRoutes(inputZip)
self.master.gtfs.selectedRoutes=self.selectedRoutes
self.selectRoutes=LabeledMultiSelecter(self)
self.routenames=[route.referredTo for route in self.master.gtfs.routes.values()]
self.routenames.sort()
self.selectRoutes.populate(self.master.gtfs.agencyName,self.routenames,self.master.gtfs.selectedRoutes)
self.selectRoutes.selecter.configure(width=60)
self.selectRoutes.selecter.pack(fill=tkinter.BOTH,expand=True)
self.selectRoutes.pack(fill=tkinter.BOTH,expand=True,padx=GtfsProcessorGui._padding,pady=GtfsProcessorGui._padding)
self.pack(side=tkinter.TOP,fill=tkinter.BOTH,expand=True,padx=GtfsProcessorGui._padding,pady=GtfsProcessorGui._padding)
def next(self):
self.pbar=tkinter.ttk.Progressbar(self.master,orient="horizontal",mode="indeterminate")
self.pbar.pack(anchor=tkinter.S,side=tkinter.BOTTOM,fill=tkinter.X,expand=False,padx=GtfsProcessorGui._padding,pady=GtfsProcessorGui._padding)
self.pack(side=tkinter.TOP,padx=GtfsProcessorGui._padding,pady=GtfsProcessorGui._padding)
self.pbar.start()
threading.Thread(None,self.finishLogic).start()
def finishLogic(self):
"""finish logic based on selected routes, and passes control to StopSelecter"""
self.master.gtfs.selectedRoutes=self.selectRoutes.getSelected()
self.master.gtfs.routes={}
self.master.gtfs.readRoutes(inputZip)
self.master.gtfs.readTrips(inputZip)
self.master.gtfs.readStops(inputZip)
self.master.gtfs.readSchedules(inputZip)
self.exclude,self.master.gtfs.excludeStops=self.master.gtfs.excludeStops,{}
self.master.gtfs.buildDataModel()
self.master.gtfs.excludeStops=self.exclude
self.pbar.stop()
self.pbar.destroy()
self.destroy()
self.master.mainframe=StopSelecter(self.master)
class StopSelecter(tkinter.ttk.Notebook):
"""subclasses tkinter.Frame for stop selection"""
def __init__(self,master):
"""extends tkinter.ttk.Frame.__init__"""
tkinter.ttk.Notebook.__init__(self,master)
self.master=master
self.master.wm_title("Select stops - "+GtfsProcessorGui._baseTitle)
self.stopSelecters={}
self.selectedRoutes=(route for route in self.master.gtfs.routes.values() if len(self.master.gtfs.selectedRoutes) == 0 or route.referredTo in self.master.gtfs.selectedRoutes)
for route in self.selectedRoutes:
stops=route.getAllStops()
displayList=[]
includeList=[]
for stopid,stop in stops.items():
if stop.name in displayList: continue
displayList.append(stop.name)
if route.referredTo not in self.master.gtfs.excludeStops or stopid not in self.master.gtfs.excludeStops[route.referredTo]:
includeList.append(stop.name)
subframe=UnlabeledMultiSelecter(self).populate(displayList,includeList)
subframe.route=route
self.add(subframe,text="{0} ({1})".format(route.shortname,route.longname))
self.stopSelecters[route.referredTo]=subframe
self.pack(side=tkinter.TOP,fill=tkinter.BOTH,expand=True,padx=GtfsProcessorGui._padding,pady=GtfsProcessorGui._padding)
def next(self):
"""finish logic based on selected stops, writes HTML, and exits application"""
self.pbar=tkinter.ttk.Progressbar(self.master,orient="horizontal",mode="indeterminate")
self.pbar.pack(anchor=tkinter.S,side=tkinter.BOTTOM,fill=tkinter.X,expand=False,padx=GtfsProcessorGui._padding,pady=GtfsProcessorGui._padding)
self.pack(side=tkinter.TOP,padx=GtfsProcessorGui._padding,pady=GtfsProcessorGui._padding)
self.pbar.start()
threading.Thread(None,self.finishLogic).start()
def finishLogic(self):
for routeName,selecter in self.stopSelecters.items():
selected=selecter.getSelected()
excludeids=[]
for stopid,stop in selecter.route.getAllStops().items():
if stop.name not in selected:
excludeids.append(stopid)
self.master.gtfs.excludeStops[routeName]=excludeids
selecter.route.reset()
self.master.gtfs.readTrips(inputZip)
self.master.gtfs.readStops(inputZip)
self.master.gtfs.buildDataModel()
self.pbar.stop()
tkinter.messagebox.showinfo(GtfsProcessorGui._baseTitle,"Wrote {0} as final output. Have a nice trip!".format(self.master.gtfs.completeOutput()),parent=self.master)
_root.destroy()
class UnlabeledMultiSelecter(tkinter.ttk.Frame):
"""create an unlabeled multiple select listbox with scrollbar"""
def __init__(self,master=None):
"""extends tkinter.ttk.Frame.__init__"""
tkinter.ttk.Frame.__init__(self,master)
self.selecter=tkinter.Listbox(self,selectmode=tkinter.MULTIPLE,exportselection=0,activestyle=tkinter.NONE)
self.yScroll=tkinter.ttk.Scrollbar(self,orient=tkinter.VERTICAL)
self.yScroll.config(command=self.selecter.yview)
self.selecter.config(yscrollcommand=self.yScroll.set)
self.selecter.pack(fill=tkinter.BOTH,expand=True)
self.pack(padx=GtfsProcessorGui._padding,pady=GtfsProcessorGui._padding,side=tkinter.LEFT,fill=tkinter.BOTH,expand=True)
def populate(self,options,selected):
"""give this thing things to display, and things to select"""
for option in options:
self.selecter.insert(tkinter.END,option)
self.selecter.pack_forget()
self.pack_forget()
self.yScroll.pack(side=tkinter.RIGHT,fill=tkinter.Y)
self.selecter.pack(fill=tkinter.BOTH,expand=True)
self.pack(padx=GtfsProcessorGui._padding,pady=GtfsProcessorGui._padding,side=tkinter.LEFT,fill=tkinter.BOTH,expand=True)
for item in selected:
try:
x=options.index(item)
self.selecter.selection_set(first=x)
except ValueError:pass
return self
def getSelected(self):
"""return tuple of listbox options selected"""
return tuple([self.selecter.get(x) for x in self.selecter.curselection()])
class LabeledMultiSelecter(tkinter.ttk.LabelFrame):
"""create a labeled multiple select listbox with scrollbar"""
def __init__(self,master=None):
"""extends tkinter.ttk.LabelFrame.__init__"""
tkinter.ttk.LabelFrame.__init__(self,master)
self.selecter=tkinter.Listbox(self,selectmode=tkinter.MULTIPLE,exportselection=0,activestyle=tkinter.NONE)
self.yScroll=tkinter.ttk.Scrollbar(self,orient=tkinter.VERTICAL)
self.yScroll.config(command=self.selecter.yview)
self.selecter.config(yscrollcommand=self.yScroll.set)
self.selecter.pack(fill=tkinter.BOTH,expand=True)
self.pack(padx=GtfsProcessorGui._padding,pady=GtfsProcessorGui._padding,side=tkinter.LEFT,fill=tkinter.BOTH,expand=True)
def populate(self,label,options,selected):
"""give this thing a label, things to display, and things to select"""
self.config(text=label)
for option in options:
self.selecter.insert(tkinter.END,option)
self.selecter.pack_forget()
self.pack_forget()
self.yScroll.pack(side=tkinter.RIGHT,fill=tkinter.Y)
self.selecter.pack(fill=tkinter.X,expand=True)
self.pack(padx=GtfsProcessorGui._padding,pady=GtfsProcessorGui._padding,side=tkinter.LEFT)
for item in selected:
try:
x=options.index(item)
self.selecter.selection_set(first=x)
except ValueError:pass
return self
def getSelected(self):
"""return tuple of listbox options selected"""
return tuple([self.selecter.get(x) for x in self.selecter.curselection()])
if "__main__"==__name__:
_root=GtfsProcessorGui()
_root.wm_title(GtfsProcessorGui._baseTitle)
zipname=tkinter.filedialog.askopenfilename(parent=_root,title="Select GTFS zip file",filetypes=(("Zip files","*.zip"),))
while ""==zipname:
if not tkinter.messagebox.askyesno("Select GTFS zip file","A GTFS zip file is required. Continue?",parent=_root):
_root.close()
exit()
zipname=tkinter.filedialog.askopenfilename(parent=_root,title="Select GTFS zip file",filetypes=(("Zip files","*.zip"),))
with ZipFile(zipname,'r') as inputZip:
_root.gtfs=t_time.GtfsProcessor()
_root.gtfs.readAgencyName(inputZip)
_root.gtfs.outputName=tkinter.filedialog.asksaveasfilename(parent=_root,title="Select output file",initialfile=_root.gtfs.outputName,filetypes=(("HTML files","*.html"),))
while ""==_root.gtfs.outputName:
_root.gtfs.outputName=None
_root.gtfs.readAgencyName(inputZip)
if not tkinter.messagebox.askyesno("Select output file","An output file will be written, and settings can be read if it already exists. Continue?",parent=_root):
_root.close()
exit()
_root.gtfs.outputName=tkinter.filedialog.asksaveasfilename(parent=_root,title="Select output file",initialfile=_root.gtfs.outputName,filetypes=(("HTML files","*.html"),))
_root.gtfs.readSettings()
_root.mainframe=RouteSelecter(_root,inputZip)
_root.mainloop()