From bccb87e66089b155dd0cd6ff1c5624bc8d2a865e Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Wed, 24 Feb 2021 23:04:57 +0100 Subject: [PATCH] handle deletions in the top layer --- owrx/config/__init__.py | 4 ++++ owrx/config/dynamic.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/owrx/config/__init__.py b/owrx/config/__init__.py index 46db653..bbd0d57 100644 --- a/owrx/config/__init__.py +++ b/owrx/config/__init__.py @@ -37,3 +37,7 @@ class Config(PropertyStack): def __setitem__(self, key, value): # in the config, all writes go to the json layer return self.storableConfig.__setitem__(key, value) + + def __delitem__(self, key): + # all deletes go to the json layer, too + return self.storableConfig.__delitem__(key) diff --git a/owrx/config/dynamic.py b/owrx/config/dynamic.py index ebb78d5..b636468 100644 --- a/owrx/config/dynamic.py +++ b/owrx/config/dynamic.py @@ -6,6 +6,8 @@ import json class DynamicConfig(PropertyLayer): + _deleted = object() + def __init__(self): super().__init__() try: @@ -39,3 +41,21 @@ class DynamicConfig(PropertyLayer): jsonContent = json.dumps(self.__dict__(), indent=4, cls=Encoder) with open(DynamicConfig._getSettingsFile(), "w") as file: file.write(jsonContent) + + def __delitem__(self, key): + self.__setitem__(key, DynamicConfig._deleted) + + def __contains__(self, item): + if not super().__contains__(item): + return False + if super().__getitem__(item) is DynamicConfig._deleted: + return False + return True + + def __getitem__(self, item): + if self.__contains__(item): + return super().__getitem__(item) + raise KeyError('Key "{key}" does not exist'.format(key=item)) + + def __dict__(self): + return {k: v for k, v in super().__dict__().items() if v is not DynamicConfig._deleted}