2019-06-10 09:59:16 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from benedict.dicts.keypath import KeypathDict
|
2019-06-10 13:50:05 +00:00
|
|
|
# from benedict.dicts.io import IODict
|
2019-06-10 09:59:16 +00:00
|
|
|
from benedict.dicts.parse import ParseDict
|
|
|
|
|
2019-06-10 13:50:05 +00:00
|
|
|
import copy
|
2019-06-10 09:59:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class benedict(KeypathDict, ParseDict):
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(benedict, self).__init__(*args, **kwargs)
|
|
|
|
|
2019-06-10 13:50:05 +00:00
|
|
|
def __getattribute__(self, name):
|
|
|
|
attr = super(benedict, self).__getattribute__(name)
|
|
|
|
if hasattr(attr, '__call__'):
|
|
|
|
def attr_wrapper(*args, **kwargs):
|
|
|
|
value = attr(*args, **kwargs)
|
|
|
|
if name.startswith('_'):
|
|
|
|
return value
|
|
|
|
else:
|
|
|
|
return benedict._cast(value)
|
|
|
|
return attr_wrapper
|
2019-06-10 12:40:05 +00:00
|
|
|
else:
|
2019-06-10 13:50:05 +00:00
|
|
|
return attr
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return benedict._cast(
|
|
|
|
super(benedict, self).__getitem__(key))
|
2019-06-10 12:40:05 +00:00
|
|
|
|
2019-06-10 13:50:05 +00:00
|
|
|
@staticmethod
|
|
|
|
def _cast(value):
|
|
|
|
if isinstance(value, dict) and not isinstance(value, benedict):
|
|
|
|
return benedict(value)
|
|
|
|
else:
|
|
|
|
return value
|
2019-06-10 09:59:16 +00:00
|
|
|
|
|
|
|
def deepcopy(self):
|
2019-06-10 13:50:05 +00:00
|
|
|
return copy.deepcopy(self)
|
2019-06-10 09:59:16 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def fromkeys(cls, sequence, value=None):
|
2019-06-10 13:50:05 +00:00
|
|
|
return benedict._cast(
|
2019-06-10 09:59:16 +00:00
|
|
|
KeypathDict.fromkeys(sequence, value))
|
|
|
|
|