Refactor Companion

This commit is contained in:
David Sangrey committed 2026-01-04 19:19:07 -05:00
1 parent f345dc6715
commit abe5f54be1
4 files changed
+286 -412

No files matched your search

-3
View File
@@ -27,9 +27,6 @@
/* companion.py: Generic error prefix - following text is from Frontier auth service; In files: companion.py:432; companion.py:517; */
"Error" = "Error";
/* companion.py: Frontier auth, no 'usr' section in returned data; companion.py: Frontier auth, no 'customer_id' in 'usr' section in returned data; In files: companion.py:475; companion.py:480; */
"Error: Couldn't check token customer_id" = "Error: Couldn't check token customer_id";
/* companion.py: Frontier auth customer_id doesn't match game session FID; In files: companion.py:486; */
"Error: customer_id doesn't match!" = "Error: customer_id doesn't match!";
+285 -407
View File
@@ -29,8 +29,9 @@ import webbrowser
from email.utils import parsedate
from pathlib import Path
from queue import Queue
from typing import TYPE_CHECKING, Any, TypeVar
from typing import TYPE_CHECKING, Any, TypeVar, Union, Iterator
from collections.abc import Mapping
from dataclasses import dataclass
import requests
import config as conf_module
import killswitch
@@ -66,33 +67,29 @@ commodity_map: dict = {}
class CAPIData(UserDict):
"""CAPI Response."""
"""Encapsulates a Companion API (CAPI) response."""
def __init__(
self,
data: str | dict[str, Any] | CAPIData | None = None,
source_host: str | None = None,
source_endpoint: str | None = None,
request_cmdr: str | None = None
self,
data: Union[str, dict[str, Any], 'CAPIData', None] = None,
source_host: str | None = None,
source_endpoint: str | None = None,
request_cmdr: str | None = None
) -> None:
if data is None:
super().__init__()
elif isinstance(data, str):
super().__init__(json.loads(data))
else:
super().__init__(data)
self.original_data = self.data.copy() # Just in case
self.original_data = self.data.copy()
# Metadata
self.source_host = source_host
self.source_endpoint = source_endpoint
self.request_cmdr = request_cmdr
if source_endpoint is None:
return
if source_endpoint == Session.FRONTIER_CAPI_PATH_SHIPYARD and self.data.get('lastStarport'):
# All the other endpoints may or may not have a lastStarport, but definitely won't have valid data
# for this check, which means it'll just make noise for no reason while we're working on other things
@@ -100,36 +97,34 @@ class CAPIData(UserDict):
def check_modules_ships(self) -> None:
"""
Sanity check our `data` for modules and ships being as expected.
Ensure 'modules' and 'ships' in lastStarport are properly structured.
This has side-effects of fixing `data` to be as expected in terms of
types of those elements.
Side-effects: fixes invalid or missing data to safe defaults.
"""
modules: dict[str, Any] = self.data['lastStarport'].get('modules')
if modules is None or not isinstance(modules, dict):
last_starport = self.data['lastStarport']
# Check modules
modules = last_starport.get('modules')
if not isinstance(modules, dict):
if modules is None:
logger.debug('modules was None. FC or Damaged Station?')
logger.debug('modules was None. FC or Damaged Station?')
elif isinstance(modules, list):
logger.debug('modules is empty list. Damaged Station?') if not modules \
else logger.error(f'modules is non-empty list: {modules!r}')
if not modules:
logger.debug('modules is empty list. Damaged Station?')
else:
logger.error(f'modules is non-empty list: {modules!r}')
else:
logger.error(f'modules was not None, a list, or a dict! type: {type(modules)}, content: {modules}')
logger.error(f'modules is not None, list, or dict! type: {type(modules)}, content: {modules}')
last_starport['modules'] = {}
# Set a safe value
self.data['lastStarport']['modules'] = modules = {}
ships: dict[str, Any] = self.data['lastStarport'].get('ships')
if ships is None or not isinstance(ships, dict):
# Check ships
ships = last_starport.get('ships')
if not isinstance(ships, dict):
if ships is None:
logger.debug('ships was None')
else:
logger.error(f'ships was neither None nor a dict! type: {type(ships)}, content: {ships}')
# Set a safe value
self.data['lastStarport']['ships'] = {'shipyard_list': {}, 'unavailable_list': []}
logger.error(f'ships is neither None nor dict! type: {type(ships)}, content: {ships}')
last_starport['ships'] = {'shipyard_list': {}, 'unavailable_list': []}
class CAPIDataEncoder(json.JSONEncoder):
@@ -140,168 +135,138 @@ class CAPIDataEncoder(json.JSONEncoder):
return o.__dict__
@dataclass
class CAPIDataRawEndpoint:
"""Last received CAPI response for a specific endpoint."""
"""Represents the last received CAPI response for a specific endpoint."""
def __init__(self, raw_data: str, query_time: datetime.datetime):
self.query_time = query_time
self.raw_data = raw_data
# TODO: Maybe requests.response status ?
raw_data: str
query_time: datetime.datetime
# TODO: Maybe requests.response status ?
class CAPIDataRaw:
"""The last obtained raw CAPI response for each endpoint."""
"""Stores the last obtained raw CAPI response for each endpoint."""
raw_data: dict[str, CAPIDataRawEndpoint] = {}
def __init__(self) -> None:
self.raw_data: dict[str, CAPIDataRawEndpoint] = {}
def record_endpoint(
self, endpoint: str,
raw_data: str,
query_time: datetime.datetime
):
def record_endpoint(self, endpoint: str, raw_data: str, query_time: datetime.datetime) -> None:
"""Record the latest raw data for the given endpoint."""
self.raw_data[endpoint] = CAPIDataRawEndpoint(raw_data, query_time)
def __str__(self):
"""Return a more readable string form of the data."""
capi_data_str = '{'
def __str__(self) -> str:
"""Return a readable string representation of the stored data."""
entries = []
for k, v in self.raw_data.items():
capi_data_str += f'"{k}":\n{{\n\t"query_time": "{v.query_time}",\n\t' \
f'"raw_data": {v.raw_data}\n}},\n\n'
entries.append(
f'"{k}": {{\n\t"query_time": "{v.query_time}",\n\t"raw_data": {v.raw_data}\n}}'
)
return '{\n' + ',\n\n'.join(entries) + '\n}'
capi_data_str = capi_data_str.removesuffix(',\n\n')
capi_data_str += '\n\n}'
def __iter__(self) -> Iterator[str]:
"""Iterate over stored endpoint keys."""
return iter(self.raw_data)
return capi_data_str
def __iter__(self):
"""Make this iterable on its raw_data dict."""
yield from self.raw_data.keys()
def __getitem__(self, item):
"""Make the raw_data dict's items get'able."""
def __getitem__(self, item: str) -> CAPIDataRawEndpoint:
"""Access the stored CAPIDataRawEndpoint by endpoint name."""
return self.raw_data[item]
def listify(thing: list | dict) -> list:
def listify(thing: list | dict | None) -> list[Any]:
"""
Convert actual JSON array or int-indexed dict into a Python list.
Convert a JSON array or int-indexed dict into a Python list.
Companion API sometimes returns an array as a json array, sometimes as
a json object indexed by "int". This seems to depend on whether the
there are 'gaps' in the Cmdr's data - i.e. whether the array is sparse.
In practice these arrays aren't very sparse so just convert them to
lists with any 'gaps' holding None.
Companion API sometimes returns arrays as JSON arrays, sometimes as
JSON objects indexed by integers. Sparse arrays are converted to
lists with gaps filled with None.
"""
if thing is None:
return [] # data is not present
return []
if isinstance(thing, list):
return list(thing) # array is not sparse
return list(thing)
if isinstance(thing, dict):
retval: list[Any] = []
for k, v in thing.items():
idx = int(k)
# Find maximum index to preallocate list
indices = [int(k) for k in thing.keys()]
max_idx = max(indices, default=-1)
retval: list[Any] = [None] * (max_idx + 1)
if idx >= len(retval):
retval.extend([None] * (idx - len(retval)))
retval.append(v)
else:
retval[idx] = v
for k, v in thing.items():
retval[int(k)] = v
return retval
raise ValueError(f"expected an array or sparse array, got {thing!r}")
class ServerError(Exception):
"""Exception Class for CAPI ServerErrors."""
class BaseCAPIException(Exception):
"""
Base class for all Companion API (CAPI) exceptions.
def __init__(self, *args) -> None:
# Raised when cannot contact the Companion API server
self.args = args
Subclasses should define a class variable `DEFAULT_MSG` for the
default error message. If an instance is created without args,
the default message will be used.
"""
DEFAULT_MSG: str = "Unknown CAPI error"
def __init__(self, *args: Any) -> None:
if not args:
# LANG: Frontier CAPI didn't respond
self.args = (tr.tl("Error: Frontier CAPI didn't respond"),)
args = (self.DEFAULT_MSG,)
super().__init__(*args)
class ServerLagging(BaseCAPIException):
"""Raised when the CAPI server is returning old or out-of-sync data."""
# LANG: Frontier CAPI data doesn't agree with latest Journal game location
DEFAULT_MSG = tr.tl('Error: Frontier CAPI data out of sync')
class CmdrError(BaseCAPIException):
"""Raised when the active Commander does not match the configured credentials."""
# LANG: Frontier CAPI authorisation not for currently game-active commander
DEFAULT_MSG = tr.tl('Error: Wrong Cmdr')
class CredentialsRequireRefresh(BaseCAPIException):
"""Raised when CAPI credentials require refresh (Access Token expired)."""
DEFAULT_MSG = 'CAPI: Requires refresh of Access Token'
class CredentialsError(BaseCAPIException):
"""Raised for generic CAPI credentials errors."""
# LANG: Generic "something went wrong with Frontier Auth" error
DEFAULT_MSG = tr.tl('Error: Invalid Credentials')
class NoMonitorStation(BaseCAPIException):
"""Raised when docked but the station is unknown (e.g., EDO Settlement)."""
# LANG: Commander is docked at an EDO settlement, got out and back in, we forgot the station
DEFAULT_MSG = tr.tl("Docked but unknown station: EDO Settlement?")
class ServerError(BaseCAPIException):
"""Raised when the Companion API server cannot be contacted."""
# LANG: Frontier CAPI didn't respond
DEFAULT_MSG = tr.tl("Error: Frontier CAPI didn't respond")
class ServerConnectionError(ServerError):
"""Exception class for CAPI connection errors."""
"""Raised for CAPI connection errors."""
class ServerLagging(Exception):
"""
Exception Class for CAPI Server lagging.
Raised when Companion API server is returning old data, e.g. when the
servers are too busy.
"""
def __init__(self, *args) -> None:
self.args = args
if not args:
# LANG: Frontier CAPI data doesn't agree with latest Journal game location
self.args = (tr.tl('Error: Frontier CAPI data out of sync'),)
class NoMonitorStation(Exception):
"""
Exception Class for being docked, but not knowing where in monitor.
Raised when CAPI says we're docked but we forgot where we were at an EDO
Settlement, Disembarked, re-Embarked and then user hit 'Update'.
As of 4.0.0.401 both Disembark and Embark say `"Onstation": false`.
"""
def __init__(self, *args) -> None:
self.args = args
if not args:
# LANG: Commander is docked at an EDO settlement, got out and back in, we forgot the station
self.args = (tr.tl("Docked but unknown station: EDO Settlement?"),)
class CredentialsError(Exception):
"""Exception Class for CAPI Credentials error."""
def __init__(self, *args) -> None:
self.args = args
if not args:
# LANG: Generic "something went wrong with Frontier Auth" error
self.args = (tr.tl('Error: Invalid Credentials'),)
class CredentialsRequireRefresh(Exception):
"""Exception Class for CAPI credentials requiring refresh."""
def __init__(self, *args) -> None:
self.args = args
if not args:
self.args = ('CAPI: Requires refresh of Access Token',)
class CmdrError(Exception):
"""Exception Class for CAPI Commander error.
Raised when the user has multiple accounts and the username/password
setting is not for the account they're currently playing OR the user has
reset their Cmdr and the Companion API server is still returning data
for the old Cmdr.
"""
def __init__(self, *args) -> None:
self.args = args
if not args:
# LANG: Frontier CAPI authorisation not for currently game-active commander
self.args = (tr.tl('Error: Wrong Cmdr'),)
DEFAULT_MSG = "Error: Could not connect to Companion API server"
class Auth:
"""Handles authentication with the Frontier CAPI service via oAuth2."""
"""Handles authentication with the Frontier CAPI service via OAuth2."""
# Currently the "Elite Dangerous Market Connector (EDCD/Athanasius)" one in
# Athanasius' Frontier account
# Obtain from https://auth.frontierstore.net/client/signup
CLIENT_ID = os.getenv('CLIENT_ID') or 'fb88d428-9110-475f-a3d2-dc151c2b9c7a'
# CLIENT_ID: Elite Dangerous Market Connector (EDCD/Athanasius)
CLIENT_ID: str = os.getenv('CLIENT_ID', 'fb88d428-9110-475f-a3d2-dc151c2b9c7a')
FRONTIER_AUTH_PATH_AUTH = '/auth'
FRONTIER_AUTH_PATH_TOKEN = '/token'
@@ -314,82 +279,66 @@ class Auth:
self.verifier: bytes | None = None
self.state: str | None = None
def __del__(self) -> None:
"""Ensure our Session is closed if we're being deleted."""
if self.requests_session:
self.requests_session.close()
def close(self) -> None:
"""Close the requests session."""
self.requests_session.close()
def refresh(self) -> str | None:
"""
Attempt use of Refresh Token to get a valid Access Token.
"""Attempt to use Refresh Token to get a valid Access Token, else start new authorization."""
logger.debug(f'Trying refresh for Commander "{self.cmdr}"')
If the Refresh Token doesn't work, make a new authorization request.
:return: Access Token if retrieved, else None.
"""
logger.debug(f'Trying for "{self.cmdr}"')
should_return: bool
new_data: dict[str, Any]
should_return, new_data = killswitch.check_killswitch('capi.auth', {})
# Killswitch check
should_return, _ = killswitch.check_killswitch('capi.auth', {})
if should_return:
logger.warning('capi.auth has been disabled via killswitch. Returning.')
return None
self.verifier = None
cmdrs = config.get_list('cmdrs', default=[])
logger.debug(f'Cmdrs: {cmdrs}')
idx = cmdrs.index(self.cmdr)
logger.debug(f'idx = {idx}')
try:
idx = cmdrs.index(self.cmdr)
except ValueError:
logger.error(f'Commander "{self.cmdr}" not in config cmdrs list.')
return None
tokens = config.get_list('fdev_apikeys', default=[])
tokens += [''] * (len(cmdrs) - len(tokens))
if tokens[idx]:
logger.debug('We have a refresh token for that idx')
# Try refresh
data = {
'grant_type': 'refresh_token',
'client_id': self.CLIENT_ID,
'grant_type': 'refresh_token',
'client_id': self.CLIENT_ID,
'refresh_token': tokens[idx],
}
logger.debug('Attempting refresh with Frontier...')
try:
r: requests.Response | None = None
r = self.requests_session.post(
FRONTIER_AUTH_SERVER + self.FRONTIER_AUTH_PATH_TOKEN,
data=data,
timeout=auth_timeout
)
if r.status_code == requests.codes.ok:
data = r.json()
tokens[idx] = data.get('refresh_token', '')
token_data = r.json()
tokens[idx] = token_data.get('refresh_token', '')
config.set('fdev_apikeys', tokens)
config.save() # Save settings now for use by command-line app
return data.get('access_token')
logger.error(f"Frontier CAPI Auth: Can't refresh token for \"{self.cmdr}\"")
config.save()
return token_data.get('access_token')
logger.error(f"Cannot refresh token for Commander '{self.cmdr}'")
self.dump(r)
except (ValueError, requests.RequestException) as e:
logger.exception(f"Frontier CAPI Auth: Can't refresh token for \"{self.cmdr}\"\n{e!r}")
if r is not None:
logger.exception(f"Cannot refresh token for Commander '{self.cmdr}': {e!r}")
if r:
self.dump(r)
else:
logger.error(f"Frontier CAPI Auth: No token for \"{self.cmdr}\"")
logger.error(f"No refresh token for Commander '{self.cmdr}'")
# New request
logger.info('Frontier CAPI Auth: New authorization request')
v = random.SystemRandom().getrandbits(8 * 32)
self.verifier = self.base64_url_encode(v.to_bytes(32, byteorder='big')).encode('utf-8')
s = random.SystemRandom().getrandbits(8 * 32)
self.state = self.base64_url_encode(s.to_bytes(32, byteorder='big'))
# Won't work under IE: https://blogs.msdn.microsoft.com/ieinternals/2011/07/13/understanding-protocols/
logger.info(f'Trying auth from scratch for Commander "{self.cmdr}"')
# Begin new authorization
logger.info(f'Frontier CAPI Auth: New authorization request for "{self.cmdr}"')
self.verifier = self._generate_verifier()
self.state = self._generate_state()
challenge = self.base64_url_encode(hashlib.sha256(self.verifier).digest())
webbrowser.open(
f'{FRONTIER_AUTH_SERVER}{self.FRONTIER_AUTH_PATH_AUTH}?response_type=code'
f'&audience=frontier,steam,epic'
@@ -400,38 +349,26 @@ class Auth:
f'&state={self.state}'
f'&redirect_uri={protocol.protocolhandler.redirect}'
)
return None
def authorize(self, payload: str) -> str: # noqa: CCR001
"""
Handle oAuth authorization callback.
:return: access token if successful
:raises CredentialsError
"""
logger.debug('Checking oAuth authorization callback')
def authorize(self, payload: str) -> str:
"""Handle OAuth callback and return Access Token."""
logger.debug('Checking OAuth authorization callback')
if '?' not in payload:
logger.error(f'Frontier CAPI Auth: Malformed response (no "?" in payload)\n{payload}\n')
raise CredentialsError('malformed payload') # Not well formed
raise CredentialsError('malformed payload')
data = urllib.parse.parse_qs(payload[(payload.index('?') + 1):])
if not self.state or not data.get('state') or data['state'][0] != self.state:
logger.error(f'Frontier CAPI Auth: Unexpected response\n{payload}\n')
data = urllib.parse.parse_qs(payload.split('?', 1)[1])
if not self.state or data.get('state', [None])[0] != self.state:
raise CredentialsError(f'Unexpected response from authorization {payload!r}')
if not data.get('code'):
logger.error(f'Frontier CAPI Auth: Negative response (no "code" in returned data)\n{payload}\n')
error = next(
(data[k] for k in ('error_description', 'error', 'message') if k in data),
'<unknown error>'
)
error_msg = next((data[k] for k in ('error_description', 'error', 'message')
if k in data), '<unknown error>')
# LANG: Generic error prefix - following text is from Frontier auth service
raise CredentialsError(f'{tr.tl("Error")}: {error!r}')
raise CredentialsError(f'{tr.tl("Error")}: {error_msg!r}')
r = None
try:
logger.debug('Got code, posting it back...')
request_data = {
'grant_type': 'authorization_code',
'client_id': self.CLIENT_ID,
@@ -439,174 +376,122 @@ class Auth:
'code': data['code'][0],
'redirect_uri': protocol.protocolhandler.redirect,
}
# import http.client as http_client
# http_client.HTTPConnection.debuglevel = 1
# import logging
# requests_log = logging.getLogger("requests.packages.urllib3")
# requests_log.setLevel(logging.DEBUG)
# requests_log.propagate = True
r = self.requests_session.post(
FRONTIER_AUTH_SERVER + self.FRONTIER_AUTH_PATH_TOKEN,
data=request_data,
timeout=auth_timeout
)
data_token = r.json()
if r.status_code == requests.codes.ok:
# Now we need to /decode the token to check the customer_id against FID
r = self.requests_session.get(
FRONTIER_AUTH_SERVER + self.FRONTIER_AUTH_PATH_DECODE,
headers={
'Authorization': f'Bearer {data_token.get("access_token", "")}',
'Content-Type': 'application/json',
},
timeout=auth_timeout
)
data_decode = r.json()
if r.status_code != requests.codes.ok:
r.raise_for_status()
if (usr := data_decode.get('usr')) is None:
logger.error('No "usr" in /decode data')
# LANG: Frontier auth, no 'usr' section in returned data
raise CredentialsError(tr.tl("Error: Couldn't check token customer_id"))
# Decode token to validate customer_id
r = self.requests_session.get(
FRONTIER_AUTH_SERVER + self.FRONTIER_AUTH_PATH_DECODE,
headers={
'Authorization': f'Bearer {data_token.get("access_token", "")}',
'Content-Type': 'application/json',
},
timeout=auth_timeout
)
data_decode = r.json()
usr = data_decode.get('usr')
if not usr or f'F{usr.get("customer_id")}' != monitor.state.get('FID'):
# LANG: Frontier auth customer_id doesn't match game session FID
raise CredentialsError(tr.tl("Error: customer_id doesn't match!"))
if (customer_id := usr.get('customer_id')) is None:
logger.error('No "usr"->"customer_id" in /decode data')
# LANG: Frontier auth, no 'customer_id' in 'usr' section in returned data
raise CredentialsError(tr.tl("Error: Couldn't check token customer_id"))
# Save refresh token
cmdrs = config.get_list('cmdrs', default=[])
idx = cmdrs.index(self.cmdr)
tokens = config.get_list('fdev_apikeys', default=[])
tokens += [''] * (len(cmdrs) - len(tokens))
tokens[idx] = data_token.get('refresh_token', '')
config.set('fdev_apikeys', tokens)
config.save()
# All 'FID' seen in Journals so far have been 'F<id>'
# Frontier, Steam and Epic
if f'F{customer_id}' != monitor.state.get('FID'):
# LANG: Frontier auth customer_id doesn't match game session FID
raise CredentialsError(tr.tl("Error: customer_id doesn't match!"))
logger.info(f'Frontier CAPI Auth: New token for \"{self.cmdr}\"')
cmdrs = config.get_list('cmdrs', default=[])
idx = cmdrs.index(self.cmdr)
tokens = config.get_list('fdev_apikeys', default=[])
tokens += [''] * (len(cmdrs) - len(tokens))
tokens[idx] = data_token.get('refresh_token', '')
config.set('fdev_apikeys', tokens)
config.save() # Save settings now for use by command-line app
return str(data_token.get('access_token'))
return str(data_token.get('access_token'))
except CredentialsError:
raise
except Exception as e:
logger.exception(f"Frontier CAPI Auth: Can't get token for \"{self.cmdr}\"")
logger.exception(f"Cannot get token for Commander '{self.cmdr}'")
if r:
self.dump(r)
# LANG: Failed to get Access Token from Frontier Auth service
raise CredentialsError(tr.tl('Error: unable to get token')) from e
logger.error(f"Frontier CAPI Auth: Can't get token for \"{self.cmdr}\"")
self.dump(r)
error = next(
(data[k] for k in ('error_description', 'error', 'message') if k in data),
'<unknown error>'
)
# LANG: Generic error prefix - following text is from Frontier auth service
raise CredentialsError(f'{tr.tl("Error")}: {error!r}')
@staticmethod
def invalidate(cmdr: str | None) -> None:
"""Invalidate Refresh Token for specified Commander."""
to_set: list | None = None
"""Invalidate Refresh Token for specified Commander or all if None."""
cmdrs = config.get_list('cmdrs', default=[])
tokens = config.get_list('fdev_apikeys', default=[])
tokens += [''] * (len(cmdrs) - len(tokens))
if cmdr is None:
logger.info('Frontier CAPI Auth: Invalidating ALL tokens!')
cmdrs = config.get_list('cmdrs', default=[])
to_set = [''] * len(cmdrs)
logger.info('Invalidating all tokens!')
tokens = [''] * len(cmdrs)
else:
logger.info(f'Frontier CAPI Auth: Invalidated token for "{cmdr}"')
cmdrs = config.get_list('cmdrs', default=[])
logger.info(f'Invalidated token for "{cmdr}"')
idx = cmdrs.index(cmdr)
to_set = config.get_list('fdev_apikeys', default=[])
to_set += [''] * (len(cmdrs) - len(to_set))
to_set[idx] = ''
tokens[idx] = ''
if to_set is None:
logger.error('REFUSING TO SET NONE AS TOKENS!')
raise ValueError('Unexpected None for tokens while resetting')
config.set('fdev_apikeys', tokens)
config.save()
config.set('fdev_apikeys', to_set)
config.save() # Save settings now for use by command-line app
# noinspection PyMethodMayBeStatic
def dump(self, r: requests.Response) -> None:
"""Dump details of HTTP failure from oAuth attempt."""
logger.debug(
f'Frontier CAPI Auth: {r.url} {r.status_code} {r.reason or "None"} {r.text}'
if r else
f'Frontier CAPI Auth: failed with `r` False: {r!r}'
)
"""Dump HTTP request details for debugging."""
if r:
logger.debug(f'{r.url} {r.status_code} {r.reason} {r.text}')
else:
logger.debug(f'Auth failed, r={r!r}')
# noinspection PyMethodMayBeStatic
def base64_url_encode(self, text: bytes) -> str:
"""Base64 encode text for URL."""
@staticmethod
def base64_url_encode(text: bytes) -> str:
"""Base64 URL-safe encode without padding."""
return base64.urlsafe_b64encode(text).decode().replace('=', '')
@staticmethod
def _generate_verifier() -> bytes:
v = random.SystemRandom().getrandbits(8 * 32)
return Auth.base64_url_encode(v.to_bytes(32, 'big')).encode('utf-8')
@staticmethod
def _generate_state() -> str:
s = random.SystemRandom().getrandbits(8 * 32)
return Auth.base64_url_encode(s.to_bytes(32, 'big'))
@dataclass(kw_only=True)
class EDMCCAPIReturn:
"""Base class for Request, Failure or Response."""
"""Base class for Request, Failure, or Response."""
def __init__(
self, query_time: int, tk_response_event: str | None = None,
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.play_sound: bool = play_sound # Whether to play good/bad sounds for success/failure.
self.auto_update: bool = auto_update # Whether this was automatically triggered.
query_time: int # When this query is considered to have started (time_t)
tk_response_event: str | None = None # Name of tk event to generate when response queued
play_sound: bool = False # Whether to play good/bad sounds for success/failure
auto_update: bool = False # Whether this was automatically triggered
@dataclass
class EDMCCAPIRequest(EDMCCAPIReturn):
"""Encapsulates a request for CAPI data."""
REQUEST_WORKER_SHUTDOWN = '__EDMC_WORKER_SHUTDOWN'
capi_host: str # The CAPI host to use
endpoint: str # The CAPI query to perform
def __init__(
self, capi_host: str, endpoint: str,
query_time: int,
tk_response_event: str | None = None,
play_sound: bool = False, auto_update: bool = False
):
super().__init__(
query_time=query_time, tk_response_event=tk_response_event,
play_sound=play_sound, auto_update=auto_update
)
self.capi_host: str = capi_host # The CAPI host to use.
self.endpoint: str = endpoint # The CAPI query to perform.
REQUEST_WORKER_SHUTDOWN: str = '__EDMC_WORKER_SHUTDOWN'
@dataclass
class EDMCCAPIResponse(EDMCCAPIReturn):
"""Encapsulates a response from CAPI quer(y|ies)."""
def __init__(
self, capi_data: CAPIData,
query_time: int, play_sound: bool = False, auto_update: bool = False
):
super().__init__(query_time=query_time, play_sound=play_sound, auto_update=auto_update)
self.capi_data: CAPIData = capi_data # Frontier CAPI response, possibly augmented (station query)
capi_data: CAPIData # Frontier CAPI response, possibly augmented (station query)
@dataclass
class EDMCCAPIFailedRequest(EDMCCAPIReturn):
"""CAPI failed query error class."""
"""Represents a failed CAPI query, including optional exception info."""
def __init__(
self, message: str,
query_time: int, play_sound: bool = False, auto_update: bool = False,
exception=None
):
super().__init__(query_time=query_time, play_sound=play_sound, auto_update=auto_update)
self.message: str = message # User-friendly reason for failure.
self.exception: Exception = exception # Exception that recipient should raise.
message: str # User-friendly reason for failure
exception: Exception | None = None # Optional exception to raise or inspect
class Session:
@@ -1194,78 +1079,73 @@ class Session:
######################################################################
def fixup(data: CAPIData) -> CAPIData: # noqa: C901, CCR001 # Can't be usefully simplified
"""
Fix up commodity names to English & miscellaneous anomalies fixes.
Fix up commodity names to English & miscellaneous anomaly fixes.
:return: a shallow copy of the received data suitable for export to
older tools.
"""
# Lazily populate commodity_map if empty
if not commodity_map:
# Lazily populate
fdev_path = config.app_dir_path / 'FDevIDs'
for f in ('commodity.csv', 'rare_commodity.csv'):
if not (config.app_dir_path / 'FDevIDs' / f).is_file():
csv_file = fdev_path / f
if not csv_file.is_file():
logger.warning(f'FDevID file {f} not found! Generating output without these commodity name rewrites.')
continue
with open(config.app_dir_path / 'FDevIDs' / f) as csvfile:
with open(csv_file, newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
commodity_map[row['symbol']] = (row['category'], row['name'])
commodities = []
for commodity in data['lastStarport'].get('commodities') or []:
# Check all required numeric fields are present and are numeric
# Catches "demandBracket": "" for some phantom commodites in
# ED 1.3 - https://github.com/Marginal/EDMarketConnector/issues/2
#
# But also see https://github.com/Marginal/EDMarketConnector/issues/32
for thing in ('buyPrice', 'sellPrice', 'demand', 'demandBracket', 'stock', 'stockBracket'):
if not isinstance(commodity.get(thing), numbers.Number):
logger.debug(f'Invalid {thing}:{commodity.get(thing)} ({type(commodity.get(thing))}) for {commodity.get("name", "")}') # noqa: E501
break
# Ensure all required numeric fields are numbers
numeric_fields = ('buyPrice', 'sellPrice', 'demand', 'demandBracket', 'stock', 'stockBracket')
if not all(isinstance(commodity.get(f), numbers.Number) for f in numeric_fields):
for f in numeric_fields:
if not isinstance(commodity.get(f), numbers.Number):
logger.debug(
f'Invalid {f}:{commodity.get(f)} ({type(commodity.get(f))}) for {commodity.get("name", "")}'
)
continue
else:
# Check not marketable i.e. Limpets
if not category_map.get(commodity['categoryname'], True):
pass
# Skip non-marketable or not normally stocked commodities
if not category_map.get(commodity['categoryname'], True):
continue
if commodity['demandBracket'] == 0 and commodity['stockBracket'] == 0:
continue
if commodity.get('legality'):
continue
# Check not normally stocked e.g. Salvage
elif commodity['demandBracket'] == 0 and commodity['stockBracket'] == 0:
pass
elif commodity.get('legality'): # Check not prohibited
pass
# Log missing fields
if not commodity.get('categoryname'):
logger.debug(f'Missing "categoryname" for {commodity.get("name", "")}')
if not commodity.get('name'):
logger.debug(f'Missing "name" for a commodity in {commodity.get("categoryname", "")}')
elif not commodity.get('categoryname'):
logger.debug(f'Missing "categoryname" for {commodity.get("name", "")}')
# Validate bracket ranges
if commodity['demandBracket'] not in range(4):
logger.debug(f'Invalid "demandBracket":{commodity["demandBracket"]} for {commodity["name"]}')
if commodity['stockBracket'] not in range(4):
logger.debug(f'Invalid "stockBracket":{commodity["stockBracket"]} for {commodity["name"]}')
elif not commodity.get('name'):
logger.debug(f'Missing "name" for a commodity in {commodity.get("categoryname", "")}')
# All checks passed, rewrite fields
new = dict(commodity) # shallow copy
if commodity['name'] in commodity_map:
new['categoryname'], new['name'] = commodity_map[commodity['name']]
elif commodity['categoryname'] in category_map:
new['categoryname'] = category_map[commodity['categoryname']]
elif not commodity['demandBracket'] in range(4):
logger.debug(f'Invalid "demandBracket":{commodity["demandBracket"]} for {commodity["name"]}')
# Force demand and stock to zero if corresponding bracket is zero
if not commodity['demandBracket']:
new['demand'] = 0
if not commodity['stockBracket']:
new['stock'] = 0
elif not commodity['stockBracket'] in range(4):
logger.debug(f'Invalid "stockBracket":{commodity["stockBracket"]} for {commodity["name"]}')
commodities.append(new)
else:
# Rewrite text fields
new = dict(commodity) # shallow copy
if commodity['name'] in commodity_map:
(new['categoryname'], new['name']) = commodity_map[commodity['name']]
elif commodity['categoryname'] in category_map:
new['categoryname'] = category_map[commodity['categoryname']]
# Force demand and stock to zero if their corresponding bracket is zero
# Fixes spurious "demand": 1 in ED 1.3
if not commodity['demandBracket']:
new['demand'] = 0
if not commodity['stockBracket']:
new['stock'] = 0
# We're good
commodities.append(new)
# return a shallow copy
# Shallow copy for return
datacopy = data.copy()
datacopy['lastStarport'] = data['lastStarport'].copy()
datacopy['lastStarport']['commodities'] = commodities
@@ -1273,25 +1153,23 @@ def fixup(data: CAPIData) -> CAPIData: # noqa: C901, CCR001 # Can't be usefully
def ship(data: CAPIData) -> CAPIData:
"""Construct a subset of the received data describing the current ship."""
"""Construct a filtered subset of the received data describing the current ship."""
skip_keys = {
'alive', 'cargo', 'cockpitBreached', 'health', 'oxygenRemaining',
'rebuilds', 'starsystem', 'station', 'dir', 'LessIsGood'
}
noisy_keys = ('locDescription', 'locName')
noisy_suffixes = ('LocDescription', 'LocName')
def filter_ship(d: CAPIData) -> CAPIData:
"""Filter provided ship data."""
filtered = CAPIData()
for k, v in d.items():
if not v:
pass # just skip empty fields for brevity
elif k in ('alive', 'cargo', 'cockpitBreached', 'health', 'oxygenRemaining',
'rebuilds', 'starsystem', 'station'):
pass # noisy
elif k in ('locDescription', 'locName') or k.endswith('LocDescription') or k.endswith('LocName'):
pass # also noisy, and redundant
elif k in ('dir', 'LessIsGood'):
pass # dir is not ASCII - remove to simplify handling
elif hasattr(v, 'items'):
continue
if k in skip_keys or k in noisy_keys or k.endswith(noisy_suffixes):
continue
if hasattr(v, 'items'): # recursively handle dict-like objects
filtered[k] = filter_ship(v)
else:
+1 -1
View File
@@ -258,8 +258,8 @@ class PluginBrowserMixIn:
self.__populate_plugins_tree()
notebook.add(
# LANG: Plugin Browser Title
plugins_frame,
# LANG: Plugin Browser Title
text=tr.tl("Plugin Browser"),
)
@@ -37,7 +37,6 @@ UninstallDisplayIcon={app}\{#MyAppExeName}
MinVersion=6.2
ChangesAssociations = yes
UsedUserAreasWarning = no
VersionInfoVersion={#MyAppVersion}
VersionInfoCompany={#MyAppPublisher}
VersionInfoDescription={#MyAppLongName}
VersionInfoCopyright={#MyAppCopyright}