From 3b85e2c2f2141c8cafdc4fdbebbb049baa078e12 Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Sun, 6 Nov 2022 01:11:54 -0500 Subject: [PATCH] move DotDict to misc --- relay/config.py | 41 ++--------------------------------------- relay/misc.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 39 deletions(-) diff --git a/relay/config.py b/relay/config.py index 951b6f3..b363580 100644 --- a/relay/config.py +++ b/relay/config.py @@ -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', diff --git a/relay/misc.py b/relay/misc.py index 9161f53..7323633 100644 --- a/relay/misc.py +++ b/relay/misc.py @@ -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)