From 1dcefafc73c83c45c8f500b4940e883afd0cf7e3 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Tue, 24 Aug 2021 12:41:02 +0100 Subject: [PATCH] CAPI: Pass tk_response_event in Request to optionally use in response EDMC.py will also need to make CAPI queries using the new threaded method, but it has no tkinter, thus we need to pass in the event name when we expect one to be generated, and only generate it if this is not None. --- EDMarketConnector.py | 3 ++- companion.py | 48 +++++++++++++++++++++++++++++++------------- stats.py | 1 + 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/EDMarketConnector.py b/EDMarketConnector.py index 901eb763..0c9fba61 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -375,6 +375,7 @@ SHIPYARD_HTML_TEMPLATE = """ class AppWindow(object): """Define the main application window.""" + _CAPI_RESPONSE_TK_EVENT_NAME = '<>' # Tkinter Event types EVENT_KEYPRESS = 2 EVENT_BUTTON = 4 @@ -655,7 +656,7 @@ class AppWindow(object): self.w.bind('', self.capi_request_data) self.w.bind('', self.capi_request_data) self.w.bind_all('<>', self.capi_request_data) # Ask for CAPI queries to be performed - self.w.bind_all('<>', self.capi_handle_response) + self.w.bind_all(self._CAPI_RESPONSE_TK_EVENT_NAME, self.capi_handle_response) self.w.bind_all('<>', self.journal_event) # Journal monitoring self.w.bind_all('<>', self.dashboard_event) # Dashboard monitoring self.w.bind_all('<>', self.plugin_error) # Statusbar diff --git a/companion.py b/companion.py index 36be1756..f45e3999 100644 --- a/companion.py +++ b/companion.py @@ -135,7 +135,7 @@ class CAPIDataRawEndpoint: class CAPIDataRaw: - """The last obtained raw CAPI response for each end point.""" + """The last obtained raw CAPI response for each endpoint.""" raw_data: Dict[str, CAPIDataRawEndpoint] = {} @@ -516,9 +516,10 @@ class EDMCCAPIReturn: """Base class for Request, Failure or Response.""" def __init__( - self, query_time: int, retrying: bool = False, - play_sound: bool = False, auto_update: bool = False + self, query_time: int, tk_response_event: Optional[str] = None, + retrying: bool = False, play_sound: bool = False, auto_update: bool = False ): + self.tk_response_event = tk_response_event # Name of tk event to generate when response queued. self.query_time: int = query_time # When this query is considered to have started (time_t). self.retrying: bool = retrying # Whether this is already a retry. self.play_sound: bool = play_sound # Whether to play good/bad sounds for success/failure. @@ -529,10 +530,14 @@ class EDMCCAPIRequest(EDMCCAPIReturn): """Encapsulates a request for CAPI data.""" def __init__( - self, endpoint: str, - query_time: int, retrying: bool = False, play_sound: bool = False, auto_update: bool = False + self, endpoint: str, query_time: int, + tk_response_event: Optional[str] = None, retrying: bool = False, + play_sound: bool = False, auto_update: bool = False ): - super().__init__(query_time=query_time, retrying=retrying, play_sound=play_sound, auto_update=auto_update) + super().__init__( + query_time=query_time, tk_response_event=tk_response_event, retrying=retrying, + play_sound=play_sound, auto_update=auto_update + ) self.endpoint: str = endpoint # The CAPI query to perform. @@ -927,7 +932,10 @@ class Session(object): ) ) - self.tk_master.event_generate('<>') + # If the query came from EDMC.(py|exe) there's no tk to send an + # event too, so assume it will be polling there response queue. + if query.tk_response_event is not None: + self.tk_master.event_generate('<>') logger.info('CAPI worker thread DONE') @@ -936,12 +944,15 @@ class Session(object): self.capi_query_queue.put(None) def query( - self, endpoint: str, - query_time: int, retrying: bool = False, play_sound: bool = False, auto_update: bool = False + self, endpoint: str, query_time: int, + tk_response_event: Optional[str] = None, retrying: bool = False, + play_sound: bool = False, auto_update: bool = False ) -> None: """ Perform a query against the specified CAPI endpoint. + :param endpoint: The CAPI endpoint to query. + :param tk_response_event: Name of tk event to generate when response queued. :param query_time: When this query was initiated. :param retrying: Whether this is a retry. :param play_sound: Whether the app should play a sound on error. @@ -950,7 +961,10 @@ class Session(object): logger.trace_if('capi.query', f'Performing query for endpoint "{endpoint}"') if self.state == Session.STATE_INIT: if self.login(): - self.query(endpoint, query_time, play_sound=play_sound, auto_update=auto_update) + self.query( + endpoint, query_time, tk_response_event=tk_response_event, play_sound=play_sound, + auto_update=auto_update + ) return elif self.state == Session.STATE_AUTH: @@ -964,6 +978,7 @@ class Session(object): self.capi_query_queue.put( EDMCCAPIRequest( endpoint=endpoint, + tk_response_event=tk_response_event, retrying=retrying, query_time=query_time, play_sound=play_sound, @@ -973,30 +988,34 @@ class Session(object): def profile( self, - query_time: int = int(time.time()), retrying: bool = False, + query_time: int = int(time.time()), + tk_response_event: Optional[str] = None, retrying: bool = False, play_sound: bool = False, auto_update: bool = False ) -> None: """ Perform general CAPI /profile endpoint query. :param query_time: When this query was initiated. + :param tk_response_event: Name of tk event to generate when response queued. :param retrying: Whether this is a retry. :param play_sound: Whether the app should play a sound on error. :param auto_update: Whether this request was triggered automatically. """ self.query( - self.FRONTIER_CAPI_PATH_PROFILE, query_time=query_time, retrying=retrying, + self.FRONTIER_CAPI_PATH_PROFILE, query_time=query_time, + tk_response_event=tk_response_event, retrying=retrying, play_sound=play_sound, auto_update=auto_update ) def station( - self, - query_time: int, retrying: bool = False, play_sound: bool = False, auto_update: bool = False + self, query_time: int, tk_response_event: Optional[str] = None, + retrying: bool = False, play_sound: bool = False, auto_update: bool = False ) -> None: """ Perform CAPI quer(y|ies) for station data. :param query_time: When this query was initiated. + :param tk_response_event: Name of tk event to generate when response queued. :param retrying: Whether this is a retry. :param play_sound: Whether the app should play a sound on error. :param auto_update: Whether this request was triggered automatically. @@ -1005,6 +1024,7 @@ class Session(object): self.capi_query_queue.put( EDMCCAPIRequest( endpoint=self._CAPI_PATH_STATION, + tk_response_event=tk_response_event, query_time=query_time, retrying=retrying, play_sound=play_sound, diff --git a/stats.py b/stats.py index 6a9c19bf..74f28e9c 100644 --- a/stats.py +++ b/stats.py @@ -281,6 +281,7 @@ class StatsDialog(): self.parent.update_idletasks() try: + # TODO: This should use cached data data = companion.session.profile() except companion.ServerError as e: