moved AttrDict cast into static method

This commit is contained in:
BluBb_mADe 2019-06-04 18:38:50 +02:00
parent a62e7bc6de
commit 0ae5fd12d7
Signed by: Gardener
GPG Key ID: 1FAEB4540A5B4D1D

View File

@ -23,17 +23,17 @@ class AttrDict(dict):
def __getattr__(self, item): def __getattr__(self, item):
return self.setdefault(item, AttrDict()) return self.setdefault(item, AttrDict())
@staticmethod
def cast_to_ad(d): def from_dict_recur(d):
if not isinstance(d, AttrDict): if not isinstance(d, AttrDict):
d = AttrDict(d) d = AttrDict(d)
for k, v in dict(d.items()).items(): for k, v in dict(d.items()).items():
if " " in k: if " " in k:
del d[k] del d[k]
d[k.replace(" ", "_")] = v d[k.replace(" ", "_")] = v
if isinstance(v, dict): if isinstance(v, dict):
d[k] = cast_to_ad(v) d[k] = AttrDict.from_dict_recur(v)
return d return d
def sizeof_fmt(num, suffix='B'): def sizeof_fmt(num, suffix='B'):
@ -155,7 +155,7 @@ if __name__ == '__main__':
file_db = defaultdict(dict) file_db = defaultdict(dict)
confname = sys.argv[1] if sys.argv[1:] and os.path.isfile(sys.argv[1]) else 'config.yaml' confname = sys.argv[1] if sys.argv[1:] and os.path.isfile(sys.argv[1]) else 'config.yaml'
with open(confname) as cf: with open(confname) as cf:
conf = cast_to_ad(yaml.load(cf)) conf = AttrDict.from_dict_recur(yaml.load(cf))
if conf.url_hash_len > 31: if conf.url_hash_len > 31:
raise ValueError('url_hash_len can\'t be bigger than 31') raise ValueError('url_hash_len can\'t be bigger than 31')
if not set(conf.max_filesize.replace(' ', ''))\ if not set(conf.max_filesize.replace(' ', ''))\