55from dataclasses import dataclass , field
66from datetime import datetime
77from typing import Any
8+ import json
9+ from collections import UserDict
10+
811
912import pyttman
1013from pyttman .core .containers import MessageMixin , Reply
@@ -32,7 +35,6 @@ def depr_graceful(message: str, version: str):
3235 out = f"{ message } - This was deprecated in version { version } ."
3336 warnings .warn (out , DeprecationWarning )
3437
35-
3638class Settings :
3739 """
3840 Dataclass holding settings configured in the settings.py
@@ -48,7 +50,10 @@ class Settings:
4850 aren't valid settings.
4951 """
5052
51- def __init__ (self , ** kwargs ):
53+ def __init__ (self , dictionary = None , ** kwargs ):
54+ if dictionary is None :
55+ dictionary = {}
56+ self .__dict__ .update (dictionary )
5257 self .APPEND_LOG_FILES : bool = True
5358 self .MIDDLEWARE : dict | None = None
5459 self .ABILITIES : list | None = None
@@ -60,14 +65,27 @@ def __init__(self, **kwargs):
6065 self .LOG_FORMAT : str | None = None
6166 self .LOG_TO_STDOUT : bool = False
6267
63- [setattr ( self , k , v ) for k , v in kwargs .items ()
68+ [self . _set_attr ( k , v ) for k , v in kwargs .items ()
6469 if not inspect .ismodule (v )
6570 and not inspect .isfunction (v )]
6671
72+ def __getitem__ (self , item ):
73+ return self .__dict__ [item ]
74+
75+ def _set_attr (self , k , v ):
76+ tmp = v
77+ if isinstance (v , dict ):
78+ tmp = Settings ._dict_to_object (v )
79+
80+ setattr (self , k , tmp )
81+
6782 def __repr__ (self ):
6883 _attrs = {name : value for name , value in self .__dict__ .items ()}
6984 return f"Settings({ _attrs } )"
7085
86+ @staticmethod
87+ def _dict_to_object (dictionary ):
88+ return json .loads (json .dumps (dictionary ), object_hook = Settings )
7189
7290def _generate_name (name ):
7391 """
0 commit comments