stree.StringTree: Derive from Mapping
This commit derives StringTree from Mapping, allowing it to be used as a drop-in-replacement for dictionaries returned from load_yaml(). Note that this is a breaking change, because it changes the return value of __getitem__() from str to StringTree. Either this or get() have to give, because the base class uses __getitem__() to implement get(), and returning different types from these, as it's currently the case would break caller expectations. To mitigate the pain of the breakage, __str__() now returns .content or "", and can be used to easily convert []'s return value to a string. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
b26e709082
commit
2a6814d125
1 changed files with 20 additions and 10 deletions
|
|
@ -4,6 +4,7 @@ import fnmatch
|
|||
import re
|
||||
|
||||
from collections import OrderedDict
|
||||
from collections.abc import Mapping
|
||||
from enum import Enum, auto
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
|
|
@ -40,7 +41,7 @@ def cleanup_string(s: str) -> str:
|
|||
return s[1:-1].replace('\\' + s[0], s[0])
|
||||
return s
|
||||
|
||||
class StringTree: # export
|
||||
class StringTree(Mapping): # export
|
||||
|
||||
def __init__(
|
||||
self, path: str, content: str, parent: StringTree | None = None
|
||||
|
|
@ -158,18 +159,27 @@ class StringTree: # export
|
|||
self.children[nibble].children[gc.content] = gc
|
||||
return self.children[nibble]
|
||||
|
||||
def __str__(self) -> str:
|
||||
def __repr__(self) -> str:
|
||||
return 'st:"{}"'.format(self.content)
|
||||
|
||||
def __getitem__(self, path: str) -> str:
|
||||
r = self.get(path)
|
||||
if r is None:
|
||||
def __str__(self) -> str:
|
||||
return self.content or ''
|
||||
|
||||
def __getitem__(self, path: str) -> StringTree:
|
||||
ret = self.get(path)
|
||||
if ret is None:
|
||||
raise KeyError(path)
|
||||
return r.value() # type: ignore
|
||||
return ret
|
||||
|
||||
def __setitem__(self, key: str, value: str) -> StringTree:
|
||||
return self.__set(key, value)
|
||||
|
||||
def __iter__(self) -> Iterator[str]:
|
||||
return iter(self.children)
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self.children)
|
||||
|
||||
def __dump(self, prio, indent = 0, **kwargs):
|
||||
caller = kwargs['caller'] if 'caller' in kwargs.keys() else get_caller_pos(1)
|
||||
slog(prio, '|' + (' ' * indent) + str(self.content), caller = caller)
|
||||
|
|
@ -209,7 +219,7 @@ class StringTree: # export
|
|||
slog(DEBUG, f'-- At "{self.content}": Adding "{path}" -> "{content}"')
|
||||
return self.__set(path, content, split)
|
||||
|
||||
def get(self, path: str) -> Optional[StringTree]:
|
||||
def get(self, path: str, default = None) -> StringTree | None:
|
||||
slog(DEBUG, 'looking for "{}" in "{}"'.format(path, self.content))
|
||||
assert not isinstance(path, int)
|
||||
_path = cleanup_string(path)
|
||||
|
|
@ -218,19 +228,19 @@ class StringTree: # export
|
|||
return self
|
||||
if is_quoted(path):
|
||||
if _path not in self.children.keys():
|
||||
return None
|
||||
return default
|
||||
return self.children[_path]
|
||||
components = _path.split('.')
|
||||
if len(components) == 0:
|
||||
return self
|
||||
name = cleanup_string(components[0])
|
||||
if not hasattr(self, "children"):
|
||||
return None
|
||||
return default
|
||||
if name not in self.children.keys():
|
||||
slog(DEBUG, "Name \"" + name + "\" is not in children of", self.content)
|
||||
for child in self.children:
|
||||
slog(DEBUG, "child = ", child)
|
||||
return None
|
||||
return default
|
||||
rel_path = '.'.join(components[1:])
|
||||
return self.children[name].get(rel_path)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue