stree.StringTree: Refactoring and fixes
This commit refactors the order of functions a little from used to using. It also fixes a bug: .find() could omit matching entries, fixes intricate case distinction, and doesn't ignore re.PatternError anylonger. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
499694e1f9
commit
0e6276e30e
1 changed files with 33 additions and 28 deletions
|
|
@ -8,20 +8,13 @@ from collections.abc import Mapping
|
|||
from enum import Enum, auto
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from typing import overload
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing import Any, Iterator
|
||||
|
||||
from ..log import DEBUG, INFO, get_caller_pos, slog
|
||||
|
||||
def quote(s: str) -> str:
|
||||
if is_quoted(s):
|
||||
return s
|
||||
s = s.strip()
|
||||
if len(s) > 0:
|
||||
if s[0] == '"':
|
||||
return "'" + s + "'"
|
||||
return '"' + s + '"'
|
||||
|
||||
def is_quoted(s: str) -> bool:
|
||||
if isinstance(s, StringTree):
|
||||
return False
|
||||
|
|
@ -33,9 +26,27 @@ def is_quoted(s: str) -> bool:
|
|||
return True
|
||||
return False
|
||||
|
||||
def quote(s: str) -> str:
|
||||
if is_quoted(s):
|
||||
return s
|
||||
s = s.strip()
|
||||
if len(s) > 0:
|
||||
if s[0] == '"':
|
||||
return "'" + s + "'"
|
||||
return '"' + s + '"'
|
||||
|
||||
@overload
|
||||
def cleanup_string(s: str) -> str:
|
||||
...
|
||||
|
||||
@overload
|
||||
def cleanup_string(s: StringTree) -> StringTree:
|
||||
...
|
||||
|
||||
def cleanup_string(s: str | StringTree) -> str | StringTree:
|
||||
if isinstance(s, StringTree):
|
||||
return s
|
||||
assert isinstance(s, str), f'Expected str or StringTree, got {type(s).__name__}'
|
||||
s = s.strip()
|
||||
if is_quoted(s):
|
||||
return s[1:-1].replace('\\' + s[0], s[0])
|
||||
|
|
@ -54,8 +65,6 @@ class StringTree(Mapping): # export
|
|||
self.children: OrderedDict[str, StringTree] = OrderedDict()
|
||||
self.content: str | None = None
|
||||
self.__set(path, content)
|
||||
|
||||
assert (hasattr(self, "content"))
|
||||
#assert self.content is not None
|
||||
|
||||
# root (content = [ symbols ])
|
||||
|
|
@ -116,9 +125,7 @@ class StringTree(Mapping): # export
|
|||
)
|
||||
return self
|
||||
_path = cleanup_string(path)
|
||||
components = _path.split('.') if split else [_path]
|
||||
length = len(components)
|
||||
if len(_path) == 0 or length == 0:
|
||||
if not _path:
|
||||
|
||||
#assert self.content is None or (
|
||||
# isinstance(content, StringTree) and content.content == self.content
|
||||
|
|
@ -139,6 +146,8 @@ class StringTree(Mapping): # export
|
|||
#self.children[content] = StringTree(None, content)
|
||||
return self
|
||||
|
||||
components = _path.split('.') if split else [_path]
|
||||
length = len(components)
|
||||
#assert self.content is not None, f'Tried to set empty content to "{path}"'
|
||||
|
||||
nibble = components[0]
|
||||
|
|
@ -155,7 +164,7 @@ class StringTree(Mapping): # export
|
|||
'', content = content, parent = self.children[nibble]
|
||||
)
|
||||
if gc.content is None:
|
||||
raise self.Error(f'Grandchild {nibble} has not content')
|
||||
raise self.Error(f'Grandchild {nibble} has no content')
|
||||
# Make sure no existing grand child is updated. It would reside too
|
||||
# far up in the grand child OrderedDict, we need it last
|
||||
if gc.content in self.children[nibble].children:
|
||||
|
|
@ -200,12 +209,6 @@ class StringTree(Mapping): # export
|
|||
prefix += '.'
|
||||
return prefix + self.content
|
||||
|
||||
def keys(self):
|
||||
return self.children.keys()
|
||||
|
||||
def items(self):
|
||||
return self.children.items()
|
||||
|
||||
def set_content(self, content):
|
||||
if content is None:
|
||||
raise self.Error("Tried to set none content")
|
||||
|
|
@ -299,8 +302,13 @@ class StringTree(Mapping): # export
|
|||
GlobArg = auto()
|
||||
GlobConf = auto()
|
||||
|
||||
def __find(self, key: str | None, val: str | None, m: Match,
|
||||
depth_first: bool) -> list[StringTree]:
|
||||
def __find(
|
||||
self,
|
||||
key: str | None,
|
||||
val: str | None,
|
||||
m: Match,
|
||||
depth_first: bool,
|
||||
) -> list[StringTree]:
|
||||
|
||||
def __children():
|
||||
for name, child in self.children.items():
|
||||
|
|
@ -315,10 +323,7 @@ class StringTree(Mapping): # export
|
|||
or (key == _content and val is None)):
|
||||
ret.append(self)
|
||||
except Exception as e:
|
||||
if isinstance(e, re.PatternError):
|
||||
pass
|
||||
else:
|
||||
raise self.Error(str(e)).with_traceback(e.__traceback__) from e
|
||||
raise self.Error(str(e)).with_traceback(e.__traceback__) from e
|
||||
|
||||
def __select_matcher(m: StringTree.Match) -> Any:
|
||||
match m:
|
||||
|
|
@ -369,7 +374,7 @@ class StringTree(Mapping): # export
|
|||
ret: list[str] = []
|
||||
for node in self.__find(key, val, m = match, depth_first = depth_first):
|
||||
if node.parent is None:
|
||||
break
|
||||
continue
|
||||
ret.append(node.parent.path)
|
||||
return ret
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue