diff --git a/EDMarketConnector.py b/EDMarketConnector.py index 21531679..470ead9c 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -168,6 +168,11 @@ if __name__ == '__main__': # noqa: C901 help='Have EDDN plugin show what it is tracking', action='store_true', ) + + parser.add_argument( + '--killswitches-file', + help='Specify a custom killswitches file', + ) ########################################################################### args = parser.parse_args() @@ -1859,10 +1864,13 @@ Locale LC_TIME: {locale.getlocale(locale.LC_TIME)}''' ) -def setup_killswitches(): +def setup_killswitches(filename: Optional[str]): """Download and setup the main killswitch list.""" logger.debug('fetching killswitches...') - killswitch.setup_main_list() + if filename is not None: + filename = "file:" + filename + + killswitch.setup_main_list(filename) def show_killswitch_poppup(root=None): @@ -1891,9 +1899,9 @@ def show_killswitch_poppup(root=None): for version in kills: tk.Label(frame, text=f'Version: {version.version}').grid(row=idx, sticky=tk.W) idx += 1 - for id, reason in version.kills.items(): + 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=reason).grid(column=1, row=idx, sticky=tk.E, padx=(0, 10)) + tk.Label(frame, text=kill.reason).grid(column=1, row=idx, sticky=tk.E, padx=(0, 10)) idx += 1 idx += 1 @@ -2026,7 +2034,8 @@ sys.path: {sys.path}''' Translations.install(config.get_str('language')) # Can generate errors so wait til log set up - setup_killswitches() + 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) diff --git a/docs/Killswitches.md b/docs/Killswitches.md index d90607a1..bae03c2d 100644 --- a/docs/Killswitches.md +++ b/docs/Killswitches.md @@ -119,10 +119,17 @@ JSON primitives and their python equivalents json compound types (`object -- {}` and `array -- []`) may be set. ### Testing +You can supply a custom killswitches file for testing against +EDMarketConnector: +```bash +python EDMarketConnector.py --killswitches-file +``` +This will be relative to the CWD of the process. -Killswitch files can be tested using the script in `scripts/killswitch_test.py`. -Providing a file as an argument or `-` for stdin will output the behaviour of -the provided file, including indicating typos, if applicable. +Alternatively, killswitch files can be independently tested using the script in +`scripts/killswitch_test.py`. Providing a file as an argument or `-` for stdin +will output the behaviour of the provided file, including indicating typos, if +applicable. ### Versions diff --git a/killswitch.py b/killswitch.py index a7701739..42d02844 100644 --- a/killswitch.py +++ b/killswitch.py @@ -1,6 +1,7 @@ """Fetch kill switches from EDMC Repo.""" from __future__ import annotations +import json import threading from copy import deepcopy from typing import ( @@ -339,6 +340,16 @@ def fetch_kill_switches(target=DEFAULT_KILLSWITCH_URL) -> Optional[KillSwitchJSO :return: a list of dicts containing kill switch data, or None """ logger.info("Attempting to fetch kill switches") + if target.startswith('file:'): + target = target.replace('file:', '') + try: + with open(target, 'r') as t: + return json.load(t) + + except FileNotFoundError: + logger.warning(f"No such file '{target}'") + return None + try: data = requests.get(target, timeout=10).json() @@ -458,13 +469,16 @@ def get_kill_switches_thread( active: KillSwitchSet = KillSwitchSet([]) -def setup_main_list(): +def setup_main_list(filename: Optional[str]): """ Set up the global set of kill switches for querying. Plugins should NOT call this EVER. """ - if (data := get_kill_switches(DEFAULT_KILLSWITCH_URL, OLD_KILLSWITCH_URL)) is None: + if filename is None: + filename = DEFAULT_KILLSWITCH_URL + + if (data := get_kill_switches(filename, OLD_KILLSWITCH_URL)) is None: logger.warning("Unable to fetch kill switches. Setting global set to an empty set") return