-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspansh.py
352 lines (287 loc) · 11.9 KB
/
spansh.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import json
import os
import pyperclip
import re
import requests
import threading
import time
import Tkinter as tk
import tkFont
# EDMC imports
from theme import theme
from config import config
HELP_MSG = """
Spansh-gin Howto
1) Plot your route on
https://spansh.co.uk
2) Copy the route URL above ^
3) Plot!
4) When you jump to a system
on the route, the plugin
will copy it for you: just
open the map and paste it
NOTES:
1) Routes get stale quickly:
replot on Spansh if the
plotting here fails.
2) If you feed a route and
you are not in a system on
that route, you may have
to copy the next system
manually as the plugin has
to assume the first system
in the route is the
desired one. To avoid this
replot the route on Spansh
from your current system.
This is ONLY needed if you
plot a route in the plugin
and you are NOT on the
route.
3) To refresh settings
changed from EDMC, you
have to close and reopen
Spansh-gin window.
"""
# TODO
# 7) switcha a logging
# DONE
# 2) mettere un messaggio se il plotting e' stale
# 3) mettere always on top (possibilmente accedendo alla config)
# 4) l'ultimo waypoint non si cancella quando viene raggiunto
# 5) quando aggiorna la textarea con qualcosa che e' in fondo la fa ripartire
# dall'inizio
# 6) controlla URL
# 8) metti degli switch di debug per disabilitare automaticamente gli import e
# le altre cagate di dbg
DEBUG = False
class SpanshViewer():
def __init__(self, parent):
self.parent = parent
self.current = ""
self.target = 0
self.arrived = False
self.systems = []
self.update_needed = False
self.timer = None
self.stop = False
self.lab = None
self.url = None
self.button = None
self.sem = threading.Semaphore()
def fetch_data(self, url):
if not DEBUG:
r = requests.get(url)
data = r.json()
else:
import parse
data = parse.data
return data
def button_callback(self):
url = self.url.get()
if url == "":
return
# we need to switch from a spansh user-link to an api one
url = url.split("?")[0]
url = url.replace("plotter", "api")
print url
if DEBUG:
url = "https://spansh.co.uk/api/results/zuppa"
# if not url.startswith("https://spansh.co.uk/api/results/"):
if not re.match(r"^https://(www\.)?spansh.co.uk/api/results/", url):
print "Wrong URL, not spansh!"
self.show_error("The URL you pasted is not a spansh.co.uk URL."
" Please copy a plot URL from https://spansh.co.uk"
" here before pressing the Plot! button")
return
data = self.fetch_data(url)
if ("status" not in data or data['status'] != "ok" or
"result" not in data or "system_jumps" not in data["result"]):
print "Did not get results!"
self.show_error("Failed to get results from the URL you pasted.\n\n"
"It's most likely due to an old link, please replot"
" the route on https://spansh.co.uk and paste a"
" fresh link here.")
return
# reset systems
self.systems = []
for sys in data['result']['system_jumps']:
self.systems.append(sys["system"])
# In case the route is plotted while already traveling on it and we are
# already on a waypoint, update the target
with self.sem:
if self.current != "":
if self.current not in self.systems:
print "Current system is not in "
self.show_error(("Warning! The current system (%s) is *not*"
"on the route. I copied the first system "
" for you, but make sure you are not "
"already some step ahead before jumping!")
% self.current)
else:
self.set_target_index(self.current)
# An update is always needed when reloading a plot
self.update_needed = True
def show_error(self, text):
# self.starz.configure(state=tk.NORMAL)
# self.starz.delete(1.0, tk.END)
# self.starz.insert("end", text)
# self.starz.see("1.0")
# self.starz.configure(state=tk.DISABLED)
ErrorPopup(self.app, text)
def update_starz(self):
with self.sem:
if self.update_needed:
print "Updating star list"
self.starz.configure(state=tk.NORMAL)
self.starz.delete(1.0, tk.END)
for sys_idx in xrange(len(self.systems)):
self.starz.insert("end", "%s\n" % self.systems[sys_idx])
if sys_idx < self.target or self.arrived:
self.starz.tag_add("done", "%s.0" % (sys_idx+1),
"%s.0" % (sys_idx+2))
elif sys_idx == self.target:
print "target: %d" % sys_idx
self.starz.tag_add("target", "%s.0" % (sys_idx+1),
"%s.0" % (sys_idx+2))
# copy system name to the clipboard
pyperclip.copy(self.systems[self.target])
# scroll so that the target line is
self.starz.see("%s.0" % (sys_idx+1))
if self.arrived: # scroll to the end
self.starz.see("%s.0" % (sys_idx+1))
self.starz.configure(state=tk.DISABLED)
self.update_needed = False
if not self.stop:
# recall self after x seconds
self.timer = threading.Timer(1.0, self.update_starz)
self.timer.start()
def set_target_index(self, system):
try:
current_idx = self.systems.index(system)
except ValueError:
print "System %s not found, doing nothing!" % system
return False
if current_idx < len(self.systems) - 1:
self.target = current_idx + 1
self.arrived = False
else: # we arrived at destination
self.target = current_idx
self.arrived = True
print "Set target to %d" % self.target
return True
def update_position(self, system):
with self.sem:
print "Updating position to %s" % system
self.current = system
self.update_needed = self.set_target_index(system)
# if an update is not needed, we possibly deviated or relogged in a
# system that is not in the route. So it's highly likely the
# clipboard has been invalidated, let's copy again the target
# system
if self.update_needed == False and len(self.systems) != 0:
pyperclip.copy(self.systems[self.target])
def close(self):
self.stop = True
if self.timer:
print "Waiting for timer thread to finish"
self.timer.join()
print "Done. Destroying window"
self.app.destroy()
print "Done."
def closed(self):
return self.stop
def start(self):
self.app = tk.Toplevel(self.parent,
background=theme.current['background'])
self.app.title("Spansh-gin - EDMC Spansh support")
self.lab = tk.Label(self.app, text = "Route URL: ",
background=theme.current['background'],
foreground=theme.current['foreground'])
self.lab.grid(row=0)
self.url = tk.Entry(self.app)
self.url.grid(row=0, column=1, sticky="W")
self.button = tk.Button(self.app, text="Plot!",
background=theme.current['background'],
foreground=theme.current['foreground'],
command=self.button_callback)
self.button.grid(row=0, column=2, sticky="W")
# Text area for stars
self.starz = tk.Text(self.app, height=30, width=30,
background=theme.current['background'],
foreground=theme.current['foreground'],
borderwidth=0)
# Prepare fonts for tags
target_font = tkFont.Font(self.starz, self.starz.cget("font"))
target_font.configure(weight="bold", underline=True)
done_font = tkFont.Font(self.starz, self.starz.cget("font"))
done_font.configure(weight="normal", overstrike=True)
# Prepare tags
self.starz.tag_configure("target", foreground="green4",
font=target_font)
self.starz.tag_configure("done", foreground="slate gray",
font=done_font)
self.starz.insert("end", HELP_MSG)
self.starz.configure(state=tk.DISABLED)
self.starz.grid(row=1, columnspan=3)
self.app.configure(background=theme.current['background'],
borderwidth=0)
self.app.protocol("WM_DELETE_WINDOW", self.close)
self.emptylabel = tk.Label(self.app,
background=theme.current['background'],
borderwidth=0,
height=max(self.button.winfo_height(),
self.url.winfo_height(),
self.lab.winfo_height()))
# Topmost
self.app.attributes('-topmost',
1 if config.getint('always_ontop') else 0)
# start the star list updater
self.update_starz()
class ErrorPopup:
def __init__(self, parent, message):
self.top = tk.Toplevel(parent)
self.top.title("Error! Spansh-gin")
self.top.configure(background=theme.current['background'],
borderwidth=0)
self.myLabel = tk.Label(self.top, text=message,
background=theme.current['background'],
foreground=theme.current['foreground'],
borderwidth=0, justify=tk.LEFT,
wraplength=2*parent.winfo_width())
self.myLabel.pack()
self.button = tk.Button(self.top, text="Ok", command=self.close)
self.button.pack()
self.top.lift(aboveThis=parent)
def close(self):
self.top.destroy()
def send_fake_positions(s):
time.sleep(10)
print "aggiorno"
s.update_position("Stuelou ER-L d8-145")
time.sleep(5)
s.update_position("Stuelou MD-I d10-137")
time.sleep(5)
s.update_position("Eol Prou OM-V d2-155")
time.sleep(5)
s.update_position("Colonia")
if __name__ == "__main__":
# launch with PYTHONPATH pointing to the EDMarketconnector dir
DEBUG = True
theme.current = {
"background" : "grey4", # OSX inactive dark titlebar color
"foreground" : "orange",
"activebackground" : "white",
"activeforeground" : "grey4",
"disabledforeground" : "grey3",
"highlight" : "grey2",
"font" : "TkDefaultFont",
}
app = tk.Tk()
s = SpanshViewer(app)
s.start()
s.update_position("Prua Phoe VD-A d1-1")
t = threading.Thread(target=send_fake_positions, args=(s, ))
t.start()
app.mainloop()
t.join()