1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-18 18:07:37 +03:00

CAPI: 'Save Raw Data' now using cached copy

It will write a file containing JSON that has a top level dict with a
key per endpoint we have data for, and within each of those a dict with
keys for query_time and the raw_data.

Everything from the opening `{` in that raw_data value until the
matching closing `}` is the raw data from the CAPI service.

The whole file happily goes through `jq -S -C '.'` to examine the
output.
This commit is contained in:
Athanasius 2021-08-23 18:01:04 +01:00
parent c71fe042cd
commit 1c93c3a9fa
No known key found for this signature in database
GPG Key ID: AE3E527847057C7D
2 changed files with 27 additions and 42 deletions

View File

@ -4,7 +4,6 @@
import argparse
import html
import json
import locale
import pathlib
import queue
@ -1558,47 +1557,30 @@ class AppWindow(object):
self.destroy()
self.__class__.showing = False
def save_raw(self) -> None: # noqa: CCR001 # Not easily broken up.
"""Save newly acquired CAPI data in the configured file."""
# LANG: Status - Attempting to retrieve data from Frontier CAPI to save to file
self.status['text'] = _('Fetching data...')
self.w.update_idletasks()
def save_raw(self) -> None:
"""
Save any CAPI data already acquired to a file.
try:
data: CAPIData = companion.session.station()
self.status['text'] = ''
This specifically does *not* cause new queries to be performed, as the
purpose is to aid in diagnosing any issues that occurred during 'normal'
queries.
"""
default_extension: str = ''
if platform == 'darwin':
default_extension = '.json'
last_system: str = data.get("lastSystem", {}).get("name", "Unknown")
last_starport: str = ''
if data['commander'].get('docked'):
last_starport = '.' + data.get('lastStarport', {}).get('name', 'Unknown')
timestamp: str = strftime('%Y-%m-%dT%H.%M.%S', localtime())
f = tkinter.filedialog.asksaveasfilename(
parent=self.w,
defaultextension=default_extension,
filetypes=[('JSON', '.json'), ('All Files', '*')],
initialdir=config.get_str('outdir'),
initialfile=f'{last_system}{last_starport}.{timestamp}'
initialfile=f'{monitor.system}{monitor.station}.{timestamp}'
)
if f:
with open(f, 'wb') as h:
h.write(json.dumps(dict(data),
ensure_ascii=False,
indent=2,
sort_keys=True,
separators=(',', ': ')).encode('utf-8'))
except companion.ServerError as e:
self.status['text'] = str(e)
except Exception as e:
logger.debug('"other" exception', exc_info=e)
self.status['text'] = str(e)
h.write(str(companion.session.capi_raw_data).encode(encoding='utf-8'))
# def exit_tray(self, systray: 'SysTrayIcon') -> None:
# """Tray icon is shutting down."""

View File

@ -149,10 +149,13 @@ class CAPIDataRaw:
def __str__(self):
"""Return a more readable string form of the data."""
capi_data_str = ''
capi_data_str = '{'
for e in self.raw_data.keys():
capi_data_str += f'"{e}":\n{{\n\t"query_time": {self.raw_data[e].query_time},\n\t' \
f'"raw_data": {self.raw_data[e].raw_data}\n}}\n\n'
capi_data_str += f'"{e}":\n{{\n\t"query_time": "{self.raw_data[e].query_time}",\n\t' \
f'"raw_data": {self.raw_data[e].raw_data}\n}},\n\n'
capi_data_str = capi_data_str.removesuffix(',\n\n')
capi_data_str += '\n\n}'
return capi_data_str