2019-06-10 09:59:16 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-09-10 14:58:26 +00:00
|
|
|
from benedict.dicts.io import IODict
|
2019-06-10 09:59:16 +00:00
|
|
|
from benedict.dicts.keypath import KeypathDict
|
|
|
|
from benedict.dicts.parse import ParseDict
|
2019-10-03 16:15:44 +00:00
|
|
|
from benedict.utils import dict_util
|
2019-06-10 09:59:16 +00:00
|
|
|
|
|
|
|
|
2019-06-17 15:33:28 +00:00
|
|
|
def benediction(method):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
value = method(*args, **kwargs)
|
2019-10-03 16:15:44 +00:00
|
|
|
if isinstance(value, dict) and not isinstance(value, benedict):
|
|
|
|
return benedict(value)
|
|
|
|
return value
|
2019-06-17 15:33:28 +00:00
|
|
|
return wrapper
|
2019-06-10 09:59:16 +00:00
|
|
|
|
2019-06-17 15:33:28 +00:00
|
|
|
|
2019-10-03 16:15:44 +00:00
|
|
|
class benedict(IODict, KeypathDict, ParseDict):
|
2019-06-10 09:59:16 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(benedict, self).__init__(*args, **kwargs)
|
|
|
|
|
2019-10-03 16:15:44 +00:00
|
|
|
def clean(self, strings=True, dicts=True, lists=True):
|
|
|
|
dict_util.clean(self, strings=strings, dicts=dicts, lists=lists)
|
2019-09-06 15:40:32 +00:00
|
|
|
|
|
|
|
def clone(self):
|
2019-10-03 16:15:44 +00:00
|
|
|
return dict_util.clone(self)
|
2019-09-06 15:40:32 +00:00
|
|
|
|
2019-06-17 15:33:28 +00:00
|
|
|
@benediction
|
|
|
|
def copy(self):
|
|
|
|
return super(benedict, self).copy()
|
|
|
|
|
|
|
|
def deepcopy(self):
|
2019-10-03 16:15:44 +00:00
|
|
|
return self.clone()
|
|
|
|
|
|
|
|
def deepupdate(self, other, *args):
|
|
|
|
self.merge(other, *args)
|
|
|
|
|
|
|
|
def dump(self, data=None):
|
|
|
|
return dict_util.dump(data or self)
|
2019-06-17 15:33:28 +00:00
|
|
|
|
2019-07-02 15:24:15 +00:00
|
|
|
def filter(self, predicate):
|
2019-10-03 16:15:44 +00:00
|
|
|
return dict_util.filter(self, predicate)
|
2019-07-02 13:15:31 +00:00
|
|
|
|
2019-09-12 15:04:22 +00:00
|
|
|
def flatten(self, separator='_'):
|
2019-10-03 16:15:44 +00:00
|
|
|
return dict_util.flatten(self, separator)
|
2019-09-12 15:04:22 +00:00
|
|
|
|
2019-06-17 15:33:28 +00:00
|
|
|
@classmethod
|
|
|
|
@benediction
|
|
|
|
def fromkeys(cls, sequence, value=None):
|
|
|
|
return KeypathDict.fromkeys(sequence, value)
|
2019-06-10 13:50:05 +00:00
|
|
|
|
2019-10-03 16:45:22 +00:00
|
|
|
@staticmethod
|
|
|
|
@benediction
|
2019-11-07 16:45:45 +00:00
|
|
|
def from_base64(s, subformat='json', encoding='utf-8', **kwargs):
|
|
|
|
return IODict.from_base64(s, subformat=subformat, encoding=encoding, **kwargs)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@benediction
|
|
|
|
def from_csv(s, columns=None, columns_row=True, **kwargs):
|
|
|
|
return IODict.from_csv(s, columns=columns, columns_row=columns_row, **kwargs)
|
2019-10-03 16:45:22 +00:00
|
|
|
|
2019-09-10 14:58:26 +00:00
|
|
|
@staticmethod
|
|
|
|
@benediction
|
|
|
|
def from_json(s, **kwargs):
|
|
|
|
return IODict.from_json(s, **kwargs)
|
|
|
|
|
2019-10-14 12:47:11 +00:00
|
|
|
@staticmethod
|
|
|
|
@benediction
|
|
|
|
def from_query_string(s, **kwargs):
|
|
|
|
return IODict.from_query_string(s, **kwargs)
|
|
|
|
|
2019-09-20 14:21:04 +00:00
|
|
|
@staticmethod
|
|
|
|
@benediction
|
|
|
|
def from_toml(s, **kwargs):
|
|
|
|
return IODict.from_toml(s, **kwargs)
|
|
|
|
|
2019-09-23 12:13:52 +00:00
|
|
|
@staticmethod
|
|
|
|
@benediction
|
|
|
|
def from_xml(s, **kwargs):
|
|
|
|
return IODict.from_xml(s, **kwargs)
|
|
|
|
|
2019-09-17 09:50:06 +00:00
|
|
|
@staticmethod
|
|
|
|
@benediction
|
|
|
|
def from_yaml(s, **kwargs):
|
|
|
|
return IODict.from_yaml(s, **kwargs)
|
|
|
|
|
2019-10-03 16:38:45 +00:00
|
|
|
def invert(self, flat=False):
|
|
|
|
return dict_util.invert(self, flat)
|
2019-06-10 09:59:16 +00:00
|
|
|
|
2019-10-03 16:38:45 +00:00
|
|
|
def items_sorted_by_keys(self, reverse=False):
|
|
|
|
return dict_util.items_sorted_by_keys(self, reverse=reverse)
|
2019-06-11 11:06:34 +00:00
|
|
|
|
2019-10-03 16:38:45 +00:00
|
|
|
def items_sorted_by_values(self, reverse=False):
|
|
|
|
return dict_util.items_sorted_by_values(self, reverse=reverse)
|
2019-06-11 11:06:34 +00:00
|
|
|
|
2019-10-29 15:31:01 +00:00
|
|
|
def keypaths(self):
|
|
|
|
sep = self._keypath_separator or '.'
|
|
|
|
return dict_util.keypaths(self, separator=sep)
|
|
|
|
|
2019-10-03 16:15:44 +00:00
|
|
|
def merge(self, other, *args):
|
2019-10-07 10:03:01 +00:00
|
|
|
dict_util.merge(self, other, *args)
|
2019-06-17 15:33:28 +00:00
|
|
|
|
2019-10-04 13:54:28 +00:00
|
|
|
def move(self, key_src, key_dest):
|
2019-10-07 10:03:01 +00:00
|
|
|
dict_util.move(self, key_src, key_dest)
|
2019-10-04 13:54:28 +00:00
|
|
|
|
2019-10-04 13:51:29 +00:00
|
|
|
def remove(self, keys, *args):
|
2019-10-07 10:03:01 +00:00
|
|
|
dict_util.remove(self, keys, *args)
|
2019-07-19 08:59:44 +00:00
|
|
|
|
2019-10-29 15:31:01 +00:00
|
|
|
def standardize(self):
|
|
|
|
dict_util.standardize(self)
|
|
|
|
|
2019-10-04 13:52:14 +00:00
|
|
|
def subset(self, keys, *args):
|
2019-10-07 10:03:01 +00:00
|
|
|
return dict_util.subset(self, keys, *args)
|
2019-10-04 13:53:54 +00:00
|
|
|
|
|
|
|
def swap(self, key1, key2):
|
2019-10-07 10:03:01 +00:00
|
|
|
dict_util.swap(self, key1, key2)
|
2019-10-07 10:03:46 +00:00
|
|
|
|
2019-10-29 15:31:01 +00:00
|
|
|
def traverse(self, callback):
|
|
|
|
dict_util.traverse(self, callback)
|
|
|
|
|
2019-10-07 10:03:46 +00:00
|
|
|
def unique(self):
|
|
|
|
dict_util.unique(self)
|