2019-06-10 09:59:16 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2020-01-30 11:56:00 +00:00
|
|
|
from benedict.dicts import KeylistDict
|
|
|
|
from benedict.utils import keypath_util
|
2019-06-10 09:59:16 +00:00
|
|
|
|
|
|
|
|
2020-01-30 11:56:00 +00:00
|
|
|
class KeypathDict(KeylistDict):
|
2019-06-10 09:59:16 +00:00
|
|
|
|
2020-01-07 15:00:06 +00:00
|
|
|
_keypath_separator = None
|
|
|
|
|
2019-06-10 09:59:16 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2019-10-29 15:30:01 +00:00
|
|
|
self._keypath_separator = kwargs.pop('keypath_separator', '.')
|
2019-06-10 09:59:16 +00:00
|
|
|
super(KeypathDict, self).__init__(*args, **kwargs)
|
2020-01-24 16:57:55 +00:00
|
|
|
keypath_util.check_keys(self, self._keypath_separator)
|
2019-10-29 15:30:01 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def keypath_separator(self):
|
|
|
|
return self._keypath_separator
|
|
|
|
|
|
|
|
@keypath_separator.setter
|
|
|
|
def keypath_separator(self, value):
|
2020-01-24 16:57:55 +00:00
|
|
|
keypath_util.check_keys(self, value)
|
2019-10-29 15:30:01 +00:00
|
|
|
self._keypath_separator = value
|
2019-06-10 09:59:16 +00:00
|
|
|
|
|
|
|
def __contains__(self, key):
|
2020-01-30 11:56:00 +00:00
|
|
|
return super(KeypathDict, self).__contains__(
|
|
|
|
self._parse_key(key))
|
2020-01-20 16:21:37 +00:00
|
|
|
|
2019-06-10 09:59:16 +00:00
|
|
|
def __delitem__(self, key):
|
2020-01-30 11:56:00 +00:00
|
|
|
super(KeypathDict, self).__delitem__(
|
|
|
|
self._parse_key(key))
|
2020-01-20 16:21:37 +00:00
|
|
|
|
2019-06-10 09:59:16 +00:00
|
|
|
def __getitem__(self, key):
|
2020-01-30 11:56:00 +00:00
|
|
|
return super(KeypathDict, self).__getitem__(
|
|
|
|
self._parse_key(key))
|
2020-01-20 16:21:37 +00:00
|
|
|
|
2019-06-10 09:59:16 +00:00
|
|
|
def __setitem__(self, key, value):
|
2020-01-24 16:57:55 +00:00
|
|
|
keypath_util.check_keys(value, self._keypath_separator)
|
2020-01-30 11:56:00 +00:00
|
|
|
super(KeypathDict, self).__setitem__(
|
|
|
|
self._parse_key(key), value)
|
|
|
|
|
|
|
|
def _parse_key(self, key):
|
|
|
|
keys = keypath_util.parse_keys(key, self._keypath_separator)
|
|
|
|
keys_count = len(keys)
|
|
|
|
if keys_count == 0:
|
|
|
|
return None
|
|
|
|
elif keys_count == 1:
|
|
|
|
return keys[0]
|
|
|
|
return keys
|
2020-01-20 16:21:37 +00:00
|
|
|
|
2019-06-10 09:59:16 +00:00
|
|
|
@classmethod
|
|
|
|
def fromkeys(cls, sequence, value=None):
|
2019-11-11 13:37:24 +00:00
|
|
|
d = cls()
|
2019-11-11 14:51:58 +00:00
|
|
|
for key in sequence:
|
2019-06-10 09:59:16 +00:00
|
|
|
d[key] = value
|
|
|
|
return d
|
|
|
|
|
|
|
|
def get(self, key, default=None):
|
2020-01-30 11:56:00 +00:00
|
|
|
return super(KeypathDict, self).get(
|
|
|
|
self._parse_key(key), default)
|
2020-01-20 16:21:37 +00:00
|
|
|
|
2020-01-24 16:57:55 +00:00
|
|
|
def pop(self, key, *args):
|
2020-01-30 11:56:00 +00:00
|
|
|
return super(KeypathDict, self).pop(
|
|
|
|
self._parse_key(key), *args)
|
2020-01-20 16:21:37 +00:00
|
|
|
|
2019-06-10 09:59:16 +00:00
|
|
|
def set(self, key, value):
|
2019-07-19 08:58:38 +00:00
|
|
|
self.__setitem__(key, value)
|
2019-06-10 09:59:16 +00:00
|
|
|
|
|
|
|
def setdefault(self, key, default=None):
|
|
|
|
if key not in self:
|
2020-01-30 11:56:00 +00:00
|
|
|
self.set(key, default)
|
2019-06-10 09:59:16 +00:00
|
|
|
return default
|
2020-01-24 16:57:55 +00:00
|
|
|
return self.__getitem__(key)
|
2019-07-09 10:55:34 +00:00
|
|
|
|
|
|
|
def update(self, other):
|
2020-01-24 16:57:55 +00:00
|
|
|
keypath_util.check_keys(other, self._keypath_separator)
|
2019-09-06 15:37:03 +00:00
|
|
|
super(KeypathDict, self).update(other)
|
2020-01-30 11:56:00 +00:00
|
|
|
|