forked from thecatfather/sumo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlauncher.py
executable file
·218 lines (180 loc) · 7.16 KB
/
launcher.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
213
214
215
216
217
218
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
# Copyright (C) 2010-2018 German Aerospace Center (DLR) and others.
# This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v2.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v20.html
# SPDX-License-Identifier: EPL-2.0
# @file launcher.py
# @author Jakob Erdmann
# @date 2015-01-18
# @version $Id$
from __future__ import absolute_import
import os
import re
import subprocess
from Tkinter import *
from ttk import *
import tkFileDialog
from sumolib.options import Option, readOptions
THISDIR = os.path.dirname(__file__)
BINDIR = os.path.join(THISDIR, '..', 'bin')
APPLICATIONS = ['netconvert', 'netgenerate', 'polyconvert', 'od2trips', 'duarouter', 'jtrrouter',
'dfrouter', 'marouter', 'sumo', 'sumo-gui', 'activitygen']
class ResizingCanvas(Canvas):
""" a subclass of Canvas for dealing with resizing of windows
http://stackoverflow.com/questions/22835289/how-to-get-tkinter-canvas-to-dynamically-resize-to-window-width
"""
def __init__(self, parent, **kwargs):
Canvas.__init__(self, parent, **kwargs)
self.bind("<Configure>", self.on_resize)
self.height = self.winfo_reqheight()
self.width = self.winfo_reqwidth()
def on_resize(self, event):
# determine the ratio of old width/height to new width/height
wscale = float(event.width) / self.width
hscale = float(event.height) / self.height
self.width = event.width - 2
self.height = event.height - 2
# print "on_resize %s %s %s %s" % (self.width, self.height,
# self.winfo_reqwidth(), self.winfo_reqheight())
# resize the canvas
self.config(width=self.width, height=self.height)
# rescale all the objects tagged with the "all" tag
self.scale("all", 0, 0, wscale, hscale)
class ScrollableFrame(Frame):
def __init__(self, root):
Frame.__init__(self, root)
self.canvas = ResizingCanvas(self, borderwidth=0)
self.frame = Frame(self.canvas)
self.vsb = Scrollbar(
self, orient="vertical", command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.vsb.set)
self.vsb.pack(side="right", fill="y")
self.canvas.pack(side="left", fill="both", expand=True)
self.canvas.create_window(
(4, 4), window=self.frame, anchor="nw", tags="self.frame")
self.frame.bind("<Configure>", self.OnFrameConfigure)
def OnFrameConfigure(self, event):
'''Reset the scroll region to encompass the inner frame'''
# print "OnFrameConfigure"
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
def buildValueWidget(frame, optType):
if optType == "FOO":
pass
else:
var = StringVar()
widget = Entry(frame, textvariable=var)
return widget, var
class Launcher:
def __init__(self, root, app, appOptions):
self.title_prefix = "SUMO Application launcher"
self.root = root
self.appVar = StringVar()
self.appVar.set(app)
self.appOptions = appOptions
self.optionValues = {}
self.root.title(self.title_prefix)
self.root.minsize(700, 200)
numButtons = self.mainButtons()
for i in range(numButtons):
root.columnconfigure(i, weight=1)
root.rowconfigure(0, weight=10)
root.rowconfigure(1, weight=1)
sFrame = ScrollableFrame(root)
sFrame.grid(row=1, column="0", columnspan=numButtons, sticky="NSEW")
self.optFrame = sFrame.frame
self.buildAppOptions(appOptions)
# define options for opening or saving a file
self.file_opt = options = {}
self.filedir = os.getcwd()
options['defaultextension'] = 'cfg'
options['filetypes'] = [('all files', '.*')]
options['initialdir'] = self.filedir
options['parent'] = root
def buildAppOptions(self, appOptions):
NAME, VALUE, HELP = range(3)
row = 0
for o in appOptions:
row += 1
Label(self.optFrame, text=o.name).grid(
row=row, column=NAME, sticky="NW")
widget, var = buildValueWidget(self.optFrame, o.type)
self.optionValues[o.name] = var
widget.grid(row=row, column=VALUE, sticky="NW")
Label(self.optFrame, text=o.help, justify=LEFT).grid(
row=row, column=HELP, sticky="NW")
def mainButtons(self):
row = 0
col = 0
self.buttons = []
mb = Menubutton(self.root, text="Select Application")
mb.menu = Menu(mb, tearoff=0)
mb["menu"] = mb.menu
for app in APPLICATIONS:
mb.menu.add_radiobutton(label=app, variable=self.appVar,
command=self.onSelectApp)
mb.grid(row=row, column=col, sticky="NEW")
col += 1
self.buttons.append(mb)
otherButtons = (
("Run %12s" % self.appVar.get(), self.runApp),
("load Config", self.loadCfg),
("Save Config", self.saveCfg),
("Save Config as", self.saveCfgAs),
("Quit", self.root.quit),
)
for text, command in otherButtons:
self.buttons.append(Button(self.root, text=text, command=command))
self.buttons[-1].grid(row=row, column=col, sticky="NEW")
col += 1
return len(self.buttons)
def onSelectApp(self):
self.buttons[1].configure(text="Run %12s" % self.appVar.get())
def runApp(self):
subprocess.call(os.path.join(BINDIR, self.appVar.get()))
def loadCfg(self):
self.file_opt['title'] = 'Load configuration file'
filename = tkFileDialog.askopenfilename(**self.file_opt)
self.root.title(self.title_prefix + " " + filename)
self.loadedOptions = readOptions(filename)
for o in self.loadedOptions:
self.optionValues[o.name].set(o.value)
def saveCfg(self):
pass
def saveCfgAs(self):
pass
def parse_help(app):
binary = os.path.join(BINDIR, app)
reOpt = re.compile("--([^ ]*) (\w*) (.*$)")
helpstring = subprocess.check_output([binary, '--help'])
options = []
optName = None
optHelp = ""
for line in helpstring.split(os.linesep):
if '--' in line:
if optName is not None:
options.append(Option(optName, None, optType, optHelp))
match = reOpt.search(line)
if match is not None:
optName = match.group(1)
optType = match.group(2)
optHelp = match.group(3).strip()
elif " " in line:
optHelp += "\n" + line.strip()
if optName is not None:
if optType == '':
optType = 'BOOL'
options.append(Option(optName, None, optType, optHelp))
return options
def main():
app = "netconvert"
appOptions = parse_help(app)
# appOptions = []
root = Tk()
app = Launcher(root, app, appOptions)
root.mainloop()
if __name__ == "__main__":
main()