mirror of
https://github.com/EDCD/EDMarketConnector.git
synced 2026-11-20 03:02:40 +03:00
Merge pull request #2679 from HullSeals/enhancement/rixs-refactor-fun
Significant Refactor and Several Bugfixes
This commit is contained in:
37 files changed
+2627
-2474
No files matched your search
@@ -13,10 +13,14 @@ import json
|
||||
import os
|
||||
import queue
|
||||
import sys
|
||||
import warnings
|
||||
from enum import IntEnum
|
||||
from pathlib import Path
|
||||
from time import sleep, time
|
||||
from time import sleep
|
||||
from datetime import datetime, timezone
|
||||
from typing import TYPE_CHECKING, Any
|
||||
from common_utils import log_locale, SERVER_RETRY
|
||||
from contextlib import suppress
|
||||
|
||||
# isort: off
|
||||
os.environ["EDMC_NO_UI"] = "1"
|
||||
@@ -54,475 +58,512 @@ import eddn # noqa: E402
|
||||
# isort: on
|
||||
tr.install_dummy()
|
||||
|
||||
EXIT_SUCCESS, EXIT_SERVER, EXIT_CREDENTIALS, EXIT_VERIFICATION, EXIT_LAGGING, EXIT_SYS_ERR, EXIT_ARGS, \
|
||||
EXIT_JOURNAL_READ_ERR, EXIT_COMMANDER_UNKNOWN = range(9)
|
||||
|
||||
class ExitCode(IntEnum):
|
||||
"""List the various available exit codes."""
|
||||
|
||||
SUCCESS = 0
|
||||
SERVER = 1
|
||||
CREDENTIALS = 2
|
||||
VERIFICATION = 3
|
||||
LAGGING = 4
|
||||
SYS_ERR = 5
|
||||
ARGS = 6
|
||||
JOURNAL_READ_ERR = 7
|
||||
COMMANDER_UNKNOWN = 8
|
||||
|
||||
|
||||
def deep_get(target: dict | companion.CAPIData, *args: str, default=None) -> Any:
|
||||
# Backwards Compatibility
|
||||
EXIT_SUCCESS = ExitCode.SUCCESS
|
||||
EXIT_SERVER = ExitCode.SERVER
|
||||
EXIT_CREDENTIALS = ExitCode.CREDENTIALS
|
||||
EXIT_VERIFICATION = ExitCode.VERIFICATION
|
||||
EXIT_LAGGING = ExitCode.LAGGING
|
||||
EXIT_SYS_ERR = ExitCode.SYS_ERR
|
||||
EXIT_ARGS = ExitCode.ARGS
|
||||
EXIT_JOURNAL_READ_ERR = ExitCode.JOURNAL_READ_ERR
|
||||
EXIT_COMMANDER_UNKNOWN = ExitCode.COMMANDER_UNKNOWN
|
||||
|
||||
|
||||
def deep_get(target: companion.CAPIData, *args: str, default=None) -> Any:
|
||||
"""
|
||||
Walk into a dict and return the specified deep value.
|
||||
|
||||
Example usage:
|
||||
|
||||
>>> thing = {'a': {'b': {'c': 'foo'} } }
|
||||
>>> deep_get(thing, ('a', 'b', 'c'), None)
|
||||
'foo'
|
||||
>>> deep_get(thing, ('a', 'b'), None)
|
||||
{'c': 'foo'}
|
||||
>>> deep_get(thing, ('a', 'd'), None)
|
||||
None
|
||||
|
||||
:param target: The dict to walk into for the desired value.
|
||||
:param args: The list of keys to walk down through.
|
||||
:param default: What to return if the target has no value.
|
||||
:return: The value at the target deep key.
|
||||
Deprecated! Will be removed in 6.4 or later.
|
||||
Use structural pattern matching or explicit dict `.get()` chains instead.
|
||||
"""
|
||||
warnings.warn(
|
||||
"deep_get is deprecated and will be removed in a future version. "
|
||||
"Use modern pattern matching (match/case) or sequential dict.get() chains instead.",
|
||||
category=DeprecationWarning,
|
||||
stacklevel=2
|
||||
)
|
||||
|
||||
if not hasattr(target, 'get'):
|
||||
raise ValueError(f"Cannot call get on {target} ({type(target)})")
|
||||
|
||||
current = target
|
||||
for arg in args:
|
||||
res = current.get(arg)
|
||||
if res is None:
|
||||
return default
|
||||
current = res
|
||||
match current:
|
||||
case dict() | companion.CAPIData():
|
||||
if (res := current.get(arg)) is None:
|
||||
return default
|
||||
current = res
|
||||
case _:
|
||||
return default
|
||||
|
||||
return current
|
||||
|
||||
|
||||
def main() -> None: # noqa: C901, CCR001
|
||||
"""Run the main code of the program."""
|
||||
def parse_cli_args() -> argparse.Namespace:
|
||||
"""Parse and return command-line arguments."""
|
||||
parser = argparse.ArgumentParser(
|
||||
prog=appcmdname,
|
||||
description='Prints the current system and station (if docked) to stdout and optionally writes player '
|
||||
'status, ship locations, ship loadout and/or station data to file. '
|
||||
'Requires prior setup through the accompanying GUI app.'
|
||||
)
|
||||
|
||||
parser.add_argument('-v', '--version', help='print program version and exit', action='store_const', const=True)
|
||||
group_loglevel = parser.add_mutually_exclusive_group()
|
||||
group_loglevel.add_argument(
|
||||
'--loglevel', metavar='loglevel',
|
||||
help='Set the logging loglevel to one of: CRITICAL, ERROR, WARNING, INFO, DEBUG, TRACE'
|
||||
)
|
||||
parser.add_argument('--trace', help='Set the Debug logging loglevel to TRACE', action='store_true')
|
||||
parser.add_argument('--trace-on', action='append',
|
||||
help='Mark the selected trace logging as active. "*" or "all" is equivalent to --trace-all')
|
||||
parser.add_argument("--trace-all", action='store_true',
|
||||
help='Force trace level logging, with all possible --trace-on values active.')
|
||||
parser.add_argument("--trace-help", action='store_true',
|
||||
help="Open the documentation to view all possible trace-on values")
|
||||
parser.add_argument('--refresh-all', action='store_true',
|
||||
help='Iterate over all known CMDRs to try and refresh access tokens')
|
||||
parser.add_argument('--config', help="Define a custom config to load EDMC from")
|
||||
|
||||
parser.add_argument('-a', metavar='FILE', help='write ship loadout to FILE in Companion API json format')
|
||||
parser.add_argument('-e', metavar='FILE', help='write ship loadout to FILE in E:D Shipyard plain text format')
|
||||
parser.add_argument('-l', metavar='FILE', help='write ship locations to FILE in CSV format')
|
||||
parser.add_argument('-m', metavar='FILE', help='write station commodity market data to FILE in CSV format')
|
||||
parser.add_argument('-o', metavar='FILE', help='write station outfitting data to FILE in CSV format')
|
||||
parser.add_argument('-s', metavar='FILE', help='write station shipyard data to FILE in CSV format')
|
||||
parser.add_argument('-t', metavar='FILE', help='write player status to FILE in CSV format')
|
||||
parser.add_argument('-d', metavar='FILE', help='write raw JSON data to FILE')
|
||||
parser.add_argument('-n', action='store_true', help='send data to EDDN')
|
||||
parser.add_argument('-p', metavar='CMDR', help='Returns data from the specified player account')
|
||||
parser.add_argument('-j', help=argparse.SUPPRESS) # Import JSON dump
|
||||
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def handle_version_check() -> None:
|
||||
"""Check for appcast updates and print version strings."""
|
||||
updater = Updater()
|
||||
newversion: EDMCVersion | None = updater.check_appcast()
|
||||
if newversion:
|
||||
# LANG: Update Available Text
|
||||
newverstr: str = tr.tl("{NEWVER} is available").format(NEWVER=newversion.title)
|
||||
print(f'{appversion()} ({newverstr})')
|
||||
else:
|
||||
print(appversion())
|
||||
|
||||
|
||||
def _determine_log_level(args: argparse.Namespace) -> int | str | None:
|
||||
"""Evaluate the correct log level with clean early-exit precedence."""
|
||||
if args.trace_all or (args.trace_on and any(x in args.trace_on for x in ('*', 'all'))):
|
||||
logger.info('Setting TRACE_ALL level debugging due to --trace-all or wildcard flag')
|
||||
return logging.TRACE_ALL # type: ignore
|
||||
|
||||
if args.trace or args.trace_on:
|
||||
logger.info('Setting TRACE level debugging due to --trace or --trace-on')
|
||||
return logging.TRACE # type: ignore
|
||||
|
||||
return args.loglevel
|
||||
|
||||
|
||||
def configure_logging_and_env(args: argparse.Namespace) -> None:
|
||||
"""Configure logger log levels, trace environments, and initial config payloads."""
|
||||
if level := _determine_log_level(args):
|
||||
if isinstance(level, int):
|
||||
logger.setLevel(level)
|
||||
edmclogger.set_channels_loglevel(level)
|
||||
else:
|
||||
edmclogger.set_channels_loglevel(level)
|
||||
|
||||
# Environment Configuration updates
|
||||
if args.config:
|
||||
config.reload_from_path(args.config)
|
||||
|
||||
logger.debug(f'Startup v{appversion()} : Running on Python v{sys.version}')
|
||||
logger.debug(
|
||||
f'Platform: {sys.platform}\n'
|
||||
f'argv[0]: {sys.argv[0]}\n'
|
||||
f'exec_prefix: {sys.exec_prefix}\n'
|
||||
f'executable: {sys.executable}\n'
|
||||
f'sys.path: {sys.path}'
|
||||
)
|
||||
|
||||
if args.trace_on:
|
||||
import config as conf_module
|
||||
conf_module.trace_on = [x.casefold() for x in args.trace_on]
|
||||
for trace_item in conf_module.trace_on:
|
||||
logger.info(f'marked {trace_item} for TRACE')
|
||||
|
||||
log_locale('Initial Locale')
|
||||
|
||||
if args.trace_help:
|
||||
import webbrowser
|
||||
logger.info("Opening Trace Help Documentation")
|
||||
webbrowser.open("https://github.com/EDCD/EDMarketConnector/blob/main/docs/Available%20Traces.md")
|
||||
|
||||
|
||||
def refresh_all_cmdrs() -> None:
|
||||
"""Attempt to iterate and refresh access tokens for all known commanders."""
|
||||
logger.debug("Refreshing all known CMDRs")
|
||||
cmdrs = config.get_list('cmdrs', default=[])
|
||||
for cmdr in cmdrs:
|
||||
logger.debug(f'Attempting to use commander "{cmdr}"')
|
||||
try:
|
||||
companion.session.login(cmdr, monitor.is_beta)
|
||||
logger.debug("Succeeded!")
|
||||
except AttributeError:
|
||||
logger.debug(f"Unable to refresh CMDR {cmdr}.")
|
||||
|
||||
|
||||
def load_json_dump(json_file_path: str) -> companion.CAPIData:
|
||||
"""Load and return CAPI data from a local JSON dump file."""
|
||||
logger.debug('Import and collate from JSON dump')
|
||||
json_file = str(Path(json_file_path).resolve())
|
||||
data = None
|
||||
|
||||
with suppress(UnicodeDecodeError), open(json_file, encoding='utf-8') as file_handle:
|
||||
data = json.load(file_handle)
|
||||
|
||||
if data is None:
|
||||
with open(json_file, encoding='utf-8') as file_handle:
|
||||
data = json.load(file_handle)
|
||||
|
||||
config.set('querytime', int(Path(json_file_path).stat().st_mtime))
|
||||
return data
|
||||
|
||||
|
||||
def load_state_from_journal_and_capi(args: argparse.Namespace) -> companion.CAPIData:
|
||||
"""Read the latest Journal state and fetch live CAPI data."""
|
||||
logger.debug('Getting state from latest journal file')
|
||||
try:
|
||||
# arg parsing
|
||||
parser = argparse.ArgumentParser(
|
||||
prog=appcmdname,
|
||||
description='Prints the current system and station (if docked) to stdout and optionally writes player '
|
||||
'status, ship locations, ship loadout and/or station data to file. '
|
||||
'Requires prior setup through the accompanying GUI app.'
|
||||
)
|
||||
monitor.currentdir = config.get_str('journaldir',
|
||||
default=config.default_journal_dir) or config.default_journal_dir
|
||||
logger.debug(f'logdir = "{monitor.currentdir}"')
|
||||
|
||||
parser.add_argument('-v', '--version', help='print program version and exit', action='store_const', const=True)
|
||||
group_loglevel = parser.add_mutually_exclusive_group()
|
||||
group_loglevel.add_argument('--loglevel',
|
||||
metavar='loglevel',
|
||||
help='Set the logging loglevel to one of: '
|
||||
'CRITICAL, ERROR, WARNING, INFO, DEBUG, TRACE',
|
||||
)
|
||||
if (logfile := monitor.journal_newest_filename(monitor.currentdir)) is None:
|
||||
raise ValueError("None from monitor.journal_newest_filename")
|
||||
|
||||
parser.add_argument(
|
||||
'--trace',
|
||||
help='Set the Debug logging loglevel to TRACE',
|
||||
action='store_true',
|
||||
)
|
||||
logger.debug(f'Using logfile "{logfile}"')
|
||||
with open(logfile, 'rb', 0) as loghandle:
|
||||
for line in loghandle:
|
||||
with suppress(Exception):
|
||||
monitor.parse_entry(line)
|
||||
continue
|
||||
logger.debug(f'Invalid journal entry {line!r}')
|
||||
|
||||
parser.add_argument(
|
||||
'--trace-on',
|
||||
help='Mark the selected trace logging as active. "*" or "all" is equivalent to --trace-all',
|
||||
action='append',
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("Can't read Journal file")
|
||||
sys.exit(ExitCode.JOURNAL_READ_ERR)
|
||||
|
||||
parser.add_argument(
|
||||
"--trace-all",
|
||||
help='Force trace level logging, with all possible --trace-on values active.',
|
||||
action='store_true'
|
||||
)
|
||||
if not monitor.cmdr:
|
||||
logger.error('Not available while E:D is at the main menu')
|
||||
sys.exit(ExitCode.COMMANDER_UNKNOWN)
|
||||
|
||||
parser.add_argument(
|
||||
"--trace-help",
|
||||
help="Open the documentation to view all possible trace-on values",
|
||||
action='store_true'
|
||||
)
|
||||
cmdrs = config.get_list('cmdrs', default=[])
|
||||
target_cmdr = args.p or monitor.cmdr
|
||||
logger.debug(f'Attempting to use commander "{target_cmdr}"' + (' from Journal File' if not args.p else ''))
|
||||
|
||||
parser.add_argument(
|
||||
'--refresh-all',
|
||||
help='Iterate over all known CMDRs to try and refresh access tokens',
|
||||
action='store_true',
|
||||
)
|
||||
try:
|
||||
cmdr_to_use = next(c for c in cmdrs if c.lower() == target_cmdr.lower())
|
||||
except StopIteration:
|
||||
raise companion.CredentialsError() from StopIteration
|
||||
|
||||
parser.add_argument('--config', help="Define a custom config to load EDMC from")
|
||||
try:
|
||||
companion.session.login(cmdr_to_use, monitor.is_beta)
|
||||
except AttributeError:
|
||||
raise companion.CredentialsError() from AttributeError
|
||||
|
||||
parser.add_argument('-a', metavar='FILE', help='write ship loadout to FILE in Companion API json format')
|
||||
parser.add_argument('-e', metavar='FILE', help='write ship loadout to FILE in E:D Shipyard plain text format')
|
||||
parser.add_argument('-l', metavar='FILE', help='write ship locations to FILE in CSV format')
|
||||
parser.add_argument('-m', metavar='FILE', help='write station commodity market data to FILE in CSV format')
|
||||
parser.add_argument('-o', metavar='FILE', help='write station outfitting data to FILE in CSV format')
|
||||
parser.add_argument('-s', metavar='FILE', help='write station shipyard data to FILE in CSV format')
|
||||
parser.add_argument('-t', metavar='FILE', help='write player status to FILE in CSV format')
|
||||
parser.add_argument('-d', metavar='FILE', help='write raw JSON data to FILE')
|
||||
parser.add_argument('-n', action='store_true', help='send data to EDDN')
|
||||
parser.add_argument('-p', metavar='CMDR', help='Returns data from the specified player account')
|
||||
parser.add_argument('-j', help=argparse.SUPPRESS) # Import JSON dump
|
||||
args = parser.parse_args()
|
||||
# Initiate CAPI queries
|
||||
querytime = int(datetime.now(timezone.utc).timestamp())
|
||||
companion.session.station(query_time=querytime)
|
||||
|
||||
if args.version:
|
||||
updater = Updater()
|
||||
newversion: EDMCVersion | None = updater.check_appcast()
|
||||
if newversion:
|
||||
# LANG: Update Available Text
|
||||
newverstr: str = tr.tl("{NEWVER} is available").format(NEWVER=newversion.title)
|
||||
print(f'{appversion()} ({newverstr})')
|
||||
else:
|
||||
print(appversion())
|
||||
return
|
||||
# Wait for the response
|
||||
_capi_request_timeout = 60
|
||||
try:
|
||||
capi_response = companion.session.capi_response_queue.get(block=True, timeout=_capi_request_timeout)
|
||||
except queue.Empty:
|
||||
logger.error(f'CAPI requests timed out after {_capi_request_timeout} seconds')
|
||||
sys.exit(ExitCode.SERVER)
|
||||
|
||||
level_to_set: int | None = None
|
||||
if args.trace or args.trace_on:
|
||||
level_to_set = logging.TRACE # type: ignore # it exists
|
||||
logger.info('Setting TRACE level debugging due to either --trace or a --trace-on')
|
||||
|
||||
if args.trace_all or (args.trace_on and ('*' in args.trace_on or 'all' in args.trace_on)):
|
||||
level_to_set = logging.TRACE_ALL # type: ignore # it exists
|
||||
logger.info('Setting TRACE_ALL level debugging due to either --trace-all or a --trace-on *|all')
|
||||
|
||||
if level_to_set is not None:
|
||||
logger.setLevel(level_to_set)
|
||||
edmclogger.set_channels_loglevel(level_to_set)
|
||||
|
||||
elif args.loglevel:
|
||||
if args.loglevel not in ('CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG', 'TRACE'):
|
||||
print('loglevel must be one of: CRITICAL, ERROR, WARNING, INFO, DEBUG, TRACE', file=sys.stderr)
|
||||
sys.exit(EXIT_ARGS)
|
||||
edmclogger.set_channels_loglevel(args.loglevel)
|
||||
|
||||
if args.config:
|
||||
config.reload_from_path(args.config)
|
||||
|
||||
logger.debug(f'Startup v{appversion()} : Running on Python v{sys.version}')
|
||||
logger.debug(f'''Platform: {sys.platform}
|
||||
argv[0]: {sys.argv[0]}
|
||||
exec_prefix: {sys.exec_prefix}
|
||||
executable: {sys.executable}
|
||||
sys.path: {sys.path}'''
|
||||
)
|
||||
if args.trace_on and len(args.trace_on) > 0:
|
||||
import config as conf_module
|
||||
|
||||
conf_module.trace_on = [x.casefold() for x in args.trace_on] # duplicate the list just in case
|
||||
for d in conf_module.trace_on:
|
||||
logger.info(f'marked {d} for TRACE')
|
||||
|
||||
log_locale('Initial Locale')
|
||||
|
||||
if args.trace_help:
|
||||
import webbrowser
|
||||
logger.info("Opening Trace Help Documentation")
|
||||
webbrowser.open("https://github.com/EDCD/EDMarketConnector/blob/main/docs/Available%20Traces.md")
|
||||
|
||||
if args.refresh_all:
|
||||
# Attempt to refresh all known CMDRs. This MAY cause additional output if a token is invalid.
|
||||
logger.debug("Refreshing all known CMDRs")
|
||||
cmdrs = config.get_list('cmdrs', default=[])
|
||||
for cmdr in cmdrs:
|
||||
logger.debug(f'Attempting to use commander "{cmdr}"')
|
||||
try:
|
||||
companion.session.login(cmdr, monitor.is_beta)
|
||||
logger.debug("Succeeded!")
|
||||
except AttributeError:
|
||||
logger.debug(f"Unable to refresh CMDR {cmdr}.")
|
||||
|
||||
if args.j:
|
||||
logger.debug('Import and collate from JSON dump')
|
||||
# Import and collate from JSON dump
|
||||
#
|
||||
# Try twice, once with the system locale and once enforcing utf-8. If the file was made on the current
|
||||
# system, chances are its the current locale, and not utf-8. Otherwise if it was copied, its probably
|
||||
# utf8. Either way, try the system FIRST because reading something like cp1251 in UTF-8 results in garbage
|
||||
# but the reverse results in an exception.
|
||||
json_file = str(Path(args.j).resolve())
|
||||
try:
|
||||
with open(json_file) as file_handle:
|
||||
data = json.load(file_handle)
|
||||
except UnicodeDecodeError:
|
||||
with open(json_file, encoding='utf-8') as file_handle:
|
||||
data = json.load(file_handle)
|
||||
config.set('querytime', int(Path(args.j).stat().st_mtime))
|
||||
|
||||
else:
|
||||
# Get state from latest Journal file
|
||||
logger.debug('Getting state from latest journal file')
|
||||
try:
|
||||
monitor.currentdir = config.get_str('journaldir', default=config.default_journal_dir)
|
||||
if not monitor.currentdir:
|
||||
monitor.currentdir = config.default_journal_dir
|
||||
|
||||
logger.debug(f'logdir = "{monitor.currentdir}"')
|
||||
logfile = monitor.journal_newest_filename(monitor.currentdir)
|
||||
if logfile is None:
|
||||
raise ValueError("None from monitor.journal_newest_filename")
|
||||
|
||||
logger.debug(f'Using logfile "{logfile}"')
|
||||
with open(logfile, 'rb', 0) as loghandle:
|
||||
for line in loghandle:
|
||||
try:
|
||||
monitor.parse_entry(line)
|
||||
|
||||
except Exception:
|
||||
logger.debug(f'Invalid journal entry {line!r}')
|
||||
|
||||
except Exception:
|
||||
logger.exception("Can't read Journal file")
|
||||
sys.exit(EXIT_JOURNAL_READ_ERR)
|
||||
|
||||
if not monitor.cmdr:
|
||||
logger.error('Not available while E:D is at the main menu')
|
||||
sys.exit(EXIT_COMMANDER_UNKNOWN)
|
||||
|
||||
# Get data from Companion API
|
||||
if args.p:
|
||||
logger.debug(f'Attempting to use commander "{args.p}"')
|
||||
cmdrs = config.get_list('cmdrs', default=[])
|
||||
if args.p in cmdrs:
|
||||
idx = cmdrs.index(args.p)
|
||||
|
||||
else:
|
||||
for idx, cmdr in enumerate(cmdrs):
|
||||
if cmdr.lower() == args.p.lower():
|
||||
break
|
||||
else:
|
||||
raise companion.CredentialsError()
|
||||
companion.session.login(cmdrs[idx], monitor.is_beta)
|
||||
|
||||
else:
|
||||
logger.debug(f'Attempting to use commander "{monitor.cmdr}" from Journal File')
|
||||
cmdrs = config.get_list('cmdrs', default=[])
|
||||
if monitor.cmdr not in cmdrs:
|
||||
raise companion.CredentialsError()
|
||||
try:
|
||||
companion.session.login(monitor.cmdr, monitor.is_beta)
|
||||
except AttributeError:
|
||||
raise companion.CredentialsError()
|
||||
|
||||
###################################################################
|
||||
# Initiate CAPI queries
|
||||
querytime = int(time())
|
||||
companion.session.station(query_time=querytime)
|
||||
|
||||
# Wait for the response
|
||||
_capi_request_timeout = 60
|
||||
try:
|
||||
capi_response = companion.session.capi_response_queue.get(
|
||||
block=True, timeout=_capi_request_timeout
|
||||
)
|
||||
|
||||
except queue.Empty:
|
||||
logger.error(f'CAPI requests timed out after {_capi_request_timeout} seconds')
|
||||
sys.exit(EXIT_SERVER)
|
||||
|
||||
###################################################################
|
||||
|
||||
# noinspection DuplicatedCode
|
||||
if isinstance(capi_response, companion.EDMCCAPIFailedRequest):
|
||||
logger.trace_if('capi.worker', f'Failed Request: {capi_response.message}')
|
||||
if capi_response.exception:
|
||||
raise capi_response.exception
|
||||
raise ValueError(capi_response.message)
|
||||
match capi_response:
|
||||
case companion.EDMCCAPIFailedRequest(message=msg, exception=exc):
|
||||
logger.trace_if('capi.worker', f'Failed Request: {msg}')
|
||||
if exc:
|
||||
raise exc
|
||||
raise ValueError(msg)
|
||||
|
||||
case companion.EDMCCAPIResponse(capi_data=capi_data):
|
||||
logger.trace_if('capi.worker', 'Answer is not a Failure')
|
||||
if not isinstance(capi_response, companion.EDMCCAPIResponse):
|
||||
raise ValueError(f"Response was neither CAPIFailedRequest nor EDMCAPIResponse: {type(capi_response)}")
|
||||
|
||||
data = capi_response.capi_data
|
||||
config.set('querytime', querytime)
|
||||
return capi_data
|
||||
|
||||
# Validation
|
||||
if not deep_get(data, 'commander', 'name', default='').strip():
|
||||
logger.error("No data['command']['name'] from CAPI")
|
||||
sys.exit(EXIT_SERVER)
|
||||
case _:
|
||||
raise ValueError(f"Response was neither CAPIFailedRequest nor EDMCAPIResponse: {type(capi_response)}")
|
||||
|
||||
elif (
|
||||
not deep_get(data, 'lastSystem', 'name') or
|
||||
data['commander'].get('docked') and not deep_get(data, 'lastStarport', 'name')
|
||||
): # Only care if docked
|
||||
|
||||
def validate_capi_data(data: companion.CAPIData, is_json_import: bool) -> None:
|
||||
"""Validate structural integrity and data lagging utilizing pattern matching."""
|
||||
# Check Commander Name
|
||||
match data:
|
||||
case {"commander": {"name": str(name)}} if name.strip():
|
||||
pass
|
||||
case _:
|
||||
logger.error("No data['commander']['name'] from CAPI")
|
||||
sys.exit(ExitCode.SERVER)
|
||||
|
||||
# Check System
|
||||
match data:
|
||||
case {"lastSystem": {"name": str(sys_name)}} if sys_name.strip():
|
||||
pass
|
||||
case _:
|
||||
logger.error("No data['lastSystem']['name'] from CAPI")
|
||||
sys.exit(EXIT_SERVER)
|
||||
sys.exit(ExitCode.SERVER)
|
||||
|
||||
elif not deep_get(data, 'ship', 'modules') or not deep_get(data, 'ship', 'name', default=''):
|
||||
logger.error("No data['ship']['modules'] from CAPI")
|
||||
sys.exit(EXIT_SERVER)
|
||||
# Check Docking & Starport
|
||||
match data:
|
||||
case {"commander": {"docked": True}}:
|
||||
match data:
|
||||
case {"lastStarport": {"name": str(port_name)}} if port_name.strip():
|
||||
pass
|
||||
case _:
|
||||
logger.error("No data['lastStarport']['name'] from CAPI (Required when docked)")
|
||||
sys.exit(ExitCode.SERVER)
|
||||
case _:
|
||||
pass
|
||||
|
||||
elif args.j:
|
||||
pass # Skip further validation
|
||||
# Check Ship Loadout Structure
|
||||
match data:
|
||||
case {"ship": {"modules": dict() | list(), "name": str(ship_name)}} if ship_name.strip():
|
||||
pass
|
||||
case _:
|
||||
logger.error("No data['ship']['modules'] or data['ship']['name'] from CAPI")
|
||||
sys.exit(ExitCode.SERVER)
|
||||
|
||||
elif data['commander']['name'] != monitor.cmdr:
|
||||
raise companion.CmdrError()
|
||||
# If importing a JSON dump, bypass lagging/journal validations
|
||||
if is_json_import:
|
||||
return
|
||||
|
||||
elif (
|
||||
data['lastSystem']['name'] != monitor.state['SystemName']
|
||||
or (
|
||||
(data['commander']['docked'] and data['lastStarport']['name'] or None) != monitor.state['StationName']
|
||||
)
|
||||
or data['ship']['id'] != monitor.state['ShipID']
|
||||
or data['ship']['name'].lower() != monitor.state['ShipType']
|
||||
):
|
||||
raise companion.ServerLagging()
|
||||
# Match Commander exactly to Journal
|
||||
if data["commander"]["name"] != monitor.cmdr:
|
||||
raise companion.CmdrError()
|
||||
|
||||
# stuff we can do when not docked
|
||||
if args.d:
|
||||
logger.debug(f'Writing raw JSON data to "{args.d}"')
|
||||
out = json.dumps(dict(data), ensure_ascii=False, indent=2, sort_keys=True, separators=(',', ': '))
|
||||
with open(args.d, 'wb') as f:
|
||||
f.write(out.encode("utf-8"))
|
||||
# Determine if server is lagging behind the journal
|
||||
is_docked = data["commander"].get("docked", False)
|
||||
capi_station = data.get("lastStarport", {}).get("name") if is_docked else None
|
||||
|
||||
if args.a:
|
||||
logger.debug(f'Writing Ship Loadout in Companion API JSON format to "{args.a}"')
|
||||
loadout.export(data, args.a)
|
||||
if (
|
||||
data["lastSystem"]["name"] != monitor.state['SystemName']
|
||||
or capi_station != monitor.state['StationName']
|
||||
or data["ship"]["id"] != monitor.state['ShipID']
|
||||
or data["ship"]["name"].lower() != monitor.state['ShipType']
|
||||
):
|
||||
raise companion.ServerLagging()
|
||||
|
||||
if args.e:
|
||||
logger.debug(f'Writing Ship Loadout in ED Shipyard plain text format to "{args.e}"')
|
||||
edshipyard.export(data, args.e)
|
||||
|
||||
if args.l:
|
||||
logger.debug(f'Writing Ship Locations in CSV format to "{args.l}"')
|
||||
stats.export_ships(data, args.l)
|
||||
def export_standard_data(data: companion.CAPIData, args: argparse.Namespace) -> None:
|
||||
"""Handle all standard, non-docked specific file export operations."""
|
||||
if args.d:
|
||||
logger.debug(f'Writing raw JSON data to "{args.d}"')
|
||||
out = json.dumps(dict(data), ensure_ascii=False, indent=2, sort_keys=True, separators=(',', ': '))
|
||||
with open(args.d, 'wb') as f:
|
||||
f.write(out.encode("utf-8"))
|
||||
|
||||
if args.t:
|
||||
logger.debug(f'Writing Player Status in CSV format to "{args.t}"')
|
||||
stats.export_status(data, args.t)
|
||||
if args.a:
|
||||
logger.debug(f'Writing Ship Loadout in Companion API JSON format to "{args.a}"')
|
||||
loadout.export(data, args.a)
|
||||
|
||||
if data['commander'].get('docked'):
|
||||
print(f'{deep_get(data, "lastSystem", "name", default="Unknown")},'
|
||||
f'{deep_get(data, "lastStarport", "name", default="Unknown")}'
|
||||
)
|
||||
if args.e:
|
||||
logger.debug(f'Writing Ship Loadout in ED Shipyard plain text format to "{args.e}"')
|
||||
edshipyard.export(data, args.e)
|
||||
|
||||
else:
|
||||
print(deep_get(data, 'lastSystem', 'name', default='Unknown'))
|
||||
if args.l:
|
||||
logger.debug(f'Writing Ship Locations in CSV format to "{args.l}"')
|
||||
stats.export_ships(data, args.l)
|
||||
|
||||
if args.m or args.o or args.s or args.n or args.j:
|
||||
if not data['commander'].get('docked'):
|
||||
logger.error("Can't use -m, -o, -s, -n or -j because you're not currently docked!")
|
||||
return
|
||||
if args.t:
|
||||
logger.debug(f'Writing Player Status in CSV format to "{args.t}"')
|
||||
stats.export_status(data, args.t)
|
||||
|
||||
if not deep_get(data, 'lastStarport', 'name'):
|
||||
logger.error("No data['lastStarport']['name'] from CAPI")
|
||||
sys.exit(EXIT_LAGGING)
|
||||
# Console stdout behavior based on docked state
|
||||
match data:
|
||||
case {"commander": {"docked": True}, "lastSystem": {"name": sys_name}, "lastStarport": {"name": port_name}}:
|
||||
print(f'{sys_name},{port_name}')
|
||||
case {"lastSystem": {"name": sys_name}}:
|
||||
print(f'{sys_name}')
|
||||
case _:
|
||||
print('Unknown')
|
||||
|
||||
# Ignore possibly missing shipyard info
|
||||
if not (data['lastStarport'].get('commodities') or data['lastStarport'].get('modules')):
|
||||
logger.error("No commodities or outfitting (modules) in CAPI data")
|
||||
return
|
||||
else:
|
||||
|
||||
def export_docked_data(data: companion.CAPIData, args: argparse.Namespace) -> None: # noqa: C901, CCR001
|
||||
"""Process market, outfitting, shipyard and EDDN exports requiring a docked state."""
|
||||
# Ensure they've requested one of the docked flags
|
||||
if not (args.m or args.o or args.s or args.n or args.j):
|
||||
return
|
||||
|
||||
match data:
|
||||
case {"commander": {"docked": True}, "lastStarport": {"name": port_name}} if port_name.strip():
|
||||
pass # Valid Docking Base
|
||||
case {"commander": {"docked": False}}:
|
||||
logger.error("Can't use -m, -o, -s, -n or -j because you're not currently docked!")
|
||||
return
|
||||
case _:
|
||||
logger.error("No data['lastStarport']['name'] from CAPI")
|
||||
sys.exit(ExitCode.LAGGING)
|
||||
|
||||
# Finally - the data looks sane and we're docked at a station
|
||||
# Ensure commodity or outfitting data is present in the CAPI payload
|
||||
has_commodities = bool(data["lastStarport"].get("commodities"))
|
||||
has_modules = bool(data["lastStarport"].get("modules"))
|
||||
if not (has_commodities or has_modules):
|
||||
logger.error("No commodities or outfitting (modules) in CAPI data")
|
||||
return
|
||||
|
||||
if args.j:
|
||||
logger.debug('Importing data from the CAPI return...')
|
||||
# Collate from JSON dump
|
||||
collate.addcommodities(data)
|
||||
collate.addmodules(data)
|
||||
collate.addships(data)
|
||||
if args.j:
|
||||
logger.debug('Importing data from the CAPI return...')
|
||||
collate.addcommodities(data)
|
||||
collate.addmodules(data)
|
||||
collate.addships(data)
|
||||
|
||||
if args.m:
|
||||
logger.debug(f'Writing Station Commodity Market Data in CSV format to "{args.m}"')
|
||||
if data['lastStarport'].get('commodities'):
|
||||
# Fixup anomalies in the commodity data
|
||||
fixed = companion.fixup(data)
|
||||
# Determine user-selected market export type (CSV, TAB, PIPE, SEMICOLON)
|
||||
mkt_type = config.get_str('mkt_export_type', default='CSV')
|
||||
if mkt_type == 'CSV':
|
||||
kind = commodity.COMMODITY_CSV
|
||||
elif mkt_type == 'CSV_NEW':
|
||||
kind = commodity.COMMODITY_CSV_NEW
|
||||
elif mkt_type == 'TAB':
|
||||
kind = commodity.COMMODITY_TAB
|
||||
elif mkt_type == 'PIPE':
|
||||
kind = commodity.COMMODITY_PIPE
|
||||
else:
|
||||
kind = commodity.COMMODITY_SEMICOLON
|
||||
commodity.export(fixed, kind, args.m)
|
||||
|
||||
else:
|
||||
logger.error("Station doesn't have a market")
|
||||
|
||||
if args.o:
|
||||
if data['lastStarport'].get('modules'):
|
||||
logger.debug(f'Writing Station Outfitting in CSV format to "{args.o}"')
|
||||
outfitting.export(data, args.o)
|
||||
|
||||
else:
|
||||
logger.error("Station doesn't supply outfitting")
|
||||
|
||||
if ((args.s or args.n) and not args.j and not data['lastStarport'].get('ships')
|
||||
and data['lastStarport']['services'].get('shipyard')):
|
||||
|
||||
# Retry for shipyard
|
||||
sleep(SERVER_RETRY)
|
||||
companion.session.station(int(time()))
|
||||
# Wait for the response
|
||||
_capi_request_timeout = 60
|
||||
if args.m:
|
||||
logger.debug(f'Writing Station Commodity Market Data in CSV format to "{args.m}"')
|
||||
if has_commodities:
|
||||
fixed = companion.fixup(data)
|
||||
mkt_type = config.get_str('mkt_export_type', default='CSV')
|
||||
try:
|
||||
capi_response = companion.session.capi_response_queue.get(
|
||||
block=True, timeout=_capi_request_timeout
|
||||
)
|
||||
kind = commodity.CommodityExportKind[mkt_type]
|
||||
except KeyError:
|
||||
kind = commodity.CommodityExportKind.SEMICOLON
|
||||
commodity.export(fixed, kind, args.m)
|
||||
else:
|
||||
logger.error("Station doesn't have a market")
|
||||
|
||||
except queue.Empty:
|
||||
logger.error(f'CAPI requests timed out after {_capi_request_timeout} seconds')
|
||||
sys.exit(EXIT_SERVER)
|
||||
if args.o:
|
||||
if has_modules:
|
||||
logger.debug(f'Writing Station Outfitting in CSV format to "{args.o}"')
|
||||
outfitting.export(data, args.o) # type: ignore
|
||||
else:
|
||||
logger.error("Station doesn't supply outfitting")
|
||||
|
||||
if isinstance(capi_response, companion.EDMCCAPIFailedRequest):
|
||||
logger.error(f'Failed Request: {capi_response.message}')
|
||||
sys.exit(EXIT_SERVER)
|
||||
# Retry logic for Shipyard if missing but reported as a station service
|
||||
has_ships = bool(data["lastStarport"].get("ships"))
|
||||
has_shipyard_service = bool(data["lastStarport"].get("services", {}).get("shipyard"))
|
||||
|
||||
new_data = capi_response.capi_data
|
||||
# might have undocked while we were waiting for retry in which case station data is unreliable
|
||||
if (
|
||||
new_data['commander'].get('docked')
|
||||
and deep_get(new_data, 'lastSystem', 'name') == monitor.state['SystemName']
|
||||
and deep_get(new_data, 'lastStarport', 'name') == monitor.state['StationName']
|
||||
):
|
||||
if (args.s or args.n) and not args.j and not has_ships and has_shipyard_service:
|
||||
sleep(SERVER_RETRY)
|
||||
companion.session.station(int(datetime.now(timezone.utc).timestamp()))
|
||||
|
||||
try:
|
||||
capi_response = companion.session.capi_response_queue.get(block=True, timeout=60)
|
||||
except queue.Empty:
|
||||
logger.error('CAPI requests timed out after 60 seconds (Shipyard Retry)')
|
||||
sys.exit(ExitCode.SERVER)
|
||||
|
||||
if isinstance(capi_response, companion.EDMCCAPIFailedRequest):
|
||||
logger.error(f'Failed Request: {capi_response.message}')
|
||||
sys.exit(ExitCode.SERVER)
|
||||
|
||||
new_data = capi_response.capi_data
|
||||
|
||||
# Verify the player didn't undock or jump during the sleep cycle
|
||||
match new_data:
|
||||
case {
|
||||
"commander": {"docked": True},
|
||||
"lastSystem": {"name": sys_name},
|
||||
"lastStarport": {"name": port_name}
|
||||
} if sys_name == monitor.state['SystemName'] and port_name == monitor.state['StationName']:
|
||||
data = new_data
|
||||
|
||||
if args.s:
|
||||
if deep_get(data, 'lastStarport', 'ships', 'shipyard_list'):
|
||||
if args.s:
|
||||
match data:
|
||||
case {"lastStarport": {"ships": {"shipyard_list": shipyard_list}}} if shipyard_list:
|
||||
logger.debug(f'Writing Station Shipyard in CSV format to "{args.s}"')
|
||||
shipyard.export(data, args.s)
|
||||
case _:
|
||||
if not args.j and monitor.stationservices and 'Shipyard' in monitor.stationservices:
|
||||
logger.error('Failed to get shipyard data')
|
||||
else:
|
||||
logger.error("Station doesn't have a shipyard")
|
||||
|
||||
elif not args.j and monitor.stationservices and 'Shipyard' in monitor.stationservices:
|
||||
logger.error('Failed to get shipyard data')
|
||||
if args.n:
|
||||
try:
|
||||
eddn_sender = eddn.EDDN(None)
|
||||
logger.debug('Sending Market, Outfitting and Shipyard data to EDDN...')
|
||||
eddn_sender.export_commodities(data, monitor.is_beta)
|
||||
eddn_sender.export_outfitting(data, monitor.is_beta)
|
||||
eddn_sender.export_shipyard(data, monitor.is_beta)
|
||||
except Exception:
|
||||
logger.exception('Failed to send data to EDDN')
|
||||
|
||||
else:
|
||||
logger.error("Station doesn't have a shipyard")
|
||||
|
||||
if args.n:
|
||||
try:
|
||||
eddn_sender = eddn.EDDN(None)
|
||||
logger.debug('Sending Market, Outfitting and Shipyard data to EDDN...')
|
||||
eddn_sender.export_commodities(data, monitor.is_beta)
|
||||
eddn_sender.export_outfitting(data, monitor.is_beta)
|
||||
eddn_sender.export_shipyard(data, monitor.is_beta)
|
||||
def main() -> None:
|
||||
"""Run the main code of the program."""
|
||||
try:
|
||||
args = parse_cli_args()
|
||||
|
||||
except Exception:
|
||||
logger.exception('Failed to send data to EDDN')
|
||||
if args.version:
|
||||
handle_version_check()
|
||||
return
|
||||
|
||||
configure_logging_and_env(args)
|
||||
|
||||
if args.trace_help:
|
||||
return # Help browser launched, terminate sequence
|
||||
|
||||
if args.refresh_all:
|
||||
refresh_all_cmdrs()
|
||||
|
||||
if args.j:
|
||||
data = load_json_dump(args.j)
|
||||
else:
|
||||
data = load_state_from_journal_and_capi(args)
|
||||
|
||||
validate_capi_data(data, is_json_import=bool(args.j))
|
||||
export_standard_data(data, args)
|
||||
export_docked_data(data, args)
|
||||
|
||||
except companion.ServerConnectionError:
|
||||
logger.exception('Exception while contacting server')
|
||||
sys.exit(EXIT_SERVER)
|
||||
sys.exit(ExitCode.SERVER)
|
||||
|
||||
except companion.ServerError:
|
||||
logger.exception('Frontier CAPI Server returned an error')
|
||||
sys.exit(EXIT_SERVER)
|
||||
sys.exit(ExitCode.SERVER)
|
||||
|
||||
except companion.CredentialsError:
|
||||
logger.error('Frontier CAPI Server: Invalid Credentials')
|
||||
sys.exit(EXIT_CREDENTIALS)
|
||||
sys.exit(ExitCode.CREDENTIALS)
|
||||
|
||||
# Companion API problem
|
||||
except companion.ServerLagging:
|
||||
logger.error(
|
||||
'Mismatch(es) between CAPI and Journal for at least one of: '
|
||||
'StarSystem, Last Star Port, Ship ID or Ship Name/Type'
|
||||
)
|
||||
sys.exit(EXIT_SERVER)
|
||||
sys.exit(ExitCode.SERVER)
|
||||
|
||||
except companion.CmdrError: # Companion API return doesn't match Journal
|
||||
except companion.CmdrError:
|
||||
logger.error(
|
||||
f'Commander "{data["commander"]["name"]}" from CAPI doesn\'t match '
|
||||
f'"{monitor.cmdr}" from Journal'
|
||||
)
|
||||
sys.exit(EXIT_SERVER)
|
||||
sys.exit(ExitCode.SERVER)
|
||||
|
||||
except Exception:
|
||||
logger.exception('"other" exception')
|
||||
sys.exit(EXIT_SERVER)
|
||||
sys.exit(ExitCode.SERVER)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
@@ -533,4 +574,4 @@ if __name__ == '__main__':
|
||||
except KeyboardInterrupt:
|
||||
logger.info("Ctrl+C Detected, Attempting Clean Shutdown")
|
||||
logger.debug('Exiting')
|
||||
sys.exit(EXIT_SUCCESS)
|
||||
sys.exit(ExitCode.SUCCESS)
|
||||
+5
-4
@@ -44,11 +44,10 @@ import os
|
||||
import pathlib
|
||||
import sys
|
||||
import warnings
|
||||
import datetime
|
||||
from fnmatch import fnmatch
|
||||
# So that any warning about accessing a protected member is only in one place.
|
||||
from threading import get_native_id as thread_native_id
|
||||
from time import gmtime
|
||||
from datetime import datetime, timezone
|
||||
from typing import TYPE_CHECKING, cast
|
||||
from loguru import logger as loguru_logger
|
||||
import config as config_mod # This has to be imported separately for trace_if to work... for some reason.
|
||||
@@ -82,7 +81,9 @@ try:
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
logging.Formatter.converter = gmtime
|
||||
logging.Formatter.converter = lambda ts: datetime.fromtimestamp(
|
||||
ts, timezone.utc # type: ignore
|
||||
).utctimetuple()
|
||||
warnings.simplefilter('default', DeprecationWarning)
|
||||
|
||||
|
||||
@@ -137,7 +138,7 @@ if TYPE_CHECKING:
|
||||
|
||||
def enhanced_formatter(record: 'Record') -> str:
|
||||
"""Format log messages using Loguru."""
|
||||
record["time"] = record["time"].astimezone(datetime.timezone.utc)
|
||||
record["time"] = record["time"].astimezone(timezone.utc)
|
||||
record["extra"]["safe_osthreadid"] = record["extra"].get("osthreadid", thread_native_id())
|
||||
record["extra"]["safe_module"] = record["extra"].get("module", record["name"])
|
||||
record["extra"]["safe_qualname"] = record["extra"].get("qualname", record["function"])
|
||||
|
||||
+16
-15
@@ -85,21 +85,22 @@ def get_sys_report(active_config: config.Config) -> str:
|
||||
journal_lock = JournalLock()
|
||||
lockable = journal_lock.open_journal_dir_lockfile()
|
||||
|
||||
report = f"EDMC Version: \n - {appversion()}\n\n"
|
||||
report += "OS Details:\n"
|
||||
report += f"- Operating System: {plt.system} {plt.release}\n"
|
||||
report += f"- Version: {plt.version}\n"
|
||||
report += f"- Machine: {plt.machine}\n"
|
||||
report += f"- Python Version: {platform.python_version()}\n"
|
||||
report += "\nEnvironment Details\n"
|
||||
report += f"- Detected Locale: {lcl[0]}\n"
|
||||
report += f"- Detected Encoding: {lcl[1]}\n"
|
||||
report += f"- Journal Directory: {monitor.currentdir}\n"
|
||||
report += f"- Game Version: {monitor.state['GameVersion']}\n"
|
||||
report += f"- Game Build: {monitor.state['GameBuild']}\n"
|
||||
report += f"- Using Odyssey: {monitor.state['Odyssey']}\n"
|
||||
report += f"- Journal Dir Lockable: {lockable}\n"
|
||||
return report
|
||||
return (
|
||||
f"EDMC Version: \n - {appversion()}\n\n"
|
||||
f"OS Details:\n"
|
||||
f"- Operating System: {plt.system} {plt.release}\n"
|
||||
f"- Version: {plt.version}\n"
|
||||
f"- Machine: {plt.machine}\n"
|
||||
f"- Python Version: {platform.python_version()}\n\n"
|
||||
f"Environment Details\n"
|
||||
f"- Detected Locale: {lcl[0]}\n"
|
||||
f"- Detected Encoding: {lcl[1]}\n"
|
||||
f"- Journal Directory: {monitor.currentdir}\n"
|
||||
f"- Game Version: {monitor.state.get('GameVersion', 'Unknown')}\n"
|
||||
f"- Game Build: {monitor.state.get('GameBuild', 'Unknown')}\n"
|
||||
f"- Using Odyssey: {monitor.state.get('Odyssey', 'Unknown')}\n"
|
||||
f"- Journal Dir Lockable: {lockable}\n"
|
||||
)
|
||||
|
||||
|
||||
def copy_sys_report(root: tk.Tk, report: str) -> None:
|
||||
|
||||
+245
-310
@@ -15,14 +15,14 @@ import os
|
||||
import pathlib
|
||||
import queue
|
||||
import re
|
||||
import signal
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import webbrowser
|
||||
from os import environ
|
||||
from time import localtime, strftime, time
|
||||
from typing import TYPE_CHECKING, Any, Literal, MutableMapping
|
||||
from datetime import datetime, timezone
|
||||
from typing import TYPE_CHECKING, Any, Literal
|
||||
from collections.abc import MutableMapping
|
||||
from constants import applongname, appname, protocolhandler_redirect
|
||||
|
||||
# Have this as early as possible for people running EDMarketConnector.exe
|
||||
@@ -63,8 +63,6 @@ if __name__ == '__main__':
|
||||
)
|
||||
|
||||
sys.stdout = sys.stderr = open(log_file_path, mode='w', buffering=1) # Do NOT use WITH here.
|
||||
# TODO: Test: Make *sure* this redirect is working, else py2exe is going to cause an exit popup
|
||||
|
||||
|
||||
# These need to be after the stdout/err redirect because they will cause
|
||||
# logging to be set up.
|
||||
@@ -225,11 +223,13 @@ if __name__ == '__main__': # noqa: C901
|
||||
|
||||
if args.capi_pretend_down:
|
||||
import config as conf_module
|
||||
|
||||
logger.info('Pretending CAPI is down')
|
||||
conf_module.capi_pretend_down = True
|
||||
|
||||
if args.capi_use_debug_access_token:
|
||||
import config as conf_module
|
||||
|
||||
with open(conf_module.config.app_dir_path / 'access_token.txt') as at:
|
||||
conf_module.capi_debug_access_token = at.readline().strip()
|
||||
|
||||
@@ -309,7 +309,7 @@ if __name__ == '__main__': # noqa: C901
|
||||
try:
|
||||
# Get thread and process IDs
|
||||
_, process_id = win32process.GetWindowThreadProcessId(hwnd)
|
||||
# Get the process handle
|
||||
# Get the process handle
|
||||
return win32api.OpenProcess(win32con.PROCESS_QUERY_LIMITED_INFORMATION, False, process_id)
|
||||
except Exception:
|
||||
return None
|
||||
@@ -359,6 +359,7 @@ if __name__ == '__main__': # noqa: C901
|
||||
return False # Indicate window found, so stop iterating
|
||||
# Indicate that EnumWindows() needs to continue iterating
|
||||
return True # Do not remove, else this function as a callback breaks
|
||||
|
||||
# This performs the edmc://auth check and forward
|
||||
# EnumWindows() will iterate through all open windows, calling
|
||||
# enumwindwsproc() on each. When an invocation returns False it
|
||||
@@ -424,11 +425,11 @@ if __name__ == '__main__': # noqa: C901
|
||||
if git_branch == "develop" or (git_branch is not None and '-alpha0' in str(appversion())):
|
||||
print("You're running in a DEVELOPMENT branch build. You might encounter bugs!")
|
||||
|
||||
|
||||
# See EDMCLogging.py docs.
|
||||
# isort: off
|
||||
if TYPE_CHECKING:
|
||||
import logging as standard_logging
|
||||
|
||||
# Tell mypy that TRACE exists as an integer literal or type
|
||||
TRACE: int = standard_logging.TRACE # type: ignore
|
||||
|
||||
@@ -436,7 +437,6 @@ if TYPE_CHECKING:
|
||||
from simplesystray import SysTrayIcon
|
||||
# isort: on
|
||||
|
||||
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import tkinter as tk
|
||||
import tkinter.filedialog
|
||||
@@ -787,7 +787,6 @@ class AppWindow:
|
||||
theme.register(frame)
|
||||
theme.apply(self.w)
|
||||
|
||||
self.w.bind('<Map>', self.onmap) # Special handling for overrideredict
|
||||
self.w.bind('<Enter>', self.onenter) # Special handling for transparency
|
||||
self.w.bind('<FocusIn>', self.onenter) # Special handling for transparency
|
||||
self.w.bind('<Leave>', self.onleave) # Special handling for transparency
|
||||
@@ -901,7 +900,7 @@ class AppWindow:
|
||||
|
||||
def suit_show_if_set(self) -> None:
|
||||
"""Show UI Suit row if we have data, else hide."""
|
||||
self.toggle_suit_row(self.suit['text'] != '')
|
||||
self.toggle_suit_row(visible=bool(self.suit['text']))
|
||||
|
||||
def toggle_suit_row(self, visible: bool | None = None) -> None:
|
||||
"""
|
||||
@@ -909,20 +908,17 @@ class AppWindow:
|
||||
|
||||
:param visible: Force visibility to this.
|
||||
"""
|
||||
self.suit_shown = not visible
|
||||
|
||||
if not self.suit_shown:
|
||||
pady = 2 if sys.platform != 'win32' else 0
|
||||
should_show = visible if visible is not None else not self.suit_shown
|
||||
|
||||
if should_show:
|
||||
pady = 0 if sys.platform == 'win32' else 2
|
||||
self.suit_label.grid(row=self.suit_grid_row, column=0, sticky=tk.W, padx=self.PADX, pady=pady)
|
||||
self.suit.grid(row=self.suit_grid_row, column=1, sticky=tk.EW, padx=self.PADX, pady=pady)
|
||||
self.suit_shown = True
|
||||
|
||||
else:
|
||||
# Hide the Suit row
|
||||
self.suit_label.grid_forget()
|
||||
self.suit.grid_forget()
|
||||
self.suit_shown = False
|
||||
|
||||
self.suit_shown = should_show
|
||||
|
||||
def postprefs(self, dologin: bool = True, **postargs):
|
||||
"""Perform necessary actions after the Preferences dialog is applied."""
|
||||
@@ -1093,16 +1089,10 @@ class AppWindow:
|
||||
if output_flags & config.OUT_MKT_CSV:
|
||||
# Determine user-selected market export type (CSV, TAB, PIPE, SEMICOLON)
|
||||
mkt_type = config.get_str('mkt_export_type', default='SEMICOLON')
|
||||
if mkt_type == 'CSV':
|
||||
kind = commodity.COMMODITY_CSV
|
||||
elif mkt_type == 'CSV_NEW':
|
||||
kind = commodity.COMMODITY_CSV_NEW
|
||||
elif mkt_type == 'TAB':
|
||||
kind = commodity.COMMODITY_TAB
|
||||
elif mkt_type == 'PIPE':
|
||||
kind = commodity.COMMODITY_PIPE
|
||||
else:
|
||||
kind = commodity.COMMODITY_SEMICOLON
|
||||
try:
|
||||
kind = commodity.CommodityExportKind[mkt_type]
|
||||
except KeyError:
|
||||
kind = commodity.CommodityExportKind.SEMICOLON
|
||||
commodity.export(fixed, kind)
|
||||
|
||||
if output_flags & config.OUT_MKT_TD:
|
||||
@@ -1116,8 +1106,7 @@ class AppWindow:
|
||||
|
||||
:param message: Status message to display.
|
||||
"""
|
||||
if not self.status['text']:
|
||||
self.status['text'] = message
|
||||
self.status['text'] = self.status['text'] or message
|
||||
|
||||
def capi_request_data(self, event=None) -> None: # noqa: CCR001
|
||||
"""
|
||||
@@ -1185,9 +1174,9 @@ class AppWindow:
|
||||
return
|
||||
|
||||
if not companion.session.retrying:
|
||||
if time() < self.capi_query_holdoff_time:
|
||||
if datetime.now(timezone.utc).timestamp() < self.capi_query_holdoff_time:
|
||||
# Invoked by key while in cooldown
|
||||
time_remaining = self.capi_query_holdoff_time - time()
|
||||
time_remaining = self.capi_query_holdoff_time - datetime.now(timezone.utc).timestamp()
|
||||
if play_sound and time_remaining < companion.capi_query_cooldown * 0.75:
|
||||
self.status['text'] = ''
|
||||
hotkeymgr.play_bad()
|
||||
@@ -1200,7 +1189,7 @@ class AppWindow:
|
||||
self.button['state'] = self.theme_button['state'] = tk.DISABLED
|
||||
self.w.update_idletasks()
|
||||
|
||||
query_time = int(time())
|
||||
query_time = int(datetime.now(timezone.utc).timestamp())
|
||||
logger.trace_if('capi.worker', 'Requesting full station data')
|
||||
config.set('querytime', query_time)
|
||||
logger.trace_if('capi.worker', 'Calling companion.session.station')
|
||||
@@ -1243,7 +1232,8 @@ class AppWindow:
|
||||
return
|
||||
|
||||
if not companion.session.retrying:
|
||||
if time() < self.capi_fleetcarrier_query_holdoff_time: # Was invoked while in cooldown
|
||||
# Was invoked while in cooldown
|
||||
if datetime.now(timezone.utc).timestamp() < self.capi_fleetcarrier_query_holdoff_time:
|
||||
logger.debug('CAPI fleetcarrier query aborted, too soon since last request')
|
||||
return
|
||||
|
||||
@@ -1251,7 +1241,7 @@ class AppWindow:
|
||||
self.status['text'] = tr.tl('Fetching data...')
|
||||
self.w.update_idletasks()
|
||||
|
||||
query_time = int(time())
|
||||
query_time = int(datetime.now(timezone.utc).timestamp())
|
||||
logger.trace_if('capi.worker', 'Requesting Fleet Carrier data')
|
||||
config.set('fleetcarrierquerytime', query_time)
|
||||
logger.trace_if('capi.worker', 'Calling companion.session.fleetcarrier')
|
||||
@@ -1286,170 +1276,171 @@ class AppWindow:
|
||||
logger.error(msg)
|
||||
raise ValueError(msg)
|
||||
|
||||
if capi_response.capi_data.source_endpoint == companion.CAPIEndpoint.FLEETCARRIER:
|
||||
# Fleetcarrier CAPI response
|
||||
# Validation
|
||||
if 'name' not in capi_response.capi_data:
|
||||
# LANG: No data was returned for the fleetcarrier from the Frontier CAPI
|
||||
err = self.status['text'] = tr.tl('CAPI: No Fleet Carrier data returned')
|
||||
match capi_response.capi_data.source_endpoint:
|
||||
case companion.CAPIEndpoint.FLEETCARRIER:
|
||||
# Fleetcarrier CAPI response
|
||||
# Validation
|
||||
if 'name' not in capi_response.capi_data:
|
||||
# LANG: No data was returned for the fleetcarrier from the Frontier CAPI
|
||||
err = self.status['text'] = tr.tl('CAPI: No Fleet Carrier data returned')
|
||||
|
||||
elif not capi_response.capi_data.get('name', {}).get('callsign'):
|
||||
# LANG: We didn't have the fleetcarrier callsign when we should have
|
||||
err = self.status['text'] = tr.tl("CAPI: Fleet Carrier data incomplete") # Shouldn't happen
|
||||
elif not capi_response.capi_data.get('name', {}).get('callsign'):
|
||||
# LANG: We didn't have the fleetcarrier callsign when we should have
|
||||
err = self.status['text'] = tr.tl("CAPI: Fleet Carrier data incomplete") # Shouldn't happen
|
||||
|
||||
else:
|
||||
if __debug__: # Recording
|
||||
companion.session.dump_capi_data(capi_response.capi_data)
|
||||
else:
|
||||
if __debug__: # Recording
|
||||
companion.session.dump_capi_data(capi_response.capi_data)
|
||||
|
||||
err = plug.notify_capi_fleetcarrierdata(capi_response.capi_data)
|
||||
self.status['text'] = err and err or ''
|
||||
if err:
|
||||
play_bad = True
|
||||
err = plug.notify_capi_fleetcarrierdata(capi_response.capi_data)
|
||||
self.status['text'] = err and err or ''
|
||||
if err:
|
||||
play_bad = True
|
||||
|
||||
self.capi_fleetcarrier_query_holdoff_time = capi_response.query_time \
|
||||
+ companion.capi_fleetcarrier_query_cooldown
|
||||
self.capi_fleetcarrier_query_holdoff_time = (capi_response.query_time
|
||||
+ companion.capi_fleetcarrier_query_cooldown)
|
||||
|
||||
# Other CAPI response
|
||||
# Validation
|
||||
elif 'commander' not in capi_response.capi_data:
|
||||
# This can happen with EGS Auth if no commander created yet
|
||||
# LANG: No data was returned for the commander from the Frontier CAPI
|
||||
err = self.status['text'] = tr.tl('CAPI: No commander data returned')
|
||||
case _:
|
||||
# Other CAPI response
|
||||
# Validation
|
||||
if 'commander' not in capi_response.capi_data:
|
||||
# This can happen with EGS Auth if no commander created yet
|
||||
# LANG: No data was returned for the commander from the Frontier CAPI
|
||||
err = self.status['text'] = tr.tl('CAPI: No commander data returned')
|
||||
|
||||
elif not capi_response.capi_data.get('commander', {}).get('name'):
|
||||
# LANG: We didn't have the commander name when we should have
|
||||
err = self.status['text'] = tr.tl("Who are you?!") # Shouldn't happen
|
||||
elif not capi_response.capi_data.get('commander', {}).get('name'):
|
||||
# LANG: We didn't have the commander name when we should have
|
||||
err = self.status['text'] = tr.tl("Who are you?!") # Shouldn't happen
|
||||
|
||||
elif (not capi_response.capi_data.get('lastSystem', {}).get('name')
|
||||
or (capi_response.capi_data['commander'].get('docked')
|
||||
and not capi_response.capi_data.get('lastStarport', {}).get('name'))):
|
||||
# LANG: We don't know where the commander is, when we should
|
||||
err = self.status['text'] = tr.tl("Where are you?!") # Shouldn't happen
|
||||
elif (not capi_response.capi_data.get('lastSystem', {}).get('name')
|
||||
or (capi_response.capi_data['commander'].get('docked')
|
||||
and not capi_response.capi_data.get('lastStarport', {}).get('name'))):
|
||||
# LANG: We don't know where the commander is, when we should
|
||||
err = self.status['text'] = tr.tl("Where are you?!") # Shouldn't happen
|
||||
|
||||
elif (
|
||||
not capi_response.capi_data.get('ship', {}).get('name')
|
||||
or not capi_response.capi_data.get('ship', {}).get('modules')
|
||||
):
|
||||
# LANG: We don't know what ship the commander is in, when we should
|
||||
err = self.status['text'] = tr.tl("What are you flying?!") # Shouldn't happen
|
||||
elif (
|
||||
not capi_response.capi_data.get('ship', {}).get('name')
|
||||
or not capi_response.capi_data.get('ship', {}).get('modules')
|
||||
):
|
||||
# LANG: We don't know what ship the commander is in, when we should
|
||||
err = self.status['text'] = tr.tl("What are you flying?!") # Shouldn't happen
|
||||
|
||||
elif monitor.cmdr and capi_response.capi_data['commander']['name'] != monitor.cmdr:
|
||||
# Companion API Commander doesn't match Journal
|
||||
logger.trace_if('capi.worker', 'Raising CmdrError()')
|
||||
raise companion.CmdrError()
|
||||
elif monitor.cmdr and capi_response.capi_data['commander']['name'] != monitor.cmdr:
|
||||
# Companion API Commander doesn't match Journal
|
||||
logger.trace_if('capi.worker', 'Raising CmdrError()')
|
||||
raise companion.CmdrError()
|
||||
|
||||
elif (
|
||||
capi_response.auto_update and not monitor.state['OnFoot']
|
||||
and not capi_response.capi_data['commander'].get('docked')
|
||||
):
|
||||
# auto update is only when just docked
|
||||
logger.warning(f"{capi_response.auto_update!r} and not {monitor.state['OnFoot']!r} and "
|
||||
f"not {capi_response.capi_data['commander'].get('docked')!r}")
|
||||
raise companion.ServerLagging()
|
||||
elif (
|
||||
capi_response.auto_update and not monitor.state['OnFoot']
|
||||
and not capi_response.capi_data['commander'].get('docked')
|
||||
):
|
||||
# auto update is only when just docked
|
||||
logger.warning(f"{capi_response.auto_update!r} and not {monitor.state['OnFoot']!r} and "
|
||||
f"not {capi_response.capi_data['commander'].get('docked')!r}")
|
||||
raise companion.ServerLagging()
|
||||
|
||||
elif capi_response.capi_data['lastSystem']['name'] != monitor.state['SystemName']:
|
||||
# CAPI system must match last journal one
|
||||
logger.warning(f"{capi_response.capi_data['lastSystem']['name']!r} != "
|
||||
f"{monitor.state['SystemName']!r}")
|
||||
raise companion.ServerLagging()
|
||||
elif capi_response.capi_data['lastSystem']['name'] != monitor.state['SystemName']:
|
||||
# CAPI system must match last journal one
|
||||
logger.warning(f"{capi_response.capi_data['lastSystem']['name']!r} != "
|
||||
f"{monitor.state['SystemName']!r}")
|
||||
raise companion.ServerLagging()
|
||||
|
||||
elif capi_response.capi_data['lastStarport']['name'] != monitor.state['StationName']:
|
||||
if monitor.state['OnFoot'] and monitor.state['StationName']:
|
||||
logger.warning(f"({capi_response.capi_data['lastStarport']['name']!r} != "
|
||||
f"{monitor.state['StationName']!r}) AND "
|
||||
f"{monitor.state['OnFoot']!r} and {monitor.state['StationName']!r}")
|
||||
raise companion.ServerLagging()
|
||||
elif capi_response.capi_data['lastStarport']['name'] != monitor.state['StationName']:
|
||||
if monitor.state['OnFoot'] and monitor.state['StationName']:
|
||||
logger.warning(f"({capi_response.capi_data['lastStarport']['name']!r} != "
|
||||
f"{monitor.state['StationName']!r}) AND "
|
||||
f"{monitor.state['OnFoot']!r} and {monitor.state['StationName']!r}")
|
||||
raise companion.ServerLagging()
|
||||
|
||||
if capi_response.capi_data['commander']['docked'] and monitor.state['StationName'] is None:
|
||||
# Likely (re-)Embarked on ship docked at an EDO settlement.
|
||||
# Both Disembark and Embark have `"Onstation": false` in Journal.
|
||||
# So there's nothing to tell us which settlement we're (still,
|
||||
# or now, if we came here in Apex and then recalled ship) docked at.
|
||||
logger.debug("docked AND monitor.state['StationName'] is None - so EDO settlement?")
|
||||
raise companion.NoMonitorStation()
|
||||
if capi_response.capi_data['commander']['docked'] and monitor.state['StationName'] is None:
|
||||
# Likely (re-)Embarked on ship docked at an EDO settlement.
|
||||
# Both Disembark and Embark have `"Onstation": false` in Journal.
|
||||
# So there's nothing to tell us which settlement we're (still,
|
||||
# or now, if we came here in Apex and then recalled ship) docked at.
|
||||
logger.debug("docked AND monitor.state['StationName'] is None - so EDO settlement?")
|
||||
raise companion.NoMonitorStation()
|
||||
|
||||
self.capi_query_holdoff_time = capi_response.query_time + companion.capi_query_cooldown
|
||||
self.capi_query_holdoff_time = capi_response.query_time + companion.capi_query_cooldown
|
||||
|
||||
elif capi_response.capi_data['lastStarport']['id'] != monitor.state['MarketID']:
|
||||
logger.warning(f"MarketID mis-match: {capi_response.capi_data['lastStarport']['id']!r} !="
|
||||
f" {monitor.state['MarketID']!r}")
|
||||
raise companion.ServerLagging()
|
||||
elif capi_response.capi_data['lastStarport']['id'] != monitor.state['MarketID']:
|
||||
logger.warning(f"MarketID mis-match: {capi_response.capi_data['lastStarport']['id']!r} !="
|
||||
f" {monitor.state['MarketID']!r}")
|
||||
raise companion.ServerLagging()
|
||||
|
||||
elif not monitor.state['OnFoot'] and capi_response.capi_data['ship']['id'] != monitor.state['ShipID']:
|
||||
# CAPI ship must match
|
||||
logger.warning(f"not {monitor.state['OnFoot']!r} and "
|
||||
f"{capi_response.capi_data['ship']['id']!r} != {monitor.state['ShipID']!r}")
|
||||
raise companion.ServerLagging()
|
||||
elif (not monitor.state['OnFoot'] and
|
||||
capi_response.capi_data['ship']['id'] != monitor.state['ShipID']):
|
||||
# CAPI ship must match
|
||||
logger.warning(f"not {monitor.state['OnFoot']!r} and "
|
||||
f"{capi_response.capi_data['ship']['id']!r} != {monitor.state['ShipID']!r}")
|
||||
raise companion.ServerLagging()
|
||||
|
||||
elif (
|
||||
not monitor.state['OnFoot']
|
||||
and capi_response.capi_data['ship']['name'].lower() != monitor.state['ShipType']
|
||||
):
|
||||
# CAPI ship type must match
|
||||
logger.warning(f"not {monitor.state['OnFoot']!r} and "
|
||||
f"{capi_response.capi_data['ship']['name'].lower()!r} != "
|
||||
f"{monitor.state['ShipType']!r}")
|
||||
raise companion.ServerLagging()
|
||||
elif (
|
||||
not monitor.state['OnFoot']
|
||||
and capi_response.capi_data['ship']['name'].lower() != monitor.state['ShipType']
|
||||
):
|
||||
# CAPI ship type must match
|
||||
logger.warning(f"not {monitor.state['OnFoot']!r} and "
|
||||
f"{capi_response.capi_data['ship']['name'].lower()!r} != "
|
||||
f"{monitor.state['ShipType']!r}")
|
||||
raise companion.ServerLagging()
|
||||
|
||||
else:
|
||||
# TODO: Change to depend on its own CL arg
|
||||
if __debug__: # Recording
|
||||
companion.session.dump_capi_data(capi_response.capi_data)
|
||||
else:
|
||||
# TODO: Change to depend on its own CL arg
|
||||
if __debug__: # Recording
|
||||
companion.session.dump_capi_data(capi_response.capi_data)
|
||||
|
||||
if not monitor.state['ShipType']: # Started game in SRV or fighter
|
||||
self.ship['text'] = ship_name_map.get(
|
||||
capi_response.capi_data['ship']['name'].lower(),
|
||||
capi_response.capi_data['ship']['name']
|
||||
)
|
||||
monitor.state['ShipID'] = capi_response.capi_data['ship']['id']
|
||||
monitor.state['ShipType'] = capi_response.capi_data['ship']['name'].lower()
|
||||
if not monitor.state['ShipType']: # Started game in SRV or fighter
|
||||
self.ship['text'] = ship_name_map.get(
|
||||
capi_response.capi_data['ship']['name'].lower(),
|
||||
capi_response.capi_data['ship']['name']
|
||||
)
|
||||
monitor.state['ShipID'] = capi_response.capi_data['ship']['id']
|
||||
monitor.state['ShipType'] = capi_response.capi_data['ship']['name'].lower()
|
||||
|
||||
if not monitor.state['Modules']:
|
||||
self.ship.configure(state=tk.DISABLED)
|
||||
if not monitor.state['Modules']:
|
||||
self.ship.configure(state=tk.DISABLED)
|
||||
|
||||
# We might have disabled this in the conditional above.
|
||||
if monitor.state['Modules']:
|
||||
self.ship.configure(state=True)
|
||||
# We might have disabled this in the conditional above.
|
||||
if monitor.state['Modules']:
|
||||
self.ship.configure(state=True)
|
||||
|
||||
if monitor.state.get('SuitCurrent') is not None:
|
||||
if (loadout := capi_response.capi_data.get('loadout')) is not None:
|
||||
if (suit := loadout.get('suit')) is not None:
|
||||
if (suitname := suit.get('edmcName')) is not None:
|
||||
# We've been paranoid about loadout->suit->suitname, now just assume loadouts is there
|
||||
loadout_name = index_possibly_sparse_list(
|
||||
capi_response.capi_data['loadouts'], loadout['loadoutSlotId']
|
||||
)['name']
|
||||
if monitor.state.get('SuitCurrent') is not None:
|
||||
match capi_response.capi_data:
|
||||
case {
|
||||
'loadout': {'suit': {'edmcName': str(suitname)}, 'loadoutSlotId': slot_id},
|
||||
'loadouts': list() as loadouts
|
||||
}:
|
||||
loadout_name = index_possibly_sparse_list(loadouts, slot_id)['name']
|
||||
self.suit['text'] = f'{suitname} ({loadout_name})'
|
||||
|
||||
self.suit['text'] = f'{suitname} ({loadout_name})'
|
||||
self.suit_show_if_set()
|
||||
# Update Odyssey Suit data
|
||||
companion.session.suit_update(capi_response.capi_data)
|
||||
|
||||
self.suit_show_if_set()
|
||||
# Update Odyssey Suit data
|
||||
companion.session.suit_update(capi_response.capi_data)
|
||||
if capi_response.capi_data['commander'].get('credits') is not None:
|
||||
monitor.state['Credits'] = capi_response.capi_data['commander']['credits']
|
||||
monitor.state['Loan'] = capi_response.capi_data['commander'].get('debt', 0)
|
||||
|
||||
if capi_response.capi_data['commander'].get('credits') is not None:
|
||||
monitor.state['Credits'] = capi_response.capi_data['commander']['credits']
|
||||
monitor.state['Loan'] = capi_response.capi_data['commander'].get('debt', 0)
|
||||
# stuff we can do when not docked
|
||||
err = plug.notify_capidata(capi_response.capi_data, monitor.is_beta)
|
||||
self.status['text'] = err and err or ''
|
||||
if err:
|
||||
play_bad = True
|
||||
|
||||
# stuff we can do when not docked
|
||||
err = plug.notify_capidata(capi_response.capi_data, monitor.is_beta)
|
||||
self.status['text'] = err and err or ''
|
||||
if err:
|
||||
play_bad = True
|
||||
should_return: bool
|
||||
new_data: dict[str, Any]
|
||||
|
||||
should_return: bool
|
||||
new_data: dict[str, Any]
|
||||
should_return, new_data = killswitch.check_killswitch('capi.request./market', {})
|
||||
if should_return:
|
||||
logger.warning("capi.request./market has been disabled by killswitch. Returning.")
|
||||
|
||||
should_return, new_data = killswitch.check_killswitch('capi.request./market', {})
|
||||
if should_return:
|
||||
logger.warning("capi.request./market has been disabled by killswitch. Returning.")
|
||||
else:
|
||||
# Export market data
|
||||
if not self.export_market_data(capi_response.capi_data):
|
||||
err = 'Error: Exporting Market data'
|
||||
play_bad = True
|
||||
|
||||
else:
|
||||
# Export market data
|
||||
if not self.export_market_data(capi_response.capi_data):
|
||||
err = 'Error: Exporting Market data'
|
||||
play_bad = True
|
||||
|
||||
self.capi_query_holdoff_time = capi_response.query_time + companion.capi_query_cooldown
|
||||
self.capi_query_holdoff_time = capi_response.query_time + companion.capi_query_cooldown
|
||||
|
||||
except queue.Empty:
|
||||
logger.error('There was no response in the queue!')
|
||||
@@ -1505,8 +1496,9 @@ class AppWindow:
|
||||
play_bad = True
|
||||
|
||||
if not err: # not self.status['text']: # no errors
|
||||
# LANG: Time when we last obtained Frontier CAPI data
|
||||
self.status['text'] = strftime(tr.tl('Last updated at %H:%M:%S'), localtime(capi_response.query_time))
|
||||
self.status['text'] = datetime.fromtimestamp(capi_response.query_time).strftime(
|
||||
tr.tl('Last updated at %H:%M:%S') # LANG: Time when we last obtained Frontier CAPI data
|
||||
)
|
||||
|
||||
if capi_response.play_sound and play_bad:
|
||||
hotkeymgr.play_bad()
|
||||
@@ -1554,11 +1546,11 @@ class AppWindow:
|
||||
Needs to be dynamic to allow for changing language.
|
||||
"""
|
||||
return {
|
||||
None: '',
|
||||
'Idle': '',
|
||||
None: '',
|
||||
'Idle': '',
|
||||
'FighterCon': tr.tl('Fighter'), # LANG: Multicrew role
|
||||
'FireCon': tr.tl('Gunner'), # LANG: Multicrew role
|
||||
'FlightCon': tr.tl('Helm'), # LANG: Multicrew role
|
||||
'FireCon': tr.tl('Gunner'), # LANG: Multicrew role
|
||||
'FlightCon': tr.tl('Helm'), # LANG: Multicrew role
|
||||
}.get(role, role)
|
||||
|
||||
if monitor.thread is None:
|
||||
@@ -1575,41 +1567,28 @@ class AppWindow:
|
||||
# Update main window
|
||||
self.cooldown()
|
||||
if monitor.cmdr and monitor.state['Captain']:
|
||||
if not config.get_bool('hide_multicrew_captain', default=False):
|
||||
self.cmdr['text'] = f'{monitor.cmdr} / {monitor.state["Captain"]}'
|
||||
|
||||
else:
|
||||
self.cmdr['text'] = f'{monitor.cmdr}'
|
||||
|
||||
# Multicrew layout
|
||||
self.cmdr['text'] = (
|
||||
monitor.cmdr if config.get_bool('hide_multicrew_captain', default=False)
|
||||
else f'{monitor.cmdr} / {monitor.state["Captain"]}'
|
||||
)
|
||||
self.ship_label['text'] = tr.tl('Role') + ':' # LANG: Multicrew role label in main window
|
||||
self.ship.configure(state=tk.NORMAL, text=crewroletext(monitor.state['Role']), url=None)
|
||||
|
||||
elif monitor.cmdr:
|
||||
if monitor.group and not config.get_bool("hide_private_group", default=False):
|
||||
self.cmdr['text'] = f'{monitor.cmdr} / {monitor.group}'
|
||||
|
||||
else:
|
||||
self.cmdr['text'] = monitor.cmdr
|
||||
|
||||
# Standard Layout
|
||||
self.cmdr['text'] = (
|
||||
monitor.cmdr if monitor.group and not config.get_bool("hide_private_group", default=False)
|
||||
else f'{monitor.cmdr} / {monitor.group}'
|
||||
)
|
||||
self.ship_label['text'] = tr.tl('Ship') + ':' # LANG: 'Ship' label in main UI
|
||||
|
||||
# TODO: Show something else when on_foot
|
||||
if monitor.state['ShipName']:
|
||||
ship_text = monitor.state['ShipName']
|
||||
|
||||
else:
|
||||
ship_text = ship_name_map.get(monitor.state['ShipType'], monitor.state['ShipType'])
|
||||
|
||||
if not ship_text:
|
||||
ship_text = ''
|
||||
ship_text = monitor.state['ShipName'] or ship_name_map.get(monitor.state['ShipType'],
|
||||
monitor.state['ShipType']) or ''
|
||||
|
||||
# Ensure the ship type/name text is clickable, if it should be.
|
||||
if monitor.state['Modules']:
|
||||
ship_state: Literal['normal', 'disabled'] = tk.NORMAL
|
||||
|
||||
else:
|
||||
ship_state = tk.DISABLED
|
||||
|
||||
ship_state: Literal['normal', 'disabled'] = tk.NORMAL if monitor.state['Modules'] else tk.DISABLED
|
||||
self.ship.configure(text=ship_text, url=self.shipyard_url, state=ship_state)
|
||||
|
||||
else:
|
||||
@@ -1851,21 +1830,21 @@ class AppWindow:
|
||||
|
||||
def cooldown(self) -> None:
|
||||
"""Display and update the cooldown timer for 'Update' button."""
|
||||
if time() < self.capi_query_holdoff_time:
|
||||
if datetime.now(timezone.utc).timestamp() < self.capi_query_holdoff_time:
|
||||
# Update button in main window
|
||||
cooldown_time = int(self.capi_query_holdoff_time - time())
|
||||
cooldown_time = int(self.capi_query_holdoff_time - datetime.now(timezone.utc).timestamp())
|
||||
# LANG: Cooldown on 'Update' button
|
||||
self.button['text'] = self.theme_button['text'] = tr.tl('cooldown {SS}s').format(SS=cooldown_time)
|
||||
self.w.after(1000, self.cooldown)
|
||||
else:
|
||||
self.button['text'] = self.theme_button['text'] = tr.tl('Update') # LANG: Update button in main window
|
||||
self.button['state'] = self.theme_button['state'] = (
|
||||
monitor.cmdr and
|
||||
monitor.mode and
|
||||
monitor.mode != 'CQC' and
|
||||
not monitor.state['Captain'] and
|
||||
monitor.state['SystemName'] and
|
||||
tk.NORMAL or tk.DISABLED
|
||||
monitor.cmdr and
|
||||
monitor.mode and
|
||||
monitor.mode != 'CQC' and
|
||||
not monitor.state['Captain'] and
|
||||
monitor.state['SystemName'] and
|
||||
tk.NORMAL or tk.DISABLED
|
||||
)
|
||||
|
||||
if sys.platform == 'win32':
|
||||
@@ -1993,7 +1972,7 @@ class AppWindow:
|
||||
"""
|
||||
default_extension: str = ''
|
||||
|
||||
timestamp: str = strftime('%Y-%m-%dT%H.%M.%S', localtime())
|
||||
timestamp: str = datetime.now().strftime('%Y-%m-%dT%H.%M.%S')
|
||||
f = tkinter.filedialog.asksaveasfilename(
|
||||
parent=self.w,
|
||||
defaultextension=default_extension,
|
||||
@@ -2121,12 +2100,6 @@ class AppWindow:
|
||||
self.w.wait_visibility() # Need main window to be re-created before returning
|
||||
theme.active = None # So theme will be re-applied on map
|
||||
|
||||
# TODO: Confirm this is unused and remove.
|
||||
def onmap(self, event=None) -> None:
|
||||
"""Perform a now unused function."""
|
||||
if event.widget == self.w:
|
||||
theme.apply(self.w)
|
||||
|
||||
def onenter(self, event=None) -> None:
|
||||
"""Handle when our window gains focus."""
|
||||
if config.get_int('theme') == theme.THEME_TRANSPARENT:
|
||||
@@ -2142,11 +2115,6 @@ class AppWindow:
|
||||
self.blank_menubar.grid(row=0, columnspan=2, sticky=tk.NSEW)
|
||||
|
||||
|
||||
def test_logging() -> None:
|
||||
"""Simple test of top level logging."""
|
||||
logger.debug('Test from EDMarketConnector.py top-level test_logging()')
|
||||
|
||||
|
||||
def setup_killswitches(filename: str | None):
|
||||
"""Download and setup the main killswitch list."""
|
||||
logger.debug('fetching killswitches...')
|
||||
@@ -2156,85 +2124,84 @@ def setup_killswitches(filename: str | None):
|
||||
killswitch.setup_main_list(filename)
|
||||
|
||||
|
||||
def show_killswitch_poppup(root=None):
|
||||
def show_killswitch_popup(root=None):
|
||||
"""Show a warning popup if there are any killswitches that match the current version."""
|
||||
if len(kills := killswitch.kills_for_version()) == 0:
|
||||
if not (kills := killswitch.kills_for_version()):
|
||||
return
|
||||
|
||||
text = (
|
||||
"Some EDMC Features have been disabled due to known issues.\n"
|
||||
"Please update EDMC as soon as possible to resolve any issues.\n"
|
||||
"Please update EDMC as soon as possible to resolve any issues."
|
||||
)
|
||||
|
||||
tl = tk.Toplevel(root)
|
||||
tl.wm_attributes('-topmost', True)
|
||||
tl.geometry(f'+{root.winfo_rootx()}+{root.winfo_rooty()}')
|
||||
|
||||
tl.attributes('-topmost', True)
|
||||
if root:
|
||||
tl.geometry(f'+{root.winfo_rootx()}+{root.winfo_rooty()}')
|
||||
tl.columnconfigure(1, weight=1)
|
||||
tl.title("EDMC Features have been disabled")
|
||||
|
||||
frame = tk.Frame(tl)
|
||||
frame.grid()
|
||||
t = tk.Label(frame, text=text)
|
||||
t.grid(columnspan=2)
|
||||
idx = 1
|
||||
frame.grid(sticky=tk.NSEW)
|
||||
|
||||
tk.Label(frame, text=text, justify=tk.LEFT).grid(row=0, column=0, columnspan=2, pady=(0, 10))
|
||||
idx = 1
|
||||
for version in kills:
|
||||
tk.Label(frame, text=f'Version: {version.version}').grid(row=idx, sticky=tk.W)
|
||||
tk.Label(
|
||||
frame, text=f'Version: {version.version}', font=('Helvetica', 10, 'bold')
|
||||
).grid(row=idx, column=0, sticky=tk.W)
|
||||
idx += 1
|
||||
for id, kill in version.kills.items():
|
||||
tk.Label(frame, text=id).grid(column=0, row=idx, sticky=tk.W, padx=(10, 0))
|
||||
tk.Label(frame, text=kill.reason).grid(column=1, row=idx, sticky=tk.E, padx=(0, 10))
|
||||
for kill_id, kill in version.kills.items():
|
||||
tk.Label(frame, text=kill_id).grid(row=idx, column=0, sticky=tk.W, padx=(10, 0))
|
||||
tk.Label(frame, text=kill.reason).grid(row=idx, column=1, sticky=tk.E, padx=(0, 10))
|
||||
idx += 1
|
||||
idx += 1
|
||||
|
||||
ok_button = ttk.Button(frame, text="Ok", command=tl.destroy)
|
||||
ok_button.grid(columnspan=2, sticky=tk.EW)
|
||||
ok_button.grid(row=idx, column=0, columnspan=2, sticky=tk.EW, pady=(10, 0))
|
||||
|
||||
|
||||
def validate_providers():
|
||||
"""Check if Config has an invalid provider set, and reset to default if we do."""
|
||||
provider_schema = {
|
||||
"station_provider": ("station_url", "EDSM", "Station"),
|
||||
"shipyard_provider": ("shipyard_url", "EDSY", "Shipyard"),
|
||||
"system_provider": ("system_url", "EDSM", "System"),
|
||||
}
|
||||
|
||||
reset_providers = {}
|
||||
station_provider: str = config.get_str("station_provider")
|
||||
if station_provider not in plug.provides('station_url'):
|
||||
logger.error("Station Provider Not Valid. Setting to Default.")
|
||||
if config.get_str('station_provider'): # Only generate a pop-up if an invalid entry exists, not no value.
|
||||
reset_providers["Station"] = (station_provider, "EDSM")
|
||||
config.set('station_provider', 'EDSM')
|
||||
|
||||
shipyard_provider: str = config.get_str("shipyard_provider")
|
||||
if shipyard_provider not in plug.provides('shipyard_url'):
|
||||
logger.error("Shipyard Provider Not Valid. Setting to Default.")
|
||||
if config.get_str('shipyard_provider'): # Only generate a pop-up if an invalid entry exists, not no value.
|
||||
reset_providers["Shipyard"] = (shipyard_provider, "EDSY")
|
||||
config.set('shipyard_provider', 'EDSY')
|
||||
|
||||
system_provider: str = config.get_str("system_provider")
|
||||
if system_provider not in plug.provides('system_url'):
|
||||
logger.error("System Provider Not Valid. Setting to Default.")
|
||||
if config.get_str('system_provider'): # Only generate a pop-up if an invalid entry exists, not no value.
|
||||
reset_providers["System"] = (system_provider, "EDSM")
|
||||
config.set('system_provider', 'EDSM')
|
||||
for config_key, (plugin_key, default_val, display_name) in provider_schema.items():
|
||||
current_provider = config.get_str(config_key)
|
||||
if current_provider not in plug.provides(plugin_key):
|
||||
logger.error(f"{display_name} Provider Not Valid. Setting to Default.")
|
||||
if current_provider:
|
||||
reset_providers[display_name] = (current_provider, default_val)
|
||||
config.set(config_key, default_val)
|
||||
|
||||
if not reset_providers:
|
||||
return
|
||||
|
||||
# LANG: Popup-text about Reset Providers
|
||||
popup_text = tr.tl(r'One or more of your URL Providers were invalid, and have been reset:\r\n\r\n')
|
||||
lines = [
|
||||
# LANG: Popup-text about Reset Providers
|
||||
tr.tl(r'One or more of your URL Providers were invalid, and have been reset:\r\n\r\n')
|
||||
]
|
||||
|
||||
for provider, (old_prov, new_prov) in reset_providers.items():
|
||||
# LANG: Text About What Provider Was Reset
|
||||
popup_text += tr.tl(r'{PROVIDER} was set to {OLDPROV}, and has been reset to {NEWPROV}\r\n').format(
|
||||
msg = tr.tl(r'{PROVIDER} was set to {OLDPROV}, and has been reset to {NEWPROV}\r\n').format(
|
||||
PROVIDER=provider,
|
||||
OLDPROV=old_prov,
|
||||
NEWPROV=new_prov
|
||||
)
|
||||
# And now we do need these to be actual \r\n
|
||||
popup_text = popup_text.replace('\\n', '\n').replace('\\r', '\r')
|
||||
lines.append(msg)
|
||||
|
||||
# Replaces the manual raw literal escaping hacks (\r\n -> actual linebreaks)
|
||||
popup_text = "".join(lines).replace('\\n', '\n').replace('\\r', '\r')
|
||||
|
||||
tk.messagebox.showinfo(
|
||||
# LANG: Popup window title for Reset Providers
|
||||
tr.tl('EDMC: Default Providers Reset'),
|
||||
popup_text,
|
||||
title=tr.tl('EDMC: Default Providers Reset'),
|
||||
message=popup_text,
|
||||
parent=root
|
||||
)
|
||||
|
||||
@@ -2287,18 +2254,12 @@ sys.path: {sys.path}'''
|
||||
# Older Windows Versions and builds have issues with UTF-8, so only
|
||||
# even attempt this where we think it will be safe.
|
||||
|
||||
if sys.platform == 'win32':
|
||||
windows_ver = sys.getwindowsversion()
|
||||
|
||||
# <https://en.wikipedia.org/wiki/Windows_10_version_history#Version_1903_(May_2019_Update)>
|
||||
# Windows 19, 1903 was build 18362
|
||||
if (
|
||||
sys.platform != 'win32'
|
||||
or (
|
||||
windows_ver.major == 10
|
||||
and windows_ver.build >= 18362
|
||||
)
|
||||
or windows_ver.major > 10 # Paranoid future check
|
||||
sys.platform != 'win32'
|
||||
or (windows_ver := sys.getwindowsversion()).major > 10 # Paranoid future check
|
||||
or (windows_ver.major == 10 and windows_ver.build >= 18362)
|
||||
):
|
||||
# Set that same language, but utf8 encoding (it was probably cp1252
|
||||
# or equivalent for other languages).
|
||||
@@ -2331,32 +2292,6 @@ sys.path: {sys.path}'''
|
||||
# Create protocol handler
|
||||
protocol.protocolhandler = protocol.get_handler_impl()()
|
||||
|
||||
# TODO: unittests in place of these
|
||||
# logger.debug('Test from __main__')
|
||||
# test_logging()
|
||||
|
||||
class A:
|
||||
"""Simple top-level class."""
|
||||
|
||||
class B:
|
||||
"""Simple second-level class."""
|
||||
|
||||
def __init__(self):
|
||||
logger.debug('A call from A.B.__init__')
|
||||
self.__test()
|
||||
_ = self.test_prop
|
||||
|
||||
def __test(self):
|
||||
logger.debug("A call from A.B.__test")
|
||||
|
||||
@property
|
||||
def test_prop(self):
|
||||
"""Test property."""
|
||||
logger.debug("test log from property")
|
||||
return "Test property is testy"
|
||||
|
||||
# abinit = A.B()
|
||||
|
||||
# Plain, not via `logger`
|
||||
print(f'{applongname} {appversion()}')
|
||||
|
||||
@@ -2365,8 +2300,9 @@ sys.path: {sys.path}'''
|
||||
setup_killswitches(args.killswitches_file)
|
||||
|
||||
root = tk.Tk(className=appname.lower())
|
||||
if sys.platform != 'win32' and ((f := config.get_str('font')) is not None or f != ''):
|
||||
size = config.get_int('font_size', default=-1)
|
||||
|
||||
if sys.platform != 'win32' and (f := config.get_str('font')):
|
||||
size = config.get_int('font_size', default=10)
|
||||
if size == -1:
|
||||
size = 10
|
||||
|
||||
@@ -2403,7 +2339,7 @@ sys.path: {sys.path}'''
|
||||
).format(ERR=err)
|
||||
detail = detail.replace('\\n', '\n').replace('\\r', '\r')
|
||||
msg = tk.messagebox.askyesno(
|
||||
title=title, message=message, detail=detail, icon=tkinter.messagebox.ERROR, type=tkinter.messagebox.YESNO,
|
||||
title=title, message=message, detail=detail, icon=tk.messagebox.ERROR, type=tk.messagebox.YESNO,
|
||||
parent=root
|
||||
) # type: ignore
|
||||
if msg:
|
||||
@@ -2411,7 +2347,8 @@ sys.path: {sys.path}'''
|
||||
"https://github.com/EDCD/EDMarketConnector/issues/new?"
|
||||
"assignees=&labels=bug%2C+unconfirmed&projects=&template=bug_report.md&title="
|
||||
)
|
||||
os.kill(os.getpid(), signal.SIGTERM)
|
||||
root.destroy()
|
||||
sys.exit(1)
|
||||
|
||||
def messagebox_broken_plugins():
|
||||
"""Display message about 'broken' plugins that failed to load."""
|
||||
@@ -2444,7 +2381,7 @@ sys.path: {sys.path}'''
|
||||
def messagebox_not_py3():
|
||||
"""Display message about plugins not updated for Python 3.x."""
|
||||
plugins_not_py3_last = config.get_int('plugins_not_py3_last', default=0)
|
||||
if (plugins_not_py3_last + 86400) < int(time()) and plug.PLUGINS_not_py3:
|
||||
if (plugins_not_py3_last + 86400) < int(datetime.now(timezone.utc).timestamp()) and plug.PLUGINS_not_py3:
|
||||
# LANG: Popup-text about 'active' plugins without Python 3.x support
|
||||
popup_text = tr.tl(
|
||||
"One or more of your enabled plugins do not yet have support for Python 3.x. Please see the "
|
||||
@@ -2470,20 +2407,18 @@ sys.path: {sys.path}'''
|
||||
popup_text,
|
||||
parent=root
|
||||
)
|
||||
config.set('plugins_not_py3_last', int(time()))
|
||||
config.set('plugins_not_py3_last', int(datetime.now(timezone.utc).timestamp()))
|
||||
|
||||
# UI Transparency
|
||||
ui_transparency = config.get_int('ui_transparency')
|
||||
if ui_transparency == 0:
|
||||
ui_transparency = 100
|
||||
ui_transparency = config.get_int('ui_transparency') or 100
|
||||
|
||||
root.wm_attributes('-alpha', ui_transparency / 100)
|
||||
root.attributes('-alpha', ui_transparency / 100) # Modernized legacy wm_attributes call
|
||||
# Display message box about plugins that failed to load
|
||||
root.after(0, messagebox_broken_plugins)
|
||||
# Display message box about plugins without Python 3.x support
|
||||
root.after(1, messagebox_not_py3)
|
||||
# Show warning popup for killswitches matching current version
|
||||
root.after(2, show_killswitch_poppup, root)
|
||||
root.after(2, show_killswitch_popup, root)
|
||||
# Start the main event loop
|
||||
try:
|
||||
check_for_datafile_updates()
|
||||
|
||||
@@ -7,13 +7,13 @@ See LICENSE file.
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
import datetime
|
||||
import shutil
|
||||
import sys
|
||||
import pathlib
|
||||
from string import Template
|
||||
import py2exe
|
||||
import zipfile
|
||||
from datetime import date
|
||||
from config import (
|
||||
appcmdname,
|
||||
appname,
|
||||
@@ -256,7 +256,7 @@ def write_dependency_markdown(dist_dir: pathlib.Path) -> None: # noqa: CCR001
|
||||
f.write("# Bundled Python Dependencies\n\n")
|
||||
f.write("This file is **auto-generated by the build system**.\n\n")
|
||||
f.write(
|
||||
f"This file was last updated for {_static_appversion} on {datetime.date.today()}.\n\n"
|
||||
f"This file was last updated for {_static_appversion} on {date.today()}.\n\n"
|
||||
)
|
||||
|
||||
f.write(
|
||||
|
||||
+75
-50
@@ -1,89 +1,114 @@
|
||||
"""Export various CSV formats."""
|
||||
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import time
|
||||
import csv
|
||||
from datetime import datetime
|
||||
from enum import IntEnum
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from collections.abc import Mapping
|
||||
from config import config
|
||||
from edmc_data import commodity_bracketmap as bracketmap
|
||||
|
||||
# DEFAULT means semi-colon separation
|
||||
# CSV means comma separation
|
||||
# TAB and PIPE are also supported
|
||||
(
|
||||
COMMODITY_SEMICOLON,
|
||||
COMMODITY_CSV,
|
||||
COMMODITY_CSV_NEW,
|
||||
COMMODITY_TAB,
|
||||
COMMODITY_PIPE,
|
||||
) = range(5)
|
||||
mkt_out_types = ('CSV', 'CSV_NEW', 'TAB', 'PIPE', 'SEMICOLON')
|
||||
|
||||
class CommodityExportKind(IntEnum):
|
||||
"""List the various available output forms."""
|
||||
|
||||
SEMICOLON = 0
|
||||
CSV = 1
|
||||
CSV_NEW = 2
|
||||
TAB = 3
|
||||
PIPE = 4
|
||||
|
||||
|
||||
def export(data, kind=COMMODITY_SEMICOLON, filename=None) -> None: # noqa: CCR001
|
||||
"""
|
||||
Export commodity data from the given CAPI data.
|
||||
# BACKWARD COMPATIBILITY
|
||||
COMMODITY_SEMICOLON = CommodityExportKind.SEMICOLON
|
||||
COMMODITY_CSV = CommodityExportKind.CSV
|
||||
COMMODITY_CSV_NEW = CommodityExportKind.CSV_NEW
|
||||
COMMODITY_TAB = CommodityExportKind.TAB
|
||||
COMMODITY_PIPE = CommodityExportKind.PIPE
|
||||
mkt_out_types = ("CSV", "CSV_NEW", "TAB", "PIPE", "SEMICOLON")
|
||||
|
||||
|
||||
def export(
|
||||
data: Mapping[str, Any],
|
||||
kind: CommodityExportKind = CommodityExportKind.SEMICOLON,
|
||||
filename: Path | str | None = None,
|
||||
) -> None:
|
||||
"""Export commodity data from the given CAPI data.
|
||||
|
||||
:param data: CAPI data.
|
||||
:param kind: The type of file to write.
|
||||
:param filename: Filename to write to, or None for a standard format name.
|
||||
:return:
|
||||
"""
|
||||
querytime = config.get_int("querytime", default=int(time.time()))
|
||||
query_time_raw = config.get_int("querytime", default=None)
|
||||
query_datetime = (
|
||||
datetime.fromtimestamp(query_time_raw)
|
||||
if query_time_raw is not None
|
||||
else datetime.now()
|
||||
)
|
||||
|
||||
# Map kind to delimiter
|
||||
if kind == COMMODITY_CSV or kind == COMMODITY_CSV_NEW:
|
||||
mkt_out_delim = ','
|
||||
elif kind == COMMODITY_TAB:
|
||||
mkt_out_delim = '\t'
|
||||
elif kind == COMMODITY_PIPE:
|
||||
mkt_out_delim = '|'
|
||||
else:
|
||||
mkt_out_delim = ';' # COMMODITY_SEMICOLON or default
|
||||
match kind:
|
||||
case CommodityExportKind.CSV | CommodityExportKind.CSV_NEW:
|
||||
mkt_out_delim = ","
|
||||
case CommodityExportKind.TAB:
|
||||
mkt_out_delim = "\t"
|
||||
case CommodityExportKind.PIPE:
|
||||
mkt_out_delim = "|"
|
||||
case _:
|
||||
mkt_out_delim = ";"
|
||||
|
||||
sysname = data["lastSystem"]["name"].strip()
|
||||
station = data["lastStarport"]["name"].strip()
|
||||
|
||||
if not filename:
|
||||
sysname = data["lastSystem"]["name"].strip()
|
||||
station = data["lastStarport"]["name"].strip()
|
||||
timestamp = time.strftime("%Y-%m-%dT%H.%M.%S", time.localtime(querytime))
|
||||
timestamp = query_datetime.strftime("%Y-%m-%dT%H.%M.%S")
|
||||
# Use .csv for comma-separated files; use .txt for other text formats
|
||||
ext = 'csv' if mkt_out_delim == ',' else 'txt'
|
||||
ext = "csv" if mkt_out_delim == "," else "txt"
|
||||
filename = (
|
||||
Path(config.get_str("outdir")) / f"{sysname}.{station}.{timestamp}.{ext}"
|
||||
)
|
||||
|
||||
system = data["lastSystem"]["name"]
|
||||
station = data["lastStarport"]["name"]
|
||||
|
||||
if kind == COMMODITY_CSV:
|
||||
# maintain old compatibility
|
||||
header = ['System', 'Station', 'Commodity', 'Sell', 'Buy', 'Demand',
|
||||
'demandBracket', 'Supply', 'stockBracket', 'Date']
|
||||
else:
|
||||
header = ['System', 'Station', 'Commodity', 'Sell', 'Buy', 'Demand',
|
||||
'demandBracket', 'Supply', 'stockBracket', 'Average', 'FDevID', 'Date']
|
||||
filename = Path(filename)
|
||||
|
||||
header = ["System", "Station", "Commodity", "Sell", "Buy", "Demand",
|
||||
"demandBracket", "Supply", "stockBracket"]
|
||||
if kind != CommodityExportKind.CSV:
|
||||
header.extend(["Average", "FDevID"])
|
||||
header.append("Date")
|
||||
|
||||
with open(filename, "w", newline="", encoding="utf-8") as output_file:
|
||||
writer = csv.writer(output_file, delimiter=mkt_out_delim)
|
||||
|
||||
writer.writerow(header)
|
||||
|
||||
for commodity in data["lastStarport"]["commodities"]:
|
||||
|
||||
def get_int_field(
|
||||
field_key: str, condition_key: str | None = None
|
||||
) -> int | str:
|
||||
"""Return the integer value of a field from the commodity."""
|
||||
check_key = condition_key or field_key
|
||||
if (
|
||||
commodity.get(check_key) is not None
|
||||
and commodity.get(field_key) is not None
|
||||
):
|
||||
return int(commodity[field_key])
|
||||
return ""
|
||||
|
||||
row = [
|
||||
system,
|
||||
sysname,
|
||||
station,
|
||||
commodity["name"],
|
||||
int(commodity["sellPrice"]) if commodity.get("sellPrice") is not None else "",
|
||||
int(commodity["buyPrice"]) if commodity.get("buyPrice") is not None else "",
|
||||
int(commodity["demand"]) if commodity.get("demandBracket") is not None else "",
|
||||
get_int_field("sellPrice"),
|
||||
get_int_field("buyPrice"),
|
||||
get_int_field("demand", "demandBracket"),
|
||||
bracketmap.get(commodity.get("demandBracket"), ""),
|
||||
int(commodity["stock"]) if commodity.get("stockBracket") is not None else "",
|
||||
get_int_field("stock", "stockBracket"),
|
||||
bracketmap.get(commodity.get("stockBracket"), ""),
|
||||
]
|
||||
|
||||
# newer export fields format
|
||||
if kind != COMMODITY_CSV:
|
||||
mean = int(commodity["meanPrice"]) if commodity.get("meanPrice") is not None else ""
|
||||
if kind != CommodityExportKind.CSV:
|
||||
mean = get_int_field("meanPrice")
|
||||
row.extend([mean, commodity["id"]])
|
||||
|
||||
row.append(data["timestamp"])
|
||||
|
||||
+28
-21
@@ -1,44 +1,44 @@
|
||||
"""
|
||||
common_utils.py - Common functions and modules.
|
||||
"""common_utils.py - Common functions and modules.
|
||||
|
||||
Copyright (c) EDCD, All Rights Reserved
|
||||
Licensed under the GNU General Public License v2 or later.
|
||||
See LICENSE file.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
import sys
|
||||
import locale
|
||||
import sys
|
||||
from typing import TYPE_CHECKING
|
||||
from EDMCLogging import get_main_logger
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import tkinter as tk
|
||||
|
||||
logger = get_main_logger()
|
||||
|
||||
SERVER_RETRY = 5 # retry pause for Companion servers [s]
|
||||
SERVER_RETRY: int = 5 # retry pause for Companion servers [s]
|
||||
|
||||
if sys.platform == 'win32':
|
||||
if sys.platform == "win32":
|
||||
import win32api
|
||||
import win32con
|
||||
|
||||
|
||||
def ensure_on_screen(self, parent: tk.Tk):
|
||||
"""
|
||||
Ensure a pop-up window is on the printable screen area.
|
||||
def ensure_on_screen(self: tk.Toplevel, parent: tk.Tk) -> None:
|
||||
"""Ensure a pop-up window is on the printable screen area.
|
||||
|
||||
:param self: The calling class instance of tk.TopLevel
|
||||
:param self: The calling class instance of tk.Toplevel
|
||||
:param parent: The parent window
|
||||
"""
|
||||
if sys.platform == 'win32':
|
||||
if sys.platform == "win32":
|
||||
try:
|
||||
# Get monitor info for the monitor containing the parent window
|
||||
monitor = win32api.MonitorFromWindow(parent.winfo_id(), win32con.MONITOR_DEFAULTTONEAREST)
|
||||
monitor_info = win32api.GetMonitorInfo(monitor)
|
||||
work_area = monitor_info['Work'] # Gets the working area (excludes taskbar)
|
||||
work_area = monitor_info["Work"] # Gets the working area (excludes taskbar)
|
||||
|
||||
# Calculate optimal position
|
||||
x = max(work_area[0], min(parent.winfo_rootx(), work_area[2] - self.winfo_width()))
|
||||
y = max(work_area[1], min(parent.winfo_rooty(), work_area[3] - self.winfo_height()))
|
||||
x = max(work_area[0], min(parent.winfo_rootx(), work_area[2] - self.winfo_width()),)
|
||||
y = max(work_area[1], min(parent.winfo_rooty(), work_area[3] - self.winfo_height()),)
|
||||
|
||||
# Update window position
|
||||
self.geometry(f"+{x}+{y}")
|
||||
@@ -48,11 +48,18 @@ def ensure_on_screen(self, parent: tk.Tk):
|
||||
|
||||
|
||||
def log_locale(prefix: str) -> None:
|
||||
"""Log all of the current local settings."""
|
||||
logger.debug(f'''Locale: {prefix}
|
||||
Locale LC_COLLATE: {locale.getlocale(locale.LC_COLLATE)}
|
||||
Locale LC_CTYPE: {locale.getlocale(locale.LC_CTYPE)}
|
||||
Locale LC_MONETARY: {locale.getlocale(locale.LC_MONETARY)}
|
||||
Locale LC_NUMERIC: {locale.getlocale(locale.LC_NUMERIC)}
|
||||
Locale LC_TIME: {locale.getlocale(locale.LC_TIME)}'''
|
||||
)
|
||||
"""Log all the current local settings."""
|
||||
|
||||
def safe_query(category: int) -> str:
|
||||
"""Safely query the current locale."""
|
||||
try:
|
||||
return locale.setlocale(category)
|
||||
except (ValueError, Exception):
|
||||
return "Unknown"
|
||||
|
||||
logger.debug(f"""Locale: {prefix}
|
||||
Locale LC_COLLATE: {safe_query(locale.LC_COLLATE)}
|
||||
Locale LC_CTYPE: {safe_query(locale.LC_CTYPE)}
|
||||
Locale LC_MONETARY: {safe_query(locale.LC_MONETARY)}
|
||||
Locale LC_NUMERIC: {safe_query(locale.LC_NUMERIC)}
|
||||
Locale LC_TIME: {safe_query(locale.LC_TIME)}""")
|
||||
+107
-93
@@ -12,32 +12,30 @@ protocol used for the callback.
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import collections
|
||||
import csv
|
||||
import datetime
|
||||
import hashlib
|
||||
import json
|
||||
import numbers
|
||||
import os
|
||||
import random
|
||||
import threading
|
||||
import time
|
||||
import tkinter as tk
|
||||
import urllib.parse
|
||||
import webbrowser
|
||||
import requests
|
||||
from email.utils import parsedate
|
||||
from enum import StrEnum
|
||||
from datetime import datetime, timezone
|
||||
from enum import StrEnum, IntEnum
|
||||
from pathlib import Path
|
||||
from queue import Queue
|
||||
from typing import TYPE_CHECKING, Any, TypeVar, Iterator
|
||||
from typing import TYPE_CHECKING, Any, Iterator, Final, ClassVar
|
||||
from collections import UserDict
|
||||
from collections.abc import Mapping
|
||||
from dataclasses import dataclass
|
||||
from requests.adapters import HTTPAdapter
|
||||
from urllib.parse import parse_qs
|
||||
from urllib3.util.retry import Retry
|
||||
import config as conf_module
|
||||
import killswitch
|
||||
import protocol
|
||||
import config as conf_module
|
||||
from config import config, user_agent, IS_FROZEN
|
||||
from edmc_data import companion_category_map as category_map
|
||||
from EDMCLogging import get_main_logger
|
||||
@@ -47,9 +45,9 @@ from l10n import translations as tr
|
||||
logger = get_main_logger()
|
||||
|
||||
if TYPE_CHECKING:
|
||||
UserDict = collections.UserDict[str, Any] # indicate to our type checkers what this generic class holds normally
|
||||
CAPIDictBase = UserDict[str, Any] # indicate to our type checkers what this generic class holds normally
|
||||
else:
|
||||
UserDict = collections.UserDict # Otherwise simply use the actual class
|
||||
CAPIDictBase = UserDict # Otherwise simply use the actual class
|
||||
|
||||
|
||||
capi_query_cooldown = 60 # Minimum time between (sets of) CAPI queries
|
||||
@@ -78,12 +76,13 @@ class CAPIData(UserDict):
|
||||
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)
|
||||
match data:
|
||||
case None:
|
||||
super().__init__()
|
||||
case str() as json_str:
|
||||
super().__init__(json.loads(json_str))
|
||||
case _:
|
||||
super().__init__(data)
|
||||
|
||||
self.original_data = self.data.copy()
|
||||
|
||||
@@ -108,15 +107,15 @@ class CAPIData(UserDict):
|
||||
# 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?')
|
||||
elif isinstance(modules, list):
|
||||
if not modules:
|
||||
match modules:
|
||||
case None:
|
||||
logger.debug('modules was None. FC or Damaged Station?')
|
||||
case []:
|
||||
logger.debug('modules is empty list. Damaged Station?')
|
||||
else:
|
||||
logger.error(f'modules is non-empty list: {modules!r}')
|
||||
else:
|
||||
logger.error(f'modules is not None, list, or dict! type: {type(modules)}, content: {modules}')
|
||||
case list(modules_list):
|
||||
logger.error(f'modules is non-empty list: {modules_list!r}')
|
||||
case _:
|
||||
logger.error(f'modules is not None, list, or dict! type: {type(modules)}, content: {modules}')
|
||||
last_starport['modules'] = {}
|
||||
|
||||
# Check ships
|
||||
@@ -142,7 +141,7 @@ class CAPIDataRawEndpoint:
|
||||
"""Represents the last received CAPI response for a specific endpoint."""
|
||||
|
||||
raw_data: str
|
||||
query_time: datetime.datetime
|
||||
query_time: datetime
|
||||
# TODO: Maybe requests.response status ?
|
||||
|
||||
|
||||
@@ -152,7 +151,7 @@ class CAPIDataRaw:
|
||||
def __init__(self) -> None:
|
||||
self.raw_data: dict[str, CAPIDataRawEndpoint] = {}
|
||||
|
||||
def record_endpoint(self, endpoint: str, raw_data: str, query_time: datetime.datetime) -> None:
|
||||
def record_endpoint(self, endpoint: str, raw_data: str, query_time: datetime) -> None:
|
||||
"""Record the latest raw data for the given endpoint."""
|
||||
self.raw_data[endpoint] = CAPIDataRawEndpoint(raw_data, query_time)
|
||||
|
||||
@@ -277,38 +276,44 @@ class Auth:
|
||||
# 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'
|
||||
FRONTIER_AUTH_PATH_AUTH = '/auth'
|
||||
FRONTIER_AUTH_PATH_TOKEN = '/token'
|
||||
FRONTIER_AUTH_PATH_DECODE = '/decode'
|
||||
CLIENT_ID: Final[str] = os.getenv('CLIENT_ID') or 'fb88d428-9110-475f-a3d2-dc151c2b9c7a'
|
||||
FRONTIER_AUTH_PATH_AUTH: Final[str] = '/auth'
|
||||
FRONTIER_AUTH_PATH_TOKEN: Final[str] = '/token'
|
||||
FRONTIER_AUTH_PATH_DECODE: Final[str] = '/decode'
|
||||
|
||||
_sessions_lock = threading.Lock()
|
||||
_sessions: dict[str, requests.Session] = {}
|
||||
_sessions_lock: ClassVar[threading.Lock] = threading.Lock()
|
||||
_sessions: ClassVar[dict[str, requests.Session]] = {}
|
||||
|
||||
cmdr: str
|
||||
verifier: bytes | None
|
||||
state: str | None
|
||||
requests_session: requests.Session
|
||||
|
||||
def __init__(self, cmdr: str) -> None:
|
||||
self.cmdr: str = cmdr
|
||||
self.verifier: bytes | None = None
|
||||
self.state: str | None = None
|
||||
self.cmdr = cmdr
|
||||
self.verifier = None
|
||||
self.state = None
|
||||
|
||||
# Thread-safe session singleton per commander
|
||||
with self._sessions_lock:
|
||||
if cmdr not in self._sessions:
|
||||
session = requests.Session()
|
||||
session.headers['User-Agent'] = user_agent
|
||||
session.mount(
|
||||
"https://",
|
||||
HTTPAdapter(
|
||||
max_retries=Retry(
|
||||
total=3,
|
||||
backoff_factor=0.5, # Exponential backoff: 0.5s, 1s, 2s
|
||||
status_forcelist=[429, 500, 502, 503, 504],
|
||||
allowed_methods=["GET", "POST"]
|
||||
if cmdr not in self._sessions:
|
||||
with self._sessions_lock:
|
||||
if cmdr not in self._sessions:
|
||||
session = requests.Session()
|
||||
session.headers['User-Agent'] = user_agent
|
||||
session.mount(
|
||||
"https://",
|
||||
HTTPAdapter(
|
||||
max_retries=Retry(
|
||||
total=3,
|
||||
backoff_factor=0.5, # Exponential backoff: 0.5s, 1s, 2s
|
||||
status_forcelist=[429, 500, 502, 503, 504],
|
||||
allowed_methods=["GET", "POST"]
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
self._sessions[cmdr] = session
|
||||
self._sessions[cmdr] = session
|
||||
|
||||
self.requests_session = self._sessions[cmdr]
|
||||
self.requests_session = self._sessions[cmdr]
|
||||
|
||||
def refresh(self) -> str | None:
|
||||
"""
|
||||
@@ -398,7 +403,7 @@ class Auth:
|
||||
if '?' not in payload:
|
||||
raise CredentialsError('malformed payload')
|
||||
|
||||
data = urllib.parse.parse_qs(payload.split('?', 1)[1])
|
||||
data = parse_qs(payload.split('?', 1)[1])
|
||||
logger.trace_if('capi.auth.refresh', f"OAuth callback params: {data}")
|
||||
if not self.state or data.get('state', [None])[0] != self.state:
|
||||
raise CredentialsError(f'Unexpected response from authorization {payload!r}')
|
||||
@@ -547,10 +552,22 @@ class CAPIEndpoint(StrEnum):
|
||||
FLEETCARRIER = "/fleetcarrier"
|
||||
|
||||
|
||||
class SessionState(IntEnum):
|
||||
"""Enum to maintain known session states."""
|
||||
|
||||
INIT = 0
|
||||
AUTH = 1
|
||||
OK = 2
|
||||
|
||||
|
||||
class Session:
|
||||
"""Methods for handling Frontier Auth and CAPI queries."""
|
||||
|
||||
STATE_INIT, STATE_AUTH, STATE_OK = list(range(3))
|
||||
# COMPATIBILITY
|
||||
STATE_INIT = SessionState.INIT
|
||||
STATE_AUTH = SessionState.AUTH
|
||||
STATE_OK = SessionState.OK
|
||||
|
||||
_sessions_lock = threading.Lock()
|
||||
_sessions: dict[tuple[str, bool, str], requests.Session] = {}
|
||||
|
||||
@@ -559,7 +576,7 @@ class Session:
|
||||
_CAPI_PATH_STATION = '_edmc_station'
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.state = Session.STATE_INIT
|
||||
self.state = SessionState.INIT
|
||||
self.credentials: dict[str, Any] | None = None
|
||||
self.requests_session: requests.Session | None = None
|
||||
self.auth: Auth | None = None
|
||||
@@ -597,7 +614,7 @@ class Session:
|
||||
self.requests_session = self._get_or_create_requests_session()
|
||||
self.requests_session.headers['Authorization'] = f'Bearer {access_token}'
|
||||
|
||||
self.state = Session.STATE_OK
|
||||
self.state = SessionState.OK
|
||||
|
||||
def login(self, cmdr: str | None = None, is_beta: bool | None = None) -> bool:
|
||||
"""
|
||||
@@ -626,13 +643,13 @@ class Session:
|
||||
logger.error('self.credentials is None')
|
||||
raise CredentialsError('Missing credentials') # Shouldn't happen
|
||||
|
||||
if self.state == Session.STATE_OK:
|
||||
if self.state == SessionState.OK:
|
||||
logger.debug('already logged in (state == STATE_OK)')
|
||||
return True # already logged in
|
||||
|
||||
else:
|
||||
credentials = {'cmdr': cmdr, 'beta': is_beta}
|
||||
if self.credentials == credentials and self.state == Session.STATE_OK:
|
||||
if self.credentials == credentials and self.state == SessionState.OK:
|
||||
logger.debug(f'already logged in (is_beta = {is_beta})')
|
||||
return True # already logged in
|
||||
|
||||
@@ -640,7 +657,7 @@ class Session:
|
||||
self.reinit_session()
|
||||
self.credentials = credentials
|
||||
|
||||
self.state = Session.STATE_INIT
|
||||
self.state = SessionState.INIT
|
||||
self.auth = Auth(self.credentials['cmdr']) # type: ignore
|
||||
|
||||
access_token = self.auth.refresh()
|
||||
@@ -651,14 +668,14 @@ class Session:
|
||||
return True
|
||||
|
||||
logger.debug('We do NOT have an access_token')
|
||||
self.state = Session.STATE_AUTH
|
||||
self.state = SessionState.AUTH
|
||||
return False # Wait for callback
|
||||
|
||||
# Callback from protocol handler
|
||||
def auth_callback(self) -> None:
|
||||
"""Handle callback from edmc:// or localhost:/auth handler."""
|
||||
logger.debug('Handling auth callback')
|
||||
if self.state != Session.STATE_AUTH:
|
||||
if self.state != SessionState.AUTH:
|
||||
# Shouldn't be getting a callback
|
||||
logger.debug('Got an auth callback while not doing auth')
|
||||
raise CredentialsError('Got an auth callback while not doing auth')
|
||||
@@ -670,7 +687,7 @@ class Session:
|
||||
|
||||
except Exception:
|
||||
logger.exception('Failed, will try again next login or query')
|
||||
self.state = Session.STATE_INIT # Will try to authorize again on next login or query
|
||||
self.state = SessionState.INIT # Will try to authorize again on next login or query
|
||||
self.auth = None
|
||||
raise # Bad thing happened
|
||||
if IS_FROZEN:
|
||||
@@ -697,7 +714,7 @@ class Session:
|
||||
|
||||
:param reopen: Whether to open a new session.
|
||||
"""
|
||||
self.state = Session.STATE_INIT
|
||||
self.state = SessionState.INIT
|
||||
self.close()
|
||||
self.requests_session = None
|
||||
|
||||
@@ -716,7 +733,7 @@ class Session:
|
||||
"""Worker thread that performs actual CAPI queries."""
|
||||
logger.debug('CAPI worker thread starting')
|
||||
|
||||
def capi_single_query(
|
||||
def capi_single_query( # noqa: CCR001
|
||||
capi_host: str,
|
||||
capi_endpoint: str,
|
||||
timeout: int = capi_default_requests_timeout
|
||||
@@ -779,7 +796,7 @@ class Session:
|
||||
capi_data = CAPIData(capi_json, capi_host, capi_endpoint, monitor.cmdr)
|
||||
self.capi_raw_data.record_endpoint(
|
||||
capi_endpoint, r.content.decode(encoding='utf-8'),
|
||||
datetime.datetime.now(datetime.timezone.utc)
|
||||
datetime.now(timezone.utc)
|
||||
)
|
||||
|
||||
except requests.ConnectionError as e:
|
||||
@@ -802,10 +819,12 @@ class Session:
|
||||
logger.error('No commander in returned data')
|
||||
|
||||
if 'timestamp' not in capi_data:
|
||||
capi_data['timestamp'] = time.strftime(
|
||||
'%Y-%m-%dT%H:%M:%SZ', parsedate(r.headers['Date']) # type: ignore
|
||||
)
|
||||
|
||||
http_date = r.headers.get('Date')
|
||||
if http_date:
|
||||
dt = datetime.strptime(http_date, "%a, %d %b %Y %H:%M:%S %Z")
|
||||
capi_data['timestamp'] = dt.replace(tzinfo=timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||
else:
|
||||
capi_data['timestamp'] = datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||
return capi_data
|
||||
|
||||
def handle_http_error(response: requests.Response, endpoint: str):
|
||||
@@ -988,7 +1007,7 @@ class Session:
|
||||
EDMCCAPIRequest(
|
||||
capi_host='',
|
||||
endpoint=EDMCCAPIRequest.REQUEST_WORKER_SHUTDOWN,
|
||||
query_time=int(time.time())
|
||||
query_time=int(datetime.now(timezone.utc).timestamp())
|
||||
)
|
||||
)
|
||||
|
||||
@@ -1117,7 +1136,7 @@ class Session:
|
||||
except (KeyError, ValueError):
|
||||
file_name += '.unknown station'
|
||||
|
||||
file_name += time.strftime('.%Y-%m-%dT%H.%M.%S', time.localtime())
|
||||
file_name += datetime.now().strftime('.%Y-%m-%dT%H.%M.%S')
|
||||
file_name += '.json'
|
||||
with open(f'dump/{file_name}', 'wb') as h:
|
||||
h.write(json.dumps(data, cls=CAPIDataEncoder,
|
||||
@@ -1156,29 +1175,27 @@ class Session:
|
||||
if not self.credentials:
|
||||
raise ValueError("No Credentials Provided!")
|
||||
|
||||
cmdr = self.credentials["cmdr"]
|
||||
is_beta = self.credentials["beta"]
|
||||
galaxy = "live" if monitor.is_live_galaxy() else "legacy"
|
||||
key = (cmdr, is_beta, galaxy)
|
||||
key = (
|
||||
self.credentials["cmdr"],
|
||||
self.credentials["beta"],
|
||||
"live" if monitor.is_live_galaxy() else "legacy"
|
||||
)
|
||||
|
||||
with self._sessions_lock:
|
||||
if key not in self._sessions:
|
||||
s = requests.Session()
|
||||
s.headers["User-Agent"] = user_agent
|
||||
if key not in self._sessions:
|
||||
with self._sessions_lock:
|
||||
if key not in self._sessions:
|
||||
s = requests.Session()
|
||||
s.headers["User-Agent"] = user_agent
|
||||
|
||||
retry = Retry(total=5, connect=5, read=5, status=5, backoff_factor=0.5,
|
||||
status_forcelist=(429, 500, 502, 503, 504), allowed_methods=frozenset(["GET"]),
|
||||
raise_on_status=False)
|
||||
retry = Retry(
|
||||
total=5, connect=5, read=5, status=5, backoff_factor=0.5,
|
||||
status_forcelist=(429, 500, 502, 503, 504),
|
||||
allowed_methods=frozenset(["GET"]), raise_on_status=False
|
||||
)
|
||||
s.mount("https://", HTTPAdapter(max_retries=retry, pool_connections=4, pool_maxsize=8))
|
||||
|
||||
adapter = HTTPAdapter(
|
||||
max_retries=retry,
|
||||
pool_connections=4,
|
||||
pool_maxsize=8,
|
||||
)
|
||||
s.mount("https://", adapter)
|
||||
|
||||
self._sessions[key] = s
|
||||
return self._sessions[key]
|
||||
self._sessions[key] = s
|
||||
return self._sessions[key]
|
||||
|
||||
|
||||
######################################################################
|
||||
@@ -1288,10 +1305,7 @@ def ship(data: CAPIData) -> CAPIData:
|
||||
return filter_ship(data['ship'])
|
||||
|
||||
|
||||
V = TypeVar('V')
|
||||
|
||||
|
||||
def index_possibly_sparse_list(data: Mapping[str, V] | list[V], key: int) -> V:
|
||||
def index_possibly_sparse_list[V](data: Mapping[str, V] | list[V], key: int) -> V: # noqa: D103
|
||||
"""
|
||||
Index into a "list" that may or may not be sparseified into a dict.
|
||||
|
||||
|
||||
+3
-6
@@ -43,9 +43,9 @@ import subprocess
|
||||
import sys
|
||||
import tomllib
|
||||
import tomli_w
|
||||
import time
|
||||
import threading
|
||||
from typing import Any, TypeVar
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
from collections import defaultdict
|
||||
import semantic_version
|
||||
from constants import GITVERSION_FILE, applongname, appname
|
||||
@@ -80,9 +80,6 @@ logger = (
|
||||
)
|
||||
|
||||
|
||||
_T = TypeVar("_T")
|
||||
|
||||
|
||||
def git_shorthash_from_head() -> str | None:
|
||||
"""
|
||||
Determine short hash for current git HEAD.
|
||||
@@ -296,7 +293,7 @@ class Config:
|
||||
|
||||
# Attempt Recovery
|
||||
|
||||
broken = self.toml_path.with_suffix(f".toml.broken.{int(time.time())}")
|
||||
broken = self.toml_path.with_suffix(f".toml.broken.{int(datetime.now(timezone.utc).timestamp())}")
|
||||
with contextlib.suppress(Exception):
|
||||
self.toml_path.replace(broken)
|
||||
|
||||
|
||||
+2
-2
@@ -17,8 +17,8 @@ if sys.platform != "linux":
|
||||
|
||||
import pathlib
|
||||
from configparser import ConfigParser
|
||||
import datetime
|
||||
import tomli_w
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -123,7 +123,7 @@ class LinuxConfigMinimal:
|
||||
"""Dump existing config to TOML file."""
|
||||
config_logger.debug("Generating New Config File")
|
||||
config_data: dict[str, Any] = {
|
||||
"generated": datetime.datetime.now(datetime.timezone.utc).isoformat(),
|
||||
"generated": datetime.now(timezone.utc).isoformat(),
|
||||
"source": "linux_ini",
|
||||
"section": self.SECTION,
|
||||
"settings": self._get_settings_dict(),
|
||||
|
||||
+2
-2
@@ -12,8 +12,8 @@ import sys
|
||||
import os
|
||||
import uuid
|
||||
import winreg
|
||||
import datetime
|
||||
import tomli_w
|
||||
from datetime import datetime, timezone
|
||||
from config import config_logger, IS_FROZEN
|
||||
from win32comext.shell import shell
|
||||
from typing import Any, TYPE_CHECKING
|
||||
@@ -76,7 +76,7 @@ class WinConfigMinimal:
|
||||
_, num_values, _ = winreg.QueryInfoKey(key)
|
||||
|
||||
config_data: dict[str, Any] = {
|
||||
"generated": datetime.datetime.now(datetime.timezone.utc).isoformat(),
|
||||
"generated": datetime.now(timezone.utc).isoformat(),
|
||||
"source": "windows_registry",
|
||||
"settings": {},
|
||||
}
|
||||
|
||||
+64
-50
@@ -1,6 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
coriolis-update-files.py - Build ship and module databases from https://github.com/EDCD/coriolis-data/.
|
||||
"""coriolis-update-files.py - Build ship and module databases from coriolis-data.
|
||||
|
||||
Copyright (c) EDCD, All Rights Reserved
|
||||
Licensed under the GNU General Public License v2 or later.
|
||||
@@ -12,86 +11,101 @@ project structure is used for this purpose. If you want to utilize the
|
||||
FDevIDs/ version of the file, copy it over the local one.
|
||||
"""
|
||||
|
||||
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
from typing import Any
|
||||
|
||||
import outfitting
|
||||
from edmc_data import coriolis_ship_map, ship_name_map
|
||||
|
||||
if __name__ == "__main__": # noqa: C901
|
||||
|
||||
def add(modules, name, attributes) -> None:
|
||||
"""Add the given module to the modules dict."""
|
||||
if name in modules and modules[name] != attributes:
|
||||
raise ValueError(f'{name}: {modules.get(name)} != {attributes}')
|
||||
if name in modules:
|
||||
raise ValueError(f'{name} already exists in modules')
|
||||
def add_module(modules: dict[str, Any], name: str, attributes: dict[str, Any]) -> None:
|
||||
"""Add the given module to the modules dict safely."""
|
||||
if name in modules:
|
||||
if modules[name] != attributes:
|
||||
raise ValueError(f"{name}: {modules[name]} != {attributes}")
|
||||
raise ValueError(f"{name} already exists in modules")
|
||||
|
||||
modules[name] = attributes
|
||||
modules[name] = attributes
|
||||
|
||||
|
||||
def main() -> None: # noqa: CCR001
|
||||
"""Run the Coriolis Updater."""
|
||||
try:
|
||||
# Regenerate coriolis-data distribution
|
||||
subprocess.check_call('npm install', cwd='coriolis-data', shell=True, stdout=sys.stdout, stderr=sys.stderr)
|
||||
subprocess.run(
|
||||
"npm install", cwd="coriolis-data", shell=True, check=True, stdout=sys.stdout, stderr=sys.stderr,
|
||||
)
|
||||
except NotADirectoryError:
|
||||
sys.exit("Coriolis-Data Directory not found! Have you set up your submodules? \n"
|
||||
"https://github.com/EDCD/EDMarketConnector/wiki/Running-from-source#obtain-a-copy-of-the-application-source") # noqa: E501
|
||||
sys.exit(
|
||||
"Coriolis-Data Directory not found! Have you set up your submodules? \n"
|
||||
"https://github.com/EDCD/EDMarketConnector/wiki/Running-from-source#obtain-a-copy-of-the-application-source"
|
||||
)
|
||||
|
||||
file_path = 'coriolis-data/dist/index.json'
|
||||
with open(file_path) as file:
|
||||
file_path = "coriolis-data/dist/index.json"
|
||||
with open(file_path, encoding="utf-8") as file:
|
||||
data = json.load(file)
|
||||
|
||||
# Symbolic name from in-game name
|
||||
reverse_ship_map = {v: k for k, v in list(ship_name_map.items())}
|
||||
|
||||
reverse_ship_map = {v: k for k, v in ship_name_map.items()}
|
||||
bulkheads = list(outfitting.armour_map)
|
||||
|
||||
ships = {}
|
||||
modules = {}
|
||||
ships: dict[str, Any] = {}
|
||||
modules: dict[str, Any] = {}
|
||||
|
||||
# Ship and armour masses
|
||||
for m in list(data['Ships'].values()):
|
||||
name = coriolis_ship_map.get(m['properties']['name'], str(m['properties']['name']))
|
||||
# Process Ship and armour masses
|
||||
for m in data["Ships"].values():
|
||||
raw_name = m["properties"]["name"]
|
||||
name = coriolis_ship_map.get(raw_name, str(raw_name))
|
||||
if name not in reverse_ship_map:
|
||||
raise ValueError(f'Unknown ship: {name}')
|
||||
ships[name] = {'hullMass': m['properties']['hullMass'],
|
||||
'reserveFuelCapacity': m['properties']['reserveFuelCapacity']}
|
||||
for i, bulkhead in enumerate(bulkheads):
|
||||
modules['_'.join([reverse_ship_map[name], 'armour', bulkhead])] = {'mass': m['bulkheads'][i]['mass']}
|
||||
raise ValueError(f"Unknown ship: {name}")
|
||||
|
||||
ships = {k: ships[k] for k in sorted(ships)}
|
||||
with open("ships.json", "w") as ships_file:
|
||||
ships[name] = {"hullMass": m["properties"]["hullMass"],
|
||||
"reserveFuelCapacity": m["properties"]["reserveFuelCapacity"]}
|
||||
|
||||
for i, bulkhead in enumerate(bulkheads):
|
||||
key = f"{reverse_ship_map[name]}_armour_{bulkhead}"
|
||||
modules[key] = {"mass": m["bulkheads"][i]["mass"]}
|
||||
|
||||
# Native dictionary sorting optimization
|
||||
ships = dict(sorted(ships.items()))
|
||||
with open("ships.json", "w", encoding="utf-8") as ships_file:
|
||||
json.dump(ships, ships_file, indent=4)
|
||||
|
||||
# Module masses
|
||||
for cat in list(data['Modules'].values()):
|
||||
for grp, mlist in list(cat.items()):
|
||||
for cat in data["Modules"].values():
|
||||
for grp, mlist in cat.items():
|
||||
for m in mlist:
|
||||
if 'symbol' not in m:
|
||||
raise ValueError(f'No symbol in {m}')
|
||||
key = str(m['symbol'].lower())
|
||||
if grp == 'fsd':
|
||||
if "symbol" not in m:
|
||||
raise ValueError(f"No symbol in {m}")
|
||||
|
||||
key = m["symbol"].lower()
|
||||
if grp == "fsd":
|
||||
modules[key] = {
|
||||
'mass': m['mass'],
|
||||
'optmass': m['optmass'],
|
||||
'maxfuel': m['maxfuel'],
|
||||
'fuelmul': m['fuelmul'],
|
||||
'fuelpower': m['fuelpower'],
|
||||
"mass": m["mass"],
|
||||
"optmass": m["optmass"],
|
||||
"maxfuel": m["maxfuel"],
|
||||
"fuelmul": m["fuelmul"],
|
||||
"fuelpower": m["fuelpower"],
|
||||
}
|
||||
elif grp == 'gfsb':
|
||||
elif grp == "gfsb":
|
||||
modules[key] = {
|
||||
'mass': m['mass'],
|
||||
'jumpboost': m['jumpboost'],
|
||||
"mass": m["mass"],
|
||||
"jumpboost": m["jumpboost"],
|
||||
}
|
||||
else:
|
||||
modules[key] = {'mass': m.get('mass', 0)} # Some modules don't have mass
|
||||
modules[key] = {"mass": m.get("mass", 0)} # Some modules don't have mass
|
||||
|
||||
# Pre 3.3 modules
|
||||
add(modules, 'int_stellarbodydiscoveryscanner_standard', {'mass': 2})
|
||||
add(modules, 'int_stellarbodydiscoveryscanner_intermediate', {'mass': 2})
|
||||
add(modules, 'int_stellarbodydiscoveryscanner_advanced', {'mass': 2})
|
||||
add_module(modules, "int_stellarbodydiscoveryscanner_standard", {"mass": 2})
|
||||
add_module(modules, "int_stellarbodydiscoveryscanner_intermediate", {"mass": 2})
|
||||
add_module(modules, "int_stellarbodydiscoveryscanner_advanced", {"mass": 2})
|
||||
|
||||
modules = {k: modules[k] for k in sorted(modules)}
|
||||
with open("modules.json", "w") as modules_file:
|
||||
modules = dict(sorted(modules.items()))
|
||||
with open("modules.json", "w", encoding="utf-8") as modules_file:
|
||||
json.dump(modules, modules_file, indent=4)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+2
-3
@@ -9,10 +9,9 @@ from __future__ import annotations
|
||||
|
||||
import json
|
||||
import sys
|
||||
import time
|
||||
import tkinter as tk
|
||||
import warnings
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
@@ -29,7 +28,7 @@ class Dashboard(FileSystemEventHandler):
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.session_start: int = int(time.time())
|
||||
self.session_start: int = int(datetime.now(timezone.utc).timestamp())
|
||||
self.root: tk.Tk = None # type: ignore
|
||||
self.currentdir: Path | None = None # The actual logdir that we're monitoring
|
||||
self.observer: Observer | PollingObserver | None = None # type: ignore
|
||||
|
||||
+73
-67
@@ -1,23 +1,24 @@
|
||||
"""Simple HTTP listener to be used with debugging various EDMC sends."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
import gzip
|
||||
from http import server
|
||||
import json
|
||||
import pathlib
|
||||
import threading
|
||||
import zlib
|
||||
from http import server
|
||||
from typing import Any, Literal
|
||||
from collections.abc import Callable
|
||||
from urllib.parse import parse_qs
|
||||
import zlib
|
||||
from config import config
|
||||
from EDMCLogging import get_main_logger
|
||||
|
||||
logger = get_main_logger()
|
||||
|
||||
output_lock = threading.Lock()
|
||||
output_data_path = pathlib.Path(config.app_dir_path / 'logs' / 'http_debug') # type: ignore
|
||||
SAFE_TRANSLATE = str.maketrans(dict.fromkeys("!@#$%^&*()./\\\r\n[]-+='\";:?<>,~`", '_'))
|
||||
output_data_path = pathlib.Path(config.app_dir_path / "logs" / "http_debug") # type: ignore
|
||||
SAFE_TRANSLATE = str.maketrans(dict.fromkeys("!@#$%^&*()./\\\r\n[]-+='\";:?<>,~`", "_"))
|
||||
|
||||
|
||||
class LoggingHandler(server.BaseHTTPRequestHandler):
|
||||
@@ -30,78 +31,74 @@ class LoggingHandler(server.BaseHTTPRequestHandler):
|
||||
def do_POST(self) -> None: # noqa: N802 # I cant change it
|
||||
"""Handle POST."""
|
||||
logger.info(f"Received a POST for {self.path!r}!")
|
||||
data_raw: bytes = self.rfile.read(int(self.headers['Content-Length']))
|
||||
|
||||
encoding = self.headers.get('Content-Encoding')
|
||||
content_length = int(self.headers.get("Content-Length", 0))
|
||||
data_raw: bytes = self.rfile.read(content_length)
|
||||
|
||||
to_save = data = self.get_printable(data_raw, encoding)
|
||||
encoding = self.headers.get("Content-Encoding")
|
||||
data = self.get_printable(data_raw, encoding)
|
||||
to_save = data
|
||||
|
||||
target_path = self.path
|
||||
if len(target_path) > 1 and target_path[0] == '/':
|
||||
target_path = target_path[1:]
|
||||
if len(self.path) > 1 and self.path.startswith("/"):
|
||||
target_path = self.path[1:]
|
||||
elif self.path == "/":
|
||||
target_path = "WEB_ROOT"
|
||||
else:
|
||||
target_path = self.path
|
||||
|
||||
elif len(target_path) == 1 and target_path[0] == '/':
|
||||
target_path = 'WEB_ROOT'
|
||||
|
||||
response: Callable[[str], str] | str | None = DEFAULT_RESPONSES.get(target_path)
|
||||
if callable(response):
|
||||
response = response(to_save)
|
||||
response_handler = DEFAULT_RESPONSES.get(target_path)
|
||||
response = (response_handler(to_save) if callable(response_handler) else response_handler)
|
||||
|
||||
self.send_response_only(200, "OK")
|
||||
if response is not None:
|
||||
self.send_header('Content-Length', str(len(response)))
|
||||
|
||||
self.send_header("Content-Length", str(len(response)))
|
||||
self.end_headers() # This is needed because send_response_only DOESN'T ACTUALLY SEND THE RESPONSE </rant>
|
||||
|
||||
if response is not None:
|
||||
self.wfile.write(response.encode())
|
||||
self.wfile.flush()
|
||||
|
||||
if target_path == 'edsm':
|
||||
if target_path == "edsm":
|
||||
# attempt to extract data from urlencoded stream
|
||||
try:
|
||||
edsm_data = extract_edsm_data(data)
|
||||
data = data + "\n" + json.dumps(edsm_data)
|
||||
to_save = f"{to_save}\n{json.dumps(edsm_data)}"
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
target_file = output_data_path / (safe_file_name(target_path) + '.log')
|
||||
target_file = output_data_path / f"{safe_file_name(target_path)}.log"
|
||||
if target_file.parent != output_data_path:
|
||||
logger.warning(f"REFUSING TO WRITE FILE THAT ISN'T IN THE RIGHT PLACE! {target_file=}")
|
||||
logger.warning(f'DATA FOLLOWS\n{data}')
|
||||
logger.warning(f"DATA FOLLOWS\n{data}")
|
||||
return
|
||||
|
||||
with output_lock, target_file.open('a') as file:
|
||||
file.write(to_save + "\n\n")
|
||||
with output_lock, target_file.open("a", encoding="utf-8") as file:
|
||||
file.write(f"{to_save}\n\n")
|
||||
|
||||
@staticmethod
|
||||
def get_printable(data: bytes, compression: Literal['deflate'] | Literal['gzip'] | str | None = None) -> str:
|
||||
"""
|
||||
Convert an incoming data stream into a string.
|
||||
def get_printable(data: bytes, compression: Literal["deflate", "gzip"] | str | None = None) -> str:
|
||||
"""Convert an incoming compressed or raw data stream into a string.
|
||||
|
||||
:param data: The data to convert
|
||||
:param compression: The compression to remove, defaults to None
|
||||
:raises ValueError: If compression is unknown
|
||||
:return: printable strings
|
||||
"""
|
||||
ret: bytes = b''
|
||||
if compression is None:
|
||||
ret = data
|
||||
match compression:
|
||||
case None:
|
||||
ret = data
|
||||
case "deflate":
|
||||
ret = zlib.decompress(data)
|
||||
case "gzip":
|
||||
ret = gzip.decompress(data)
|
||||
case _:
|
||||
raise ValueError(f"Unknown encoding for data {compression!r}")
|
||||
|
||||
elif compression == 'deflate':
|
||||
ret = zlib.decompress(data)
|
||||
|
||||
elif compression == 'gzip':
|
||||
ret = gzip.decompress(data)
|
||||
|
||||
else:
|
||||
raise ValueError(f'Unknown encoding for data {compression!r}')
|
||||
|
||||
return ret.decode('utf-8', errors='replace')
|
||||
return ret.decode("utf-8", errors="replace")
|
||||
|
||||
|
||||
def safe_file_name(name: str):
|
||||
"""
|
||||
Escape special characters out of a file name.
|
||||
def safe_file_name(name: str) -> str:
|
||||
"""Escape special characters out of a file name safely.
|
||||
|
||||
This is a nicety. Don't rely on it to be ultra secure.
|
||||
"""
|
||||
@@ -115,57 +112,66 @@ def generate_inara_response(raw_data: str) -> str:
|
||||
except json.JSONDecodeError:
|
||||
return "UNKNOWN REQUEST"
|
||||
|
||||
events = data.get("events") or []
|
||||
out = {
|
||||
'header': {
|
||||
'eventStatus': 200
|
||||
},
|
||||
|
||||
'events': [
|
||||
"header": {"eventStatus": 200},
|
||||
"events": [
|
||||
{
|
||||
'eventName': e['eventName'], 'eventStatus': 200, 'eventStatusText': "DEBUG STUFF"
|
||||
} for e in data.get('events')
|
||||
]
|
||||
"eventName": e["eventName"],
|
||||
"eventStatus": 200,
|
||||
"eventStatusText": "DEBUG STUFF",
|
||||
}
|
||||
for e in events
|
||||
if "eventName" in e
|
||||
],
|
||||
}
|
||||
|
||||
return json.dumps(out)
|
||||
|
||||
|
||||
def extract_edsm_data(data: str) -> dict[str, Any]:
|
||||
"""Extract relevant data from edsm data."""
|
||||
res = parse_qs(data)
|
||||
return {name: data[0] for name, data in res.items()}
|
||||
return {name: values[0] for name, values in res.items() if values}
|
||||
|
||||
|
||||
def generate_edsm_response(raw_data: str) -> str:
|
||||
"""Generate nonstatic data for edsm plugin."""
|
||||
try:
|
||||
data = extract_edsm_data(raw_data)
|
||||
events = json.loads(data['message'])
|
||||
except (json.JSONDecodeError, Exception):
|
||||
logger.exception("????")
|
||||
events = json.loads(data["message"])
|
||||
except (json.JSONDecodeError, KeyError, Exception):
|
||||
logger.exception("Failed to parse EDSM debug request envelope payload")
|
||||
return "UNKNOWN REQUEST"
|
||||
|
||||
out = {
|
||||
'msgnum': 100, # Ok
|
||||
'msg': 'debug stuff',
|
||||
'events': [
|
||||
{'event': e['event'], 'msgnum': 100, 'msg': 'debug stuff'} for e in events
|
||||
]
|
||||
"msgnum": 100, # OK Status Code
|
||||
"msg": "debug stuff",
|
||||
"events": [
|
||||
(
|
||||
{
|
||||
"event": e["get"]("event", "unknown"),
|
||||
"msgnum": 100,
|
||||
"msg": "debug stuff",
|
||||
}
|
||||
if isinstance(e, dict)
|
||||
else {"event": "unknown", "msgnum": 100, "msg": "debug stuff"}
|
||||
)
|
||||
for e in events
|
||||
],
|
||||
}
|
||||
|
||||
return json.dumps(out)
|
||||
|
||||
|
||||
DEFAULT_RESPONSES = {
|
||||
'inara': generate_inara_response,
|
||||
'edsm': generate_edsm_response
|
||||
DEFAULT_RESPONSES: dict[str, Callable[[str], str]] = {
|
||||
"inara": generate_inara_response,
|
||||
"edsm": generate_edsm_response,
|
||||
}
|
||||
|
||||
|
||||
def run_listener(host: str = "127.0.0.1", port: int = 9090) -> None:
|
||||
"""Run a listener thread."""
|
||||
output_data_path.mkdir(exist_ok=True)
|
||||
logger.info(f'Starting HTTP listener on {host=} {port=}!')
|
||||
logger.info(f"Starting HTTP listener on {host=} {port=}!")
|
||||
listener = server.HTTPServer((host, port), LoggingHandler)
|
||||
logger.info(listener)
|
||||
threading.Thread(target=listener.serve_forever, daemon=True).start()
|
||||
|
||||
+134
-124
@@ -1,215 +1,225 @@
|
||||
"""Export ship loadout in ED Shipyard plain text format."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import defaultdict
|
||||
from collections.abc import Mapping
|
||||
import json
|
||||
import pathlib
|
||||
import re
|
||||
import time
|
||||
from collections import defaultdict
|
||||
from update import check_for_datafile_updates
|
||||
import outfitting
|
||||
import util_ships
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
from config import config
|
||||
from edmc_data import edshipyard_slot_map as slot_map
|
||||
from edmc_data import ship_name_map
|
||||
from EDMCLogging import get_main_logger
|
||||
import outfitting
|
||||
from update import check_for_datafile_updates
|
||||
import util_ships
|
||||
|
||||
logger = get_main_logger()
|
||||
|
||||
__Module = dict[str, str | list[str]]
|
||||
type ModuleData = dict[str, Any]
|
||||
|
||||
# Map API ship names to ED Shipyard names
|
||||
ship_map = ship_name_map.copy()
|
||||
|
||||
# Ship masses
|
||||
ships_file = config.app_dir_path / "ships.json"
|
||||
if not ships_file.is_file():
|
||||
check_for_datafile_updates()
|
||||
ships_file = config.app_dir_path / "ships.json" # Probably first boot. Force update.
|
||||
ships_file = (config.app_dir_path / "ships.json") # Probably first boot. Force update.
|
||||
with open(ships_file, encoding="utf-8") as ships_file_handle:
|
||||
ships = json.load(ships_file_handle)
|
||||
ships: dict[str, Any] = json.load(ships_file_handle)
|
||||
|
||||
|
||||
def export(data, filename=None) -> None: # noqa: C901, CCR001
|
||||
"""
|
||||
Export ship loadout in E:D Shipyard plain text format.
|
||||
def export(data: Mapping[str, Any], filename: str | pathlib.Path | None = None) -> None: # noqa: C901, CCR001
|
||||
"""Export ship loadout in E:D Shipyard plain text format.
|
||||
|
||||
:param data: CAPI data.
|
||||
:param filename: Override default file name.
|
||||
"""
|
||||
def class_rating(module: __Module) -> str:
|
||||
"""
|
||||
Return a string representation of the class of the given module.
|
||||
|
||||
:param module: Module data dict.
|
||||
:return: Rating of the module.
|
||||
"""
|
||||
mod_class = module['class']
|
||||
mod_rating = module['rating']
|
||||
mod_mount = module.get('mount')
|
||||
mod_guidance: str = str(module.get('guidance'))
|
||||
def class_rating(mod: ModuleData) -> str:
|
||||
"""Return a string representation of the class and grading of the given module."""
|
||||
mod_class = mod.get("class", "")
|
||||
mod_rating = mod.get("rating", "")
|
||||
mod_mount = mod.get("mount")
|
||||
mod_guidance = str(mod.get("guidance", ""))
|
||||
|
||||
ret = f'{mod_class}{mod_rating}'
|
||||
if 'guidance' in module: # Missiles
|
||||
if mod_mount is not None:
|
||||
mount = mod_mount[0]
|
||||
ret = f"{mod_class}{mod_rating}"
|
||||
|
||||
else:
|
||||
mount = 'F'
|
||||
if "guidance" in mod:
|
||||
mount = (mod_mount[0] if isinstance(mod_mount, (list, str)) and mod_mount else "F")
|
||||
guidance = mod_guidance[0] if mod_guidance else ""
|
||||
ret += f"/{mount}{guidance}"
|
||||
elif "mount" in mod: # Hardpoints
|
||||
ret += f"/{mod_mount}"
|
||||
elif "Cabin" in str(mod.get("name", "")): # Passenger cabins
|
||||
name_str = str(mod.get("name", ""))
|
||||
ret += f"/{name_str[0]}" if name_str else "/"
|
||||
|
||||
guidance = mod_guidance[0]
|
||||
ret += f'/{mount}{guidance}'
|
||||
return f"{ret} "
|
||||
|
||||
elif 'mount' in module: # Hardpoints
|
||||
ret += f'/{mod_mount}'
|
||||
querytime = config.get_int("querytime", default=int(datetime.now(timezone.utc).timestamp()))
|
||||
|
||||
elif 'Cabin' in module['name']: # Passenger cabins
|
||||
ret += f'/{module["name"][0]}'
|
||||
|
||||
return ret + ' '
|
||||
|
||||
querytime = config.get_int('querytime', default=int(time.time()))
|
||||
|
||||
loadout = defaultdict(list)
|
||||
loadout: dict[str, list[str]] = defaultdict(list)
|
||||
mass = 0.0
|
||||
fuel = 0
|
||||
cargo = 0
|
||||
fsd = None
|
||||
fsd: ModuleData | None = None
|
||||
jumpboost = 0
|
||||
|
||||
for slot in sorted(data['ship']['modules']):
|
||||
v = data['ship']['modules'][slot]
|
||||
ship_payload = data.get("ship", {})
|
||||
modules_payload = ship_payload.get("modules", {})
|
||||
|
||||
for slot in sorted(modules_payload):
|
||||
v = modules_payload[slot]
|
||||
try:
|
||||
if not v:
|
||||
if not v or "module" not in v:
|
||||
continue
|
||||
|
||||
module: __Module | None = outfitting.lookup(v['module'], ship_map)
|
||||
if not module:
|
||||
raw_module = outfitting.lookup(v["module"], ship_map)
|
||||
if not raw_module:
|
||||
continue
|
||||
|
||||
module = dict(raw_module)
|
||||
|
||||
cr = class_rating(module)
|
||||
mods = v.get('modifications') or v.get('WorkInProgress_modifications') or {}
|
||||
if mods.get('OutfittingFieldType_Mass'):
|
||||
mass += float(module.get('mass', 0.0) * mods['OutfittingFieldType_Mass']['value'])
|
||||
mods = v.get("modifications") or v.get("WorkInProgress_modifications") or {}
|
||||
|
||||
base_mass = float(module.get("mass", 0.0))
|
||||
if "OutfittingFieldType_Mass" in mods:
|
||||
mass += base_mass * float(mods["OutfittingFieldType_Mass"].get("value", 1.0))
|
||||
else:
|
||||
mass += float(module.get('mass', 0.0)) # type: ignore
|
||||
mass += base_mass
|
||||
|
||||
module_name = str(module.get("name", ""))
|
||||
module_class = str(module.get("class", "0"))
|
||||
|
||||
# Specials
|
||||
if 'Fuel Tank' in module['name']:
|
||||
fuel += 2**int(module['class']) # type: ignore
|
||||
name = f'{module["name"]} (Capacity: {2**int(module["class"])})' # type: ignore
|
||||
|
||||
elif 'Cargo Rack' in module['name']:
|
||||
cargo += 2**int(module['class']) # type: ignore
|
||||
name = f'{module["name"]} (Capacity: {2**int(module["class"])})' # type: ignore
|
||||
|
||||
if "Fuel Tank" in module_name:
|
||||
capacity = 2 ** int(module_class) if module_class.isdigit() else 0
|
||||
fuel += capacity
|
||||
name = f"{module_name} (Capacity: {capacity})"
|
||||
elif "Cargo Rack" in module_name:
|
||||
capacity = 2 ** int(module_class) if module_class.isdigit() else 0
|
||||
cargo += capacity
|
||||
name = f"{module_name} (Capacity: {capacity})"
|
||||
else:
|
||||
name = module['name'] # type: ignore
|
||||
name = module_name
|
||||
|
||||
if name in ['Frame Shift Drive', 'Frame Shift Drive (SCO)']:
|
||||
if name in ("Frame Shift Drive", "Frame Shift Drive (SCO)"):
|
||||
fsd = module # save for range calculation
|
||||
if "OutfittingFieldType_FSDOptimalMass" in mods:
|
||||
fsd["optmass"] = float(fsd.get("optmass", 0.0)) * float(
|
||||
mods["OutfittingFieldType_FSDOptimalMass"].get("value", 1.0)
|
||||
)
|
||||
if "OutfittingFieldType_MaxFuelPerJump" in mods:
|
||||
fsd["maxfuel"] = float(fsd.get("maxfuel", 0.0)) * float(
|
||||
mods["OutfittingFieldType_MaxFuelPerJump"].get("value", 1.0)
|
||||
)
|
||||
|
||||
if mods.get('OutfittingFieldType_FSDOptimalMass'):
|
||||
fsd['optmass'] *= mods['OutfittingFieldType_FSDOptimalMass']['value']
|
||||
|
||||
if mods.get('OutfittingFieldType_MaxFuelPerJump'):
|
||||
fsd['maxfuel'] *= mods['OutfittingFieldType_MaxFuelPerJump']['value']
|
||||
|
||||
jumpboost += module.get('jumpboost', 0) # type: ignore
|
||||
jumpboost += int(module.get("jumpboost", 0))
|
||||
|
||||
slot_lower = slot.lower()
|
||||
for slot_prefix, index in slot_map.items():
|
||||
if slot.lower().startswith(slot_prefix):
|
||||
if slot_lower.startswith(slot_prefix):
|
||||
loadout[index].append(cr + name)
|
||||
break
|
||||
|
||||
else:
|
||||
if slot.lower().startswith('slot'):
|
||||
if slot_lower.startswith("slot"):
|
||||
loadout[slot[-1]].append(cr + name)
|
||||
elif not slot.lower().startswith('planetaryapproachsuite'):
|
||||
logger.debug(f'EDShipyard: Unknown slot {slot}')
|
||||
elif not slot_lower.startswith("planetaryapproachsuite"):
|
||||
logger.debug(f"EDShipyard: Unknown slot {slot}")
|
||||
|
||||
except ValueError as e:
|
||||
logger.debug(f'EDShipyard: {e!r}')
|
||||
logger.debug(f"EDShipyard: Parsing validation break: {e!r}")
|
||||
continue # Silently skip unrecognized modules
|
||||
|
||||
except Exception:
|
||||
if __debug__:
|
||||
raise
|
||||
raw_ship_name = ship_payload.get("name", "")
|
||||
ship = ship_map.get(raw_ship_name.lower(), raw_ship_name)
|
||||
custom_name = ship_payload.get("shipName")
|
||||
|
||||
# Construct description
|
||||
ship = ship_map.get(data['ship']['name'].lower(), data['ship']['name'])
|
||||
if data['ship'].get('shipName') is not None:
|
||||
_ships = f'{ship}, {data["ship"]["shipName"]}'
|
||||
|
||||
else:
|
||||
_ships = ship
|
||||
|
||||
string = f'[{_ships}]\n'
|
||||
_ships = f"{ship}, {custom_name}" if custom_name is not None else ship
|
||||
string = f"[{_ships}]\n"
|
||||
|
||||
slot_types = (
|
||||
'H', 'L', 'M', 'S', 'U', None, 'BH', 'RB', 'TM', 'FH', 'EC', 'PC', 'SS', 'FS', None, 'MC', None, '9', '8',
|
||||
'7', '6', '5', '4', '3', '2', '1'
|
||||
)
|
||||
for slot in slot_types:
|
||||
if not slot:
|
||||
string += '\n'
|
||||
for slot_key in slot_types:
|
||||
if not slot_key:
|
||||
string += "\n"
|
||||
elif slot_key in loadout:
|
||||
for name in loadout[slot_key]:
|
||||
string += f"{slot_key}: {name}\n"
|
||||
|
||||
elif slot in loadout:
|
||||
for name in loadout[slot]:
|
||||
string += f'{slot}: {name}\n'
|
||||
|
||||
string += f'---\nCargo : {cargo} T\nFuel : {fuel} T\n'
|
||||
string += f"---\nCargo : {cargo} T\nFuel : {fuel} T\n"
|
||||
|
||||
# Add mass and range
|
||||
ship_name = data['ship']['name'].lower()
|
||||
if ship_name not in ship_name_map:
|
||||
raise ValueError(f"Ship name '{data['ship']['name']}' not found in ship_name_map")
|
||||
if ship_name_map[ship_name] not in ships:
|
||||
raise ValueError(f"Mapped ship name '{ship_name_map[ship_name]}' not found in ships")
|
||||
ship_lower_name = raw_ship_name.lower()
|
||||
if ship_lower_name not in ship_name_map:
|
||||
raise ValueError(f"Ship name '{raw_ship_name}' not found in ship_name_map")
|
||||
|
||||
mapped_name = ship_name_map[ship_lower_name]
|
||||
if mapped_name not in ships:
|
||||
raise ValueError(
|
||||
f"Mapped ship name '{mapped_name}' not found in ships database"
|
||||
)
|
||||
|
||||
try:
|
||||
mass += ships[ship_name_map[data['ship']['name'].lower()]]['hullMass']
|
||||
string += f'Mass : {mass:.2f} T empty\n {mass + fuel + cargo:.2f} T full\n'
|
||||
maxfuel = fsd.get('maxfuel', 0) # type: ignore
|
||||
fuelmul = fsd.get('fuelmul', 0) # type: ignore
|
||||
mass += ships[mapped_name]["hullMass"]
|
||||
string += (
|
||||
f"Mass : {mass:.2f} T empty\n {mass + fuel + cargo:.2f} T full\n"
|
||||
)
|
||||
|
||||
try:
|
||||
multiplier = pow(min(fuel, maxfuel) / fuelmul, 1.0 / fsd['fuelpower']) * fsd['optmass'] # type: ignore
|
||||
range_unladen = multiplier / (mass + fuel) + jumpboost
|
||||
range_laden = multiplier / (mass + fuel + cargo) + jumpboost
|
||||
# As of 2021-04-07 edsy.org says text import not yet implemented, so ignore the possible issue with
|
||||
# a locale that uses comma for decimal separator.
|
||||
except ZeroDivisionError:
|
||||
if fsd is not None:
|
||||
maxfuel = float(fsd.get("maxfuel", 0.0))
|
||||
fuelmul = float(fsd.get("fuelmul", 0.0))
|
||||
optmass = float(fsd.get("optmass", 0.0))
|
||||
fuelpower = float(fsd.get("fuelpower", 1.0))
|
||||
|
||||
try:
|
||||
multiplier = (pow(min(fuel, maxfuel) / fuelmul, 1.0 / fuelpower) * optmass)
|
||||
range_unladen = multiplier / (mass + fuel) + jumpboost
|
||||
range_laden = multiplier / (mass + fuel + cargo) + jumpboost
|
||||
except (ZeroDivisionError, ValueError):
|
||||
range_unladen = range_laden = 0.0
|
||||
else:
|
||||
range_unladen = range_laden = 0.0
|
||||
string += (f'Range : {range_unladen:.2f} LY unladen\n'
|
||||
f' {range_laden:.2f} LY laden\n')
|
||||
|
||||
string += f"Range : {range_unladen:.2f} LY unladen\n {range_laden:.2f} LY laden\n"
|
||||
|
||||
except Exception:
|
||||
if __debug__:
|
||||
raise
|
||||
|
||||
if filename:
|
||||
with open(filename, 'w') as h:
|
||||
with open(filename, "w", encoding="utf-8") as h:
|
||||
h.write(string)
|
||||
return
|
||||
|
||||
# Look for last ship of this type
|
||||
ship = util_ships.ship_file_name(data['ship'].get('shipName'), data['ship']['name'])
|
||||
regexp = re.compile(re.escape(ship) + r'\.\d{4}-\d\d-\d\dT\d\d\.\d\d\.\d\d\.txt')
|
||||
out_dir = pathlib.Path(config.get_str('outdir'))
|
||||
oldfiles = sorted(
|
||||
[x for x in out_dir.iterdir() if regexp.match(x.name)],
|
||||
key=lambda p: p.name # Sort based on the filename string
|
||||
ship_filename_base = util_ships.ship_file_name(custom_name, raw_ship_name)
|
||||
regexp = re.compile(
|
||||
re.escape(ship_filename_base) + r"\.\d{4}-\d\d-\d\dT\d\d\.\d\d\.\d\d\.txt"
|
||||
)
|
||||
if oldfiles:
|
||||
with (pathlib.Path(config.get_str('outdir')) / oldfiles[-1]).open() as h:
|
||||
if h.read() == string:
|
||||
return # same as last time - don't write
|
||||
out_dir = pathlib.Path(config.get_str("outdir"))
|
||||
|
||||
if out_dir.is_dir():
|
||||
oldfiles = sorted(
|
||||
[x for x in out_dir.iterdir() if regexp.match(x.name)],
|
||||
key=lambda p: p.name, # Sort based on the filename string
|
||||
)
|
||||
if oldfiles:
|
||||
with oldfiles[-1].open(encoding="utf-8") as h:
|
||||
if h.read() == string:
|
||||
return # same as last time - don't write
|
||||
|
||||
# Write
|
||||
timestamp = time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime(querytime))
|
||||
filename = pathlib.Path(config.get_str('outdir')) / f'{ship}.{timestamp}.txt'
|
||||
timestamp = datetime.fromtimestamp(querytime).strftime("%Y-%m-%dT%H.%M.%S")
|
||||
final_output_path = out_dir / f"{ship_filename_base}.{timestamp}.txt"
|
||||
|
||||
with open(filename, 'w') as h:
|
||||
with open(final_output_path, "w", encoding="utf-8") as h:
|
||||
h.write(string)
|
||||
+2
-5
@@ -1,14 +1,11 @@
|
||||
"""Handle keyboard input for manual update triggering."""
|
||||
from __future__ import annotations
|
||||
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import abc
|
||||
import sys
|
||||
from abc import abstractmethod
|
||||
from abc import abstractmethod, ABC
|
||||
|
||||
|
||||
class AbstractHotkeyMgr(abc.ABC):
|
||||
class AbstractHotkeyMgr(ABC):
|
||||
"""Abstract root class of all platforms specific HotKeyMgr."""
|
||||
|
||||
@abstractmethod
|
||||
|
||||
+36
-42
@@ -12,7 +12,7 @@ import threading
|
||||
from copy import deepcopy
|
||||
from typing import (
|
||||
TYPE_CHECKING, Any, NamedTuple,
|
||||
TypedDict, TypeVar, cast
|
||||
TypedDict, cast
|
||||
)
|
||||
from collections.abc import Callable, Mapping, MutableMapping, MutableSequence, Sequence
|
||||
import requests
|
||||
@@ -29,10 +29,8 @@ CURRENT_KILLSWITCH_VERSION = 2
|
||||
UPDATABLE_DATA = Mapping | Sequence
|
||||
_current_version: semantic_version.Version = config.appversion_nobuild()
|
||||
|
||||
T = TypeVar('T', bound=UPDATABLE_DATA)
|
||||
|
||||
|
||||
class SingleKill(NamedTuple):
|
||||
class SingleKill[T: Mapping[Any, Any] | Sequence[Any]](NamedTuple): # noqa: D101
|
||||
"""A single KillSwitch. Possibly with additional rules."""
|
||||
|
||||
match: str
|
||||
@@ -78,30 +76,27 @@ def _apply(target: UPDATABLE_DATA, key: str, to_set: Any = None, delete: bool =
|
||||
:raises ValueError: when an unexpected target type is passed
|
||||
:raises IndexError: when an invalid index is set or deleted
|
||||
"""
|
||||
if isinstance(target, MutableMapping):
|
||||
if delete:
|
||||
target.pop(key, None)
|
||||
else:
|
||||
target[key] = to_set
|
||||
match target:
|
||||
case MutableMapping():
|
||||
if delete:
|
||||
target.pop(key, None)
|
||||
else:
|
||||
target[key] = to_set
|
||||
case MutableSequence():
|
||||
idx = _get_int(key)
|
||||
if idx is None:
|
||||
raise ValueError(f'Cannot use string {key!r} as int for index into Sequence')
|
||||
|
||||
elif isinstance(target, MutableSequence):
|
||||
idx = _get_int(key)
|
||||
if idx is None:
|
||||
raise ValueError(f'Cannot use string {key!r} as int for index into Sequence')
|
||||
|
||||
if delete and len(target) > 0:
|
||||
length = len(target)
|
||||
if idx in range(-length, length):
|
||||
target.pop(idx)
|
||||
|
||||
elif len(target) == idx:
|
||||
target.append(to_set)
|
||||
|
||||
else:
|
||||
target[idx] = to_set # this can raise, that's fine
|
||||
|
||||
else:
|
||||
raise ValueError(f'Dont know how to apply data to {type(target)} {target!r}')
|
||||
if delete and len(target) > 0:
|
||||
length = len(target)
|
||||
if idx in range(-length, length):
|
||||
target.pop(idx)
|
||||
elif len(target) == idx:
|
||||
target.append(to_set)
|
||||
else:
|
||||
target[idx] = to_set # this can raise, that's fine
|
||||
case _:
|
||||
raise ValueError(f'Dont know how to apply data to {type(target)} {target!r}')
|
||||
|
||||
|
||||
def _deep_apply(target: UPDATABLE_DATA, path: str, to_set=None, delete=False): # noqa: CCR001 # Recursive silliness.
|
||||
@@ -144,18 +139,17 @@ def _deep_apply(target: UPDATABLE_DATA, path: str, to_set=None, delete=False):
|
||||
else:
|
||||
key, _, path = path.partition('.')
|
||||
|
||||
if isinstance(current, Mapping):
|
||||
current = current[key] # type: ignore # I really don't know at this point what you want from me mypy.
|
||||
|
||||
elif isinstance(current, Sequence):
|
||||
target_idx = _get_int(key) # mypy is broken. doesn't like := here.
|
||||
if target_idx is not None:
|
||||
current = current[target_idx]
|
||||
else:
|
||||
raise ValueError(f'Cannot index sequence with non-int key {key!r}')
|
||||
|
||||
else:
|
||||
raise ValueError(f'Dont know how to index a {type(current)} ({current!r})')
|
||||
match current:
|
||||
case Mapping():
|
||||
current = current[key] # type: ignore # I really don't know at this point what you want from me mypy.
|
||||
case Sequence():
|
||||
target_idx = _get_int(key) # mypy is broken. doesn't like := here.
|
||||
if target_idx is not None:
|
||||
current = current[target_idx]
|
||||
else:
|
||||
raise ValueError(f'Cannot index sequence with non-int key {key!r}')
|
||||
case _:
|
||||
raise ValueError(f'Dont know how to index a {type(current)} ({current!r})')
|
||||
|
||||
_apply(current, path, to_set, delete)
|
||||
|
||||
@@ -212,7 +206,7 @@ class DisabledResult(NamedTuple):
|
||||
return self.has_kill() and self.kill.has_rules # type: ignore
|
||||
|
||||
|
||||
class KillSwitchSet:
|
||||
class KillSwitchSet[T]:
|
||||
"""Queryable set of kill switches."""
|
||||
|
||||
def __init__(self, kill_switches: list[KillSwitches]) -> None:
|
||||
@@ -504,12 +498,12 @@ def get_disabled(id: str, *, version: semantic_version.Version = _current_versio
|
||||
return active.get_disabled(id, version=version)
|
||||
|
||||
|
||||
def check_killswitch(name: str, data: T, log=logger) -> tuple[bool, T]:
|
||||
def check_killswitch[T](name: str, data: T, log=logger) -> tuple[bool, T]: # noqa: D103
|
||||
"""Query the global KillSwitchSet#check_killswitch method."""
|
||||
return active.check_killswitch(name, data, log)
|
||||
|
||||
|
||||
def check_multiple_killswitches(data: T, *names: str, log=logger) -> tuple[bool, T]:
|
||||
def check_multiple_killswitches[T](data: T, *names: str, log=logger) -> tuple[bool, T]: # noqa: D103
|
||||
"""Query the global KillSwitchSet#check_multiple method."""
|
||||
return active.check_multiple_killswitches(data, *names, log=log)
|
||||
|
||||
|
||||
@@ -87,16 +87,13 @@ class Translations:
|
||||
if not lang:
|
||||
# Choose the default language
|
||||
for preferred in Locale.preferred_languages():
|
||||
components = preferred.split('-')
|
||||
if preferred in available:
|
||||
lang = preferred
|
||||
|
||||
elif '-'.join(components[0:2]) in available:
|
||||
lang = '-'.join(components[0:2]) # language-script
|
||||
|
||||
elif components[0] in available:
|
||||
lang = components[0] # just base language
|
||||
|
||||
match preferred.split('-'):
|
||||
case [lang_code, script_code, *_] if f"{lang_code}-{script_code}" in available:
|
||||
lang = f"{lang_code}-{script_code}"
|
||||
case [base_lang, *_] if base_lang in available:
|
||||
lang = base_lang
|
||||
case _ if preferred in available:
|
||||
lang = preferred
|
||||
if lang:
|
||||
break
|
||||
|
||||
|
||||
+52
-29
@@ -1,60 +1,83 @@
|
||||
"""
|
||||
loadout.py - Export ship loadout in Companion API json format.
|
||||
"""loadout.py - Export ship loadout in Companion API json format.
|
||||
|
||||
Copyright (c) EDCD, All Rights Reserved
|
||||
Licensed under the GNU General Public License v2 or later.
|
||||
See LICENSE file.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import pathlib
|
||||
import re
|
||||
import time
|
||||
from pathlib import Path
|
||||
import companion
|
||||
import util_ships
|
||||
from datetime import datetime, timezone
|
||||
from config import config
|
||||
from EDMCLogging import get_main_logger
|
||||
import util_ships
|
||||
|
||||
logger = get_main_logger()
|
||||
|
||||
|
||||
def export(data: companion.CAPIData, requested_filename: str | None = None) -> None:
|
||||
"""
|
||||
Write Ship Loadout in Companion API JSON format.
|
||||
def export(
|
||||
data: companion.CAPIData, requested_filename: str | pathlib.Path | None = None
|
||||
) -> None:
|
||||
"""Write Ship Loadout in Companion API JSON format.
|
||||
|
||||
:param data: CAPI data containing ship loadout.
|
||||
:param requested_filename: Name of file to write to.
|
||||
:param requested_filename: Explicit name or Path of file to write to, or None to auto-generate.
|
||||
"""
|
||||
# Pretty-Print
|
||||
string = json.dumps(
|
||||
companion.ship(data), cls=companion.CAPIDataEncoder,
|
||||
ensure_ascii=False, indent=2, sort_keys=True, separators=(',', ': ')
|
||||
) # pretty print
|
||||
companion.ship(data),
|
||||
cls=companion.CAPIDataEncoder,
|
||||
ensure_ascii=False,
|
||||
indent=2,
|
||||
sort_keys=True,
|
||||
separators=(",", ": "),
|
||||
)
|
||||
|
||||
if requested_filename is not None and requested_filename:
|
||||
with open(requested_filename, 'w') as h:
|
||||
if requested_filename:
|
||||
target_path = pathlib.Path(requested_filename)
|
||||
with open(target_path, "w", encoding="utf-8") as h:
|
||||
h.write(string)
|
||||
return
|
||||
if not requested_filename:
|
||||
logger.error(f"{requested_filename=} is not valid")
|
||||
|
||||
if requested_filename == "":
|
||||
logger.error("provided requested_filename is an invalid blank string sequence.")
|
||||
return
|
||||
|
||||
# Look for last ship of this type
|
||||
ship = util_ships.ship_file_name(data['ship'].get('shipName'), data['ship']['name'])
|
||||
regexp = re.compile(re.escape(ship) + r'\.\d\d\d\d-\d\d-\d\dT\d\d\.\d\d\.\d\d\.txt')
|
||||
oldfiles = sorted([x.name for x in Path(config.get_str('outdir')).iterdir() if regexp.match(x.name)])
|
||||
if oldfiles:
|
||||
with open(Path(config.get_str('outdir')) / Path(oldfiles[-1])) as h:
|
||||
if h.read() == string:
|
||||
return # same as last time - don't write
|
||||
ship_data = data.get("ship", {})
|
||||
custom_name = ship_data.get("shipName")
|
||||
raw_ship_type = ship_data.get("name", "UnknownShip")
|
||||
|
||||
query_time = config.get_int('querytime', default=int(time.time()))
|
||||
ship_file_base = util_ships.ship_file_name(custom_name, raw_ship_type)
|
||||
|
||||
# Write
|
||||
regexp = re.compile(
|
||||
re.escape(ship_file_base) + r"\.\d{4}-\d{2}-\d{2}T\d{2}\.\d{2}\.\d{2}\.txt"
|
||||
)
|
||||
|
||||
output_directory = Path(config.get_str('outdir'))
|
||||
ship_time = time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime(query_time))
|
||||
file_path = output_directory / f"{ship}.{ship_time}.txt"
|
||||
out_dir = pathlib.Path(config.get_str("outdir"))
|
||||
|
||||
with open(file_path, 'w') as h:
|
||||
if out_dir.is_dir():
|
||||
oldfiles = sorted(
|
||||
[x for x in out_dir.iterdir() if regexp.match(x.name)], key=lambda p: p.name
|
||||
)
|
||||
|
||||
# Deduplication
|
||||
if oldfiles:
|
||||
try:
|
||||
with open(oldfiles[-1], encoding="utf-8") as h:
|
||||
if h.read() == string:
|
||||
return
|
||||
except (OSError, UnicodeDecodeError) as e:
|
||||
logger.debug(f"Could not read historical loadout log for deduplication: {e!r}")
|
||||
|
||||
querytime = config.get_int("querytime", default=int(datetime.now(timezone.utc).timestamp()))
|
||||
timestamp = datetime.fromtimestamp(querytime).strftime("%Y-%m-%dT%H.%M.%S")
|
||||
|
||||
final_output_file = out_dir / f"{ship_file_base}.{timestamp}.txt"
|
||||
|
||||
with open(final_output_file, "w", encoding="utf-8") as h:
|
||||
h.write(string)
|
||||
+114
-139
@@ -13,10 +13,10 @@ import queue
|
||||
import re
|
||||
import sys
|
||||
import threading
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
from collections import defaultdict
|
||||
from os import SEEK_END, SEEK_SET
|
||||
from time import gmtime, localtime, mktime, sleep, strftime, strptime, time
|
||||
from os import SEEK_SET
|
||||
from time import sleep
|
||||
from typing import TYPE_CHECKING, Any, BinaryIO
|
||||
from collections.abc import MutableMapping
|
||||
import psutil
|
||||
@@ -371,11 +371,11 @@ class EDLogs(FileSystemEventHandler):
|
||||
# https://mail.python.org/pipermail/tkinter-discuss/2013-November/003522.html
|
||||
|
||||
logger.debug(f'Starting on logfile "{self.logfile}"')
|
||||
# Seek to the end of the latest log file
|
||||
log_pos = -1 # make this bound, but with something that should go bang if its misused
|
||||
log_pos = 0
|
||||
logfile = self.logfile
|
||||
loghandle: BinaryIO | None = None
|
||||
if logfile:
|
||||
loghandle: BinaryIO = open(logfile, 'rb', 0) # unbuffered
|
||||
loghandle = open(logfile, 'rb', 0) # unbuffered
|
||||
|
||||
self.catching_up = True
|
||||
for line in loghandle:
|
||||
@@ -389,17 +389,13 @@ class EDLogs(FileSystemEventHandler):
|
||||
logger.debug(f'Invalid journal entry:\n{line!r}\n', exc_info=ex)
|
||||
|
||||
# One-shot attempt to read in latest NavRoute, if present
|
||||
navroute_data = self._parse_navroute_file()
|
||||
if navroute_data is not None:
|
||||
if (navroute_data := self._parse_navroute_file()) is not None:
|
||||
# If it's NavRouteClear contents, just keep those anyway.
|
||||
self.state['NavRoute'] = navroute_data
|
||||
|
||||
self.catching_up = False
|
||||
log_pos = loghandle.tell()
|
||||
|
||||
else:
|
||||
loghandle = None # type: ignore
|
||||
|
||||
logger.debug('Now at end of latest file.')
|
||||
|
||||
self.game_was_running = self.game_running()
|
||||
@@ -409,8 +405,7 @@ class EDLogs(FileSystemEventHandler):
|
||||
logger.info("Game is/was running, so synthesizing StartUp event for plugins")
|
||||
# Game is running locally
|
||||
entry = self.synthesize_startup_event()
|
||||
|
||||
self.event_queue.put(json.dumps(entry, separators=(', ', ':')))
|
||||
self.event_queue.put(json.dumps(entry, separators=(',', ':')))
|
||||
|
||||
else:
|
||||
# Generate null event to update the display (with possibly out-of-date info)
|
||||
@@ -418,95 +413,94 @@ class EDLogs(FileSystemEventHandler):
|
||||
self.live = False
|
||||
|
||||
emitter = None
|
||||
# Watchdog thread -- there is a way to get this by using self.observer.emitters and checking for an attribute:
|
||||
# watch, but that may have unforseen differences in behaviour.
|
||||
# Watchdog thread -- extract internal emitter via safe dictionary query
|
||||
if self.observed:
|
||||
if self.observer is None:
|
||||
raise RuntimeError("Observer was None but is it in use?")
|
||||
# Note: Uses undocumented attribute
|
||||
emitter = self.observed and self.observer._emitter_for_watch[self.observed]
|
||||
emitter = self.observer._emitter_for_watch.get(self.observed)
|
||||
|
||||
logger.debug('Entering loop...')
|
||||
while True:
|
||||
try:
|
||||
while True:
|
||||
current_thread = threading.current_thread()
|
||||
|
||||
# Check whether new log file started, e.g. client (re)started.
|
||||
if emitter and emitter.is_alive():
|
||||
new_journal_file: str | None = self.logfile # updated by on_created watchdog callback
|
||||
if current_thread != self.thread:
|
||||
logger.info("We're not meant to be running, exiting...")
|
||||
return
|
||||
|
||||
else:
|
||||
# Poll
|
||||
try:
|
||||
new_journal_file = self.journal_newest_filename(self.currentdir)
|
||||
if emitter and emitter.is_alive():
|
||||
new_journal_file = self.logfile # updated by on_created watchdog callback
|
||||
else:
|
||||
# Poll
|
||||
try:
|
||||
new_journal_file = self.journal_newest_filename(self.currentdir)
|
||||
except Exception:
|
||||
logger.exception('Failed to find latest logfile')
|
||||
new_journal_file = None
|
||||
|
||||
except Exception:
|
||||
logger.exception('Failed to find latest logfile')
|
||||
new_journal_file = None
|
||||
if logfile and loghandle:
|
||||
# Update, June 2026:Removed `loghandle.seek(0, SEEK_END)`. This was only needed for macOS
|
||||
# SMB network filesystem updates. macOS is explicitly deprecated, native Linux and Windows work.
|
||||
loghandle.seek(log_pos, SEEK_SET) # reset EOF flag
|
||||
|
||||
if logfile:
|
||||
loghandle.seek(0, SEEK_END) # required for macOS to notice log change over SMB. TODO: Do we need this?
|
||||
loghandle.seek(log_pos, SEEK_SET) # reset EOF flag # TODO: log_pos reported as possibly unbound
|
||||
for line in loghandle:
|
||||
# Paranoia check to see if we're shutting down
|
||||
if threading.current_thread() != self.thread:
|
||||
logger.info("We're not meant to be running, exiting...")
|
||||
return # Terminate
|
||||
for line in loghandle:
|
||||
# Paranoia check to see if we're shutting down
|
||||
if current_thread != self.thread:
|
||||
logger.info("We're not meant to be running, exiting...")
|
||||
return # Terminate
|
||||
|
||||
if b'"event":"Continue"' in line:
|
||||
for _ in range(10):
|
||||
logger.trace_if('journal.continuation', "****")
|
||||
logger.trace_if('journal.continuation', 'Found a Continue event, its being added to the list, '
|
||||
'we will finish this file up and then continue with the next')
|
||||
if b'"event":"Continue"' in line:
|
||||
for _ in range(10):
|
||||
logger.trace_if('journal.continuation', "****")
|
||||
logger.trace_if('journal.continuation', 'Found a Continue event...')
|
||||
|
||||
self.event_queue.put(line)
|
||||
self.event_queue.put(line)
|
||||
|
||||
if not self.event_queue.empty():
|
||||
if not config.shutting_down:
|
||||
if not self.event_queue.empty() and not config.shutting_down:
|
||||
logger.trace_if('journal.queue', 'Sending <<JournalEvent>>')
|
||||
self.root.event_generate('<<JournalEvent>>', when="tail")
|
||||
|
||||
log_pos = loghandle.tell()
|
||||
log_pos = loghandle.tell()
|
||||
|
||||
if logfile != new_journal_file:
|
||||
for _ in range(10):
|
||||
logger.trace_if('journal.file', "****")
|
||||
logger.info(f'New Journal File. Was "{logfile}", now "{new_journal_file}"')
|
||||
logfile = new_journal_file
|
||||
if loghandle:
|
||||
loghandle.close()
|
||||
if logfile != new_journal_file:
|
||||
for _ in range(10):
|
||||
logger.trace_if('journal.file', "****")
|
||||
logger.info(f'New Journal File. Was "{logfile}", now "{new_journal_file}"')
|
||||
|
||||
if logfile:
|
||||
loghandle = open(logfile, 'rb', 0) # unbuffered
|
||||
log_pos = 0
|
||||
logfile = new_journal_file
|
||||
if loghandle:
|
||||
loghandle.close()
|
||||
loghandle = None
|
||||
|
||||
if self.game_was_running:
|
||||
sleep(self._POLL)
|
||||
else:
|
||||
sleep(self._INACTIVE_POLL)
|
||||
if logfile:
|
||||
loghandle = open(logfile, 'rb', 0) # unbuffered
|
||||
log_pos = 0
|
||||
|
||||
# Check whether we're still supposed to be running
|
||||
if threading.current_thread() != self.thread:
|
||||
logger.info("We're not meant to be running, exiting...")
|
||||
if loghandle:
|
||||
loghandle.close()
|
||||
sleep(self._POLL if self.game_was_running else self._INACTIVE_POLL)
|
||||
|
||||
return # Terminate
|
||||
# Check whether we're still supposed to be running post-sleep
|
||||
if current_thread != self.thread:
|
||||
logger.info("We're not meant to be running, exiting...")
|
||||
return # Terminate
|
||||
|
||||
if self.game_was_running:
|
||||
if not self.game_running():
|
||||
logger.info('Detected exit from game, synthesising ShutDown event')
|
||||
timestamp = strftime('%Y-%m-%dT%H:%M:%SZ', gmtime())
|
||||
self.event_queue.put(
|
||||
f'{{ "timestamp":"{timestamp}", "event":"ShutDown" }}'
|
||||
)
|
||||
if self.game_was_running:
|
||||
if not self.game_running():
|
||||
logger.info('Detected exit from game, synthesising ShutDown event')
|
||||
timestamp = datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||
self.event_queue.put(
|
||||
f'{{ "timestamp":"{timestamp}", "event":"ShutDown" }}'
|
||||
)
|
||||
|
||||
if not config.shutting_down:
|
||||
logger.trace_if('journal.queue', 'Sending <<JournalEvent>>')
|
||||
self.root.event_generate('<<JournalEvent>>', when="tail")
|
||||
if not config.shutting_down:
|
||||
logger.trace_if('journal.queue', 'Sending <<JournalEvent>>')
|
||||
self.root.event_generate('<<JournalEvent>>', when="tail")
|
||||
|
||||
self.game_was_running = False
|
||||
|
||||
else:
|
||||
self.game_was_running = self.game_running()
|
||||
self.game_was_running = False
|
||||
else:
|
||||
self.game_was_running = self.game_running()
|
||||
finally:
|
||||
if loghandle:
|
||||
loghandle.close()
|
||||
|
||||
def synthesize_startup_event(self) -> dict[str, Any]:
|
||||
"""
|
||||
@@ -519,12 +513,12 @@ class EDLogs(FileSystemEventHandler):
|
||||
:return: Synthesized event as a dict
|
||||
"""
|
||||
entry: dict[str, Any] = {
|
||||
'timestamp': strftime('%Y-%m-%dT%H:%M:%SZ', gmtime()),
|
||||
'event': 'StartUp',
|
||||
'StarSystem': self.state['SystemName'],
|
||||
'StarPos': self.state['StarPos'],
|
||||
'SystemAddress': self.state['SystemAddress'],
|
||||
'Population': self.state['SystemPopulation'],
|
||||
'timestamp': datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ'),
|
||||
'event': 'StartUp',
|
||||
'StarSystem': self.state['SystemName'],
|
||||
'StarPos': self.state['StarPos'],
|
||||
'SystemAddress': self.state['SystemAddress'],
|
||||
'Population': self.state['SystemPopulation'],
|
||||
}
|
||||
|
||||
if self.state['Body']:
|
||||
@@ -1568,7 +1562,7 @@ class EDLogs(FileSystemEventHandler):
|
||||
|
||||
elif event_type == 'navroute' and not self.catching_up:
|
||||
# assume we've failed out the gate, then pull it back if things are fine
|
||||
self._last_navroute_journal_timestamp = mktime(strptime(entry['timestamp'], '%Y-%m-%dT%H:%M:%SZ'))
|
||||
self._last_navroute_journal_timestamp = datetime.fromisoformat(entry['timestamp']).timestamp()
|
||||
self._navroute_retries_remaining = 11
|
||||
|
||||
# Added in ED 3.7 - multi-hop route details in NavRoute.json
|
||||
@@ -1578,7 +1572,7 @@ class EDLogs(FileSystemEventHandler):
|
||||
|
||||
elif event_type == 'fcmaterials' and not self.catching_up:
|
||||
# assume we've failed out the gate, then pull it back if things are fine
|
||||
self._last_fcmaterials_journal_timestamp = mktime(strptime(entry['timestamp'], '%Y-%m-%dT%H:%M:%SZ'))
|
||||
self._last_fcmaterials_journal_timestamp = datetime.fromisoformat(entry['timestamp']).timestamp()
|
||||
self._fcmaterials_retries_remaining = 11
|
||||
|
||||
# Added in ED 4.0.0.1300 - Fleet Carrier Materials market in FCMaterials.json
|
||||
@@ -2188,7 +2182,7 @@ class EDLogs(FileSystemEventHandler):
|
||||
self.event_queue.put(json.dumps(entry, separators=(', ', ':')))
|
||||
|
||||
elif self.live and entry['event'] == 'Music' and entry.get('MusicTrack') == 'MainMenu':
|
||||
ts = strftime('%Y-%m-%dT%H:%M:%SZ', gmtime())
|
||||
ts = datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||
self.event_queue.put(
|
||||
f'{{ "timestamp":"{ts}", "event":"ShutDown" }}'
|
||||
)
|
||||
@@ -2242,7 +2236,7 @@ class EDLogs(FileSystemEventHandler):
|
||||
|
||||
d: MutableMapping[str, Any] = {}
|
||||
if timestamped:
|
||||
d['timestamp'] = strftime('%Y-%m-%dT%H:%M:%SZ', gmtime())
|
||||
d['timestamp'] = datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||
|
||||
d['event'] = 'Loadout'
|
||||
d['Ship'] = self.state['ShipType']
|
||||
@@ -2274,89 +2268,69 @@ class EDLogs(FileSystemEventHandler):
|
||||
|
||||
return d
|
||||
|
||||
def export_ship(self, filename=None) -> None: # noqa: C901, CCR001
|
||||
def export_ship(self, filename: str | None = None) -> None: # noqa: C901, CCR001
|
||||
"""
|
||||
Export ship loadout as a Loadout event.
|
||||
|
||||
Writes either to the specified filename or to a formatted filename based on
|
||||
the ship name and a date+timestamp.
|
||||
|
||||
:param filename: Name of file to write to, if not default.
|
||||
"""
|
||||
# TODO(A_D): Some type checking has been disabled in here due to config.get getting weird outputs
|
||||
string = json.dumps(self.ship(False), ensure_ascii=False, indent=2, separators=(',', ': ')) # pretty print
|
||||
string = json.dumps(self.ship(False), ensure_ascii=False, indent=2, separators=(',', ': '))
|
||||
outdir_str = config.get_str('outdir')
|
||||
outdir = pathlib.Path(outdir_str) if outdir_str else pathlib.Path('.')
|
||||
|
||||
if filename:
|
||||
try:
|
||||
with open(filename, 'w', encoding='utf-8') as h:
|
||||
h.write(string)
|
||||
|
||||
except UnicodeError:
|
||||
logger.exception("UnicodeError writing ship loadout to specified filename with utf-8 encoding"
|
||||
", trying without..."
|
||||
)
|
||||
|
||||
except (UnicodeError, OSError) as e:
|
||||
logger.exception(f"Error writing ship loadout to specified filename: {e}")
|
||||
try:
|
||||
with open(filename, 'w') as h:
|
||||
h.write(string)
|
||||
|
||||
except OSError:
|
||||
logger.exception("OSError writing ship loadout to specified filename with default encoding"
|
||||
", aborting."
|
||||
)
|
||||
|
||||
except OSError:
|
||||
logger.exception("OSError writing ship loadout to specified filename with utf-8 encoding, aborting.")
|
||||
|
||||
logger.exception(
|
||||
"Fatal OSError writing ship loadout to specified filename with default encoding, aborting.")
|
||||
return
|
||||
|
||||
ship = util_ships.ship_file_name(self.state['ShipName'], self.state['ShipType'])
|
||||
regexp = re.compile(re.escape(ship) + r'\.\d{4}-\d\d-\d\dT\d\d\.\d\d\.\d\d\.txt')
|
||||
oldfiles = sorted(x.name for x in pathlib.Path(config.get_str('outdir')).iterdir() if regexp.match(x.name))
|
||||
|
||||
try:
|
||||
oldfiles = sorted(x.name for x in outdir.iterdir() if regexp.match(x.name))
|
||||
except OSError:
|
||||
oldfiles = []
|
||||
|
||||
if oldfiles:
|
||||
target_file = outdir / oldfiles[-1]
|
||||
try:
|
||||
with (pathlib.Path(config.get_str('outdir')) / oldfiles[-1]).open(encoding='utf-8') as h:
|
||||
with target_file.open(encoding='utf-8') as h:
|
||||
if h.read() == string:
|
||||
return # same as last time - don't write
|
||||
|
||||
except UnicodeError:
|
||||
logger.exception("UnicodeError reading old ship loadout with utf-8 encoding, trying without...")
|
||||
return # Contents match, don't write again.
|
||||
except (UnicodeError, OSError, ValueError) as e:
|
||||
logger.trace_if('capi.worker',
|
||||
f"Failed to read old ship loadout natively ({e}), attempting fallback...")
|
||||
try:
|
||||
with (pathlib.Path(config.get_str('outdir')) / oldfiles[-1]).open(encoding='utf-8') as h:
|
||||
with target_file.open() as h:
|
||||
if h.read() == string:
|
||||
return # same as last time - don't write
|
||||
|
||||
return
|
||||
except OSError:
|
||||
logger.exception("OSError reading old ship loadout default encoding.")
|
||||
|
||||
except ValueError:
|
||||
# User was on $OtherEncoding, updated windows to be sane and use utf8 everywhere, thus
|
||||
# the above open() fails, likely with a UnicodeDecodeError, which subclasses UnicodeError which
|
||||
# subclasses ValueError, this catches ValueError _instead_ of UnicodeDecodeError just to be sure
|
||||
# that if some other encoding error crops up we grab it too.
|
||||
logger.exception('ValueError when reading old ship loadout default encoding')
|
||||
|
||||
except OSError:
|
||||
logger.exception("OSError reading old ship loadout with default encoding")
|
||||
logger.exception("OSError reading old ship loadout with default system encoding.")
|
||||
|
||||
# Write
|
||||
ts = strftime('%Y-%m-%dT%H.%M.%S', localtime(time()))
|
||||
filename = str(pathlib.Path(config.get_str('outdir')) / f'{ship}.{ts}.txt')
|
||||
ts = datetime.now().strftime('%Y-%m-%dT%H.%M.%S')
|
||||
filename = str(outdir / f'{ship}.{ts}.txt')
|
||||
|
||||
try:
|
||||
with open(filename, 'w', encoding='utf-8') as h:
|
||||
h.write(string)
|
||||
|
||||
except UnicodeError:
|
||||
logger.exception("UnicodeError writing ship loadout to new filename with utf-8 encoding, trying without...")
|
||||
except (UnicodeError, OSError):
|
||||
logger.exception("Error writing ship loadout to new filename with utf-8 encoding, trying fallback...")
|
||||
try:
|
||||
with open(filename, 'w') as h:
|
||||
h.write(string)
|
||||
|
||||
except OSError:
|
||||
logger.exception("OSError writing ship loadout to new filename with default encoding, aborting.")
|
||||
|
||||
except OSError:
|
||||
logger.exception("OSError writing ship loadout to new filename with utf-8 encoding, aborting.")
|
||||
logger.exception("Fatal OSError writing ship loadout to new filename with default encoding, aborting.")
|
||||
|
||||
def coalesce_cargo(self, raw_cargo: list[MutableMapping[str, Any]]) -> list[MutableMapping[str, Any]]:
|
||||
"""
|
||||
@@ -2474,7 +2448,8 @@ class EDLogs(FileSystemEventHandler):
|
||||
|
||||
@staticmethod
|
||||
def _parse_journal_timestamp(source: str) -> float:
|
||||
return mktime(strptime(source, '%Y-%m-%dT%H:%M:%SZ'))
|
||||
"""Parse a UTC ISO 8601 journal timestamp string into a local Unix epoch float."""
|
||||
return datetime.fromisoformat(source).timestamp()
|
||||
|
||||
def __navroute_retry(self) -> bool:
|
||||
"""Retry reading navroute files."""
|
||||
|
||||
+260
-170
@@ -5,9 +5,12 @@ Copyright (c) EDCD, All Rights Reserved
|
||||
Licensed under the GNU General Public License v2 or later.
|
||||
See LICENSE file.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
import json
|
||||
from typing import Any
|
||||
from config import config
|
||||
from edmc_data import (
|
||||
outfitting_armour_map as armour_map,
|
||||
@@ -34,10 +37,12 @@ from EDMCLogging import get_main_logger
|
||||
logger = get_main_logger()
|
||||
|
||||
# Module mass, FSD data etc
|
||||
moduledata: dict = {}
|
||||
moduledata: dict[str, Any] = {}
|
||||
|
||||
|
||||
def lookup(module, ship_map, entitled=False) -> dict | None: # noqa: C901, CCR001
|
||||
def lookup( # noqa: C901, CCR001
|
||||
module: dict, ship_map: dict, entitled: bool = False
|
||||
) -> dict | None:
|
||||
"""
|
||||
Produce a standard dict description of the given module.
|
||||
|
||||
@@ -58,216 +63,283 @@ def lookup(module, ship_map, entitled=False) -> dict | None: # noqa: C901, CCR0
|
||||
modules_path = config.app_dir_path / "modules.json"
|
||||
moduledata.update(json.loads(modules_path.read_text()))
|
||||
|
||||
if not module.get('name'):
|
||||
if not module.get("name"):
|
||||
raise ValueError(f"Module with ID {module['id']} is missing a 'name' field")
|
||||
|
||||
name = module['name'].lower().split('_')
|
||||
new = {'id': module['id'], 'symbol': module['name']}
|
||||
raw_name = module["name"]
|
||||
name = raw_name.lower().split("_")
|
||||
new: dict[str, Any] = {"id": module["id"], "symbol": raw_name}
|
||||
|
||||
# Armour - e.g. Federation_Dropship_Armour_Grade2
|
||||
if name[-2] == 'armour':
|
||||
if len(name) >= 3 and name[-2] == "armour":
|
||||
# Armour is ship-specific, and ship names can have underscores
|
||||
ship_name, armour, armour_grade = module["name"].lower().rsplit("_", 2)[0:3]
|
||||
ship_name, armour, armour_grade = raw_name.lower().rsplit("_", 2)[0:3]
|
||||
if ship_name not in ship_map:
|
||||
raise ValueError(f"Unknown ship: {ship_name}")
|
||||
new['category'] = 'standard'
|
||||
new["category"] = "standard"
|
||||
new["name"] = armour_map[armour_grade]
|
||||
new["ship"] = ship_map[ship_name]
|
||||
new['class'] = '1'
|
||||
new['rating'] = 'I'
|
||||
new["class"] = "1"
|
||||
new["rating"] = "I"
|
||||
|
||||
# Skip uninteresting stuff - some no longer present in ED 3.1 cAPI data
|
||||
elif (name[0] in (
|
||||
'bobble',
|
||||
'decal',
|
||||
'nameplate',
|
||||
'paintjob',
|
||||
'enginecustomisation',
|
||||
'voicepack',
|
||||
'weaponcustomisation'
|
||||
)
|
||||
or name[1].startswith('shipkit')):
|
||||
elif name[0] in (
|
||||
"bobble",
|
||||
"decal",
|
||||
"nameplate",
|
||||
"paintjob",
|
||||
"enginecustomisation",
|
||||
"voicepack",
|
||||
"weaponcustomisation",
|
||||
) or name[1].startswith("shipkit"):
|
||||
return None
|
||||
|
||||
# Shouldn't be listing player-specific paid stuff or broker/powerplay-specific modules in outfitting,
|
||||
# other than Horizons
|
||||
elif not entitled and module.get('sku') and module['sku'] != 'ELITE_HORIZONS_V_PLANETARY_LANDINGS':
|
||||
elif (
|
||||
not entitled
|
||||
and module.get("sku")
|
||||
and module["sku"] != "ELITE_HORIZONS_V_PLANETARY_LANDINGS"
|
||||
):
|
||||
return None
|
||||
|
||||
# Don't report Planetary Approach Suite in outfitting
|
||||
elif not entitled and name[1] == 'planetapproachsuite':
|
||||
elif not entitled and name[1] == "planetapproachsuite":
|
||||
return None
|
||||
|
||||
# V2 Shutdown Field Neutralizer - Hpt_AntiUnknownShutdown_Tiny_V2
|
||||
elif name[0] == 'hpt' and name[1] in countermeasure_map and len(name) == 4 and name[3] == 'v2':
|
||||
new['category'] = 'utility'
|
||||
new['name'], new['rating'] = countermeasure_map[name[1]]
|
||||
new['class'] = weaponclass_map[name[-2]]
|
||||
|
||||
# Countermeasures - e.g. Hpt_PlasmaPointDefence_Turret_Tiny
|
||||
elif name[0] == 'hpt' and name[1] in countermeasure_map:
|
||||
new['category'] = 'utility'
|
||||
new['name'], new['rating'] = countermeasure_map[name[1]]
|
||||
new['class'] = weaponclass_map[name[-1]]
|
||||
|
||||
# Utility - e.g. Hpt_CargoScanner_Size0_Class1
|
||||
elif name[0] == 'hpt' and name[1] in utility_map:
|
||||
new['category'] = 'utility'
|
||||
new['name'] = utility_map[name[1]]
|
||||
if not name[2].startswith('size') or not name[3].startswith('class'):
|
||||
raise ValueError(f'{module["id"]}: Unknown class/rating "{name[2]}/{name[3]}"')
|
||||
|
||||
new['class'] = str(name[2][4:])
|
||||
new['rating'] = rating_map[name[3][5:]]
|
||||
|
||||
# Hardpoints - e.g. Hpt_Slugshot_Fixed_Medium
|
||||
elif name[0] == 'hpt':
|
||||
# Hack 'Guardian' and 'Mining' prefixes
|
||||
if len(name) > 3 and name[3] in weaponmount_map:
|
||||
prefix = name.pop(1)
|
||||
name[1] = f'{prefix}_{name[1]}'
|
||||
|
||||
if name[1] not in weapon_map:
|
||||
raise ValueError(f'{module["id"]}: Unknown weapon "{name[0]}"')
|
||||
|
||||
if name[2] not in weaponmount_map:
|
||||
raise ValueError(f'{module["id"]}: Unknown weapon mount "{name[2]}"')
|
||||
|
||||
if name[3] not in weaponclass_map:
|
||||
raise ValueError(f'{module["id"]}: Unknown weapon class "{name[3]}"')
|
||||
|
||||
new['category'] = 'hardpoint'
|
||||
if len(name) > 4:
|
||||
if name[4] in weaponoldvariant_map: # Old variants e.g. Hpt_PulseLaserBurst_Turret_Large_OC
|
||||
new['name'] = weapon_map[name[1]] + ' ' + weaponoldvariant_map[name[4]]
|
||||
new['rating'] = '?'
|
||||
|
||||
elif '_'.join(name[:4]) not in weaponrating_map:
|
||||
raise ValueError(f'{module["id"]}: Unknown weapon rating "{module["name"]}"')
|
||||
|
||||
else:
|
||||
# PP faction-specific weapons e.g. Hpt_Slugshot_Fixed_Large_Range
|
||||
new['name'] = weapon_map[(name[1], name[4])]
|
||||
new['rating'] = weaponrating_map['_'.join(name[:4])] # assumes same rating as base weapon
|
||||
|
||||
elif module['name'].lower() not in weaponrating_map:
|
||||
raise ValueError(f'{module["id"]}: Unknown weapon rating "{module["name"]}"')
|
||||
|
||||
else:
|
||||
new['name'] = weapon_map[name[1]]
|
||||
new['rating'] = weaponrating_map[module['name'].lower()] # no obvious rule - needs lookup table
|
||||
|
||||
new['mount'] = weaponmount_map[name[2]]
|
||||
if name[1] in missiletype_map:
|
||||
# e.g. Hpt_DumbfireMissileRack_Fixed_Small
|
||||
new['guidance'] = missiletype_map[name[1]]
|
||||
|
||||
new['class'] = weaponclass_map[name[3]]
|
||||
|
||||
elif name[0] != 'int':
|
||||
raise ValueError(f'{module["id"]}: Unknown prefix "{name[0]}"')
|
||||
|
||||
# Miscellaneous Class 1
|
||||
# e.g. Int_PlanetApproachSuite, Int_StellarBodyDiscoveryScanner_Advanced, Int_DockingComputer_Standard
|
||||
elif name[1] in misc_internal_map:
|
||||
new['category'] = 'internal'
|
||||
new['name'], new['rating'] = misc_internal_map[name[1]]
|
||||
new['class'] = '1'
|
||||
|
||||
elif len(name) > 2 and (name[1], name[2]) in misc_internal_map:
|
||||
# Reported category is not necessarily helpful. e.g. "Int_DockingComputer_Standard" has category "utility"
|
||||
new['category'] = 'internal'
|
||||
new['name'], new['rating'] = misc_internal_map[(name[1], name[2])]
|
||||
new['class'] = '1'
|
||||
|
||||
else:
|
||||
# Standard & Internal
|
||||
if name[1] == 'dronecontrol': # e.g. Int_DroneControl_Collection_Size1_Class1
|
||||
name.pop(0)
|
||||
# Match pattern routing for utility, countermeasures, and hardpoints
|
||||
match name:
|
||||
# V2 Shutdown Field Neutralizer - Hpt_AntiUnknownShutdown_Tiny_V2
|
||||
case ["hpt", cm_type, _, "v2"] if cm_type in countermeasure_map:
|
||||
new["category"] = "utility"
|
||||
new["name"], new["rating"] = countermeasure_map[cm_type]
|
||||
new["class"] = weaponclass_map[name[-2]]
|
||||
|
||||
elif name[1] == 'multidronecontrol': # e.g. Int_MultiDroneControl_Rescue_Size3_Class3
|
||||
name.pop(0)
|
||||
# Countermeasures - e.g. Hpt_PlasmaPointDefence_Turret_Tiny
|
||||
case ["hpt", cm_type, *_, size] if cm_type in countermeasure_map:
|
||||
new["category"] = "utility"
|
||||
new["name"], new["rating"] = countermeasure_map[cm_type]
|
||||
new["class"] = weaponclass_map[size]
|
||||
|
||||
elif name[-1] == 'free': # Starter Sidewinder or Freagle modules - just treat them like vanilla modules
|
||||
name.pop()
|
||||
# Utility - e.g. Hpt_CargoScanner_Size0_Class1
|
||||
case ["hpt", ut_type, size, rating_val] if ut_type in utility_map:
|
||||
if not size.startswith("size") or not rating_val.startswith("class"):
|
||||
raise ValueError(
|
||||
f'{module["id"]}: Unknown class/rating "{size}/{rating_val}"'
|
||||
)
|
||||
new["category"] = "utility"
|
||||
new["name"] = utility_map[ut_type]
|
||||
new["class"] = str(size[4:])
|
||||
new["rating"] = rating_map[rating_val[5:]]
|
||||
|
||||
if name[1] in standard_map: # e.g. Int_Engine_Size2_Class1, Int_ShieldGenerator_Size8_Class5_Strong
|
||||
new['category'] = 'standard'
|
||||
if name[2] == 'overcharge':
|
||||
new['name'] = standard_map[(name[1], name[2])]
|
||||
else:
|
||||
new['name'] = standard_map[len(name) > 4 and (name[1], name[4]) or name[1]]
|
||||
# Hardpoints - e.g. Hpt_Slugshot_Fixed_Medium
|
||||
case ["hpt", *weapons_info]:
|
||||
# Hack 'Guardian' and 'Mining' prefixes
|
||||
if len(weapons_info) > 2 and weapons_info[2] in weaponmount_map:
|
||||
weapons_info = [f"{weapons_info[0]}_{weapons_info[1]}"] + weapons_info[2:]
|
||||
# Re-sync local 'name' slice representation for downline logic dependency compatibility
|
||||
name = ["hpt", weapons_info[0]] + weapons_info[1:]
|
||||
|
||||
elif name[1] in internal_map: # e.g. Int_CargoRack_Size8_Class1
|
||||
new['category'] = 'internal'
|
||||
if name[1] == 'passengercabin':
|
||||
new['name'] = cabin_map[name[3][5:]]
|
||||
w_name, w_mount, w_class, *extra = weapons_info
|
||||
|
||||
else:
|
||||
new['name'] = internal_map[len(name) > 4 and (name[1], name[4]) or name[1]]
|
||||
if w_name not in weapon_map:
|
||||
raise ValueError(f'{module["id"]}: Unknown weapon "hpt"')
|
||||
|
||||
else:
|
||||
raise ValueError(f'{module["id"]}: Unknown module "{name[1]}"')
|
||||
if w_mount not in weaponmount_map:
|
||||
raise ValueError(
|
||||
f'{module["id"]}: Unknown weapon mount "{w_mount}"'
|
||||
)
|
||||
|
||||
if len(name) < 4 and name[1] == 'unkvesselresearch': # Hack! No size or class.
|
||||
(new['class'], new['rating']) = ('1', 'E')
|
||||
if w_class not in weaponclass_map:
|
||||
raise ValueError(
|
||||
f'{module["id"]}: Unknown weapon class "{w_class}"'
|
||||
)
|
||||
|
||||
elif len(name) < 4 and name[1] == 'resourcesiphon': # Hack! 128066402 has no size or class.
|
||||
(new['class'], new['rating']) = ('1', 'I')
|
||||
new["category"] = "hardpoint"
|
||||
|
||||
elif len(name) < 4 and name[1] in ('guardianpowerdistributor', 'guardianpowerplant'): # Hack! No class.
|
||||
(new['class'], new['rating']) = (str(name[2][4:]), 'A')
|
||||
if extra:
|
||||
variant = extra[0]
|
||||
# Old variants e.g. Hpt_PulseLaserBurst_Turret_Large_OC
|
||||
if variant in weaponoldvariant_map:
|
||||
new["name"] = (weapon_map[w_name] + " " + weaponoldvariant_map[variant])
|
||||
new["rating"] = "?"
|
||||
|
||||
elif len(name) < 4 and name[1] == 'guardianfsdbooster': # Hack! No class.
|
||||
(new['class'], new['rating']) = (str(name[2][4:]), 'H')
|
||||
elif "_".join(name[:4]) not in weaponrating_map:
|
||||
raise ValueError(f'{module["id"]}: Unknown weapon rating "{raw_name}"')
|
||||
|
||||
elif len(name) > 4 and name[1] == 'hyperdrive': # e.g. Int_Hyperdrive_Overcharge_Size6_Class3
|
||||
(new['class'], new['rating']) = (str(name[3][-1:]), rating_map[name[4][-1:]])
|
||||
else:
|
||||
# PP faction-specific weapons e.g. Hpt_Slugshot_Fixed_Large_Range
|
||||
new["name"] = weapon_map[(w_name, variant)]
|
||||
# assumes same rating as base weapon
|
||||
new["rating"] = weaponrating_map["_".join(name[:4])]
|
||||
|
||||
else:
|
||||
if len(name) < 3:
|
||||
raise ValueError(f'{name}: length < 3]')
|
||||
elif raw_name.lower() not in weaponrating_map:
|
||||
raise ValueError(f'{module["id"]}: Unknown weapon rating "{raw_name}"')
|
||||
|
||||
if not name[2].startswith('size') or not name[3].startswith('class'):
|
||||
raise ValueError(f'{module["id"]}: Unknown class/rating "{name[2]}/{name[3]}"')
|
||||
else:
|
||||
new["name"] = weapon_map[w_name]
|
||||
new["rating"] = weaponrating_map[raw_name.lower()]
|
||||
# no obvious rule - needs lookup table
|
||||
|
||||
new['class'] = str(name[2][4:])
|
||||
new['rating'] = (name[1] == 'buggybay' and planet_rating_map or
|
||||
name[1] == 'fighterbay' and fighter_rating_map or
|
||||
name[1] == 'corrosionproofcargorack' and corrosion_rating_map or
|
||||
rating_map)[name[3][5:]]
|
||||
new["mount"] = weaponmount_map[w_mount]
|
||||
if w_name in missiletype_map:
|
||||
# e.g. Hpt_DumbfireMissileRack_Fixed_Small
|
||||
new["guidance"] = missiletype_map[w_name]
|
||||
|
||||
new["class"] = weaponclass_map[w_class]
|
||||
|
||||
case [prefix, *_] if prefix != "int":
|
||||
raise ValueError(f'{module["id"]}: Unknown prefix "{prefix}"')
|
||||
|
||||
# Fallback handling for Standard & Internal sub-branches
|
||||
case _:
|
||||
# Miscellaneous Class 1
|
||||
# e.g. Int_PlanetApproachSuite, Int_StellarBodyDiscoveryScanner_Advanced, Int_DockingComputer_Standard
|
||||
if name[1] in misc_internal_map:
|
||||
new["category"] = "internal"
|
||||
new["name"], new["rating"] = misc_internal_map[name[1]]
|
||||
new["class"] = "1"
|
||||
|
||||
elif len(name) > 2 and (name[1], name[2]) in misc_internal_map:
|
||||
# Reported category is not necessarily helpful.
|
||||
# e.g. "Int_DockingComputer_Standard" has category "utility"
|
||||
new["category"] = "internal"
|
||||
new["name"], new["rating"] = misc_internal_map[(name[1], name[2])]
|
||||
new["class"] = "1"
|
||||
|
||||
else:
|
||||
# Standard & Internal
|
||||
if (
|
||||
name[1] == "dronecontrol"
|
||||
): # e.g. Int_DroneControl_Collection_Size1_Class1
|
||||
name.pop(0)
|
||||
|
||||
elif (
|
||||
name[1] == "multidronecontrol"
|
||||
): # e.g. Int_MultiDroneControl_Rescue_Size3_Class3
|
||||
name.pop(0)
|
||||
|
||||
elif (
|
||||
name[-1] == "free"
|
||||
): # Starter Sidewinder or Freagle modules - just treat them like vanilla modules
|
||||
name.pop()
|
||||
|
||||
if (
|
||||
name[1] in standard_map
|
||||
): # e.g. Int_Engine_Size2_Class1, Int_ShieldGenerator_Size8_Class5_Strong
|
||||
new["category"] = "standard"
|
||||
if name[2] == "overcharge":
|
||||
new["name"] = standard_map[(name[1], name[2])]
|
||||
else:
|
||||
new["name"] = standard_map[
|
||||
len(name) > 4 and (name[1], name[4]) or name[1]
|
||||
]
|
||||
|
||||
elif name[1] in internal_map: # e.g. Int_CargoRack_Size8_Class1
|
||||
new["category"] = "internal"
|
||||
if name[1] == "passengercabin":
|
||||
new["name"] = cabin_map[name[3][5:]]
|
||||
|
||||
else:
|
||||
new["name"] = internal_map[
|
||||
len(name) > 4 and (name[1], name[4]) or name[1]
|
||||
]
|
||||
|
||||
if (
|
||||
len(name) > 4 and name[1] == "hyperdrive"
|
||||
): # e.g. Int_Hyperdrive_Overcharge_Size6_Class3
|
||||
new["class"], new["rating"] = (
|
||||
str(name[3][-1:]),
|
||||
rating_map[name[4][-1:]],
|
||||
)
|
||||
else:
|
||||
raise ValueError(f'{module["id"]}: Unknown module "{name[1]}"')
|
||||
|
||||
if (
|
||||
len(name) < 4 and name[1] == "unkvesselresearch"
|
||||
): # Hack! No size or class.
|
||||
new["class"], new["rating"] = ("1", "E")
|
||||
|
||||
elif (
|
||||
len(name) < 4 and name[1] == "resourcesiphon"
|
||||
): # Hack! 128066402 has no size or class.
|
||||
new["class"], new["rating"] = ("1", "I")
|
||||
|
||||
elif len(name) < 4 and name[1] in (
|
||||
"guardianpowerdistributor",
|
||||
"guardianpowerplant",
|
||||
): # Hack! No class.
|
||||
new["class"], new["rating"] = (str(name[2][4:]), "A")
|
||||
|
||||
elif (
|
||||
len(name) < 4 and name[1] == "guardianfsdbooster"
|
||||
): # Hack! No class.
|
||||
new["class"], new["rating"] = (str(name[2][4:]), "H")
|
||||
|
||||
elif (
|
||||
len(name) > 4 and name[1] == "hyperdrive"
|
||||
): # e.g. Int_Hyperdrive_Overcharge_Size6_Class3
|
||||
new["class"], new["rating"] = (
|
||||
str(name[3][-1:]),
|
||||
rating_map[name[4][-1:]],
|
||||
)
|
||||
|
||||
else:
|
||||
if len(name) < 3:
|
||||
raise ValueError(f"{name}: length < 3]")
|
||||
|
||||
if not name[2].startswith("size") or not name[3].startswith(
|
||||
"class"
|
||||
):
|
||||
raise ValueError(
|
||||
f'{module["id"]}: Unknown class/rating "{name[2]}/{name[3]}"'
|
||||
)
|
||||
|
||||
new["class"] = str(name[2][4:])
|
||||
new["rating"] = (
|
||||
name[1] == "buggybay"
|
||||
and planet_rating_map
|
||||
or name[1] == "fighterbay"
|
||||
and fighter_rating_map
|
||||
or name[1] == "corrosionproofcargorack"
|
||||
and corrosion_rating_map
|
||||
or rating_map
|
||||
)[name[3][5:]]
|
||||
|
||||
# Disposition of fitted modules
|
||||
if 'on' in module and 'priority' in module:
|
||||
new['enabled'], new['priority'] = module['on'], module['priority'] # priority is zero-based
|
||||
if "on" in module and "priority" in module:
|
||||
new["enabled"], new["priority"] = (
|
||||
module["on"],
|
||||
module["priority"],
|
||||
) # priority is zero-based
|
||||
|
||||
# Entitlements
|
||||
if module.get('sku'):
|
||||
new['entitlement'] = module['sku']
|
||||
if module.get("sku"):
|
||||
new["entitlement"] = module["sku"]
|
||||
|
||||
# Extra module data
|
||||
if module['name'].endswith('_free'):
|
||||
key = module['name'][:-5].lower() # starter modules - treated like vanilla modules
|
||||
|
||||
if raw_name.endswith("_free"):
|
||||
key = raw_name[:-5].lower() # starter modules - treated like vanilla modules
|
||||
else:
|
||||
key = module['name'].lower()
|
||||
key = raw_name.lower()
|
||||
|
||||
if __debug__:
|
||||
m = moduledata.get(key, {})
|
||||
if not m:
|
||||
print(f'No data for module {key}')
|
||||
print(f"No data for module {key}")
|
||||
|
||||
elif new['name'] == 'Frame Shift Drive' or new['name'] == 'Frame Shift Drive (SCO)':
|
||||
required_keys = ['mass', 'optmass', 'maxfuel', 'fuelmul', 'fuelpower']
|
||||
elif new["name"] in ("Frame Shift Drive", "Frame Shift Drive (SCO)"):
|
||||
required_keys = ["mass", "optmass", "maxfuel", "fuelmul", "fuelpower"]
|
||||
missing_keys = [key for key in required_keys if key not in m]
|
||||
|
||||
if missing_keys:
|
||||
raise KeyError(f"Missing keys: {', '.join(missing_keys)} in {m}")
|
||||
else:
|
||||
if 'mass' not in m:
|
||||
if "mass" not in m:
|
||||
raise KeyError(f"Missing key 'mass' in {m}")
|
||||
|
||||
new.update(moduledata.get(module['name'].lower(), {}))
|
||||
new.update(moduledata.get(raw_name.lower(), {}))
|
||||
|
||||
# Check we've filled out mandatory fields
|
||||
mandatory_fields = ["id", "symbol", "category", "name", "class", "rating"]
|
||||
@@ -275,37 +347,55 @@ def lookup(module, ship_map, entitled=False) -> dict | None: # noqa: C901, CCR0
|
||||
if not new.get(field):
|
||||
raise ValueError(f'{module["id"]}: failed to set {field}')
|
||||
|
||||
if new['category'] == 'hardpoint' and not new.get('mount'):
|
||||
if new["category"] == "hardpoint" and not new.get("mount"):
|
||||
raise ValueError(f'{module["id"]}: failed to set mount')
|
||||
|
||||
return new
|
||||
|
||||
|
||||
def export(data, filename) -> None:
|
||||
def export(data: dict, filename: str) -> None:
|
||||
"""
|
||||
Export given data about module availability.
|
||||
|
||||
:param data: CAPI data to export.
|
||||
:param filename: Filename to export into.
|
||||
"""
|
||||
if "name" not in data["lastSystem"]:
|
||||
if "name" not in data.get("lastSystem", {}):
|
||||
raise KeyError("Missing 'name' key in 'lastSystem'")
|
||||
if "name" not in data["lastStarport"]:
|
||||
if "name" not in data.get("lastStarport", {}):
|
||||
raise KeyError("Missing 'name' key in 'lastStarport'")
|
||||
|
||||
header = 'System,Station,Category,Name,Mount,Guidance,Ship,Class,Rating,FDevID,Date\n'
|
||||
rowheader = f'{data["lastSystem"]["name"]},{data["lastStarport"]["name"]}'
|
||||
header = [
|
||||
"System", "Station", "Category", "Name", "Mount",
|
||||
"Guidance", "Ship", "Class", "Rating", "FDevID", "Date",
|
||||
]
|
||||
system_name = data["lastSystem"]["name"]
|
||||
starport_name = data["lastStarport"]["name"]
|
||||
timestamp = data["timestamp"]
|
||||
|
||||
with open(filename, 'w') as h:
|
||||
h.write(header)
|
||||
with open(filename, "w", newline="", encoding="utf-8") as h:
|
||||
writer = csv.writer(h)
|
||||
writer.writerow(header)
|
||||
for v in data["lastStarport"].get("modules", {}).values():
|
||||
try:
|
||||
m = lookup(v, ship_name_map)
|
||||
if m:
|
||||
h.write(f'{rowheader}, {m["category"]}, {m["name"]}, {m.get("mount", "")},'
|
||||
f'{m.get("guidance", "")}, {m.get("ship", "")}, {m["class"]}, {m["rating"]},'
|
||||
f'{m["id"]}, {data["timestamp"]}\n')
|
||||
writer.writerow(
|
||||
[
|
||||
system_name,
|
||||
starport_name,
|
||||
m["category"],
|
||||
m["name"],
|
||||
m.get("mount", ""),
|
||||
m.get("guidance", ""),
|
||||
m.get("ship", ""),
|
||||
m["class"],
|
||||
m["rating"],
|
||||
m["id"],
|
||||
timestamp,
|
||||
]
|
||||
)
|
||||
|
||||
except ValueError as e:
|
||||
# Log unrecognised modules
|
||||
logger.debug('Outfitting', exc_info=e)
|
||||
logger.debug("Outfitting", exc_info=e)
|
||||
@@ -50,7 +50,7 @@ last_error = LastError()
|
||||
class Plugin:
|
||||
"""An EDMC plugin."""
|
||||
|
||||
def __init__( # noqa: CCR001
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
loadfile: Path | None,
|
||||
@@ -103,18 +103,18 @@ class Plugin:
|
||||
logger.error(f'Plugin "{name}" could not be loaded, even via fallback path.')
|
||||
raise ImportError(f"Cannot load plugin {name} from {loadfile}")
|
||||
# Plugin startup logic
|
||||
if module:
|
||||
if getattr(module, 'plugin_start3', None):
|
||||
match module:
|
||||
case None:
|
||||
logger.error(f'Failed to load Plugin "{name}" from file "{loadfile}"')
|
||||
case _ if hasattr(module, 'plugin_start3'):
|
||||
newname = module.plugin_start3(Path(loadfile).resolve().parent)
|
||||
self.name = str(newname) if newname else self.name
|
||||
self.module = module
|
||||
elif getattr(module, 'plugin_start', None):
|
||||
case _ if hasattr(module, 'plugin_start'):
|
||||
logger.warning(f'plugin {name} needs migrating\n')
|
||||
PLUGINS_not_py3.append(self)
|
||||
else:
|
||||
case _:
|
||||
logger.error(f'plugin {name} has no plugin_start3() function')
|
||||
else:
|
||||
logger.error(f'Failed to load Plugin "{name}" from file "{loadfile}"')
|
||||
except Exception:
|
||||
logger.exception(f': Failed for Plugin "{name}"')
|
||||
raise
|
||||
|
||||
+2
-1
@@ -15,7 +15,8 @@ import pathlib
|
||||
from tkinter import ttk
|
||||
import tkinter.font as tkfont
|
||||
from PIL import Image, ImageTk
|
||||
from typing import Callable, cast
|
||||
from typing import cast
|
||||
from collections.abc import Callable
|
||||
import requests
|
||||
from io import BytesIO
|
||||
from datetime import datetime
|
||||
|
||||
+405
-380
File diff suppressed because it is too large.
Load diff
+80
-83
@@ -19,7 +19,6 @@ referenced in this file (or only in any other core plugin), and if so...
|
||||
IN AN END-USER INSTALLATION ON WINDOWS.
|
||||
"""
|
||||
|
||||
import json
|
||||
from typing import Any
|
||||
from tkinter import ttk
|
||||
import requests
|
||||
@@ -34,57 +33,50 @@ from plugins.common_coreutils import PADX, PADY, BUTTONX
|
||||
|
||||
logger = get_main_logger()
|
||||
|
||||
# Module-level globals (Replacing the "This" class architecture)
|
||||
APP_NAME = "EDAstro"
|
||||
EDASTRO_PUSH = "https://edastro.com/api/journal"
|
||||
|
||||
class This:
|
||||
"""Holds module globals."""
|
||||
LOG_ENABLED_VAR: tk.BooleanVar | None = None
|
||||
LOG_BUTTON: ttk.Checkbutton | None = None
|
||||
|
||||
app_name = "EDAstro"
|
||||
edastro_push = "https://edastro.com/api/journal"
|
||||
|
||||
event_filters = {
|
||||
"CarrierStats": [
|
||||
"timestamp",
|
||||
"event",
|
||||
"Callsign",
|
||||
"Name",
|
||||
"CarrierID",
|
||||
"AllowNotorious",
|
||||
"PendingDecommission",
|
||||
"DockingAccess",
|
||||
"FuelLevel",
|
||||
"Crew",
|
||||
],
|
||||
"CarrierJumpRequest": [
|
||||
"timestamp",
|
||||
"event",
|
||||
"SystemName",
|
||||
"SystemAddress",
|
||||
"CarrierID",
|
||||
"Body",
|
||||
"BodyID"
|
||||
"DepartureTime"
|
||||
],
|
||||
"ScanOrganic": [
|
||||
"timestamp",
|
||||
"ScanType",
|
||||
"Genus_Localised",
|
||||
"Species_Localised",
|
||||
"Genus",
|
||||
"event",
|
||||
"Body",
|
||||
"Species",
|
||||
"Variant",
|
||||
"SystemAddress",
|
||||
"Variant_Localised",
|
||||
]
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
self.log: tk.BooleanVar | None = None
|
||||
self.log_button: ttk.Checkbutton | None = None
|
||||
|
||||
|
||||
this = This()
|
||||
EVENT_FILTERS: dict[str, list[str]] = {
|
||||
"CarrierStats": [
|
||||
"timestamp",
|
||||
"event",
|
||||
"Callsign",
|
||||
"Name",
|
||||
"CarrierID",
|
||||
"AllowNotorious",
|
||||
"PendingDecommission",
|
||||
"DockingAccess",
|
||||
"FuelLevel",
|
||||
"Crew",
|
||||
],
|
||||
"CarrierJumpRequest": [
|
||||
"timestamp",
|
||||
"event",
|
||||
"SystemName",
|
||||
"SystemAddress",
|
||||
"CarrierID",
|
||||
"Body",
|
||||
"BodyID",
|
||||
"DepartureTime",
|
||||
],
|
||||
"ScanOrganic": [
|
||||
"timestamp",
|
||||
"ScanType",
|
||||
"Genus_Localised",
|
||||
"Species_Localised",
|
||||
"Genus",
|
||||
"event",
|
||||
"Body",
|
||||
"Species",
|
||||
"Variant",
|
||||
"SystemAddress",
|
||||
"Variant_Localised",
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
def set_config_first_run() -> None:
|
||||
@@ -116,10 +108,11 @@ def plugin_app(parent: tk.Tk) -> None:
|
||||
:param parent: tkinter parent frame.
|
||||
:return: See PLUGINS.md#display
|
||||
"""
|
||||
this.log = tk.BooleanVar(value=config.get_bool("edastro_send"))
|
||||
global LOG_ENABLED_VAR
|
||||
LOG_ENABLED_VAR = tk.BooleanVar(value=config.get_bool("edastro_send"))
|
||||
|
||||
|
||||
def plugin_prefs(parent, cmdr: str, is_beta: bool) -> nb.Frame:
|
||||
def plugin_prefs(parent: Any, cmdr: str, is_beta: bool) -> nb.Frame:
|
||||
"""
|
||||
Set up Preferences pane for this plugin.
|
||||
|
||||
@@ -128,6 +121,7 @@ def plugin_prefs(parent, cmdr: str, is_beta: bool) -> nb.Frame:
|
||||
:param is_beta: `bool` - True if this is a beta version of the Game.
|
||||
:return: The tkinter frame we created.
|
||||
"""
|
||||
global LOG_BUTTON
|
||||
edastroframe = nb.Frame(parent)
|
||||
edastroframe.columnconfigure(0, weight=1)
|
||||
|
||||
@@ -138,18 +132,18 @@ def plugin_prefs(parent, cmdr: str, is_beta: bool) -> nb.Frame:
|
||||
background=nb.Label().cget("background"),
|
||||
url="https://edastro.com",
|
||||
underline=True,
|
||||
).grid(
|
||||
row=cur_row, padx=PADX, pady=PADY, sticky=tk.W
|
||||
) # Don't translate
|
||||
).grid(row=cur_row, padx=PADX, pady=PADY, sticky=tk.W) # Don't translate
|
||||
|
||||
cur_row += 1
|
||||
this.log_button = nb.Checkbutton(
|
||||
LOG_BUTTON = nb.Checkbutton(
|
||||
edastroframe,
|
||||
# LANG: Settings>EDAstro - Label on checkbox for 'send data'
|
||||
text=tr.tl("Send data to EDAstro"),
|
||||
variable=this.log,
|
||||
variable=LOG_ENABLED_VAR,
|
||||
)
|
||||
if this.log_button:
|
||||
this.log_button.grid(
|
||||
|
||||
if LOG_BUTTON:
|
||||
LOG_BUTTON.grid(
|
||||
row=cur_row, columnspan=2, padx=BUTTONX, pady=PADY, sticky=tk.W
|
||||
)
|
||||
cur_row += 1
|
||||
@@ -164,55 +158,57 @@ def prefs_changed(cmdr: str, is_beta: bool) -> None:
|
||||
:param cmdr: Name of Commander.
|
||||
:param is_beta: Whether game beta was detected.
|
||||
"""
|
||||
if this.log:
|
||||
config.set("edastro_send", this.log.get())
|
||||
if LOG_ENABLED_VAR:
|
||||
config.set("edastro_send", LOG_ENABLED_VAR.get())
|
||||
|
||||
|
||||
def filter_event_data(entry) -> dict[str, Any]:
|
||||
"""Format Journal Data for EDAstro."""
|
||||
if entry["event"] in this.event_filters:
|
||||
return {
|
||||
key: entry[key]
|
||||
for key in this.event_filters[entry["event"]]
|
||||
if key in entry
|
||||
}
|
||||
def filter_event_data(entry: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Format Journal Data for EDAstro using specific element structures."""
|
||||
event_name = entry.get("event")
|
||||
if event_name in EVENT_FILTERS:
|
||||
allowed_keys = EVENT_FILTERS[event_name]
|
||||
return {key: entry[key] for key in allowed_keys if key in entry}
|
||||
return entry
|
||||
|
||||
|
||||
def edastro_update(system, entry, state):
|
||||
def edastro_update(system: str, entry: dict[str, Any], state: dict[str, Any]) -> None:
|
||||
"""Send a processed event to EDAstro."""
|
||||
event_name = str(entry["event"])
|
||||
event_name = str(entry.get("event", "UnknownEvent"))
|
||||
filtered_entry = filter_event_data(entry)
|
||||
|
||||
app_header = {
|
||||
"appName": this.app_name,
|
||||
"appName": APP_NAME,
|
||||
"odyssey": state.get("Odyssey"),
|
||||
"system": system,
|
||||
}
|
||||
event_object = [app_header, filtered_entry]
|
||||
event_data = json.dumps(event_object)
|
||||
|
||||
try:
|
||||
json_header = {"Content-Type": "application/json"}
|
||||
# Use requests json conversion instead of manual
|
||||
response = requests.post(
|
||||
url=this.edastro_push, headers=json_header, data=event_data, timeout=20
|
||||
url=EDASTRO_PUSH, json=event_object, timeout=20
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
edastro = json.loads(response.text)
|
||||
if str(edastro["status"]) == "200" or str(edastro["status"]) == "401":
|
||||
edastro = response.json()
|
||||
status = edastro.get("status")
|
||||
|
||||
if status in (200, "200", 401, "401"):
|
||||
# 200 = at least one event accepted, 401 = none were accepted, but no errors either
|
||||
logger.info(f"EDAstro: Data sent! ({event_name})")
|
||||
else:
|
||||
logger.debug(
|
||||
f"Error Response:\nRequest: {this.edastro_push}\n "
|
||||
f'Response ({edastro["status"]}): \n{edastro["message"]}'
|
||||
f"Error Response:\nRequest: {EDASTRO_PUSH}\n "
|
||||
f'Response ({status}): \n{edastro.get("message")}'
|
||||
)
|
||||
else:
|
||||
logger.debug(
|
||||
f"Unexpected Response:\nRequest: {this.edastro_push}\n "
|
||||
f"Unexpected Response:\nRequest: {EDASTRO_PUSH}\n "
|
||||
f"Response ({response.status_code}):\n{response.text}"
|
||||
)
|
||||
except Exception as ex:
|
||||
logger.warning(
|
||||
f"Failed to submit EDAstro data:\nRequest: {this.edastro_push}",
|
||||
f"Failed to submit EDAstro data:\nRequest: {EDASTRO_PUSH}",
|
||||
exc_info=ex,
|
||||
)
|
||||
|
||||
@@ -237,8 +233,9 @@ def journal_entry(
|
||||
:return: None if no error, else an error string.
|
||||
"""
|
||||
if (
|
||||
this.log is not None and this.log.get()
|
||||
and entry["event"] in ["CarrierStats", "CarrierJumpRequest", "ScanOrganic"]
|
||||
LOG_ENABLED_VAR is not None
|
||||
and LOG_ENABLED_VAR.get()
|
||||
and entry.get("event") in EVENT_FILTERS
|
||||
):
|
||||
edastro_update(system, entry, state)
|
||||
|
||||
|
||||
+40
-44
@@ -687,22 +687,26 @@ class EDDN:
|
||||
:param data: The raw CAPI data.
|
||||
:return: Sanity-checked data.
|
||||
"""
|
||||
modules: dict[str, Any] = data['lastStarport'].get('modules')
|
||||
if modules is None or not isinstance(modules, dict):
|
||||
if modules is None:
|
||||
modules: Any = data['lastStarport'].get('modules')
|
||||
match modules:
|
||||
case dict():
|
||||
pass # It's already a valid dict, do nothing
|
||||
|
||||
case None:
|
||||
logger.debug('modules was None. FC or Damaged Station?')
|
||||
modules = {}
|
||||
|
||||
elif isinstance(modules, list):
|
||||
if len(modules) == 0:
|
||||
logger.debug('modules is empty list. FC or Damaged Station?')
|
||||
case []:
|
||||
logger.debug('modules is empty list. FC or Damaged Station?')
|
||||
modules = {}
|
||||
|
||||
else:
|
||||
logger.error(f'modules is non-empty list: {modules!r}')
|
||||
case list() as modules_list: # Use 'as' to capture the variable safely
|
||||
logger.error(f'modules is non-empty list: {modules_list!r}')
|
||||
modules = {}
|
||||
|
||||
else:
|
||||
case _:
|
||||
logger.error(f'modules was not None, a list, or a dict! type = {type(modules)}')
|
||||
# Set a safe value
|
||||
modules = {}
|
||||
modules = {}
|
||||
|
||||
ships: dict[str, Any] = data['lastStarport'].get('ships')
|
||||
if ships is None or not isinstance(ships, dict):
|
||||
@@ -2221,18 +2225,15 @@ def filter_localised(d: Mapping[str, Any]) -> dict[str, Any]:
|
||||
"""
|
||||
filtered: dict[str, Any] = {}
|
||||
for k, v in d.items():
|
||||
if k.endswith('_Localised'):
|
||||
pass
|
||||
|
||||
elif hasattr(v, 'items'): # dict -> recurse
|
||||
filtered[k] = filter_localised(v)
|
||||
|
||||
elif isinstance(v, list): # list of dicts -> recurse
|
||||
filtered[k] = [filter_localised(x) if hasattr(x, 'items') else x for x in v]
|
||||
|
||||
else:
|
||||
filtered[k] = v
|
||||
|
||||
match k, v:
|
||||
case key, _ if key.endswith('_Localised'):
|
||||
pass
|
||||
case _, dict() as dict_val: # noqa: F841 Keep 'type() as x'
|
||||
filtered[k] = filter_localised(v)
|
||||
case _, list() as list_val: # noqa: F841
|
||||
filtered[k] = [filter_localised(x) if hasattr(x, 'items') else x for x in v]
|
||||
case _:
|
||||
filtered[k] = v
|
||||
return filtered
|
||||
|
||||
|
||||
@@ -2245,18 +2246,15 @@ def capi_filter_localised(d: Mapping[str, Any]) -> dict[str, Any]:
|
||||
"""
|
||||
filtered: dict[str, Any] = {}
|
||||
for k, v in d.items():
|
||||
if EDDN.CAPI_LOCALISATION_RE.search(k):
|
||||
pass
|
||||
|
||||
elif hasattr(v, 'items'): # dict -> recurse
|
||||
filtered[k] = capi_filter_localised(v)
|
||||
|
||||
elif isinstance(v, list): # list of dicts -> recurse
|
||||
filtered[k] = [capi_filter_localised(x) if hasattr(x, 'items') else x for x in v]
|
||||
|
||||
else:
|
||||
filtered[k] = v
|
||||
|
||||
match k, v:
|
||||
case key, _ if EDDN.CAPI_LOCALISATION_RE.search(key):
|
||||
pass
|
||||
case _, dict() as dict_val:
|
||||
filtered[k] = capi_filter_localised(dict_val)
|
||||
case _, list() as list_val:
|
||||
filtered[k] = [capi_filter_localised(x) if isinstance(x, dict) else x for x in list_val]
|
||||
case _:
|
||||
filtered[k] = v
|
||||
return filtered
|
||||
|
||||
|
||||
@@ -2506,15 +2504,13 @@ def journal_entry( # noqa: C901, CCR001
|
||||
event_name_augment = entry_augment['event'].lower()
|
||||
entry_augment['odyssey'] = this.odyssey
|
||||
|
||||
if event_name_augment == 'market':
|
||||
this.eddn.export_journal_commodities(cmdr, is_beta, entry_augment)
|
||||
|
||||
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)
|
||||
|
||||
match event_name_augment:
|
||||
case 'market':
|
||||
this.eddn.export_journal_commodities(cmdr, is_beta, entry_augment)
|
||||
case 'outfitting':
|
||||
this.eddn.export_journal_outfitting(cmdr, is_beta, entry_augment)
|
||||
case '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)
|
||||
return tr.tl("Error: Can't connect to EDDN") # LANG: Error while trying to send data to EDDN
|
||||
|
||||
+2
-1
@@ -340,7 +340,8 @@ def prefs_changed(cmdr: str, is_beta: bool) -> None:
|
||||
if this.log.get() and changed:
|
||||
this.newuser = True # Send basic info at next Journal event
|
||||
new_add_event(
|
||||
'getCommanderProfile', time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()), {'searchName': cmdr}
|
||||
'getCommanderProfile', datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ'),
|
||||
{'searchName': cmdr}
|
||||
)
|
||||
|
||||
|
||||
|
||||
+57
-56
@@ -18,13 +18,14 @@ referenced in this file (or only in any other core plugin), and if so...
|
||||
`build.py` TO ENSURE THE FILES ARE ACTUALLY PRESENT
|
||||
IN AN END-USER INSTALLATION ON WINDOWS.
|
||||
"""
|
||||
# pylint: disable=import-error
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
import tkinter as tk
|
||||
from tkinter import ttk
|
||||
from typing import Any
|
||||
import requests
|
||||
from urllib.parse import quote
|
||||
from companion import CAPIData
|
||||
from config import appname, config
|
||||
from EDMCLogging import get_main_logger
|
||||
@@ -33,24 +34,25 @@ from plugins.common_coreutils import (station_link_common, this_format_common,
|
||||
|
||||
logger = get_main_logger()
|
||||
|
||||
# Module-Level Application State Constants & Variables
|
||||
IDENTIFIER = "Spansh"
|
||||
|
||||
class This:
|
||||
"""Holds module globals."""
|
||||
PARENT_ROOT: tk.Tk | None = None
|
||||
SHUTTING_DOWN: bool = False
|
||||
|
||||
def __init__(self):
|
||||
self.parent: tk.Tk
|
||||
self.shutting_down = False # Plugin is shutting down.
|
||||
self.system_link: ttk.Widget = None # type: ignore
|
||||
self.system_name: str | None = None
|
||||
self.system_address: str | None = None
|
||||
self.system_population: int | None = None
|
||||
self.station_link: ttk.Widget = None # type: ignore
|
||||
self.station_name = None
|
||||
self.station_marketid = None
|
||||
self.on_foot = False
|
||||
SYSTEM_LINK: ttk.Widget | None = None
|
||||
SYSTEM_NAME: str | None = None
|
||||
SYSTEM_ADDRESS: int | str | None = None
|
||||
SYSTEM_POPULATION: int | None = None
|
||||
|
||||
STATION_LINK: ttk.Widget | None = None
|
||||
STATION_NAME: str | None = None
|
||||
STATION_MARKETID: int | str | None = None
|
||||
STATION_TYPE: str | None = None
|
||||
ON_FOOT: bool = False
|
||||
|
||||
this = This()
|
||||
# Resolve module namespace reference once globally for common_coreutils compatibility
|
||||
CURRENT_MODULE = sys.modules[__name__]
|
||||
|
||||
|
||||
def plugin_start3(plugin_dir: str) -> str:
|
||||
@@ -60,7 +62,7 @@ def plugin_start3(plugin_dir: str) -> str:
|
||||
:param plugin_dir: Name of directory this was loaded from.
|
||||
:return: Identifier string for this plugin.
|
||||
"""
|
||||
return 'Spansh'
|
||||
return IDENTIFIER
|
||||
|
||||
|
||||
def plugin_app(parent: tk.Tk) -> None:
|
||||
@@ -68,16 +70,20 @@ def plugin_app(parent: tk.Tk) -> None:
|
||||
Construct this plugin's main UI, if any.
|
||||
|
||||
:param parent: The tk parent to place our widgets into.
|
||||
:return: See PLUGINS.md#display
|
||||
"""
|
||||
this.parent = parent
|
||||
this.system_link = parent.nametowidget(f".{appname.lower()}.system")
|
||||
this.station_link = parent.nametowidget(f".{appname.lower()}.station")
|
||||
global PARENT_ROOT, SYSTEM_LINK, STATION_LINK
|
||||
|
||||
PARENT_ROOT = parent
|
||||
base_path = f".{appname.lower()}"
|
||||
|
||||
SYSTEM_LINK = parent.nametowidget(f"{base_path}.system")
|
||||
STATION_LINK = parent.nametowidget(f"{base_path}.station")
|
||||
|
||||
|
||||
def plugin_stop() -> None:
|
||||
"""Plugin shutdown hook."""
|
||||
this.shutting_down = True
|
||||
global SHUTTING_DOWN
|
||||
SHUTTING_DOWN = True
|
||||
|
||||
|
||||
def journal_entry(
|
||||
@@ -94,22 +100,20 @@ def journal_entry(
|
||||
:param state: `monitor.state`
|
||||
:return: None if no error, else an error string.
|
||||
"""
|
||||
this_format_common(this, state)
|
||||
this_format_common(CURRENT_MODULE, state)
|
||||
|
||||
# Only actually change URLs if we are current provider.
|
||||
if config.get_str('system_provider') == 'spansh':
|
||||
this.system_link['text'] = this.system_name
|
||||
# Do *NOT* set 'url' here, as it's set to a function that will call
|
||||
# through correctly. We don't want a static string.
|
||||
this.system_link.update_idletasks()
|
||||
# Only actually change URLs if we are the designated active provider.
|
||||
if config.get_str("system_provider") == "spansh" and SYSTEM_LINK:
|
||||
SYSTEM_LINK["text"] = SYSTEM_NAME
|
||||
# Do *NOT* set 'url' here, as it's set to a function that will call through correctly.
|
||||
SYSTEM_LINK.update_idletasks()
|
||||
|
||||
if config.get_str('station_provider') == 'spansh':
|
||||
station_name_setter_common(this)
|
||||
# Do *NOT* set 'url' here, as it's set to a function that will call
|
||||
# through correctly. We don't want a static string.
|
||||
this.station_link.update_idletasks()
|
||||
if config.get_str("station_provider") == "spansh" and STATION_LINK:
|
||||
station_name_setter_common(CURRENT_MODULE)
|
||||
# Do *NOT* set 'url' here, as it's set to a function that will call through correctly.
|
||||
STATION_LINK.update_idletasks()
|
||||
|
||||
return ''
|
||||
return ""
|
||||
|
||||
|
||||
def cmdr_data(data: CAPIData, is_beta: bool) -> str | None:
|
||||
@@ -120,37 +124,34 @@ def cmdr_data(data: CAPIData, is_beta: bool) -> str | None:
|
||||
:param is_beta: Whether game beta was detected.
|
||||
:return: Optional error string.
|
||||
"""
|
||||
cmdr_data_initial_common(this, data)
|
||||
cmdr_data_initial_common(CURRENT_MODULE, data)
|
||||
|
||||
# Override standard URL functions
|
||||
if config.get_str('system_provider') == 'spansh':
|
||||
this.system_link['text'] = this.system_name
|
||||
# Do *NOT* set 'url' here, as it's set to a function that will call
|
||||
# through correctly. We don't want a static string.
|
||||
this.system_link.update_idletasks()
|
||||
if config.get_str('station_provider') == 'spansh':
|
||||
station_link_common(data, this)
|
||||
# Do *NOT* set 'url' here, as it's set to a function that will call
|
||||
# through correctly. We don't want a static string.
|
||||
this.station_link.update_idletasks()
|
||||
# Override standard URL labels if targeted
|
||||
if config.get_str("system_provider") == "spansh" and SYSTEM_LINK:
|
||||
SYSTEM_LINK["text"] = SYSTEM_NAME
|
||||
SYSTEM_LINK.update_idletasks()
|
||||
|
||||
return ''
|
||||
if config.get_str("station_provider") == "spansh" and STATION_LINK:
|
||||
station_link_common(data, CURRENT_MODULE)
|
||||
STATION_LINK.update_idletasks()
|
||||
|
||||
return ""
|
||||
|
||||
|
||||
def system_url(system_name: str) -> str:
|
||||
"""
|
||||
Construct an appropriate spansh URL for the provided system.
|
||||
|
||||
:param system_name:
|
||||
:param system_name: Name of the star system.
|
||||
:return: The URL, empty if no data was available to construct it.
|
||||
"""
|
||||
if system_name:
|
||||
return requests.utils.requote_uri(f'https://www.spansh.co.uk/search/{system_name}')
|
||||
return f"https://www.spansh.co.uk/search/{quote(system_name)}"
|
||||
|
||||
if this.system_address:
|
||||
return requests.utils.requote_uri(f'https://www.spansh.co.uk/system/{this.system_address}')
|
||||
if SYSTEM_ADDRESS:
|
||||
return f"https://www.spansh.co.uk/system/{quote(str(SYSTEM_ADDRESS))}"
|
||||
|
||||
return ''
|
||||
return ""
|
||||
|
||||
|
||||
def station_url(system_name: str, station_name: str) -> str:
|
||||
@@ -164,7 +165,7 @@ def station_url(system_name: str, station_name: str) -> str:
|
||||
if system_name:
|
||||
return system_url(system_name)
|
||||
|
||||
if this.station_marketid:
|
||||
return requests.utils.requote_uri(f'https://www.spansh.co.uk/station/{this.station_marketid}')
|
||||
if STATION_MARKETID:
|
||||
return f"https://www.spansh.co.uk/station/{quote(str(STATION_MARKETID))}"
|
||||
|
||||
return ''
|
||||
return ""
|
||||
@@ -306,55 +306,56 @@ def main(): # noqa: CCR001
|
||||
|
||||
output = []
|
||||
|
||||
if args.compare_lang:
|
||||
seen = set()
|
||||
template = parse_template(args.compare_lang)
|
||||
for file, calls in res.items():
|
||||
for c in calls:
|
||||
arg = get_arg(c)
|
||||
if arg in template:
|
||||
seen.add(arg)
|
||||
else:
|
||||
output.append(f"NEW! {file}:{c.lineno}: {arg!r}")
|
||||
match vars(args):
|
||||
case {"compare_lang": str() as compare_file}:
|
||||
seen = set()
|
||||
template = parse_template(compare_file)
|
||||
for file, calls in res.items():
|
||||
for c in calls:
|
||||
arg = get_arg(c)
|
||||
if arg in template:
|
||||
seen.add(arg)
|
||||
else:
|
||||
output.append(f"NEW! {file}:{c.lineno}: {arg!r}")
|
||||
for old in set(template) ^ seen:
|
||||
output.append(f"No longer used: {old!r}")
|
||||
|
||||
for old in set(template) ^ seen:
|
||||
output.append(f"No longer used: {old!r}")
|
||||
case {"json": True}:
|
||||
to_print_data = [
|
||||
{
|
||||
"path": str(path),
|
||||
"string": get_arg(c),
|
||||
"reconstructed": ast.unparse(c),
|
||||
"start_line": c.lineno,
|
||||
"start_offset": c.col_offset,
|
||||
"end_line": c.end_lineno,
|
||||
"end_offset": c.end_col_offset,
|
||||
"comment": getattr(c, "comment", None),
|
||||
}
|
||||
for path, calls in res.items()
|
||||
for c in calls
|
||||
]
|
||||
output.append(json.dumps(to_print_data, indent=2))
|
||||
|
||||
elif args.json:
|
||||
to_print_data = [
|
||||
{
|
||||
"path": str(path),
|
||||
"string": get_arg(c),
|
||||
"reconstructed": ast.unparse(c),
|
||||
"start_line": c.lineno,
|
||||
"start_offset": c.col_offset,
|
||||
"end_line": c.end_lineno,
|
||||
"end_offset": c.end_col_offset,
|
||||
"comment": getattr(c, "comment", None),
|
||||
}
|
||||
for path, calls in res.items()
|
||||
for c in calls
|
||||
]
|
||||
output.append(json.dumps(to_print_data, indent=2))
|
||||
case {"lang": str() as lang_dest}:
|
||||
lang_template = generate_lang_template(res)
|
||||
if lang_dest == "-":
|
||||
output.append(lang_template)
|
||||
else:
|
||||
with open(lang_dest, mode="w+", newline="\n", encoding="UTF-8") as langfile:
|
||||
langfile.writelines(lang_template)
|
||||
|
||||
elif args.lang:
|
||||
lang_template = generate_lang_template(res)
|
||||
if args.lang == "-":
|
||||
output.append(lang_template)
|
||||
else:
|
||||
with open(args.lang, mode="w+", newline="\n", encoding="UTF-8") as langfile:
|
||||
langfile.writelines(lang_template)
|
||||
|
||||
else:
|
||||
for path, calls in res.items():
|
||||
if not calls:
|
||||
continue
|
||||
output.append(str(path))
|
||||
for c in calls:
|
||||
output.append(
|
||||
f" {c.lineno:4d}({c.col_offset:3d}):{c.end_lineno:4d}({c.end_col_offset:3d})\t{ast.unparse(c)}"
|
||||
)
|
||||
output.append("")
|
||||
case _:
|
||||
for path, calls in res.items():
|
||||
if not calls:
|
||||
continue
|
||||
output.append(str(path))
|
||||
for c in calls:
|
||||
output.append(
|
||||
f" {c.lineno:4d}({c.col_offset:3d}):{c.end_lineno:4d}"
|
||||
f"({c.end_col_offset:3d})\t{ast.unparse(c)}"
|
||||
)
|
||||
output.append("")
|
||||
|
||||
# Print all collected output at the end
|
||||
if output:
|
||||
|
||||
@@ -8,9 +8,9 @@ from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import ast
|
||||
import datetime
|
||||
import pathlib
|
||||
import sys
|
||||
from datetime import date
|
||||
|
||||
|
||||
def get_func_name(thing: ast.AST) -> str:
|
||||
@@ -60,7 +60,7 @@ def generate_markdown(all_found_data: list[dict], project_root: pathlib.Path):
|
||||
|
||||
lines = [
|
||||
"# Available Traces",
|
||||
f"This file was last updated on {datetime.date.today()}." "",
|
||||
f"This file was last updated on {date.today()}." "",
|
||||
"This document lists all of the available `--trace-on` options to enable additional debug logging.",
|
||||
"",
|
||||
f"| {h_key:<{w_key}} | {h_msg:<{w_msg}} | {h_loc:<{w_loc}} |",
|
||||
|
||||
+10
-7
@@ -1,4 +1,6 @@
|
||||
"""Export list of ships as CSV."""
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
from itertools import chain
|
||||
import companion
|
||||
@@ -12,16 +14,16 @@ def export(data: companion.CAPIData, filename: str) -> None:
|
||||
:param data: The CAPI data.
|
||||
:param filename: Target CSV filename
|
||||
"""
|
||||
system_name = data['lastSystem'].get('name')
|
||||
if not system_name:
|
||||
if not (system_name := data.get('lastSystem', {}).get('name')):
|
||||
raise ValueError("Missing 'name' in 'lastSystem'")
|
||||
starport = data['lastStarport']
|
||||
station_name = starport.get('name')
|
||||
ships_info = starport.get('ships')
|
||||
|
||||
if not station_name:
|
||||
if not (starport := data.get('lastStarport')):
|
||||
raise ValueError("Missing 'lastStarport' in data")
|
||||
|
||||
if not (station_name := starport.get('name')):
|
||||
raise ValueError("Missing 'name' in 'lastStarport'")
|
||||
if not ships_info:
|
||||
|
||||
if not (ships_info := starport.get('ships')):
|
||||
raise ValueError("Missing 'ships' in 'lastStarport'")
|
||||
|
||||
shipyard_list = ships_info.get('shipyard_list', {}).values()
|
||||
@@ -31,6 +33,7 @@ def export(data: companion.CAPIData, filename: str) -> None:
|
||||
with open(filename, 'w', newline='', encoding='utf-8') as f:
|
||||
writer = csv.writer(f)
|
||||
writer.writerow(['System', 'Station', 'Ship', 'FDevID', 'Date'])
|
||||
|
||||
for ship in all_ships:
|
||||
name = ship_name_map.get(ship['name'].lower(), ship['name'])
|
||||
fdevid = ship['id']
|
||||
|
||||
@@ -32,7 +32,7 @@ RANK_LINES_END = 9
|
||||
POWERPLAY_LINES_START = 9
|
||||
|
||||
|
||||
def status(data: dict[str, Any]) -> list[list[str]]:
|
||||
def status(data: companion.CAPIData) -> list[list[str]]:
|
||||
"""
|
||||
Get the current status of the cmdr referred to by data.
|
||||
|
||||
@@ -198,7 +198,7 @@ def status(data: dict[str, Any]) -> list[list[str]]:
|
||||
return res
|
||||
|
||||
|
||||
def export_status(data: dict[str, Any], filename: AnyStr) -> None:
|
||||
def export_status(data: companion.CAPIData, filename: AnyStr) -> None:
|
||||
"""
|
||||
Export status data to a CSV file.
|
||||
|
||||
@@ -223,7 +223,7 @@ class ShipRet(NamedTuple):
|
||||
value: str
|
||||
|
||||
|
||||
def ships(companion_data: dict[str, Any]) -> list[ShipRet]:
|
||||
def ships(companion_data: companion.CAPIData) -> list[ShipRet]:
|
||||
"""
|
||||
Return a list of 5 tuples of ship information.
|
||||
|
||||
@@ -271,7 +271,7 @@ def ships(companion_data: dict[str, Any]) -> list[ShipRet]:
|
||||
]
|
||||
|
||||
|
||||
def export_ships(companion_data: dict[str, Any], filename: AnyStr) -> None:
|
||||
def export_ships(companion_data: companion.CAPIData, filename: AnyStr) -> None:
|
||||
"""
|
||||
Export the current ships to a CSV file.
|
||||
|
||||
@@ -342,7 +342,7 @@ class StatsDialog:
|
||||
class StatsResults(tk.Toplevel):
|
||||
"""Status window."""
|
||||
|
||||
def __init__(self, parent: tk.Tk, data: dict[str, Any]) -> None:
|
||||
def __init__(self, parent: tk.Tk, data: companion.CAPIData) -> None:
|
||||
tk.Toplevel.__init__(self, parent)
|
||||
|
||||
self.parent = parent
|
||||
|
||||
@@ -1,56 +1,90 @@
|
||||
"""Export data for Trade Dangerous."""
|
||||
|
||||
import pathlib
|
||||
from datetime import datetime
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import defaultdict
|
||||
from datetime import datetime
|
||||
from operator import itemgetter
|
||||
from pathlib import Path
|
||||
from platform import system
|
||||
from typing import Any
|
||||
|
||||
from companion import CAPIData
|
||||
from config import applongname, appversion, config
|
||||
|
||||
# These are specific to Trade Dangerous, so don't move to edmc_data.py
|
||||
demandbracketmap = {0: '?',
|
||||
1: 'L',
|
||||
2: 'M',
|
||||
3: 'H', }
|
||||
stockbracketmap = {0: '-',
|
||||
1: 'L',
|
||||
2: 'M',
|
||||
3: 'H', }
|
||||
demandbracketmap = {
|
||||
0: "?",
|
||||
1: "L",
|
||||
2: "M",
|
||||
3: "H",
|
||||
}
|
||||
stockbracketmap = {
|
||||
0: "-",
|
||||
1: "L",
|
||||
2: "M",
|
||||
3: "H",
|
||||
}
|
||||
|
||||
|
||||
def export(data: CAPIData) -> None:
|
||||
def export(data: CAPIData) -> None: # noqa: CCR001
|
||||
"""Export market data in TD format."""
|
||||
data_path = pathlib.Path(config.get_str('outdir'))
|
||||
data_path = Path(config.get_str("outdir"))
|
||||
|
||||
# parse timestamp once
|
||||
ts = datetime.fromisoformat(data['timestamp'].replace('Z', '+00:00'))
|
||||
ts_file = ts.strftime('%Y-%m-%dT%H.%M.%S')
|
||||
ts_data = ts.strftime('%Y-%m-%d %H:%M:%S')
|
||||
if not (raw_timestamp := data.get("timestamp")):
|
||||
raise ValueError("Missing 'timestamp' in data payload")
|
||||
|
||||
system_name = data['lastSystem']['name'].strip()
|
||||
starport_name = data['lastStarport']['name'].strip()
|
||||
cmdr_name = data['commander']['name'].strip()
|
||||
ts = datetime.fromisoformat(raw_timestamp.replace("Z", "+00:00"))
|
||||
ts_file = ts.strftime("%Y-%m-%dT%H.%M.%S")
|
||||
ts_data = ts.strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
if not (system_node := data.get("lastSystem")) or not (
|
||||
system_name := system_node.get("name", "").strip()
|
||||
):
|
||||
raise KeyError("Missing or empty 'name' key in 'lastSystem'")
|
||||
|
||||
if not (starport_node := data.get("lastStarport")) or not (
|
||||
starport_name := starport_node.get("name", "").strip()
|
||||
):
|
||||
raise KeyError("Missing or empty 'name' key in 'lastStarport'")
|
||||
|
||||
if not (cmdr_node := data.get("commander")) or not (
|
||||
cmdr_name := cmdr_node.get("name", "").strip()
|
||||
):
|
||||
raise KeyError("Missing or empty 'name' key in 'commander'")
|
||||
|
||||
data_filename = f"{system_name}.{starport_name}.{ts_file}.prices"
|
||||
|
||||
with open(data_path / data_filename, 'w', encoding='utf-8', newline='\n') as h:
|
||||
h.write('#! trade.py import -\n')
|
||||
h.write(f'# Created by {applongname} {appversion()} on {system()} for Cmdr {cmdr_name}.\n')
|
||||
h.write('#\n# <item name> <sellCR> <buyCR> <demand> <stock> <timestamp>\n\n')
|
||||
h.write(f'@ {system_name}/{starport_name}\n')
|
||||
with open(data_path / data_filename, "w", encoding="utf-8", newline="\n") as h:
|
||||
h.write("#! trade.py import -\n")
|
||||
h.write(
|
||||
f"# Created by {applongname} {appversion()} on {system()} for Cmdr {cmdr_name}.\n"
|
||||
)
|
||||
h.write(
|
||||
"#\n# <item name> <sellCR> <buyCR> <demand> <stock> <timestamp>\n\n"
|
||||
)
|
||||
h.write(f"@ {system_name}/{starport_name}\n")
|
||||
|
||||
# group by category
|
||||
by_category = defaultdict(list)
|
||||
for commodity in data['lastStarport']['commodities']:
|
||||
by_category[commodity['categoryname']].append(commodity)
|
||||
by_category: dict[str, list[dict[str, Any]]] = defaultdict(list)
|
||||
for commodity in starport_node.get("commodities", []):
|
||||
if category_name := commodity.get("categoryname"):
|
||||
by_category[category_name].append(commodity)
|
||||
|
||||
for category in sorted(by_category):
|
||||
h.write(f' + {category}\n')
|
||||
for commodity in sorted(by_category[category], key=itemgetter('name')):
|
||||
demand_val = str(int(commodity['demand'])) if commodity['demandBracket'] else ''
|
||||
stock_val = str(int(commodity['stock'])) if commodity['stockBracket'] else ''
|
||||
h.write(f" + {category}\n")
|
||||
for commodity in sorted(by_category[category], key=itemgetter("name")):
|
||||
demand_val = (
|
||||
str(int(commodity["demand"]))
|
||||
if commodity.get("demandBracket")
|
||||
else ""
|
||||
)
|
||||
stock_val = (
|
||||
str(int(commodity["stock"]))
|
||||
if commodity.get("stockBracket")
|
||||
else ""
|
||||
)
|
||||
|
||||
h.write(
|
||||
f" {commodity['name']:<23}"
|
||||
|
||||
@@ -102,7 +102,7 @@ class TestOutfitting:
|
||||
|
||||
content = export_file.read_text()
|
||||
assert "System,Station,Category,Name" in content
|
||||
assert "Sol,Daedalus, hardpoint, Laser" in content
|
||||
assert "Sol,Daedalus,hardpoint,Laser" in content
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
|
||||
@@ -8,13 +8,13 @@ See LICENSE file.
|
||||
Because of various ttk limitations this app is an unholy mix of Tk and ttk widgets.
|
||||
So can't use ttk's theme support. So have to change colors manually.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import tkinter as tk
|
||||
from tkinter import font as tk_font
|
||||
from tkinter import ttk
|
||||
from typing import Any
|
||||
from collections.abc import Callable
|
||||
from l10n import translations as tr
|
||||
from config import config
|
||||
@@ -26,14 +26,12 @@ logger = get_main_logger()
|
||||
if __debug__:
|
||||
from traceback import print_exc
|
||||
|
||||
if sys.platform == "linux":
|
||||
from ctypes import POINTER, Structure, byref, c_char_p, c_int, c_long, c_uint, c_ulong, c_void_p, cdll
|
||||
|
||||
|
||||
if sys.platform == 'win32':
|
||||
# Can't use Case matching here due to bug in flake8-naming
|
||||
if sys.platform == "win32":
|
||||
import ctypes
|
||||
from ctypes.wintypes import DWORD, LPCVOID, LPCWSTR
|
||||
import win32gui
|
||||
import win32con
|
||||
AddFontResourceEx = ctypes.windll.gdi32.AddFontResourceExW
|
||||
AddFontResourceEx.argtypes = [LPCWSTR, DWORD, LPCVOID]
|
||||
AddFontResourceEx.restype = ctypes.c_int
|
||||
@@ -42,19 +40,20 @@ if sys.platform == 'win32':
|
||||
font_path = str(config.respath_path / 'EUROCAPS.TTF')
|
||||
if not AddFontResourceEx(font_path, FR_PRIVATE, None):
|
||||
raise RuntimeError(f"Failed to load font {font_path}")
|
||||
dpy = None
|
||||
|
||||
elif sys.platform == 'linux':
|
||||
# pyright: reportUnboundVariable=false
|
||||
XID = c_ulong # from X.h: typedef unsigned long XID
|
||||
elif sys.platform == "linux":
|
||||
from ctypes import POINTER, Structure, byref, c_char_p, c_int, c_long, c_uint, c_ulong, c_void_p, cdll
|
||||
|
||||
XID = c_ulong
|
||||
Window = XID
|
||||
Atom = c_ulong
|
||||
Display = c_void_p # Opaque
|
||||
Display = c_void_p
|
||||
|
||||
PropModeReplace = 0
|
||||
PropModePrepend = 1
|
||||
PropModeAppend = 2
|
||||
|
||||
# From xprops.h
|
||||
MWM_HINTS_FUNCTIONS = 1 << 0
|
||||
MWM_HINTS_DECORATIONS = 1 << 1
|
||||
MWM_HINTS_INPUT_MODE = 1 << 2
|
||||
@@ -102,8 +101,8 @@ elif sys.platform == 'linux':
|
||||
XOpenDisplay.argtypes = [c_char_p]
|
||||
XOpenDisplay.restype = POINTER(Display)
|
||||
XQueryTree = xlib.XQueryTree
|
||||
XQueryTree.argtypes = [POINTER(Display), Window, POINTER(
|
||||
Window), POINTER(Window), POINTER(Window), POINTER(c_uint)]
|
||||
XQueryTree.argtypes = [POINTER(Display), Window, POINTER(Window),
|
||||
POINTER(Window), POINTER(Window), POINTER(c_uint)]
|
||||
XQueryTree.restype = c_int
|
||||
dpy = xlib.XOpenDisplay(None)
|
||||
if not dpy:
|
||||
@@ -116,14 +115,19 @@ elif sys.platform == 'linux':
|
||||
MWM_DECOR_BORDER | MWM_DECOR_RESIZEH | MWM_DECOR_TITLE | MWM_DECOR_MENU | MWM_DECOR_MINIMIZE,
|
||||
0, 0
|
||||
)
|
||||
motif_wm_hints_dark = MotifWmHints(MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS,
|
||||
MWM_FUNC_RESIZE | MWM_FUNC_MOVE | MWM_FUNC_MINIMIZE | MWM_FUNC_CLOSE,
|
||||
0, 0, 0)
|
||||
motif_wm_hints_dark = MotifWmHints(
|
||||
MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS,
|
||||
MWM_FUNC_RESIZE | MWM_FUNC_MOVE | MWM_FUNC_MINIMIZE | MWM_FUNC_CLOSE,
|
||||
0, 0, 0
|
||||
)
|
||||
except Exception:
|
||||
if __debug__:
|
||||
print_exc()
|
||||
|
||||
dpy = None
|
||||
else:
|
||||
dpy = None
|
||||
else:
|
||||
dpy = None
|
||||
|
||||
|
||||
class _Theme:
|
||||
@@ -138,20 +142,19 @@ class _Theme:
|
||||
def __init__(self) -> None:
|
||||
self.active: int | None = None # Starts out with no theme
|
||||
self.minwidth: int | None = None
|
||||
self.widgets: dict[tk.Widget | tk.BitmapImage, set] = {}
|
||||
self.widgets_pair: list = []
|
||||
self.defaults: dict = {}
|
||||
self.current: dict = {}
|
||||
self.widgets: dict[tk.Widget | tk.BitmapImage, set[str]] = {}
|
||||
self.widgets_pair: list[tuple[tuple[Any, ...], dict[str, Any]]] = []
|
||||
self.defaults: dict[str, Any] = {}
|
||||
self.current: dict[str, Any] = {}
|
||||
self.default_ui_scale: float | None = None # None == not yet known
|
||||
self.startup_ui_scale: int | None = None
|
||||
|
||||
def register(self, widget: tk.Widget | tk.BitmapImage) -> None: # noqa: CCR001, C901
|
||||
def register(self, widget: tk.Widget | tk.BitmapImage) -> None: # noqa: CCR001
|
||||
# Note widget and children for later application of a theme. Note if
|
||||
# the widget has explicit fg or bg attributes.
|
||||
if not isinstance(widget, (tk.Widget, tk.BitmapImage)):
|
||||
raise TypeError(f'Expected widget, got {type(widget)}')
|
||||
if not self.defaults:
|
||||
# Can't initialise this til window is created # Windows
|
||||
self.defaults = {
|
||||
'fg': tk.Label()['foreground'], # SystemButtonText, systemButtonText
|
||||
'bg': tk.Label()['background'], # SystemButtonFace, White
|
||||
@@ -170,40 +173,40 @@ class _Theme:
|
||||
if widget not in self.widgets:
|
||||
# No general way to tell whether the user has overridden, so compare against widget-type specific defaults
|
||||
attribs = set()
|
||||
if isinstance(widget, tk.BitmapImage):
|
||||
if widget['foreground'] not in ['', self.defaults['bitmapfg']]:
|
||||
attribs.add('fg')
|
||||
if widget['background'] not in ['', self.defaults['bitmapbg']]:
|
||||
attribs.add('bg')
|
||||
elif isinstance(widget, (tk.Entry, ttk.Entry)):
|
||||
if widget['foreground'] not in ['', self.defaults['entryfg']]:
|
||||
attribs.add('fg')
|
||||
if widget['background'] not in ['', self.defaults['entrybg']]:
|
||||
attribs.add('bg')
|
||||
if 'font' in widget.keys() and str(widget['font']) not in ['', self.defaults['entryfont']]:
|
||||
attribs.add('font')
|
||||
elif isinstance(widget, (tk.Canvas, tk.Frame, ttk.Frame)):
|
||||
if (
|
||||
('background' in widget.keys() or isinstance(widget, tk.Canvas))
|
||||
and widget['background'] not in ['', self.defaults['frame']]
|
||||
):
|
||||
attribs.add('bg')
|
||||
elif isinstance(widget, HyperlinkLabel):
|
||||
pass # Hack - HyperlinkLabel changes based on state, so skip
|
||||
elif isinstance(widget, tk.Menu):
|
||||
if widget['foreground'] not in ['', self.defaults['menufg']]:
|
||||
attribs.add('fg')
|
||||
if widget['background'] not in ['', self.defaults['menubg']]:
|
||||
attribs.add('bg')
|
||||
if widget['font'] not in ['', self.defaults['menufont']]:
|
||||
attribs.add('font')
|
||||
else: # tk.Button, tk.Label
|
||||
if 'foreground' in widget.keys() and widget['foreground'] not in ['', self.defaults['fg']]:
|
||||
attribs.add('fg')
|
||||
if 'background' in widget.keys() and widget['background'] not in ['', self.defaults['bg']]:
|
||||
attribs.add('bg')
|
||||
if 'font' in widget.keys() and widget['font'] not in ['', self.defaults['font']]:
|
||||
attribs.add('font')
|
||||
match widget:
|
||||
case tk.BitmapImage():
|
||||
if widget['foreground'] not in ('', self.defaults['bitmapfg']):
|
||||
attribs.add('fg')
|
||||
if widget['background'] not in ('', self.defaults['bitmapbg']):
|
||||
attribs.add('bg')
|
||||
case tk.Entry() | ttk.Entry():
|
||||
if widget['foreground'] not in ('', self.defaults['entryfg']):
|
||||
attribs.add('fg')
|
||||
if widget['background'] not in ('', self.defaults['entrybg']):
|
||||
attribs.add('bg')
|
||||
if 'font' in widget.keys() and str(widget['font']) not in ('', self.defaults['entryfont']):
|
||||
attribs.add('font')
|
||||
case tk.Canvas() | tk.Frame() | ttk.Frame():
|
||||
if (('background' in widget.keys() or isinstance(widget, tk.Canvas))
|
||||
and widget['background'] not in ('', self.defaults['frame'])):
|
||||
attribs.add('bg')
|
||||
case HyperlinkLabel():
|
||||
pass
|
||||
case tk.Menu():
|
||||
if widget['foreground'] not in ('', self.defaults['menufg']):
|
||||
attribs.add('fg')
|
||||
if widget['background'] not in ('', self.defaults['menubg']):
|
||||
attribs.add('bg')
|
||||
if widget['font'] not in ('', self.defaults['menufont']):
|
||||
attribs.add('font')
|
||||
case _:
|
||||
w_keys = widget.keys() if hasattr(widget, 'keys') else []
|
||||
if 'foreground' in w_keys and widget['foreground'] not in ('', self.defaults['fg']):
|
||||
attribs.add('fg')
|
||||
if 'background' in w_keys and widget['background'] not in ('', self.defaults['bg']):
|
||||
attribs.add('bg')
|
||||
if 'font' in w_keys and widget['font'] not in ('', self.defaults['font']):
|
||||
attribs.add('font')
|
||||
self.widgets[widget] = attribs
|
||||
|
||||
if isinstance(widget, (tk.Frame, ttk.Frame)):
|
||||
@@ -213,68 +216,66 @@ class _Theme:
|
||||
def register_alternate(self, pair: tuple, gridopts: dict) -> None:
|
||||
self.widgets_pair.append((pair, gridopts))
|
||||
|
||||
def button_bind(
|
||||
self, widget: tk.Widget, command: Callable, image: tk.BitmapImage | None = None
|
||||
) -> None:
|
||||
def button_bind(self, widget: tk.Widget, command: Callable, image: tk.BitmapImage | None = None) -> None:
|
||||
widget.bind('<Button-1>', command)
|
||||
widget.bind('<Enter>', lambda e: self._enter(e, image))
|
||||
widget.bind('<Leave>', lambda e: self._leave(e, image))
|
||||
widget.bind('<Enter>', lambda e: self._enter(e, widget=widget, image=image))
|
||||
widget.bind('<Leave>', lambda e: self._leave(e, widget=widget, image=image))
|
||||
|
||||
def _enter(self, event, widget=None, image=None): # noqa: CCR001
|
||||
def _enter(self, event: tk.Event, widget: tk.Widget | None = None, # noqa: CCR001
|
||||
image: tk.BitmapImage | None = None) -> None:
|
||||
"""Handle mouse entering a widget or image."""
|
||||
if widget is not None:
|
||||
# Only apply foreground/background if the widget supports them
|
||||
config_keys = widget.keys() if hasattr(widget, 'keys') else []
|
||||
kwargs = {}
|
||||
if 'foreground' in config_keys:
|
||||
kwargs['foreground'] = self.current.get('highlightforeground', '')
|
||||
if 'background' in config_keys:
|
||||
kwargs['background'] = self.current.get('highlightbackground', '')
|
||||
if 'foreground' in config_keys and self.current.get('highlightforeground'):
|
||||
kwargs['foreground'] = self.current['highlightforeground']
|
||||
if 'background' in config_keys and self.current.get('highlightbackground'):
|
||||
kwargs['background'] = self.current['highlightbackground']
|
||||
if kwargs:
|
||||
widget.configure(**kwargs)
|
||||
|
||||
if image is not None:
|
||||
if image is not None and not isinstance(image, tk.PhotoImage):
|
||||
# PhotoImage cannot be configured with fg/bg, skip safely
|
||||
if not isinstance(image, tk.PhotoImage):
|
||||
try:
|
||||
config_keys = image.keys() if hasattr(image, 'keys') else []
|
||||
kwargs = {}
|
||||
if 'foreground' in config_keys:
|
||||
kwargs['foreground'] = self.current.get('highlightforeground', '')
|
||||
if 'background' in config_keys:
|
||||
kwargs['background'] = self.current.get('highlightbackground', '')
|
||||
if kwargs:
|
||||
image.configure(**kwargs)
|
||||
except tk.TclError:
|
||||
# silently ignore unsupported image types
|
||||
pass
|
||||
try:
|
||||
config_keys = image.keys() if hasattr(image, 'keys') else []
|
||||
kwargs = {}
|
||||
if 'foreground' in config_keys and self.current.get('highlightforeground'):
|
||||
kwargs['foreground'] = self.current['highlightforeground']
|
||||
if 'background' in config_keys and self.current.get('highlightbackground'):
|
||||
kwargs['background'] = self.current['highlightbackground']
|
||||
if kwargs:
|
||||
image.configure(**kwargs)
|
||||
except tk.TclError:
|
||||
# silently ignore unsupported image types
|
||||
pass
|
||||
|
||||
def _leave(self, event, widget=None, image=None): # noqa: CCR001
|
||||
def _leave(self, event: tk.Event, widget: tk.Widget | None = None, # noqa: CCR001
|
||||
image: tk.BitmapImage | None = None) -> None:
|
||||
"""Handle mouse leaving a widget or image."""
|
||||
if widget is not None:
|
||||
config_keys = widget.keys() if hasattr(widget, 'keys') else []
|
||||
kwargs = {}
|
||||
if 'foreground' in config_keys:
|
||||
kwargs['foreground'] = self.current.get('foreground', '')
|
||||
if 'background' in config_keys:
|
||||
kwargs['background'] = self.current.get('background', '')
|
||||
if 'foreground' in config_keys and self.current.get('foreground'):
|
||||
kwargs['foreground'] = self.current['foreground']
|
||||
if 'background' in config_keys and self.current.get('background'):
|
||||
kwargs['background'] = self.current['background']
|
||||
if kwargs:
|
||||
widget.configure(**kwargs)
|
||||
|
||||
if image is not None:
|
||||
if image is not None and not isinstance(image, tk.PhotoImage):
|
||||
# Skip PhotoImage objects; safely handle other types
|
||||
if not isinstance(image, tk.PhotoImage):
|
||||
try:
|
||||
config_keys = image.keys() if hasattr(image, 'keys') else []
|
||||
kwargs = {}
|
||||
if 'foreground' in config_keys:
|
||||
kwargs['foreground'] = self.current.get('foreground', '')
|
||||
if 'background' in config_keys:
|
||||
kwargs['background'] = self.current.get('background', '')
|
||||
if kwargs:
|
||||
image.configure(**kwargs)
|
||||
except tk.TclError:
|
||||
pass
|
||||
try:
|
||||
config_keys = image.keys() if hasattr(image, 'keys') else []
|
||||
kwargs = {}
|
||||
if 'foreground' in config_keys and self.current.get('foreground'):
|
||||
kwargs['foreground'] = self.current['foreground']
|
||||
if 'background' in config_keys and self.current.get('background'):
|
||||
kwargs['background'] = self.current['background']
|
||||
if kwargs:
|
||||
image.configure(**kwargs)
|
||||
except tk.TclError:
|
||||
pass
|
||||
|
||||
# Set up colors
|
||||
def _colors(self, root: tk.Tk, theme: int) -> None:
|
||||
@@ -288,37 +289,40 @@ class _Theme:
|
||||
if not config.get_str('dark_highlight'):
|
||||
config.set('dark_highlight', 'white')
|
||||
|
||||
if theme == self.THEME_DEFAULT:
|
||||
# (Mostly) system colors
|
||||
style = ttk.Style()
|
||||
self.current = {
|
||||
'background': (style.lookup('TLabel', 'background')),
|
||||
'foreground': style.lookup('TLabel', 'foreground'),
|
||||
'activebackground': (sys.platform == 'win32' and 'SystemHighlight' or
|
||||
style.lookup('TLabel', 'background', ['active'])),
|
||||
'activeforeground': (sys.platform == 'win32' and 'SystemHighlightText' or
|
||||
style.lookup('TLabel', 'foreground', ['active'])),
|
||||
'disabledforeground': style.lookup('TLabel', 'foreground', ['disabled']),
|
||||
'highlight': 'blue',
|
||||
'font': 'TkDefaultFont',
|
||||
}
|
||||
match theme:
|
||||
case self.THEME_DEFAULT:
|
||||
self.current = {
|
||||
'background': style.lookup('TLabel', 'background'),
|
||||
'foreground': style.lookup('TLabel', 'foreground'),
|
||||
'activebackground': 'SystemHighlight' if sys.platform == 'win32' else style.lookup('TLabel',
|
||||
'background',
|
||||
['active']),
|
||||
'activeforeground': 'SystemHighlightText' if sys.platform == 'win32' else style.lookup('TLabel',
|
||||
'foreground',
|
||||
['active']),
|
||||
'disabledforeground': style.lookup('TLabel', 'foreground', ['disabled']),
|
||||
'highlight': 'blue',
|
||||
'font': 'TkDefaultFont',
|
||||
}
|
||||
case _:
|
||||
r, g, b = root.winfo_rgb(config.get_str('dark_text'))
|
||||
|
||||
else: # Dark *or* Transparent
|
||||
(r, g, b) = root.winfo_rgb(config.get_str('dark_text'))
|
||||
self.current = {
|
||||
'background': 'grey4', # OSX inactive dark titlebar color
|
||||
'foreground': config.get_str('dark_text'),
|
||||
'activebackground': config.get_str('dark_text'),
|
||||
'activeforeground': 'grey4',
|
||||
'disabledforeground': f'#{int(r/384):02x}{int(g/384):02x}{int(b/384):02x}',
|
||||
'highlight': config.get_str('dark_highlight'),
|
||||
# Font only supports Latin 1 / Supplement / Extended, and a
|
||||
# few General Punctuation and Mathematical Operators
|
||||
# LANG: Label for commander name in main window
|
||||
'font': (theme > 1 and not 0x250 < ord(tr.tl('Cmdr')[0]) < 0x3000 and
|
||||
tk_font.Font(family='Euro Caps', size=10, weight=tk_font.NORMAL) or
|
||||
'TkDefaultFont'),
|
||||
}
|
||||
use_euro_caps = theme > 1 and not (0x250 < ord(tr.tl('Cmdr')[0]) < 0x3000)
|
||||
chosen_font = tk_font.Font(family='Euro Caps', size=10,
|
||||
weight=tk_font.NORMAL) if use_euro_caps else 'TkDefaultFont'
|
||||
|
||||
self.current = {
|
||||
'background': 'grey4',
|
||||
'foreground': config.get_str('dark_text'),
|
||||
'activebackground': config.get_str('dark_text'),
|
||||
'activeforeground': 'grey4',
|
||||
'disabledforeground': f'#{int(r / 384):02x}{int(g / 384):02x}{int(b / 384):02x}',
|
||||
'highlight': config.get_str('dark_highlight'),
|
||||
'font': chosen_font,
|
||||
}
|
||||
|
||||
def update(self, widget: tk.Widget) -> None:
|
||||
"""
|
||||
@@ -339,89 +343,73 @@ class _Theme:
|
||||
self._update_widget(child) # type: ignore
|
||||
|
||||
# Apply current theme to a single widget
|
||||
def _update_widget(self, widget: tk.Widget | tk.BitmapImage) -> None: # noqa: CCR001, C901
|
||||
def _update_widget(self, widget: tk.Widget | tk.BitmapImage) -> None: # noqa: CCR001
|
||||
if widget not in self.widgets:
|
||||
if isinstance(widget, tk.Widget):
|
||||
w_class = widget.winfo_class()
|
||||
w_keys: list[str] = widget.keys()
|
||||
w_class = widget.winfo_class() if isinstance(widget, tk.Widget) else ''
|
||||
w_keys = widget.keys() if isinstance(widget, tk.Widget) else []
|
||||
w_text = widget["text"] if "text" in w_keys else ""
|
||||
raise ValueError(f'{w_class} {widget} "{w_text}"')
|
||||
|
||||
else:
|
||||
# There is no tk.BitmapImage.winfo_class()
|
||||
w_class = ''
|
||||
# There is no tk.BitmapImage.keys()
|
||||
w_keys = []
|
||||
|
||||
assert_str = f'{w_class} {widget} "{"text" in w_keys and widget["text"]}"'
|
||||
raise ValueError(assert_str)
|
||||
|
||||
attribs: set = self.widgets.get(widget, set())
|
||||
attribs = self.widgets.get(widget, set())
|
||||
|
||||
try:
|
||||
if isinstance(widget, tk.BitmapImage):
|
||||
# not a widget
|
||||
if 'fg' not in attribs:
|
||||
widget['foreground'] = self.current['foreground']
|
||||
match widget:
|
||||
case tk.BitmapImage():
|
||||
if 'fg' not in attribs:
|
||||
widget['foreground'] = self.current['foreground']
|
||||
if 'bg' not in attribs:
|
||||
widget['background'] = self.current['background']
|
||||
|
||||
if 'bg' not in attribs:
|
||||
widget['background'] = self.current['background']
|
||||
case _ if 'cursor' in widget.keys() and str(widget['cursor']) not in ('', 'arrow'):
|
||||
# Hack - highlight widgets like HyperlinkLabel with a non-default cursor
|
||||
if 'fg' not in attribs:
|
||||
widget['foreground'] = self.current['highlight']
|
||||
if 'insertbackground' in widget.keys(): # tk.Entry
|
||||
widget['insertbackground'] = self.current['foreground']
|
||||
if 'bg' not in attribs:
|
||||
widget['background'] = self.current['background']
|
||||
if 'highlightbackground' in widget.keys(): # tk.Entry
|
||||
widget['highlightbackground'] = self.current['background']
|
||||
if 'font' not in attribs:
|
||||
widget['font'] = self.current['font']
|
||||
|
||||
elif 'cursor' in widget.keys() and str(widget['cursor']) not in ['', 'arrow']:
|
||||
# Hack - highlight widgets like HyperlinkLabel with a non-default cursor
|
||||
if 'fg' not in attribs:
|
||||
widget['foreground'] = self.current['highlight']
|
||||
if 'insertbackground' in widget.keys(): # tk.Entry
|
||||
widget['insertbackground'] = self.current['foreground']
|
||||
case _ if 'activeforeground' in widget.keys():
|
||||
# e.g. tk.Button, tk.Label, tk.Menu
|
||||
if 'fg' not in attribs:
|
||||
widget['foreground'] = self.current['foreground']
|
||||
widget['activeforeground'] = self.current['activeforeground']
|
||||
widget['disabledforeground'] = self.current['disabledforeground']
|
||||
if 'bg' not in attribs:
|
||||
widget['background'] = self.current['background']
|
||||
widget['activebackground'] = self.current['activebackground']
|
||||
if 'font' not in attribs:
|
||||
widget['font'] = self.current['font']
|
||||
|
||||
if 'bg' not in attribs:
|
||||
widget['background'] = self.current['background']
|
||||
if 'highlightbackground' in widget.keys(): # tk.Entry
|
||||
widget['highlightbackground'] = self.current['background']
|
||||
case _ if 'foreground' in widget.keys():
|
||||
# e.g. ttk.Label
|
||||
if 'fg' not in attribs:
|
||||
widget['foreground'] = self.current['foreground']
|
||||
if 'bg' not in attribs:
|
||||
widget['background'] = self.current['background']
|
||||
if 'font' not in attribs:
|
||||
widget['font'] = self.current['font']
|
||||
|
||||
if 'font' not in attribs:
|
||||
widget['font'] = self.current['font']
|
||||
|
||||
elif 'activeforeground' in widget.keys():
|
||||
# e.g. tk.Button, tk.Label, tk.Menu
|
||||
if 'fg' not in attribs:
|
||||
widget['foreground'] = self.current['foreground']
|
||||
widget['activeforeground'] = self.current['activeforeground']
|
||||
widget['disabledforeground'] = self.current['disabledforeground']
|
||||
|
||||
if 'bg' not in attribs:
|
||||
widget['background'] = self.current['background']
|
||||
widget['activebackground'] = self.current['activebackground']
|
||||
|
||||
if 'font' not in attribs:
|
||||
widget['font'] = self.current['font']
|
||||
|
||||
elif 'foreground' in widget.keys():
|
||||
# e.g. ttk.Label
|
||||
if 'fg' not in attribs:
|
||||
widget['foreground'] = self.current['foreground']
|
||||
|
||||
if 'bg' not in attribs:
|
||||
widget['background'] = self.current['background']
|
||||
|
||||
if 'font' not in attribs:
|
||||
widget['font'] = self.current['font']
|
||||
|
||||
elif 'background' in widget.keys() or isinstance(widget, tk.Canvas):
|
||||
# e.g. Frame, Canvas
|
||||
if 'bg' not in attribs:
|
||||
widget['background'] = self.current['background']
|
||||
widget['highlightbackground'] = self.current['disabledforeground']
|
||||
case _ if 'background' in widget.keys() or isinstance(widget, tk.Canvas):
|
||||
# e.g. Frame, Canvas
|
||||
if 'bg' not in attribs:
|
||||
widget['background'] = self.current['background']
|
||||
widget['highlightbackground'] = self.current['disabledforeground']
|
||||
|
||||
except Exception:
|
||||
logger.exception(f'Plugin widget issue ? {widget=}')
|
||||
logger.exception(f'Plugin widget issue? {widget=}')
|
||||
|
||||
# Apply configured theme
|
||||
|
||||
def apply(self, root: tk.Tk) -> None: # noqa: CCR001, C901
|
||||
def apply(self, root: tk.Tk) -> None: # noqa: CCR001
|
||||
theme = config.get_int('theme')
|
||||
self._colors(root, theme)
|
||||
|
||||
# Apply colors
|
||||
for widget in set(self.widgets):
|
||||
for widget in list(self.widgets.keys()):
|
||||
if isinstance(widget, tk.Widget) and not widget.winfo_exists():
|
||||
self.widgets.pop(widget) # has been destroyed
|
||||
else:
|
||||
@@ -434,13 +422,12 @@ class _Theme:
|
||||
widget.grid_remove()
|
||||
|
||||
if isinstance(pair[0], tk.Menu):
|
||||
if theme == self.THEME_DEFAULT:
|
||||
root['menu'] = pair[0]
|
||||
|
||||
else: # Dark *or* Transparent
|
||||
root['menu'] = ''
|
||||
pair[theme].grid(**gridopts)
|
||||
|
||||
match theme:
|
||||
case self.THEME_DEFAULT:
|
||||
root['menu'] = pair[0]
|
||||
case _: # Dark *or* Transparent
|
||||
root['menu'] = ''
|
||||
pair[theme].grid(**gridopts)
|
||||
else:
|
||||
pair[theme].grid(**gridopts)
|
||||
|
||||
@@ -449,66 +436,38 @@ class _Theme:
|
||||
self.active = theme
|
||||
|
||||
if sys.platform == 'win32':
|
||||
import win32con
|
||||
|
||||
# FIXME: Lose the "treat this like a boolean" bullshit
|
||||
if theme == self.THEME_DEFAULT:
|
||||
root.overrideredirect(False)
|
||||
|
||||
else:
|
||||
root.overrideredirect(True)
|
||||
|
||||
if theme == self.THEME_TRANSPARENT:
|
||||
root.attributes("-transparentcolor", 'grey4')
|
||||
|
||||
else:
|
||||
root.attributes("-transparentcolor", '')
|
||||
root.overrideredirect(theme != self.THEME_DEFAULT)
|
||||
root.attributes("-transparentcolor", 'grey4' if theme == self.THEME_TRANSPARENT else '')
|
||||
|
||||
root.withdraw()
|
||||
root.update_idletasks() # Size and windows styles get recalculated here
|
||||
hwnd = win32gui.GetParent(root.winfo_id())
|
||||
win32gui.SetWindowLong(hwnd, win32con.GWL_STYLE,
|
||||
win32gui.GetWindowLong(hwnd, win32con.GWL_STYLE)
|
||||
& ~win32con.WS_MAXIMIZEBOX) # disable maximize
|
||||
win32gui.SetWindowLong(
|
||||
hwnd,
|
||||
win32con.GWL_STYLE,
|
||||
win32gui.GetWindowLong(hwnd, win32con.GWL_STYLE) & ~win32con.WS_MAXIMIZEBOX
|
||||
)
|
||||
|
||||
ex_style = win32con.WS_EX_APPWINDOW
|
||||
if theme == self.THEME_TRANSPARENT:
|
||||
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE,
|
||||
win32con.WS_EX_APPWINDOW | win32con.WS_EX_LAYERED) # Add to taskbar
|
||||
|
||||
else:
|
||||
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, win32con.WS_EX_APPWINDOW) # Add to taskbar
|
||||
ex_style |= win32con.WS_EX_LAYERED
|
||||
|
||||
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, ex_style)
|
||||
root.deiconify()
|
||||
root.wait_visibility() # need main window to be displayed before returning
|
||||
|
||||
else:
|
||||
root.withdraw()
|
||||
root.update_idletasks() # Size gets recalculated here
|
||||
if dpy:
|
||||
xroot = Window()
|
||||
parent = Window()
|
||||
children = Window()
|
||||
xroot, parent, children = Window(), Window(), Window()
|
||||
nchildren = c_uint()
|
||||
XQueryTree(dpy, root.winfo_id(), byref(xroot), byref(parent), byref(children), byref(nchildren))
|
||||
if theme == self.THEME_DEFAULT:
|
||||
wm_hints = motif_wm_hints_normal
|
||||
|
||||
else: # Dark *or* Transparent
|
||||
wm_hints = motif_wm_hints_dark
|
||||
|
||||
XChangeProperty(
|
||||
dpy, parent, motif_wm_hints_property, motif_wm_hints_property, 32, PropModeReplace, wm_hints, 5
|
||||
)
|
||||
|
||||
wm_hints = motif_wm_hints_normal if theme == self.THEME_DEFAULT else motif_wm_hints_dark
|
||||
XChangeProperty(dpy, parent, motif_wm_hints_property, motif_wm_hints_property, 32, PropModeReplace,
|
||||
wm_hints, 5)
|
||||
XFlush(dpy)
|
||||
|
||||
else:
|
||||
if theme == self.THEME_DEFAULT:
|
||||
root.overrideredirect(False)
|
||||
|
||||
else: # Dark *or* Transparent
|
||||
root.overrideredirect(True)
|
||||
|
||||
root.overrideredirect(theme != self.THEME_DEFAULT)
|
||||
root.deiconify()
|
||||
root.wait_visibility() # need main window to be displayed before returning
|
||||
|
||||
@@ -517,5 +476,5 @@ class _Theme:
|
||||
root.minsize(self.minwidth, -1)
|
||||
|
||||
|
||||
# singleton
|
||||
# Singleton
|
||||
theme = _Theme()
|
||||
+35
-30
@@ -19,11 +19,12 @@ In addition to standard ttk.Label arguments, takes the following arguments:
|
||||
May be imported by plugins
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import html
|
||||
from functools import partial
|
||||
import sys
|
||||
import tkinter as tk
|
||||
import webbrowser
|
||||
from functools import partial
|
||||
from tkinter import font as tk_font
|
||||
from tkinter import ttk
|
||||
from typing import Any
|
||||
@@ -36,11 +37,11 @@ SHIPYARD_HTML_TEMPLATE = """
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="refresh" content="0; url={link}">
|
||||
<meta http-equiv=\"refresh\" content=\"0; url={link}\">
|
||||
<title>Redirecting you to your {ship_name} at {provider_name}...</title>
|
||||
</head>
|
||||
<body>
|
||||
<a href="{link}">
|
||||
<a href=\"{link}\">
|
||||
You should be redirected to your {ship_name} at {provider_name} shortly...
|
||||
</a>
|
||||
</body>
|
||||
@@ -97,12 +98,14 @@ class HyperlinkLabel(tk.Label or ttk.Label): # type: ignore
|
||||
target = plug.invoke(url, 'EDSY', 'shipyard_url', loadout, monitor.is_beta)
|
||||
file_name = config.app_dir_path / "last_shipyard.html"
|
||||
|
||||
with open(file_name, 'w') as f:
|
||||
f.write(SHIPYARD_HTML_TEMPLATE.format(
|
||||
file_name.write_text(
|
||||
SHIPYARD_HTML_TEMPLATE.format(
|
||||
link=html.escape(str(target)),
|
||||
provider_name=html.escape(str(url)),
|
||||
ship_name=html.escape("Ship")
|
||||
))
|
||||
),
|
||||
encoding='utf-8'
|
||||
)
|
||||
|
||||
webbrowser.open(f'file://localhost/{file_name}')
|
||||
|
||||
@@ -192,33 +195,35 @@ class HyperlinkLabel(tk.Label or ttk.Label): # type: ignore
|
||||
# LANG: Label for 'Copy' as in 'Copy and Paste'
|
||||
menu.add_command(label=tr.tl('Copy'), command=self.copy) # As in Copy and Paste
|
||||
|
||||
if self.name == 'ship':
|
||||
# LANG: Copy the Inara SLEF Format of the active ship to the clipboard
|
||||
menu.add_command(label=tr.tl('Copy Inara SLEF'), command=self.copy_slef, state=tk.DISABLED)
|
||||
menu.entryconfigure(1, state=monitor.slef and tk.NORMAL or tk.DISABLED)
|
||||
match self.name:
|
||||
case 'ship':
|
||||
state_val = tk.NORMAL if monitor.slef else tk.DISABLED
|
||||
# LANG: Copy the Inara SLEF Format of the active ship to the clipboard
|
||||
menu.add_command(label=tr.tl('Copy Inara SLEF'), command=self.copy_slef, state=tk.DISABLED)
|
||||
menu.entryconfigure(1, state=state_val)
|
||||
|
||||
menu.add_separator()
|
||||
for url in plug.provides('shipyard_url'):
|
||||
menu.add_command(
|
||||
label=tr.tl("Open in {URL}").format(URL=url), # LANG: Open Element In Selected Provider
|
||||
command=partial(self.open_shipyard, url)
|
||||
)
|
||||
menu.add_separator()
|
||||
for url in plug.provides('shipyard_url'):
|
||||
menu.add_command(
|
||||
label=tr.tl("Open in {URL}").format(URL=url), # LANG: Open Element In Selected Provider
|
||||
command=partial(self.open_shipyard, url)
|
||||
)
|
||||
|
||||
if self.name == 'station':
|
||||
menu.add_separator()
|
||||
for url in plug.provides('station_url'):
|
||||
menu.add_command(
|
||||
label=tr.tl("Open in {URL}").format(URL=url), # LANG: Open Element In Selected Provider
|
||||
command=partial(self.open_station, url)
|
||||
)
|
||||
case 'station':
|
||||
menu.add_separator()
|
||||
for url in plug.provides('station_url'):
|
||||
menu.add_command(
|
||||
label=tr.tl("Open in {URL}").format(URL=url), # LANG: Open Element In Selected Provider
|
||||
command=partial(self.open_station, url)
|
||||
)
|
||||
|
||||
if self.name == 'system':
|
||||
menu.add_separator()
|
||||
for url in plug.provides('system_url'):
|
||||
menu.add_command(
|
||||
label=tr.tl("Open in {URL}").format(URL=url), # LANG: Open Element In Selected Provider
|
||||
command=partial(self.open_system, url)
|
||||
)
|
||||
case 'system':
|
||||
menu.add_separator()
|
||||
for url in plug.provides('system_url'):
|
||||
menu.add_command(
|
||||
label=tr.tl("Open in {URL}").format(URL=url), # LANG: Open Element In Selected Provider
|
||||
command=partial(self.open_system, url)
|
||||
)
|
||||
|
||||
if self['text'] and (self.popup_copy(self['text']) if callable(self.popup_copy) else self.popup_copy):
|
||||
menu.post(event.x_root, event.y_root)
|
||||
|
||||
+10
-6
@@ -5,23 +5,27 @@ Copyright (c) EDCD, All Rights Reserved
|
||||
Licensed under the GNU General Public License v2 or later.
|
||||
See LICENSE file.
|
||||
"""
|
||||
import os
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
import os
|
||||
from pathlib import Path
|
||||
from edmc_data import ship_name_map
|
||||
|
||||
# Character mapping dictionary for illegal filesystem characters
|
||||
_ILLEGAL_CHAR_MAP = {ord(x): "_" for x in ("\0", "<", ">", ":", '"', "/", "\\", "|", "?", "*")}
|
||||
|
||||
|
||||
def ship_file_name(ship_name: str, ship_type: str) -> str:
|
||||
"""Return a ship name suitable for a filename."""
|
||||
name = str(ship_name or ship_name_map.get(ship_type.lower(), ship_type)).strip()
|
||||
|
||||
# Handle suffix using Pathlib's with_suffix method
|
||||
name = Path(name).with_suffix("").name
|
||||
path_obj = Path(name).with_suffix("")
|
||||
clean_name = path_obj.name
|
||||
|
||||
# Check if the name is a reserved filename
|
||||
if sys.platform == 'win32' and os.path.isreserved(name):
|
||||
name += "_"
|
||||
clean_name += "_"
|
||||
|
||||
return name.translate(
|
||||
{ord(x): "_" for x in ("\0", "<", ">", ":", '"', "/", "\\", "|", "?", "*")}
|
||||
)
|
||||
return clean_name.translate(_ILLEGAL_CHAR_MAP)
|
||||
Reference in new issue
Block a user