1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-06-05 18:03:17 +03:00

autoformatted code with autopep8

This commit is contained in:
A_D 2020-08-18 10:19:08 +02:00
parent 0f12959d53
commit 37c53e233e
No known key found for this signature in database
GPG Key ID: 4BE9EB7DF45076C4

View File

@ -31,17 +31,17 @@ EDSM_POLL = 0.1
_TIMEOUT = 20 _TIMEOUT = 20
this = sys.modules[__name__] # For holding module globals this = sys.modules[__name__] # For holding module globals
this.session = requests.Session() this.session = requests.Session()
this.queue = Queue() # Items to be sent to EDSM by worker thread this.queue = Queue() # Items to be sent to EDSM by worker thread
this.discardedEvents = [] # List discarded events from EDSM this.discardedEvents = [] # List discarded events from EDSM
this.lastlookup = False # whether the last lookup succeeded this.lastlookup = False # whether the last lookup succeeded
# Game state # Game state
this.multicrew = False # don't send captain's ship info to EDSM while on a crew this.multicrew = False # don't send captain's ship info to EDSM while on a crew
this.coordinates = None this.coordinates = None
this.newgame = False # starting up - batch initial burst of events this.newgame = False # starting up - batch initial burst of events
this.newgame_docked = False # starting up while docked this.newgame_docked = False # starting up while docked
this.navbeaconscan = 0 # batch up burst of Scan events after NavBeaconScan this.navbeaconscan = 0 # batch up burst of Scan events after NavBeaconScan
this.system_link = None this.system_link = None
this.system = None this.system = None
@ -53,8 +53,6 @@ this.station_marketid = None # Frontier MarketID
STATION_UNDOCKED: str = '×' # "Station" name to display when not docked = U+00D7 STATION_UNDOCKED: str = '×' # "Station" name to display when not docked = U+00D7
# Main window clicks # Main window clicks
def system_url(system_name): def system_url(system_name):
if this.system_address: if this.system_address:
@ -65,6 +63,7 @@ def system_url(system_name):
return '' return ''
def station_url(system_name, station_name): def station_url(system_name, station_name):
if system_name and station_name: if system_name and station_name:
return requests.utils.requote_uri(f'https://www.edsm.net/en/system?systemName={system_name}&stationName={station_name}') return requests.utils.requote_uri(f'https://www.edsm.net/en/system?systemName={system_name}&stationName={station_name}')
@ -80,10 +79,10 @@ def station_url(system_name, station_name):
def plugin_start3(plugin_dir): def plugin_start3(plugin_dir):
# Can't be earlier since can only call PhotoImage after window is created # Can't be earlier since can only call PhotoImage after window is created
this._IMG_KNOWN = tk.PhotoImage(data = 'R0lGODlhEAAQAMIEAFWjVVWkVWS/ZGfFZ////////////////yH5BAEKAAQALAAAAAAQABAAAAMvSLrc/lAFIUIkYOgNXt5g14Dk0AQlaC1CuglM6w7wgs7rMpvNV4q932VSuRiPjQQAOw==') # green circle this._IMG_KNOWN = tk.PhotoImage(data='R0lGODlhEAAQAMIEAFWjVVWkVWS/ZGfFZ////////////////yH5BAEKAAQALAAAAAAQABAAAAMvSLrc/lAFIUIkYOgNXt5g14Dk0AQlaC1CuglM6w7wgs7rMpvNV4q932VSuRiPjQQAOw==') # green circle
this._IMG_UNKNOWN = tk.PhotoImage(data = 'R0lGODlhEAAQAKEDAGVLJ+ddWO5fW////yH5BAEKAAMALAAAAAAQABAAAAItnI+pywYRQBtA2CtVvTwjDgrJFlreEJRXgKSqwB5keQ6vOKq1E+7IE5kIh4kCADs=') # red circle this._IMG_UNKNOWN = tk.PhotoImage(data='R0lGODlhEAAQAKEDAGVLJ+ddWO5fW////yH5BAEKAAMALAAAAAAQABAAAAItnI+pywYRQBtA2CtVvTwjDgrJFlreEJRXgKSqwB5keQ6vOKq1E+7IE5kIh4kCADs=') # red circle
this._IMG_NEW = tk.PhotoImage(data = 'R0lGODlhEAAQAMZwANKVHtWcIteiHuiqLPCuHOS1MN22ZeW7ROG6Zuu9MOy+K/i8Kf/DAuvCVf/FAP3BNf/JCf/KAPHHSv7ESObHdv/MBv/GRv/LGP/QBPXOPvjPQfjQSvbRSP/UGPLSae7Sfv/YNvLXgPbZhP7dU//iI//mAP/jH//kFv7fU//fV//ebv/iTf/iUv/kTf/iZ/vgiP/hc/vgjv/jbfriiPriiv7ka//if//jd//sJP/oT//tHv/mZv/sLf/rRP/oYv/rUv/paP/mhv/sS//oc//lkf/mif/sUf/uPv/qcv/uTv/uUv/vUP/qhP/xP//pm//ua//sf//ubf/wXv/thv/tif/slv/tjf/smf/yYP/ulf/2R//2Sv/xkP/2av/0gP/ylf/2df/0i//0j//0lP/5cP/7a//1p//5gf/7ev/3o//2sf/5mP/6kv/2vP/3y//+jP///////////////////////////////////////////////////////////////yH5BAEKAH8ALAAAAAAQABAAAAePgH+Cg4SFhoJKPIeHYT+LhVppUTiPg2hrUkKPXWdlb2xHJk9jXoNJQDk9TVtkYCUkOy4wNjdGfy1UXGJYOksnPiwgFwwYg0NubWpmX1ArHREOFYUyWVNIVkxXQSoQhyMoNVUpRU5EixkcMzQaGy8xhwsKHiEfBQkSIg+GBAcUCIIBBDSYYGiAAUMALFR6FAgAOw==') this._IMG_NEW = tk.PhotoImage(data='R0lGODlhEAAQAMZwANKVHtWcIteiHuiqLPCuHOS1MN22ZeW7ROG6Zuu9MOy+K/i8Kf/DAuvCVf/FAP3BNf/JCf/KAPHHSv7ESObHdv/MBv/GRv/LGP/QBPXOPvjPQfjQSvbRSP/UGPLSae7Sfv/YNvLXgPbZhP7dU//iI//mAP/jH//kFv7fU//fV//ebv/iTf/iUv/kTf/iZ/vgiP/hc/vgjv/jbfriiPriiv7ka//if//jd//sJP/oT//tHv/mZv/sLf/rRP/oYv/rUv/paP/mhv/sS//oc//lkf/mif/sUf/uPv/qcv/uTv/uUv/vUP/qhP/xP//pm//ua//sf//ubf/wXv/thv/tif/slv/tjf/smf/yYP/ulf/2R//2Sv/xkP/2av/0gP/ylf/2df/0i//0j//0lP/5cP/7a//1p//5gf/7ev/3o//2sf/5mP/6kv/2vP/3y//+jP///////////////////////////////////////////////////////////////yH5BAEKAH8ALAAAAAAQABAAAAePgH+Cg4SFhoJKPIeHYT+LhVppUTiPg2hrUkKPXWdlb2xHJk9jXoNJQDk9TVtkYCUkOy4wNjdGfy1UXGJYOksnPiwgFwwYg0NubWpmX1ArHREOFYUyWVNIVkxXQSoQhyMoNVUpRU5EixkcMzQaGy8xhwsKHiEfBQkSIg+GBAcUCIIBBDSYYGiAAUMALFR6FAgAOw==')
this._IMG_ERROR = tk.PhotoImage(data = 'R0lGODlhEAAQAKEBAAAAAP///////////yH5BAEKAAIALAAAAAAQABAAAAIwlBWpeR0AIwwNPRmZuVNJinyWuClhBlZjpm5fqnIAHJPtOd3Hou9mL6NVgj2LplEAADs=') # BBC Mode 5 '?' this._IMG_ERROR = tk.PhotoImage(data='R0lGODlhEAAQAKEBAAAAAP///////////yH5BAEKAAIALAAAAAAQABAAAAIwlBWpeR0AIwwNPRmZuVNJinyWuClhBlZjpm5fqnIAHJPtOd3Hou9mL6NVgj2LplEAADs=') # BBC Mode 5 '?'
# Migrate old settings # Migrate old settings
if not config.get('edsm_cmdrs'): if not config.get('edsm_cmdrs'):
@ -102,17 +101,19 @@ def plugin_start3(plugin_dir):
config.delete('edsm_autoopen') config.delete('edsm_autoopen')
config.delete('edsm_historical') config.delete('edsm_historical')
this.thread = Thread(target = worker, name = 'EDSM worker') this.thread = Thread(target=worker, name='EDSM worker')
this.thread.daemon = True this.thread.daemon = True
this.thread.start() this.thread.start()
return 'EDSM' return 'EDSM'
def plugin_app(parent): def plugin_app(parent):
this.system_link = parent.children['system'] # system label in main window this.system_link = parent.children['system'] # system label in main window
this.system_link.bind_all('<<EDSMStatus>>', update_status) this.system_link.bind_all('<<EDSMStatus>>', update_status)
this.station_link = parent.children['station'] # station label in main window this.station_link = parent.children['station'] # station label in main window
def plugin_stop(): def plugin_stop():
# Signal thread to close and wait for it # Signal thread to close and wait for it
this.queue.put(None) this.queue.put(None)
@ -121,35 +122,39 @@ def plugin_stop():
# Suppress 'Exception ignored in: <function Image.__del__ at ...>' errors # Suppress 'Exception ignored in: <function Image.__del__ at ...>' errors
this._IMG_KNOWN = this._IMG_UNKNOWN = this._IMG_NEW = this._IMG_ERROR = None this._IMG_KNOWN = this._IMG_UNKNOWN = this._IMG_NEW = this._IMG_ERROR = None
def plugin_prefs(parent, cmdr, is_beta): def plugin_prefs(parent, cmdr, is_beta):
PADX = 10 PADX = 10
BUTTONX = 12 # indent Checkbuttons and Radiobuttons BUTTONX = 12 # indent Checkbuttons and Radiobuttons
PADY = 2 # close spacing PADY = 2 # close spacing
frame = nb.Frame(parent) frame = nb.Frame(parent)
frame.columnconfigure(1, weight=1) frame.columnconfigure(1, weight=1)
HyperlinkLabel(frame, text='Elite Dangerous Star Map', background=nb.Label().cget('background'), url='https://www.edsm.net/', underline=True).grid(columnspan=2, padx=PADX, sticky=tk.W) # Don't translate HyperlinkLabel(frame, text='Elite Dangerous Star Map', background=nb.Label().cget('background'),
this.log = tk.IntVar(value = config.getint('edsm_out') and 1) url='https://www.edsm.net/', underline=True).grid(columnspan=2, padx=PADX, sticky=tk.W) # Don't translate
this.log_button = nb.Checkbutton(frame, text=_('Send flight log and Cmdr status to EDSM'), variable=this.log, command=prefsvarchanged) this.log = tk.IntVar(value=config.getint('edsm_out') and 1)
this.log_button.grid(columnspan=2, padx=BUTTONX, pady=(5,0), sticky=tk.W) this.log_button = nb.Checkbutton(frame, text=_('Send flight log and Cmdr status to EDSM'),
variable=this.log, command=prefsvarchanged)
this.log_button.grid(columnspan=2, padx=BUTTONX, pady=(5, 0), sticky=tk.W)
nb.Label(frame).grid(sticky=tk.W) # big spacer nb.Label(frame).grid(sticky=tk.W) # big spacer
this.label = HyperlinkLabel(frame, text=_('Elite Dangerous Star Map credentials'), background=nb.Label().cget('background'), url='https://www.edsm.net/settings/api', underline=True) # Section heading in settings this.label = HyperlinkLabel(frame, text=_('Elite Dangerous Star Map credentials'), background=nb.Label().cget(
'background'), url='https://www.edsm.net/settings/api', underline=True) # Section heading in settings
this.label.grid(columnspan=2, padx=PADX, sticky=tk.W) this.label.grid(columnspan=2, padx=PADX, sticky=tk.W)
this.cmdr_label = nb.Label(frame, text=_('Cmdr')) # Main window this.cmdr_label = nb.Label(frame, text=_('Cmdr')) # Main window
this.cmdr_label.grid(row=10, padx=PADX, sticky=tk.W) this.cmdr_label.grid(row=10, padx=PADX, sticky=tk.W)
this.cmdr_text = nb.Label(frame) this.cmdr_text = nb.Label(frame)
this.cmdr_text.grid(row=10, column=1, padx=PADX, pady=PADY, sticky=tk.W) this.cmdr_text.grid(row=10, column=1, padx=PADX, pady=PADY, sticky=tk.W)
this.user_label = nb.Label(frame, text=_('Commander Name')) # EDSM setting this.user_label = nb.Label(frame, text=_('Commander Name')) # EDSM setting
this.user_label.grid(row=11, padx=PADX, sticky=tk.W) this.user_label.grid(row=11, padx=PADX, sticky=tk.W)
this.user = nb.Entry(frame) this.user = nb.Entry(frame)
this.user.grid(row=11, column=1, padx=PADX, pady=PADY, sticky=tk.EW) this.user.grid(row=11, column=1, padx=PADX, pady=PADY, sticky=tk.EW)
this.apikey_label = nb.Label(frame, text=_('API Key')) # EDSM setting this.apikey_label = nb.Label(frame, text=_('API Key')) # EDSM setting
this.apikey_label.grid(row=12, padx=PADX, sticky=tk.W) this.apikey_label.grid(row=12, padx=PADX, sticky=tk.W)
this.apikey = nb.Entry(frame) this.apikey = nb.Entry(frame)
this.apikey.grid(row=12, column=1, padx=PADX, pady=PADY, sticky=tk.EW) this.apikey.grid(row=12, column=1, padx=PADX, pady=PADY, sticky=tk.EW)
@ -158,6 +163,7 @@ def plugin_prefs(parent, cmdr, is_beta):
return frame return frame
def prefs_cmdr_changed(cmdr, is_beta): def prefs_cmdr_changed(cmdr, is_beta):
this.log_button['state'] = cmdr and not is_beta and tk.NORMAL or tk.DISABLED this.log_button['state'] = cmdr and not is_beta and tk.NORMAL or tk.DISABLED
this.user['state'] = tk.NORMAL this.user['state'] = tk.NORMAL
@ -174,9 +180,11 @@ def prefs_cmdr_changed(cmdr, is_beta):
this.cmdr_text['text'] = _('None') # No hotkey/shortcut currently defined this.cmdr_text['text'] = _('None') # No hotkey/shortcut currently defined
this.label['state'] = this.cmdr_label['state'] = this.cmdr_text['state'] = this.user_label['state'] = this.user['state'] = this.apikey_label['state'] = this.apikey['state'] = cmdr and not is_beta and this.log.get() and tk.NORMAL or tk.DISABLED this.label['state'] = this.cmdr_label['state'] = this.cmdr_text['state'] = this.user_label['state'] = this.user['state'] = this.apikey_label['state'] = this.apikey['state'] = cmdr and not is_beta and this.log.get() and tk.NORMAL or tk.DISABLED
def prefsvarchanged(): def prefsvarchanged():
this.label['state'] = this.cmdr_label['state'] = this.cmdr_text['state'] = this.user_label['state'] = this.user['state'] = this.apikey_label['state'] = this.apikey['state'] = this.log.get() and this.log_button['state'] or tk.DISABLED this.label['state'] = this.cmdr_label['state'] = this.cmdr_text['state'] = this.user_label['state'] = this.user['state'] = this.apikey_label['state'] = this.apikey['state'] = this.log.get() and this.log_button['state'] or tk.DISABLED
def prefs_changed(cmdr, is_beta): def prefs_changed(cmdr, is_beta):
config.set('edsm_out', this.log.get()) config.set('edsm_out', this.log.get())
@ -293,9 +301,9 @@ entry: {entry!r}'''
materials = { materials = {
'timestamp': entry['timestamp'], 'timestamp': entry['timestamp'],
'event': 'Materials', 'event': 'Materials',
'Raw': [ { 'Name': k, 'Count': v } for k,v in state['Raw'].items() ], 'Raw': [{'Name': k, 'Count': v} for k, v in state['Raw'].items()],
'Manufactured': [ { 'Name': k, 'Count': v } for k,v in state['Manufactured'].items() ], 'Manufactured': [{'Name': k, 'Count': v} for k, v in state['Manufactured'].items()],
'Encoded': [ { 'Name': k, 'Count': v } for k,v in state['Encoded'].items() ], 'Encoded': [{'Name': k, 'Count': v} for k, v in state['Encoded'].items()],
} }
materials.update(transient) materials.update(transient)
this.queue.put((cmdr, materials)) this.queue.put((cmdr, materials))
@ -346,7 +354,7 @@ def cmdr_data(data, is_beta):
# Worker thread # Worker thread
def worker(): def worker():
pending = [] # Unsent events pending = [] # Unsent events
closing = False closing = False
while True: while True:
@ -354,7 +362,7 @@ def worker():
if item: if item:
(cmdr, entry) = item (cmdr, entry) = item
else: else:
closing = True # Try to send any unsent events before we close closing = True # Try to send any unsent events before we close
retrying = 0 retrying = 0
while retrying < 3: while retrying < 3:
@ -372,9 +380,10 @@ def worker():
r = this.session.get('https://www.edsm.net/api-journal-v1/discard', timeout=_TIMEOUT) r = this.session.get('https://www.edsm.net/api-journal-v1/discard', timeout=_TIMEOUT)
r.raise_for_status() r.raise_for_status()
this.discardedEvents = set(r.json()) this.discardedEvents = set(r.json())
this.discardedEvents.discard('Docked') # should_send() assumes that we send 'Docked' events this.discardedEvents.discard('Docked') # should_send() assumes that we send 'Docked' events
assert this.discardedEvents # wouldn't expect this to be empty assert this.discardedEvents # wouldn't expect this to be empty
pending = [x for x in pending if x['event'] not in this.discardedEvents] # Filter out unwanted events pending = [x for x in pending if x['event']
not in this.discardedEvents] # Filter out unwanted events
if should_send(pending): if should_send(pending):
if any([p for p in pending if p['event'] in ('CarrierJump', 'FSDJump', 'Location', 'Docked')]): if any([p for p in pending if p['event'] in ('CarrierJump', 'FSDJump', 'Location', 'Docked')]):
@ -450,9 +459,9 @@ def should_send(entries):
return True return True
elif this.newgame: elif this.newgame:
pass pass
elif entry['event'] not in ['CommunityGoal', # Spammed periodically elif entry['event'] not in ['CommunityGoal', # Spammed periodically
'ModuleBuy', 'ModuleSell', 'ModuleSwap', # will be shortly followed by "Loadout" 'ModuleBuy', 'ModuleSell', 'ModuleSwap', # will be shortly followed by "Loadout"
'ShipyardBuy', 'ShipyardNew', 'ShipyardSwap']: # " 'ShipyardBuy', 'ShipyardNew', 'ShipyardSwap']: # "
return True return True
return False return False
@ -469,7 +478,7 @@ def edsm_notify_system(reply):
if not reply: if not reply:
this.system_link['image'] = this._IMG_ERROR this.system_link['image'] = this._IMG_ERROR
plug.show_error(_("Error: Can't connect to EDSM")) plug.show_error(_("Error: Can't connect to EDSM"))
elif reply['msgnum'] // 100 not in (1,4): elif reply['msgnum'] // 100 not in (1, 4):
this.system_link['image'] = this._IMG_ERROR this.system_link['image'] = this._IMG_ERROR
plug.show_error(_('Error: EDSM {MSG}').format(MSG=reply['msg'])) plug.show_error(_('Error: EDSM {MSG}').format(MSG=reply['msg']))
elif reply.get('systemCreated'): elif reply.get('systemCreated'):