handle deletions in the top layer
This commit is contained in:
parent
0c1dc70217
commit
bccb87e660
@ -37,3 +37,7 @@ class Config(PropertyStack):
|
|||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
# in the config, all writes go to the json layer
|
# in the config, all writes go to the json layer
|
||||||
return self.storableConfig.__setitem__(key, value)
|
return self.storableConfig.__setitem__(key, value)
|
||||||
|
|
||||||
|
def __delitem__(self, key):
|
||||||
|
# all deletes go to the json layer, too
|
||||||
|
return self.storableConfig.__delitem__(key)
|
||||||
|
@ -6,6 +6,8 @@ import json
|
|||||||
|
|
||||||
|
|
||||||
class DynamicConfig(PropertyLayer):
|
class DynamicConfig(PropertyLayer):
|
||||||
|
_deleted = object()
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
try:
|
try:
|
||||||
@ -39,3 +41,21 @@ class DynamicConfig(PropertyLayer):
|
|||||||
jsonContent = json.dumps(self.__dict__(), indent=4, cls=Encoder)
|
jsonContent = json.dumps(self.__dict__(), indent=4, cls=Encoder)
|
||||||
with open(DynamicConfig._getSettingsFile(), "w") as file:
|
with open(DynamicConfig._getSettingsFile(), "w") as file:
|
||||||
file.write(jsonContent)
|
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}
|
||||||
|
Loading…
Reference in New Issue
Block a user