forked from EDCD/EDMarketConnector
/
EDMarketConnector.py
executable file
·290 lines (244 loc) · 13 KB
/
EDMarketConnector.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from sys import platform
import json
from os import mkdir
from os.path import expanduser, isdir, join
import re
import requests
from time import time, localtime, strftime
import Tkinter as tk
import ttk
if __debug__:
from traceback import print_exc
import companion
import bpc
import td
import eddn
import loadout
import flightlog
import stats
import prefs
from config import appname, applongname, config
class AppWindow:
def __init__(self, master):
self.holdofftime = config.getint('querytime') + companion.holdoff
self.session = companion.Session()
self.w = master
self.w.title(applongname)
self.w.rowconfigure(0, weight=1)
self.w.columnconfigure(0, weight=1)
if platform == 'win32':
self.w.wm_iconbitmap(default='EDMarketConnector.ico')
elif platform == 'linux2':
from PIL import Image, ImageTk
icon = ImageTk.PhotoImage(Image.open("EDMarketConnector.png"))
self.w.tk.call('wm', 'iconphoto', self.w, '-default', icon)
style = ttk.Style()
style.theme_use('clam')
frame = ttk.Frame(self.w)
frame.grid(sticky=tk.NSEW)
frame.columnconfigure(1, weight=1)
frame.rowconfigure(4, weight=1)
ttk.Label(frame, text="Cmdr:").grid(row=0, column=0, sticky=tk.W)
ttk.Label(frame, text="System:").grid(row=1, column=0, sticky=tk.W)
ttk.Label(frame, text="Station:").grid(row=2, column=0, sticky=tk.W)
self.cmdr = ttk.Label(frame, width=-20)
self.system = ttk.Label(frame, width=-20)
self.station = ttk.Label(frame, width=-20)
self.button = ttk.Button(frame, text='Update', command=self.getandsend, default=tk.ACTIVE, state=tk.DISABLED)
self.status = ttk.Label(frame, width=-25)
self.w.bind('<Return>', self.getandsend)
self.w.bind('<KP_Enter>', self.getandsend)
self.cmdr.grid(row=0, column=1, sticky=tk.W)
self.system.grid(row=1, column=1, sticky=tk.W)
self.station.grid(row=2, column=1, sticky=tk.W)
self.button.grid(row=3, column=0, columnspan=2, sticky=tk.NSEW)
self.status.grid(row=4, column=0, columnspan=2, sticky=tk.SW)
for child in frame.winfo_children():
child.grid_configure(padx=5, pady=(platform=='darwin' and 3 or 2))
menubar = tk.Menu()
if platform=='darwin':
from Foundation import NSBundle
# https://www.tcl.tk/man/tcl/TkCmd/menu.htm
apple_menu = tk.Menu(menubar, name='apple')
apple_menu.add_command(label="About %s" % applongname, command=lambda:self.w.call('tk::mac::standardAboutPanel'))
apple_menu.add_command(label="Statistics", command=lambda:stats.StatsDialog(self.w, self.session))
apple_menu.add_command(label="Check for Update", command=lambda:self.updater.checkForUpdates())
menubar.add_cascade(menu=apple_menu)
window_menu = tk.Menu(menubar, name='window')
menubar.add_cascade(menu=window_menu)
# https://www.tcl.tk/man/tcl/TkCmd/tk_mac.htm
self.w.createcommand('tkAboutDialog', lambda:self.w.call('tk::mac::standardAboutPanel'))
self.w.createcommand("::tk::mac::Quit", self.onexit)
self.w.createcommand("::tk::mac::ShowPreferences", lambda:prefs.PreferencesDialog(self.w, self.login))
self.w.createcommand("::tk::mac::ReopenApplication", self.w.deiconify) # click on app in dock = restore
self.w.protocol("WM_DELETE_WINDOW", self.w.withdraw) # close button shouldn't quit app
else:
file_menu = tk.Menu(menubar, tearoff=tk.FALSE)
file_menu.add_command(label="Statistics", command=lambda:stats.StatsDialog(self.w, self.session))
file_menu.add_command(label="Check for Update", command=lambda:self.updater.checkForUpdates())
file_menu.add_command(label="Settings", command=lambda:prefs.PreferencesDialog(self.w, self.login))
file_menu.add_separator()
file_menu.add_command(label="Exit", command=self.onexit)
menubar.add_cascade(label="File", menu=file_menu)
self.w.protocol("WM_DELETE_WINDOW", self.onexit)
if platform == 'linux2':
# Fix up menu to use same styling as everything else
(fg, bg, afg, abg) = (style.lookup('TLabel.label', 'foreground'),
style.lookup('TLabel.label', 'background'),
style.lookup('TButton.label', 'foreground', ['active']),
style.lookup('TButton.label', 'background', ['active']))
menubar.configure( fg = fg, bg = bg, activeforeground = afg, activebackground = abg)
file_menu.configure(fg = fg, bg = bg, activeforeground = afg, activebackground = abg)
self.w['menu'] = menubar
# update geometry
if config.get('geometry'):
match = re.match('\+([\-\d]+)\+([\-\d]+)', config.get('geometry'))
if match and (platform!='darwin' or int(match.group(2))>0): # http://core.tcl.tk/tk/tktview/c84f660833546b1b84e7
self.w.geometry(config.get('geometry'))
self.w.update_idletasks()
self.w.wait_visibility()
(w, h) = (self.w.winfo_width(), self.w.winfo_height())
self.w.minsize(w, h) # Minimum size = initial size
if platform != 'linux2': # update_idletasks() doesn't allow for the menubar on Linux
self.w.maxsize(-1, h) # Maximum height = initial height
# First run
if not config.get('username') or not config.get('password'):
prefs.PreferencesDialog(self.w, self.login)
else:
self.login()
# Load updater after UI creation (for WinSparkle)
import update
self.updater = update.Updater(self.w)
self.w.bind_all('<<Quit>>', self.onexit) # user-generated
# call after credentials have changed
def login(self):
self.status['text'] = 'Logging in...'
self.button['state'] = tk.DISABLED
self.w.update_idletasks()
try:
self.session.login(config.get('username'), config.get('password'))
self.status['text'] = ''
# Try to obtain exclusive lock on flight log ASAP
if config.getint('output') & config.OUT_LOG:
try:
flightlog.openlog()
except Exception as e:
if __debug__: print_exc()
self.status['text'] = str(e)
except companion.VerificationRequired:
# don't worry about authentication now - prompt on query
self.status['text'] = ''
except companion.ServerError as e:
self.status['text'] = str(e)
except Exception as e:
if __debug__: print_exc()
self.status['text'] = str(e)
self.cooldown()
# callback after verification code
def verify(self, code):
try:
self.session.verify(code)
except Exception as e:
if __debug__: print_exc()
self.status['text'] = str(e)
else:
return self.getandsend() # try again
def getandsend(self, event=None, retrying=False):
if not retrying:
if time() < self.holdofftime: return # Was invoked by Return key while in cooldown
self.cmdr['text'] = self.system['text'] = self.station['text'] = ''
self.status['text'] = 'Fetching station data...'
self.button['state'] = tk.DISABLED
self.w.update_idletasks()
try:
querytime = int(time())
data = self.session.query()
self.cmdr['text'] = data.get('commander') and data.get('commander').get('name') or ''
self.system['text'] = data.get('lastSystem') and data.get('lastSystem').get('name') or ''
self.station['text'] = data.get('commander') and data.get('commander').get('docked') and data.get('lastStarport') and data.get('lastStarport').get('name') or '-'
config.set('querytime', querytime)
self.holdofftime = querytime + companion.holdoff
# Validation
if not data.get('commander') or not data['commander'].get('name','').strip():
self.status['text'] = "Who are you?!" # Shouldn't happen
elif not data.get('lastSystem') or not data['lastSystem'].get('name','').strip() or not data.get('lastStarport') or not data['lastStarport'].get('name','').strip():
self.status['text'] = "Where are you?!" # Shouldn't happen
elif not data.get('ship') or not data['ship'].get('modules') or not data['ship'].get('name','').strip():
self.status['text'] = "What are you flying?!" # Shouldn't happen
elif (config.getint('output') & config.OUT_EDDN) and not data['lastStarport'].get('ships') and not retrying:
# API is flakey about shipyard info - retry if missing (<1s is usually sufficient - 2.5s for margin).
self.w.after(2500, lambda:self.getandsend(retrying=True))
return
else:
if __debug__ and retrying: print data['lastStarport'].get('ships') and 'Retry for shipyard - Success' or 'Retry for shipyard - Fail'
# stuff we can do when not docked
if __debug__: # Recording
with open('%s%s.%s.json' % (data['lastSystem']['name'], data['commander'].get('docked') and '.'+data['lastStarport']['name'] or '', strftime('%Y-%m-%dT%H.%M.%S', localtime())), 'wt') as h:
h.write(json.dumps(data, indent=2, sort_keys=True))
if config.getint('output') & config.OUT_LOG:
flightlog.export(data)
if config.getint('output') & config.OUT_SHIP:
loadout.export(data)
if not (config.getint('output') & (config.OUT_CSV|config.OUT_TD|config.OUT_BPC|config.OUT_EDDN)):
# no further output requested
self.status['text'] = strftime('Last updated at %H:%M:%S', localtime(querytime))
elif not data['commander'].get('docked'):
self.status['text'] = "You're not docked at a station!"
else:
if data['lastStarport'].get('commodities'):
# Fixup anomalies in the commodity data
self.session.fixup(data['lastStarport']['commodities'])
if config.getint('output') & config.OUT_CSV:
bpc.export(data, True)
if config.getint('output') & config.OUT_TD:
td.export(data)
if config.getint('output') & config.OUT_BPC:
bpc.export(data, False)
if config.getint('output') & config.OUT_EDDN:
if data['lastStarport'].get('commodities') or data['lastStarport'].get('modules') or data['lastStarport'].get('ships'):
self.status['text'] = 'Sending data to EDDN...'
self.w.update_idletasks()
eddn.export(data)
self.status['text'] = strftime('Last updated at %H:%M:%S', localtime(querytime))
else:
self.status['text'] = "Station doesn't have anything!"
elif not data['lastStarport'].get('commodities'):
self.status['text'] = "Station doesn't have a market!"
else:
self.status['text'] = strftime('Last updated at %H:%M:%S', localtime(querytime))
except companion.VerificationRequired:
return prefs.AuthenticationDialog(self.w, self.verify)
# Companion API problem
except companion.ServerError as e:
self.status['text'] = str(e)
except requests.exceptions.ConnectionError as e:
if __debug__: print_exc()
self.status['text'] = "Error: Can't connect to EDDN"
except requests.exceptions.Timeout as e:
if __debug__: print_exc()
self.status['text'] = "Error: Connection to EDDN timed out"
except Exception as e:
if __debug__: print_exc()
self.status['text'] = str(e)
self.cooldown()
def cooldown(self):
if time() < self.holdofftime:
self.button['text'] = 'cool down %ds' % (self.holdofftime - time())
self.w.after(1000, self.cooldown)
else:
self.button['text'] = 'Update'
self.button['state'] = tk.NORMAL
def onexit(self, event=None):
if platform!='darwin' or self.w.winfo_rooty()>0: # http://core.tcl.tk/tk/tktview/c84f660833546b1b84e7
config.set('geometry', '+{1}+{2}'.format(*self.w.geometry().split('+')))
config.close()
self.session.close()
self.w.destroy()
# Run the app
if __name__ == "__main__":
root = tk.Tk()
app = AppWindow(root)
root.mainloop()