move DotDict to misc

This commit is contained in:
Izalia Mae 2022-11-06 01:11:54 -05:00
parent f713f54306
commit 3b85e2c2f2
2 changed files with 43 additions and 39 deletions

View file

@ -4,6 +4,8 @@ import yaml
from pathlib import Path
from urllib.parse import urlparse
from .misc import DotDict
relay_software_names = [
'activityrelay',
@ -13,45 +15,6 @@ relay_software_names = [
]
class DotDict(dict):
def __getattr__(self, k):
try:
return self[k]
except KeyError:
raise AttributeError(f'{self.__class__.__name__} object has no attribute {k}') from None
def __setattr__(self, k, v):
try:
if k in self._ignore_keys:
super().__setattr__(k, v)
except AttributeError:
pass
if k.startswith('_'):
super().__setattr__(k, v)
else:
self[k] = v
def __setitem__(self, k, v):
if type(v) == dict:
v = DotDict(v)
super().__setitem__(k, v)
def __delattr__(self, k):
try:
dict.__delitem__(self, k)
except KeyError:
raise AttributeError(f'{self.__class__.__name__} object has no attribute {k}') from None
class RelayConfig(DotDict):
apkeys = {
'host',

View file

@ -318,3 +318,44 @@ async def validate_signature(actor, http_request):
logging.debug(f'validates? {result}')
return result
class DotDict(dict):
def __getattr__(self, k):
try:
return self[k]
except KeyError:
raise AttributeError(f'{self.__class__.__name__} object has no attribute {k}') from None
def __setattr__(self, k, v):
if k.startswith('_'):
super().__setattr__(k, v)
else:
self[k] = v
def __setitem__(self, k, v):
if type(v) == dict:
v = DotDict(v)
super().__setitem__(k, v)
def __delattr__(self, k):
try:
dict.__delitem__(self, k)
except KeyError:
raise AttributeError(f'{self.__class__.__name__} object has no attribute {k}') from None
@classmethod
def new_from_json(cls, data):
return cls(json.loads(data))
def to_json(self, indent=None):
return json.dumps(self, indent=indent)