-
Notifications
You must be signed in to change notification settings - Fork 241
/
pmca-gui.py
240 lines (180 loc) · 6.26 KB
/
pmca-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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env python3
"""A simple gui interface"""
from __future__ import print_function
import sys
import traceback
import config
from pmca.commands.usb import *
from pmca.ui import *
if getattr(sys, 'frozen', False):
from frozenversion import version
else:
version = None
class PrintRedirector(object):
"""Redirect writes to a function"""
def __init__(self, func, parent=None):
self.func = func
self.parent = parent
def write(self, str):
if self.parent:
self.parent.write(str)
self.func(str)
def flush(self):
self.parent.flush()
class AppLoadTask(BackgroundTask):
def doBefore(self):
self.ui.setAppList([])
self.ui.appLoadButton.config(state=DISABLED)
def do(self, arg):
try:
print('')
return listApps(config.appengineServer)
except Exception:
traceback.print_exc()
def doAfter(self, result):
if result:
self.ui.setAppList(result)
self.ui.appLoadButton.config(state=NORMAL)
class InfoTask(BackgroundTask):
"""Task to run infoCommand()"""
def doBefore(self):
self.ui.infoButton.config(state=DISABLED)
def do(self, arg):
try:
print('')
infoCommand(config.appengineServer)
except Exception:
traceback.print_exc()
def doAfter(self, result):
self.ui.infoButton.config(state=NORMAL)
class InstallTask(BackgroundTask):
"""Task to run installCommand()"""
def doBefore(self):
self.ui.installButton.config(state=DISABLED)
return self.ui.getMode(), self.ui.getSelectedApk(), self.ui.getSelectedApp()
def do(self, args):
(mode, apkFilename, app) = args
try:
print('')
if mode == self.ui.MODE_APP and app:
installCommand(config.appengineServer, None, None, app.package)
elif mode == self.ui.MODE_APK and apkFilename:
with open(apkFilename, 'rb') as f:
installCommand(config.appengineServer, None, f)
else:
installCommand(config.appengineServer)
except Exception:
traceback.print_exc()
def doAfter(self, result):
self.ui.installButton.config(state=NORMAL)
class FirmwareUpdateTask(BackgroundTask):
"""Task to run firmwareUpdateCommand()"""
def doBefore(self):
self.ui.fwUpdateButton.config(state=DISABLED)
return self.ui.getSelectedDat()
def do(self, datFile):
try:
if datFile:
print('')
with open(datFile, 'rb') as f:
firmwareUpdateCommand(f)
except Exception:
traceback.print_exc()
def doAfter(self, result):
self.ui.fwUpdateButton.config(state=NORMAL)
class InstallerUi(UiRoot):
"""Main window"""
def __init__(self, title):
UiRoot.__init__(self)
self.title(title)
self.geometry('450x500')
tabs = Notebook(self, padding=5)
tabs.pack(fill=X)
tabs.add(InfoFrame(self, padding=10), text='Camera info')
tabs.add(InstallerFrame(self, padding=10), text='Install app')
tabs.add(FirmwareFrame(self, padding=10), text='Update firmware')
self.logText = ScrollingText(self)
self.logText.text.configure(state=DISABLED)
self.logText.pack(fill=BOTH, expand=True)
self.redirectStreams()
def log(self, msg):
self.logText.text.configure(state=NORMAL)
self.logText.text.insert(END, msg)
self.logText.text.configure(state=DISABLED)
self.logText.text.see(END)
def redirectStreams(self):
for stream in ['stdout', 'stderr']:
setattr(sys, stream, PrintRedirector(lambda str: self.run(lambda: self.log(str)), getattr(sys, stream)))
class InfoFrame(UiFrame):
def __init__(self, parent, **kwargs):
UiFrame.__init__(self, parent, **kwargs)
self.infoButton = Button(self, text='Get camera info', command=InfoTask(self).run, padding=5)
self.infoButton.pack(fill=X)
class InstallerFrame(UiFrame):
MODE_APP = 0
MODE_APK = 1
def __init__(self, parent, **kwargs):
UiFrame.__init__(self, parent, **kwargs)
self.modeVar = IntVar()
self.modeVar.set(self.MODE_APP)
appFrame = Labelframe(self, padding=5)
appFrame['labelwidget'] = Radiobutton(appFrame, text='Select an app from the app list', variable=self.modeVar, value=self.MODE_APP)
appFrame.pack(fill=X)
self.appCombo = Combobox(appFrame, state='readonly')
self.appCombo.bind('<<ComboboxSelected>>', lambda e: self.modeVar.set(self.MODE_APP))
self.appCombo.pack(side=LEFT, fill=X, expand=True)
self.setAppList([])
self.appLoadButton = Button(appFrame, text='Refresh', command=AppLoadTask(self).run)
self.appLoadButton.pack()
apkFrame = Labelframe(self, padding=5)
apkFrame['labelwidget'] = Radiobutton(apkFrame, text='Select an apk', variable=self.modeVar, value=self.MODE_APK)
apkFrame.pack(fill=X)
self.apkFile = Entry(apkFrame)
self.apkFile.pack(side=LEFT, fill=X, expand=True)
self.apkSelectButton = Button(apkFrame, text='Open apk...', command=self.openApk)
self.apkSelectButton.pack()
self.installButton = Button(self, text='Install selected app', command=InstallTask(self).run, padding=5)
self.installButton.pack(fill=X, pady=(5, 0))
self.run(AppLoadTask(self).run)
def getMode(self):
return self.modeVar.get()
def openApk(self):
fn = askopenfilename(filetypes=[('Apk files', '.apk'), ('All files', '.*')])
if fn:
self.apkFile.delete(0, END)
self.apkFile.insert(0, fn)
self.modeVar.set(self.MODE_APK)
def getSelectedApk(self):
return self.apkFile.get()
def setAppList(self, apps):
self.appList = apps
self.appCombo['values'] = [''] + [app.name for app in apps]
self.appCombo.current(0)
def getSelectedApp(self):
if self.appCombo.current() > 0:
return self.appList[self.appCombo.current() - 1]
class FirmwareFrame(UiFrame):
def __init__(self, parent, **kwargs):
UiFrame.__init__(self, parent, **kwargs)
datFrame = Labelframe(self, padding=5)
datFrame['labelwidget'] = Label(datFrame, text='Firmware file')
datFrame.pack(fill=X)
self.datFile = Entry(datFrame)
self.datFile.pack(side=LEFT, fill=X, expand=True)
self.datSelectButton = Button(datFrame, text='Open...', command=self.openDat)
self.datSelectButton.pack()
self.fwUpdateButton = Button(self, text='Update firmware', command=FirmwareUpdateTask(self).run, padding=5)
self.fwUpdateButton.pack(fill=X, pady=(5, 0))
def openDat(self):
fn = askopenfilename(filetypes=[('Firmware files', '.dat'), ('All files', '.*')])
if fn:
self.datFile.delete(0, END)
self.datFile.insert(0, fn)
def getSelectedDat(self):
return self.datFile.get()
def main():
"""Gui main"""
ui = InstallerUi('pmca-gui' + (' ' + version if version else ''))
ui.mainloop()
if __name__ == '__main__':
main()