From e693af1283a65a27418de0b79c0da9265bacfc90 Mon Sep 17 00:00:00 2001 From: Athanasius <github@miggy.org> Date: Tue, 8 Sep 2020 18:51:17 +0100 Subject: [PATCH 1/5] EDDN: Log the message we tried to send if it fails. --- plugins/eddn.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/eddn.py b/plugins/eddn.py index b16fc98b..a0713e92 100644 --- a/plugins/eddn.py +++ b/plugins/eddn.py @@ -143,7 +143,7 @@ class EDDN: r = self.session.post(self.UPLOAD, data=json.dumps(to_send), timeout=self.TIMEOUT) if r.status_code != requests.codes.ok: - logger.debug(f':\nStatus\t{r.status_code}URL\t{r.url}Headers\t{r.headers}Content:\n{r.text}') + logger.debug(f':\nStatus\t{r.status_code}URL\t{r.url}Headers\t{r.headers}Content:\n{r.text}\nMsg:\n{msg}\n') r.raise_for_status() From 5d612822da8978a9b6f03197649e2233dc196902 Mon Sep 17 00:00:00 2001 From: Athanasius <github@miggy.org> Date: Wed, 9 Sep 2020 13:51:15 +0100 Subject: [PATCH 2/5] eddn: Tweak the extra EDDN failed message logging format. It's more readable with \n between each part, and added a prefix. --- plugins/eddn.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/eddn.py b/plugins/eddn.py index a0713e92..3526085e 100644 --- a/plugins/eddn.py +++ b/plugins/eddn.py @@ -143,7 +143,7 @@ class EDDN: r = self.session.post(self.UPLOAD, data=json.dumps(to_send), timeout=self.TIMEOUT) if r.status_code != requests.codes.ok: - logger.debug(f':\nStatus\t{r.status_code}URL\t{r.url}Headers\t{r.headers}Content:\n{r.text}\nMsg:\n{msg}\n') + logger.debug(f'Status from POST wasn\'t OK:\nStatus\t{r.status_code}\nURL\t{r.url}\nHeaders\t{r.headers}\nContent:\n{r.text}\nMsg:\n{msg}') r.raise_for_status() From 48077f6a3024dadf8694dd3a7b8fe2db70e27348 Mon Sep 17 00:00:00 2001 From: Athanasius <github@miggy.org> Date: Wed, 9 Sep 2020 14:03:40 +0100 Subject: [PATCH 3/5] eddn: Straighten out replay.jsonl opening & minor cleanups. * The logic for opening replay.jsonl, and detecting if the file was already there or not was tortured. No longer. * Changed a few logger.debug(..., exc_info=) to logger.exception(). * Changed all logger.warn() (deprecated) to logger.warning(). --- plugins/eddn.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/plugins/eddn.py b/plugins/eddn.py index 3526085e..a5d91fee 100644 --- a/plugins/eddn.py +++ b/plugins/eddn.py @@ -81,18 +81,14 @@ class EDDN: # Try to open existing file self.replayfile = open(filename, 'r+', buffering=1) - except Exception: - if exists(filename): - raise # Couldn't open existing file - - else: - self.replayfile = open(filename, 'w+', buffering=1) # Create file + except FileNotFoundError: + self.replayfile = open(filename, 'w+', buffering=1) # Create file if sys.platform != 'win32': # open for writing is automatically exclusive on Windows lockf(self.replayfile, LOCK_EX | LOCK_NB) - except Exception as e: - logger.debug('Failed opening "replay.jsonl"', exc_info=e) + except Exception: + logger.exception('Failed opening "replay.jsonl"') if self.replayfile: self.replayfile.close() @@ -179,6 +175,9 @@ class EDDN: self.replaylog.pop(0) else: + # TODO: Check message against *current* relevant schema so we don't try + # to send an old message that's now invalid. + # Rewrite old schema name if msg['$schemaRef'].startswith('http://schemas.elite-markets.net/eddn/'): msg['$schemaRef'] = str(msg['$schemaRef']).replace( @@ -639,21 +638,21 @@ def journal_entry( # add mandatory StarSystem, StarPos and SystemAddress properties to Scan events if 'StarSystem' not in entry: if not system: - logger.warn("system is None, can't add StarSystem") + logger.warning("system is None, can't add StarSystem") return "system is None, can't add StarSystem" entry['StarSystem'] = system if 'StarPos' not in entry: if not this.coordinates: - logger.warn("this.coordinates is None, can't add StarPos") + logger.warning("this.coordinates is None, can't add StarPos") return "this.coordinates is None, can't add StarPos" entry['StarPos'] = list(this.coordinates) if 'SystemAddress' not in entry: if not this.systemaddress: - logger.warn("this.systemaddress is None, can't add SystemAddress") + logger.warning("this.systemaddress is None, can't add SystemAddress") return "this.systemaddress is None, can't add SystemAddress" entry['SystemAddress'] = this.systemaddress From 5e35012611cb7345e15f28377b917b50270a3f3f Mon Sep 17 00:00:00 2001 From: Athanasius <github@miggy.org> Date: Wed, 9 Sep 2020 14:17:39 +0100 Subject: [PATCH 4/5] eddn: flake8 cleanups * That "EDDN send failed" was too long, so now it's '''-formatted. * journal_entry() cognitive complexity isn't changing any time soon. --- plugins/eddn.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/plugins/eddn.py b/plugins/eddn.py index a5d91fee..a7cc7a4b 100644 --- a/plugins/eddn.py +++ b/plugins/eddn.py @@ -9,11 +9,11 @@ import sys import tkinter as tk from collections import OrderedDict from os import SEEK_SET -from os.path import exists, join +from os.path import join from platform import system from typing import TYPE_CHECKING, Any, AnyStr, Dict, Iterator, List, Mapping, MutableMapping, Optional from typing import OrderedDict as OrderedDictT -from typing import Sequence, TextIO, Tuple, Union +from typing import Sequence, TextIO, Tuple import requests @@ -139,7 +139,12 @@ class EDDN: r = self.session.post(self.UPLOAD, data=json.dumps(to_send), timeout=self.TIMEOUT) if r.status_code != requests.codes.ok: - logger.debug(f'Status from POST wasn\'t OK:\nStatus\t{r.status_code}\nURL\t{r.url}\nHeaders\t{r.headers}\nContent:\n{r.text}\nMsg:\n{msg}') + logger.debug(f'''Status from POST wasn't OK: +Status\t{r.status_code} +URL\t{r.url} +Headers\t{r.headers} +Content:\n{r.text} +Msg:\n{msg}''') r.raise_for_status() @@ -556,7 +561,7 @@ def plugin_stop() -> None: this.eddn.close() -def journal_entry( +def journal_entry( # noqa: C901 cmdr: str, is_beta: bool, system: str, station: str, entry: MutableMapping[str, Any], state: Mapping[str, Any] ) -> Optional[str]: # Recursively filter '*_Localised' keys from dict From 99bac688af32ca01ea6b78564d44b7713ef9a1a1 Mon Sep 17 00:00:00 2001 From: Athanasius <github@miggy.org> Date: Wed, 9 Sep 2020 14:36:38 +0100 Subject: [PATCH 5/5] eddn: Tweak replay.jsonl try/except flow * Catch OSError instead of Exception (which is *too* general). * Put the set of self.replaylog in try/else so it only runs when there are no errors. --- plugins/eddn.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/plugins/eddn.py b/plugins/eddn.py index a7cc7a4b..62ebbaa5 100644 --- a/plugins/eddn.py +++ b/plugins/eddn.py @@ -87,7 +87,7 @@ class EDDN: if sys.platform != 'win32': # open for writing is automatically exclusive on Windows lockf(self.replayfile, LOCK_EX | LOCK_NB) - except Exception: + except OSError: logger.exception('Failed opening "replay.jsonl"') if self.replayfile: self.replayfile.close() @@ -95,8 +95,9 @@ class EDDN: self.replayfile = None return False - self.replaylog = [line.strip() for line in self.replayfile] - return True + else: + self.replaylog = [line.strip() for line in self.replayfile] + return True def flush(self): """