From 1c93c3a9fa40d103b421d0f021bd19143de82711 Mon Sep 17 00:00:00 2001
From: Athanasius <github@miggy.org>
Date: Mon, 23 Aug 2021 18:01:04 +0100
Subject: [PATCH] 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.
---
 EDMarketConnector.py | 60 ++++++++++++++++----------------------------
 companion.py         |  9 ++++---
 2 files changed, 27 insertions(+), 42 deletions(-)

diff --git a/EDMarketConnector.py b/EDMarketConnector.py
index fc1b0432..901eb763 100755
--- a/EDMarketConnector.py
+++ b/EDMarketConnector.py
@@ -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'] = ''
-            default_extension: str = ''
+        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'
+        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}'
-            )
-            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)
+        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'{monitor.system}{monitor.station}.{timestamp}'
+        )
+        if f:
+            with open(f, 'wb') as h:
+                h.write(str(companion.session.capi_raw_data).encode(encoding='utf-8'))
 
     # def exit_tray(self, systray: 'SysTrayIcon') -> None:
     #     """Tray icon is shutting down."""
diff --git a/companion.py b/companion.py
index c3a7000f..36be1756 100644
--- a/companion.py
+++ b/companion.py
@@ -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