1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-14 08:17:13 +03:00

killswitches: Implement --killswitches-file CL arg

* New CL arge `--killswitches-file`.  This needs to reference a file either
  with an absolute path, or relative to the CWD of the process.
* Internally if the argument is provided it is prefixed with `"file:"` in
  order to actually be loaded.  This is because `requests` doesn't have an
  adapter for `file:` URLs.
* Also fixes a visual bug with reporting of active killswitches.  The entire
  SingleKill object was used instead of just its `reason` property.  mypy
  type checks caught this.
This commit is contained in:
Athanasius 2022-12-16 14:02:59 +00:00
parent f1b2022aa2
commit 12ee3deb59
No known key found for this signature in database
GPG Key ID: 772697E181BB2767
3 changed files with 40 additions and 10 deletions

View File

@ -168,6 +168,11 @@ if __name__ == '__main__': # noqa: C901
help='Have EDDN plugin show what it is tracking', help='Have EDDN plugin show what it is tracking',
action='store_true', action='store_true',
) )
parser.add_argument(
'--killswitches-file',
help='Specify a custom killswitches file',
)
########################################################################### ###########################################################################
args = parser.parse_args() 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.""" """Download and setup the main killswitch list."""
logger.debug('fetching killswitches...') 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): def show_killswitch_poppup(root=None):
@ -1891,9 +1899,9 @@ def show_killswitch_poppup(root=None):
for version in kills: 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}').grid(row=idx, sticky=tk.W)
idx += 1 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=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
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 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()) root = tk.Tk(className=appname.lower())
if sys.platform != 'win32' and ((f := config.get_str('font')) is not None or f != ''): if sys.platform != 'win32' and ((f := config.get_str('font')) is not None or f != ''):
size = config.get_int('font_size', default=-1) size = config.get_int('font_size', default=-1)

View File

@ -119,10 +119,17 @@ JSON primitives and their python equivalents
json compound types (`object -- {}` and `array -- []`) may be set. json compound types (`object -- {}` and `array -- []`) may be set.
### Testing ### Testing
You can supply a custom killswitches file for testing against
EDMarketConnector:
```bash
python EDMarketConnector.py --killswitches-file <filename>
```
This will be relative to the CWD of the process.
Killswitch files can be tested using the script in `scripts/killswitch_test.py`. Alternatively, killswitch files can be independently tested using the script in
Providing a file as an argument or `-` for stdin will output the behaviour of `scripts/killswitch_test.py`. Providing a file as an argument or `-` for stdin
the provided file, including indicating typos, if applicable. will output the behaviour of the provided file, including indicating typos, if
applicable.
### Versions ### Versions

View File

@ -1,6 +1,7 @@
"""Fetch kill switches from EDMC Repo.""" """Fetch kill switches from EDMC Repo."""
from __future__ import annotations from __future__ import annotations
import json
import threading import threading
from copy import deepcopy from copy import deepcopy
from typing import ( 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 :return: a list of dicts containing kill switch data, or None
""" """
logger.info("Attempting to fetch kill switches") 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: try:
data = requests.get(target, timeout=10).json() data = requests.get(target, timeout=10).json()
@ -458,13 +469,16 @@ def get_kill_switches_thread(
active: KillSwitchSet = KillSwitchSet([]) active: KillSwitchSet = KillSwitchSet([])
def setup_main_list(): def setup_main_list(filename: Optional[str]):
""" """
Set up the global set of kill switches for querying. Set up the global set of kill switches for querying.
Plugins should NOT call this EVER. 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") logger.warning("Unable to fetch kill switches. Setting global set to an empty set")
return return