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 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 a9f25d57..8da1954f 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_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: 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_name, 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 """ @@ -1090,6 +1103,48 @@ class EDDN: this.eddn.export_journal_entry(cmdr, entry, msg) return None + def export_journal_approachsettlement( + 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 + """ + # { + # "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, system_name, system_starpos) + 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. @@ -1331,6 +1386,7 @@ def journal_entry( # noqa: C901, CCR001 return None entry = new_data + event_name = entry['event'].lower() this.on_foot = state['OnFoot'] @@ -1340,8 +1396,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 +1405,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 +1424,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 +1436,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 +1444,34 @@ 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': - return this.eddn.export_journal_fssdiscoveryscan(cmdr, system, is_beta, entry) - if entry['event'].lower() == 'navbeaconscan': - return this.eddn.export_journal_navbeaconscan(cmdr, system, is_beta, entry) + if event_name == 'fssdiscoveryscan': + return this.eddn.export_journal_fssdiscoveryscan(cmdr, system, state['StarPos'], is_beta, entry) - if entry['event'].lower() == 'codexentry': - return this.eddn.export_journal_codexentry(cmdr, is_beta, entry) + elif event_name == 'navbeaconscan': + return this.eddn.export_journal_navbeaconscan(cmdr, system, state['StarPos'], is_beta, entry) - if entry['event'].lower() == 'scanbarycentre': - return this.eddn.export_journal_scanbarycentre(cmdr, is_beta, entry) + elif event_name == 'codexentry': + return this.eddn.export_journal_codexentry(cmdr, state['StarPos'], is_beta, entry) - if entry['event'].lower() == 'navroute': + elif event_name == 'scanbarycentre': + 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, + system, + 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 - (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 +1501,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 +1545,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 +1560,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)