From d5b62d957b2cc5730c7ad7eed19f268050b5e6d0 Mon Sep 17 00:00:00 2001 From: A_D Date: Sat, 24 Oct 2020 16:19:27 +0200 Subject: [PATCH] Added support for comments in kill switches --- docs/Killswitches.md | 16 +++++++++------- killswitch.py | 21 ++++++++++++++------- plugins/eddn.py | 4 ++-- plugins/edsm.py | 6 ++++-- plugins/inara.py | 4 ++-- 5 files changed, 31 insertions(+), 20 deletions(-) diff --git a/docs/Killswitches.md b/docs/Killswitches.md index c24197e1..5ea69268 100644 --- a/docs/Killswitches.md +++ b/docs/Killswitches.md @@ -6,18 +6,18 @@ EDMarketConnector implements a Kill Switch system that allows us to disable feat Killswitches are stored in a JSON file that is queried by EDMC on startup. The format is as follows: -| Key | Type | Description | -| --------------: | :-----: | :------------------------------------------------------------ | -| `version` | integer | the version of the Kill Switch JSON file, always 1 | -| `last_updated` | string | When last the kill switches were updated (for human use only) | -| `kill_switches` | array | The kill switches this file contains (expanded below) | +| Key | Type | Description | +| --------------: | :------: | :------------------------------------------------------------ | +| `version` | `int` | the version of the Kill Switch JSON file, always 1 | +| `last_updated` | `string` | When last the kill switches were updated (for human use only) | +| `kill_switches` | `array` | The kill switches this file contains (expanded below) | The `kill_switches` array contains kill switch objects. Each contains two fields: | Key | Type | Description | | --------: | :----------------: | :---------------------------------------------------------------------- | | `version` | `semantic version` | The version of EDMC these kill switches apply to (Must be valid semver) | -| `kills` | array of strings | The different keys to disable | +| `kills` | `Dict[str, str]` | The different keys to disable, and the reason for the disable | An example follows: ```json @@ -27,7 +27,9 @@ An example follows: "kill_switches": [ { "version": "1.0.0", - "kills": ["plugins.eddn.send"] + "kills": { + "plugins.eddn.send": "some reason" + } } ] } diff --git a/killswitch.py b/killswitch.py index 226f8ca0..c6139527 100644 --- a/killswitch.py +++ b/killswitch.py @@ -1,5 +1,5 @@ """Fetch kill switches from EDMC Repo.""" -from typing import Dict, List, NamedTuple, Optional, Union, cast +from typing import Dict, List, NamedTuple, Optional, Tuple, Union, cast import requests import semantic_version @@ -19,7 +19,14 @@ class KillSwitch(NamedTuple): """One version's set of kill switches.""" version: semantic_version.Version - kills: List[str] + kills: Dict[str, str] + + +class DisabledResult(NamedTuple): + """DisabledResult is the result returned from various is_disabled calls.""" + + disbled: bool + reason: str class KillSwitchSet: @@ -28,7 +35,7 @@ class KillSwitchSet: def __init__(self, kill_switches: List[KillSwitch]) -> None: self.kill_switches = kill_switches - def is_disabled(self, id: str, *, version=_current_version) -> bool: + def is_disabled(self, id: str, *, version=_current_version) -> DisabledResult: """ Return whether or not the given feature ID is disabled by a killswitch for the given version. @@ -40,9 +47,9 @@ class KillSwitchSet: if version != ks.version: continue - return id in ks.kills + return DisabledResult(id in ks.kills, ks.kills.get(id, "")) - return False + return DisabledResult(False, "") def __str__(self) -> str: """Return a string representation of KillSwitchSet.""" @@ -111,7 +118,7 @@ def parse_kill_switches(data: KILL_SWITCH_JSON_DICT) -> List[KillSwitch]: logger.warning(f'could not parse killswitch idx {idx}: {e}') continue - ks = KillSwitch(version=ver, kills=cast(List[str], ks_data['kills'])) + ks = KillSwitch(version=ver, kills=cast(Dict[str, str], ks_data['kills'])) out.append(ks) return out @@ -148,7 +155,7 @@ def setup_main_list(): active = KillSwitchSet(parse_kill_switches(data)) -def is_disabled(id: str, *, version: semantic_version.Version = _current_version) -> bool: +def is_disabled(id: str, *, version: semantic_version.Version = _current_version) -> DisabledResult: """ Query the global KillSwitchSet for whether or not a given ID is disabled. diff --git a/plugins/eddn.py b/plugins/eddn.py index e4813b88..d952b219 100644 --- a/plugins/eddn.py +++ b/plugins/eddn.py @@ -127,8 +127,8 @@ class EDDN: :param cmdr: the CMDR to use as the uploader ID :param msg: the payload to send """ - if killswitch.is_disabled('plugins.eddn.send'): - logger.warning("eddn.send has been disabled via killswitch. Returning") + if (res := killswitch.is_disabled('plugins.eddn.send')).disbled: + logger.warning(f"eddn.send has been disabled via killswitch. Returning. ({res.reason})") return uploader_id = cmdr diff --git a/plugins/edsm.py b/plugins/edsm.py index 67ae4db8..4260e280 100644 --- a/plugins/edsm.py +++ b/plugins/edsm.py @@ -511,8 +511,10 @@ def worker() -> None: retrying = 0 while retrying < 3: - if killswitch.is_disabled("plugins.edsm.worker"): - logger.warning('EDSM worker has been disabled via kill switch. Not uploading data.') + if (res := killswitch.is_disabled("plugins.edsm.worker")).disbled: + logger.warning( + f'EDSM worker has been disabled via kill switch. Not uploading data. ({res.reason})' + ) break try: if TYPE_CHECKING: diff --git a/plugins/inara.py b/plugins/inara.py index 4f7d8d5a..abdfe7b9 100644 --- a/plugins/inara.py +++ b/plugins/inara.py @@ -1203,8 +1203,8 @@ def new_worker(): logger.debug('Starting...') while True: events = get_events() - if killswitch.is_disabled("plugins.inara.worker"): - logger.warning("Inara worker disabled via killswitch") + if (res := killswitch.is_disabled("plugins.inara.worker")).disbled: + logger.warning(f"Inara worker disabled via killswitch. ({res.reason})") continue for creds, event_list in events.items():