1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-17 17:42:20 +03:00

CAPI: Convert StatsDialog.showstats() to using cached CAPI data

This will just bail if no data has yet been received for /profile, but
that's the same behaviour as if we don't yet have `monitor.cmdr`.
This commit is contained in:
Athanasius 2021-08-25 14:08:36 +01:00
parent 7e1e35fd48
commit 120414ac59
No known key found for this signature in database
GPG Key ID: AE3E527847057C7D
2 changed files with 24 additions and 21 deletions

View File

@ -167,6 +167,14 @@ class CAPIDataRaw:
return capi_data_str return capi_data_str
def __iter__(self):
"""Make this iterable on its raw_data dict."""
yield from self.raw_data
def __getitem__(self, item):
"""Make the raw_data dict's items get'able."""
return self.raw_data.get(item)
def listify(thing: Union[List, Dict]) -> List: def listify(thing: Union[List, Dict]) -> List:
""" """

View File

@ -1,5 +1,6 @@
"""CMDR Status information.""" """CMDR Status information."""
import csv import csv
import json
import tkinter import tkinter
import tkinter as tk import tkinter as tk
from sys import platform from sys import platform
@ -276,46 +277,40 @@ class StatsDialog():
return return
# TODO: This needs to use cached data # TODO: This needs to use cached data
# LANG: Fetching data from Frontier CAPI in order to display on File > Status if companion.session.FRONTIER_CAPI_PATH_PROFILE not in companion.session.capi_raw_data:
self.status['text'] = _('Fetching data...') logger.info('No cached data, aborting...')
self.parent.update_idletasks()
try:
# TODO: This should use cached data
data = companion.session.profile()
except companion.ServerError as e:
self.status['text'] = str(e)
return return
except Exception as e: capi_data = json.loads(
logger.exception("error while attempting to show status") companion.session.capi_raw_data[companion.session.FRONTIER_CAPI_PATH_PROFILE].raw_data
self.status['text'] = str(e) )
return
if not data.get('commander') or not data['commander'].get('name', '').strip(): if not capi_data.get('commander') or not capi_data['commander'].get('name', '').strip():
# Shouldn't happen # Shouldn't happen
# LANG: Unknown commander # LANG: Unknown commander
self.status['text'] = _("Who are you?!") self.status['text'] = _("Who are you?!")
elif ( elif (
not data.get('lastSystem') not capi_data.get('lastSystem')
or not data['lastSystem'].get('name', '').strip() or not capi_data['lastSystem'].get('name', '').strip()
or not data.get('lastStarport') or not capi_data.get('lastStarport')
or not data['lastStarport'].get('name', '').strip() or not capi_data['lastStarport'].get('name', '').strip()
): ):
# Shouldn't happen # Shouldn't happen
# LANG: Unknown location # LANG: Unknown location
self.status['text'] = _("Where are you?!") self.status['text'] = _("Where are you?!")
elif not data.get('ship') or not data['ship'].get('modules') or not data['ship'].get('name', '').strip(): elif (
not capi_data.get('ship') or not capi_data['ship'].get('modules')
or not capi_data['ship'].get('name', '').strip()
):
# Shouldn't happen # Shouldn't happen
# LANG: Unknown ship # LANG: Unknown ship
self.status['text'] = _("What are you flying?!") self.status['text'] = _("What are you flying?!")
else: else:
self.status['text'] = '' self.status['text'] = ''
StatsResults(self.parent, data) StatsResults(self.parent, capi_data)
class StatsResults(tk.Toplevel): class StatsResults(tk.Toplevel):