Allow getattr in loaded json configs

This commit is contained in:
Tim Van Baak 2020-01-18 23:35:26 -08:00
parent d65eec12fe
commit 16c2f19ec5
1 changed files with 20 additions and 1 deletions

View File

@ -7,6 +7,20 @@ import os
# Module imports
from errors import ReadOnlyError
class AttrOrderedDict(OrderedDict):
"""An ordered dictionary with access via __getattr__"""
def __getattr__(self, key):
if key not in self:
raise AttributeError(key)
return self[key]
def __setattr__(self, key, value):
if key not in self:
raise AttributeError(key)
self[key] = value
class ReadOnlyOrderedDict(OrderedDict):
"""An ordered dictionary that cannot be modified"""
def __readonly__(self, *args, **kwargs):
@ -22,6 +36,11 @@ class ReadOnlyOrderedDict(OrderedDict):
self.update = self.__readonly__
self.setdefault = self.__readonly__
def __getattr__(self, key):
if key not in self:
raise AttributeError(key)
return self[key]
class open_lock():
def __init__(self, path, mode, lock_type):
self.fd = open(path, mode, encoding='utf8')
@ -57,7 +76,7 @@ class json_rw(open_ex):
self.config = None
def __enter__(self):
self.config = json.load(self.fd, object_pairs_hook=OrderedDict)
self.config = json.load(self.fd, object_pairs_hook=AttrOrderedDict)
return self.config
def __exit__(self, exc_type, exc_value, traceback):