From d59dc18b1b8cdf29227365d1984ad89abcacdd20 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Wed, 9 Feb 2022 13:50:42 +0000 Subject: [PATCH 1/6] eddn: make the exclusive nature of 'own schema' events explicit --- plugins/eddn.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/eddn.py b/plugins/eddn.py index a9f25d57..3ffbbdf7 100644 --- a/plugins/eddn.py +++ b/plugins/eddn.py @@ -1391,16 +1391,16 @@ def journal_entry( # noqa: C901, CCR001 if entry['event'].lower() == 'fssdiscoveryscan': return this.eddn.export_journal_fssdiscoveryscan(cmdr, system, is_beta, entry) - if entry['event'].lower() == 'navbeaconscan': + elif entry['event'].lower() == 'navbeaconscan': return this.eddn.export_journal_navbeaconscan(cmdr, system, is_beta, entry) - if entry['event'].lower() == 'codexentry': + elif entry['event'].lower() == 'codexentry': return this.eddn.export_journal_codexentry(cmdr, is_beta, entry) - if entry['event'].lower() == 'scanbarycentre': + elif entry['event'].lower() == 'scanbarycentre': return this.eddn.export_journal_scanbarycentre(cmdr, is_beta, entry) - if entry['event'].lower() == 'navroute': + elif entry['event'].lower() == 'navroute': return this.eddn.export_journal_navroute(cmdr, is_beta, entry) # Send journal schema events to EDDN, but not when on a crew From 91c36a5b1ab005d542b8ea2da80ecdd739265647 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Wed, 9 Feb 2022 14:08:36 +0000 Subject: [PATCH 2/6] eddn: Change to the ".lower() Journal event name once" paradigm Also, whilst the *current* code has the 'augment files' handled last in then if/else tree we shouldn't assume that will always be the case. So, be paranoid and use different variables for the augment-loaded entry and its .lower event_name. 'cos you just know one of us will trip up on it later if there's ever some 'finally' code after that conditional tree. --- plugins/eddn.py | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/plugins/eddn.py b/plugins/eddn.py index 3ffbbdf7..9efe2c6a 100644 --- a/plugins/eddn.py +++ b/plugins/eddn.py @@ -1331,6 +1331,7 @@ def journal_entry( # noqa: C901, CCR001 return None entry = new_data + event_name = entry['event'].lower() this.on_foot = state['OnFoot'] @@ -1340,8 +1341,8 @@ def journal_entry( # noqa: C901, CCR001 this.odyssey = entry['odyssey'] = state['Odyssey'] # Track location - if entry['event'] in ('Location', 'FSDJump', 'Docked', 'CarrierJump'): - if entry['event'] in ('Location', 'CarrierJump'): + if event_name in ('location', 'fsdjump', 'docked', 'carrierjump'): + if event_name in ('location', 'carrierjump'): if entry.get('BodyType') == 'Planet': this.body_name = entry.get('Body') this.body_id = entry.get('BodyID') @@ -1349,7 +1350,7 @@ def journal_entry( # noqa: C901, CCR001 else: this.body_name = None - elif entry['event'] == 'FSDJump': + elif event_name == 'fsdjump': this.body_name = None this.body_id = None @@ -1368,11 +1369,11 @@ def journal_entry( # noqa: C901, CCR001 # Yes, explicitly state `None` here, so it's crystal clear. this.systemaddress = entry.get('SystemAddress', None) # type: ignore - elif entry['event'] == 'ApproachBody': + elif event_name == 'approachbody': this.body_name = entry['Body'] this.body_id = entry.get('BodyID') - elif entry['event'] == 'LeaveBody': + elif event_name == 'leavebody': # NB: **NOT** SupercruiseEntry, because we won't get a fresh # ApproachBody if we don't leave Orbital Cruise and land again. # *This* is triggered when you go above Orbital Cruise altitude. @@ -1380,7 +1381,7 @@ def journal_entry( # noqa: C901, CCR001 this.body_name = None this.body_id = None - elif entry['event'] == 'Music': + elif event_name == 'music': if entry['MusicTrack'] == 'MainMenu': this.body_name = None this.body_id = None @@ -1388,24 +1389,25 @@ def journal_entry( # noqa: C901, CCR001 # Events with their own EDDN schema if config.get_int('output') & config.OUT_SYS_EDDN and not state['Captain']: - if entry['event'].lower() == 'fssdiscoveryscan': + + if event_name == 'fssdiscoveryscan': return this.eddn.export_journal_fssdiscoveryscan(cmdr, system, is_beta, entry) - elif entry['event'].lower() == 'navbeaconscan': + elif event_name == 'navbeaconscan': return this.eddn.export_journal_navbeaconscan(cmdr, system, is_beta, entry) - elif entry['event'].lower() == 'codexentry': + elif event_name == 'codexentry': return this.eddn.export_journal_codexentry(cmdr, is_beta, entry) - elif entry['event'].lower() == 'scanbarycentre': + elif event_name == 'scanbarycentre': return this.eddn.export_journal_scanbarycentre(cmdr, is_beta, entry) - elif entry['event'].lower() == 'navroute': + elif event_name == 'navroute': return this.eddn.export_journal_navroute(cmdr, is_beta, entry) # Send journal schema events to EDDN, but not when on a crew if (config.get_int('output') & config.OUT_SYS_EDDN and not state['Captain'] and - (entry['event'] in ('Location', 'FSDJump', 'Docked', 'Scan', 'SAASignalsFound', 'CarrierJump')) and + (event_name in ('location', 'fsdjump', 'docked', 'scan', 'saasignalsfound', 'carrierjump')) and ('StarPos' in entry or this.coordinates)): # strip out properties disallowed by the schema @@ -1435,7 +1437,7 @@ def journal_entry( # noqa: C901, CCR001 ] # add planet to Docked event for planetary stations if known - if entry['event'] == 'Docked' and this.body_name: + if event_name == 'docked' and this.body_name: entry['Body'] = this.body_name entry['BodyType'] = 'Planet' @@ -1479,7 +1481,7 @@ def journal_entry( # noqa: C901, CCR001 return str(e) elif (config.get_int('output') & config.OUT_MKT_EDDN and not state['Captain'] and - entry['event'] in ('Market', 'Outfitting', 'Shipyard')): + event_name in ('market', 'outfitting', 'shipyard')): # Market.json, Outfitting.json or Shipyard.json to process try: @@ -1494,16 +1496,19 @@ def journal_entry( # noqa: C901, CCR001 path = pathlib.Path(journaldir) / f'{entry["event"]}.json' with path.open('rb') as f: - entry = json.load(f) - entry['odyssey'] = this.odyssey - if entry['event'] == 'Market': - this.eddn.export_journal_commodities(cmdr, is_beta, entry) + # Don't assume we can definitely stomp entry & event_name here + entry_augment = json.load(f) + event_name_augment = entry_augment['event'].lower() + entry_augment['odyssey'] = this.odyssey - elif entry['event'] == 'Outfitting': - this.eddn.export_journal_outfitting(cmdr, is_beta, entry) + if event_name_augment == 'market': + this.eddn.export_journal_commodities(cmdr, is_beta, entry_augment) - elif entry['event'] == 'Shipyard': - this.eddn.export_journal_shipyard(cmdr, is_beta, entry) + elif event_name_augment == 'outfitting': + this.eddn.export_journal_outfitting(cmdr, is_beta, entry_augment) + + elif event_name_augment == 'shipyard': + this.eddn.export_journal_shipyard(cmdr, is_beta, entry_augment) except requests.exceptions.RequestException as e: logger.debug(f'Failed exporting {entry["event"]}', exc_info=e) From 4f0ecb0289c7b119647b7e1f9a2c3d27afa1b470 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Wed, 9 Feb 2022 14:48:56 +0000 Subject: [PATCH 3/6] .pre-commit-config: .mypy.ini sets these options --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bc2d8a73..e9b0cc92 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -44,7 +44,7 @@ repos: rev: 'v0.931' hooks: - id: mypy - args: [ "--follow-imports", "skip", "--ignore-missing-imports", "--scripts-are-modules" ] + # args: [ "--follow-imports", "skip", "--ignore-missing-imports", "--scripts-are-modules" ] ### # pydocstyle.exe ### - repo: https://github.com/FalconSocial/pre-commit-mirrors-pep257 From 3a0dbb906a7f39eba572ffb567f1e5ab8b96a791 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Wed, 9 Feb 2022 15:01:56 +0000 Subject: [PATCH 4/6] eddn: export_journal_approachsettlement() implementation --- plugins/eddn.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/plugins/eddn.py b/plugins/eddn.py index 9efe2c6a..f2185156 100644 --- a/plugins/eddn.py +++ b/plugins/eddn.py @@ -1090,6 +1090,46 @@ class EDDN: this.eddn.export_journal_entry(cmdr, entry, msg) return None + def export_journal_approachsettlement( + self, cmdr: str, is_beta: bool, entry: MutableMapping[str, Any] + ) -> Optional[str]: + """ + Send an ApproachSettlement to EDDN on the correct schema. + + :param cmdr: the commander under which this upload is made + :param is_beta: whether or not we are in beta mode + :param entry: the journal entry to send + """ + # { + # "BodyID": 32, + # "BodyName": "Ix 5 a a", + # "Latitude": 17.090912, + # "Longitude": 160.236679, + # "MarketID": 3915738368, + # "Name": "Arnold Defence Base", + # "SystemAddress": 2381282543963, + # "event": "ApproachSettlement", + # "timestamp": "2021-10-14T12:37:54Z" + # } + ####################################################################### + # Augmentations + ####################################################################### + # In this case should add StarSystem and StarPos + ret = this.eddn.entry_augment_system_data(entry, entry['StarSystem']) + if isinstance(ret, str): + return ret + + entry = ret + ####################################################################### + + msg = { + '$schemaRef': f'https://eddn.edcd.io/schemas/approachsettlement/1{"/test" if is_beta else ""}', + 'message': entry + } + + this.eddn.export_journal_entry(cmdr, entry, msg) + return None + def canonicalise(self, item: str) -> str: """ Canonicalise the given commodity name. @@ -1405,6 +1445,9 @@ def journal_entry( # noqa: C901, CCR001 elif event_name == 'navroute': return this.eddn.export_journal_navroute(cmdr, is_beta, entry) + elif event_name == 'approachsettlement': + return this.eddn.export_journal_approachsettlement(cmdr, is_beta, entry) + # Send journal schema events to EDDN, but not when on a crew if (config.get_int('output') & config.OUT_SYS_EDDN and not state['Captain'] and (event_name in ('location', 'fsdjump', 'docked', 'scan', 'saasignalsfound', 'carrierjump')) and From f1ba8ee5e1561c2b1d17546ca40d0fa937c37f92 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Wed, 9 Feb 2022 16:07:16 +0000 Subject: [PATCH 5/6] eddn: Fix up approachsettlement & adjust other such function signatures 1. We need StarPos (as well as StarSystem) 2. Adding more state tracking in this plugin is misguided. 3. So added it in monitor instead, putting *copies* of data in the monitor.state dictionary. 4. So we reference those, but only available in journal_entry() itself, else we'd need to pass the whole of `state` in. 5. So instead pass in the bits of `state` only when we need them. --- monitor.py | 14 +++++++++-- plugins/eddn.py | 63 ++++++++++++++++++++++++++++++++----------------- 2 files changed, 54 insertions(+), 23 deletions(-) diff --git a/monitor.py b/monitor.py index a90397e4..9c049429 100644 --- a/monitor.py +++ b/monitor.py @@ -180,6 +180,8 @@ class EDLogs(FileSystemEventHandler): # type: ignore # See below 'SuitLoadouts': {}, 'Taxi': None, # True whenever we are _in_ a taxi. ie, this is reset on Disembark etc. 'Dropship': None, # Best effort as to whether or not the above taxi is a dropship. + 'StarSystem': None, # Best effort name of current system. + 'StarPos': None, # Best effort current system's galaxy position. 'Body': None, 'BodyType': None, @@ -801,13 +803,21 @@ class EDLogs(FileSystemEventHandler): # type: ignore # See below self.state['BodyType'] = None if 'StarPos' in entry: - self.coordinates = tuple(entry['StarPos']) # type: ignore + # Plugins need this as well, so copy in state + self.state['StarPos'] = self.coordinates = tuple(entry['StarPos']) # type: ignore self.systemaddress = entry.get('SystemAddress') self.systempopulation = entry.get('Population') - self.system = 'CQC' if entry['StarSystem'] == 'ProvingGround' else entry['StarSystem'] + if entry['StarSystem'] == 'ProvingGround': + to_set = 'CQC' + + else: + to_set = entry['StarSystem'] + + # EDDN plugin needs this in `state` + self.state['StarSystem'] = self.system = to_set self.station = entry.get('StationName') # May be None # If on foot in-station 'Docked' is false, but we have a diff --git a/plugins/eddn.py b/plugins/eddn.py index f2185156..a630cfd8 100644 --- a/plugins/eddn.py +++ b/plugins/eddn.py @@ -789,14 +789,15 @@ class EDDN: def entry_augment_system_data( self, entry: MutableMapping[str, Any], - system_name: str + system_name: str, + system_coordinates: list ) -> Union[str, MutableMapping[str, Any]]: """ Augment a journal entry with necessary system data. :param entry: The journal entry to be augmented. :param system_name: Name of current star system. - :param systemname_field_name: Name of journal key for system name. + :param system_coordinates: Coordinates of current star system. :return: The augmented version of entry. """ # If 'SystemName' or 'System' is there, it's directly from a journal event. @@ -818,21 +819,29 @@ class EDDN: entry['SystemAddress'] = this.systemaddress if 'StarPos' not in entry: - if not this.coordinates: - logger.warning("this.coordinates is None, can't add StarPos") - return "this.coordinates is None, can't add StarPos" + # Prefer the passed-in, probably monitor.state version + if system_coordinates is not None: + entry['StarPos'] = system_coordinates - entry['StarPos'] = list(this.coordinates) + # TODO: Deprecate in-plugin tracking + elif this.coordinates is not None: + entry['StarPos'] = list(this.coordinates) + + else: + logger.warning("Neither this_coordinates or this.coordinates set, can't add StarPos") + return 'No source for adding StarPos to approachsettlement/1 !' return entry def export_journal_fssdiscoveryscan( - self, cmdr: str, system: str, is_beta: bool, entry: Mapping[str, Any] + self, cmdr: str, system: str, system_starpos: list, is_beta: bool, entry: Mapping[str, Any] ) -> Optional[str]: """ Send an FSSDiscoveryScan to EDDN on the correct schema. :param cmdr: the commander under which this upload is made + :param system: Name of current star system + :param system_starpos: Coordinates of current star system :param is_beta: whether or not we are in beta mode :param entry: the journal entry to send """ @@ -845,7 +854,7 @@ class EDDN: ####################################################################### # Augmentations ####################################################################### - ret = this.eddn.entry_augment_system_data(entry, system) + ret = this.eddn.entry_augment_system_data(entry, system, system_starpos) if isinstance(ret, str): return ret @@ -861,13 +870,14 @@ class EDDN: return None def export_journal_navbeaconscan( - self, cmdr: str, system_name: str, is_beta: bool, entry: Mapping[str, Any] + self, cmdr: str, system_name: str, system_starpos: list, is_beta: bool, entry: Mapping[str, Any] ) -> Optional[str]: """ Send an NavBeaconScan to EDDN on the correct schema. :param cmdr: the commander under which this upload is made :param system_name: Name of the current system. + :param system_starpos: Coordinates of current star system :param is_beta: whether or not we are in beta mode :param entry: the journal entry to send """ @@ -880,7 +890,7 @@ class EDDN: ####################################################################### # Augmentations ####################################################################### - ret = this.eddn.entry_augment_system_data(entry, system_name) + ret = this.eddn.entry_augment_system_data(entry, system_name, system_starpos) if isinstance(ret, str): return ret @@ -896,12 +906,13 @@ class EDDN: return None def export_journal_codexentry( # noqa: CCR001 - self, cmdr: str, is_beta: bool, entry: MutableMapping[str, Any] + self, cmdr: str, system_starpos: list, is_beta: bool, entry: MutableMapping[str, Any] ) -> Optional[str]: """ Send a CodexEntry to EDDN on the correct schema. :param cmdr: the commander under which this upload is made + :param system_starpos: Coordinates of current star system :param is_beta: whether or not we are in beta mode :param entry: the journal entry to send """ @@ -936,7 +947,7 @@ class EDDN: # Augmentations ####################################################################### # General 'system' augmentations - ret = this.eddn.entry_augment_system_data(entry, entry['System']) + ret = this.eddn.entry_augment_system_data(entry, entry['System'], system_starpos) if isinstance(ret, str): return ret @@ -976,12 +987,13 @@ class EDDN: return None def export_journal_scanbarycentre( - self, cmdr: str, is_beta: bool, entry: Mapping[str, Any] + self, cmdr: str, system_starpos: list, is_beta: bool, entry: Mapping[str, Any] ) -> Optional[str]: """ Send a ScanBaryCentre to EDDN on the correct schema. :param cmdr: the commander under which this upload is made + :param system_starpos: Coordinates of current star system :param is_beta: whether or not we are in beta mode :param entry: the journal entry to send """ @@ -1007,7 +1019,7 @@ class EDDN: ####################################################################### # Augmentations ####################################################################### - ret = this.eddn.entry_augment_system_data(entry, entry['StarSystem']) + ret = this.eddn.entry_augment_system_data(entry, entry['StarSystem'], system_starpos) if isinstance(ret, str): return ret @@ -1029,6 +1041,7 @@ class EDDN: Send a NavRoute to EDDN on the correct schema. :param cmdr: the commander under which this upload is made + :param system_starpos: Coordinates of current star system :param is_beta: whether or not we are in beta mode :param entry: the journal entry to send """ @@ -1091,12 +1104,14 @@ class EDDN: return None def export_journal_approachsettlement( - self, cmdr: str, is_beta: bool, entry: MutableMapping[str, Any] + self, cmdr: str, system_name: str, system_starpos: list, is_beta: bool, entry: MutableMapping[str, Any] ) -> Optional[str]: """ Send an ApproachSettlement to EDDN on the correct schema. :param cmdr: the commander under which this upload is made + :param system_name: Name of current star system + :param system_starpos: Coordinates of current star system :param is_beta: whether or not we are in beta mode :param entry: the journal entry to send """ @@ -1115,7 +1130,7 @@ class EDDN: # Augmentations ####################################################################### # In this case should add StarSystem and StarPos - ret = this.eddn.entry_augment_system_data(entry, entry['StarSystem']) + ret = this.eddn.entry_augment_system_data(entry, system_name, system_starpos) if isinstance(ret, str): return ret @@ -1431,22 +1446,28 @@ def journal_entry( # noqa: C901, CCR001 if config.get_int('output') & config.OUT_SYS_EDDN and not state['Captain']: if event_name == 'fssdiscoveryscan': - return this.eddn.export_journal_fssdiscoveryscan(cmdr, system, is_beta, entry) + return this.eddn.export_journal_fssdiscoveryscan(cmdr, system, state['StarPos'], is_beta, entry) elif event_name == 'navbeaconscan': - return this.eddn.export_journal_navbeaconscan(cmdr, system, is_beta, entry) + return this.eddn.export_journal_navbeaconscan(cmdr, system, state['StarPos'], is_beta, entry) elif event_name == 'codexentry': - return this.eddn.export_journal_codexentry(cmdr, is_beta, entry) + return this.eddn.export_journal_codexentry(cmdr, state['StarPos'], is_beta, entry) elif event_name == 'scanbarycentre': - return this.eddn.export_journal_scanbarycentre(cmdr, is_beta, entry) + return this.eddn.export_journal_scanbarycentre(cmdr, state['StarPos'], is_beta, entry) elif event_name == 'navroute': return this.eddn.export_journal_navroute(cmdr, is_beta, entry) elif event_name == 'approachsettlement': - return this.eddn.export_journal_approachsettlement(cmdr, is_beta, entry) + return this.eddn.export_journal_approachsettlement( + cmdr, + state['StarSystem'], + state['StarPos'], + is_beta, + entry + ) # Send journal schema events to EDDN, but not when on a crew if (config.get_int('output') & config.OUT_SYS_EDDN and not state['Captain'] and From 6f0095e5fe9ae8d1b4e33901e0f5376e2a114e52 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Wed, 9 Feb 2022 16:14:15 +0000 Subject: [PATCH 6/6] eddn: Slightly rationalise passed StarSystem * Call it system_name in `export_journal_fssdiscoveryscan()`, as `system` could be amigbuous. * Might as well use `system` passed in to `journal_entry()` when calling `export_journal_approachsettlement()`. --- plugins/eddn.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/eddn.py b/plugins/eddn.py index a630cfd8..8da1954f 100644 --- a/plugins/eddn.py +++ b/plugins/eddn.py @@ -834,13 +834,13 @@ class EDDN: return entry def export_journal_fssdiscoveryscan( - self, cmdr: str, system: str, system_starpos: list, is_beta: bool, entry: Mapping[str, Any] + self, cmdr: str, system_name: str, system_starpos: list, is_beta: bool, entry: Mapping[str, Any] ) -> Optional[str]: """ Send an FSSDiscoveryScan to EDDN on the correct schema. :param cmdr: the commander under which this upload is made - :param system: Name of current star system + :param system_name: Name of current star system :param system_starpos: Coordinates of current star system :param is_beta: whether or not we are in beta mode :param entry: the journal entry to send @@ -854,7 +854,7 @@ class EDDN: ####################################################################### # Augmentations ####################################################################### - ret = this.eddn.entry_augment_system_data(entry, system, system_starpos) + ret = this.eddn.entry_augment_system_data(entry, system_name, system_starpos) if isinstance(ret, str): return ret @@ -1463,7 +1463,7 @@ def journal_entry( # noqa: C901, CCR001 elif event_name == 'approachsettlement': return this.eddn.export_journal_approachsettlement( cmdr, - state['StarSystem'], + system, state['StarPos'], is_beta, entry