handle deletions in the top layer

This commit is contained in:
Jakob Ketterl 2021-02-24 23:04:57 +01:00
parent 0c1dc70217
commit bccb87e660
2 changed files with 24 additions and 0 deletions

View File

@ -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)

View File

@ -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}