# Copyright (c) 2020, <NAME>

#from argparse import Namespace

__all__ = ['AttrDict', 'fromdict']

class AttrDict(dict):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
    def __getattr__(self, k):
        try:
            return self[k]
        except KeyError:
            raise AttributeError(k)
    def __setattr__(self, k, v):
        self[k] = fromdict(v) if type(v) is dict else v
    def __delattr__(self, k):
        del self[k]

def walk_obj(obj, parent_obj, key):
    if type(obj) is dict:
        for k in obj:
            walk_obj(obj[k], obj, k)
        parent_obj[key] = AttrDict(obj)
    elif type(obj) is list:
        for i, e in enumerate(obj):
            walk_obj(obj[i], obj, i)
        parent_obj[key] = obj

def fromdict(d):
    coll = [d]
    walk_obj(coll[0], coll, 0)
    return coll[0]

def fromjsonfile(fn, encoding=None):
    import json
    with open(fn, 'r', encoding=encoding) as f:
        return fromdict(json.load(f))
