2020-04-10 17:55:36 +02:00
|
|
|
from __future__ import annotations
|
2025-05-29 12:05:10 +02:00
|
|
|
|
2026-06-10 13:28:35 +02:00
|
|
|
import fnmatch
|
|
|
|
|
import re
|
2024-12-15 15:35:29 +01:00
|
|
|
|
2025-05-29 12:05:10 +02:00
|
|
|
from collections import OrderedDict
|
2026-07-13 13:20:52 +02:00
|
|
|
from collections.abc import MutableMapping
|
2025-05-29 12:05:10 +02:00
|
|
|
from enum import Enum, auto
|
2026-07-13 13:48:44 +02:00
|
|
|
from typing import TYPE_CHECKING, override
|
2025-05-29 12:05:10 +02:00
|
|
|
|
2026-07-13 12:52:21 +02:00
|
|
|
from typing import overload
|
|
|
|
|
|
stree: Type annotate StringTree and serdes
The StringTree class and its companion serdes module have minimal
type annotations — only method signatures and a few instance
attributes were typed. This commit adds proper type annotations to
every parameter, return type, and local variable across both files.
cleanup_string() gains overloaded signatures so callers passing str
get str back, avoiding the need to widen the return type everywhere.
The matcher return type in __find is narrowed from Any to
Callable[[str, str], bool]. Casts are used sparingly where the
runtime guarantees a narrower type than the checker can infer (e.g.
self.value() always returns str when called without path, despite the
return annotation being str | None).
Variable annotations in StringTree.__set, get, __find, child_list, and
dump cover local bindings. serdes gains full parameter and return type
coverage plus annotations for all local variables in _cleanup_line,
parse, _read_lines_from_one_path, and _read_lines.
The get() default parameter remains Any because overriding
Mapping.get with a concrete type breaks the Liskov substitution
principle — Mapping.get is generic over the default type.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.80.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-13 13:19:10 +02:00
|
|
|
from typing import cast
|
|
|
|
|
|
2026-06-10 13:28:35 +02:00
|
|
|
if TYPE_CHECKING:
|
stree: Type annotate StringTree and serdes
The StringTree class and its companion serdes module have minimal
type annotations — only method signatures and a few instance
attributes were typed. This commit adds proper type annotations to
every parameter, return type, and local variable across both files.
cleanup_string() gains overloaded signatures so callers passing str
get str back, avoiding the need to widen the return type everywhere.
The matcher return type in __find is narrowed from Any to
Callable[[str, str], bool]. Casts are used sparingly where the
runtime guarantees a narrower type than the checker can infer (e.g.
self.value() always returns str when called without path, despite the
return annotation being str | None).
Variable annotations in StringTree.__set, get, __find, child_list, and
dump cover local bindings. serdes gains full parameter and return type
coverage plus annotations for all local variables in _cleanup_line,
parse, _read_lines_from_one_path, and _read_lines.
The get() default parameter remains Any because overriding
Mapping.get with a concrete type breaks the Liskov substitution
principle — Mapping.get is generic over the default type.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.80.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-13 13:19:10 +02:00
|
|
|
from collections.abc import Callable
|
2026-07-02 11:10:43 +02:00
|
|
|
from typing import Any, Iterator
|
2026-06-10 13:28:35 +02:00
|
|
|
|
2026-07-02 11:10:43 +02:00
|
|
|
from ..log import DEBUG, INFO, get_caller_pos, slog
|
2017-11-01 13:26:10 +01:00
|
|
|
|
2020-04-10 17:55:36 +02:00
|
|
|
def is_quoted(s: str) -> bool:
|
2017-11-01 13:26:10 +01:00
|
|
|
if isinstance(s, StringTree):
|
|
|
|
|
return False
|
|
|
|
|
s = s.strip()
|
|
|
|
|
if len(s) < 2:
|
|
|
|
|
return False
|
|
|
|
|
d = s[0]
|
2026-06-10 13:28:35 +02:00
|
|
|
if d == s[-1] and d in ['"', "'"]:
|
2017-11-01 13:26:10 +01:00
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
2026-07-13 12:52:21 +02:00
|
|
|
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
|
2020-04-10 17:55:36 +02:00
|
|
|
def cleanup_string(s: str) -> str:
|
2026-07-13 12:52:21 +02:00
|
|
|
...
|
|
|
|
|
|
|
|
|
|
@overload
|
|
|
|
|
def cleanup_string(s: StringTree) -> StringTree:
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
def cleanup_string(s: str | StringTree) -> str | StringTree:
|
2017-11-01 13:26:10 +01:00
|
|
|
if isinstance(s, StringTree):
|
|
|
|
|
return s
|
2026-07-13 12:52:21 +02:00
|
|
|
assert isinstance(s, str), f'Expected str or StringTree, got {type(s).__name__}'
|
2017-11-01 13:26:10 +01:00
|
|
|
s = s.strip()
|
|
|
|
|
if is_quoted(s):
|
2017-11-05 16:35:41 +01:00
|
|
|
return s[1:-1].replace('\\' + s[0], s[0])
|
2017-11-01 13:26:10 +01:00
|
|
|
return s
|
|
|
|
|
|
2026-07-13 13:20:52 +02:00
|
|
|
class StringTree(MutableMapping): # export
|
2017-11-01 13:26:10 +01:00
|
|
|
|
2026-07-02 11:57:09 +02:00
|
|
|
class Error(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
2026-07-13 13:48:44 +02:00
|
|
|
class Match(Enum):
|
|
|
|
|
Equal = auto()
|
|
|
|
|
RegExArg = auto()
|
|
|
|
|
RegExConf = auto()
|
|
|
|
|
GlobArg = auto()
|
|
|
|
|
GlobConf = auto()
|
2017-11-01 13:26:10 +01:00
|
|
|
|
2026-07-13 13:48:44 +02:00
|
|
|
__parent: StringTree | None
|
|
|
|
|
children: OrderedDict[str, StringTree]
|
|
|
|
|
content: str | None
|
2017-11-01 13:26:10 +01:00
|
|
|
|
2026-07-02 11:10:43 +02:00
|
|
|
def __adopt_children(self, parent: StringTree) -> None:
|
2025-01-28 05:08:50 +01:00
|
|
|
assert isinstance(parent, StringTree)
|
|
|
|
|
slog(DEBUG, f'At {self.content}: Adopting children of {parent}')
|
|
|
|
|
#parent.dump(INFO, "These children are added")
|
|
|
|
|
self.content = parent.content
|
|
|
|
|
for name, c in parent.children.items():
|
2026-06-10 13:28:35 +02:00
|
|
|
if name not in self.children.keys():
|
2025-01-28 05:08:50 +01:00
|
|
|
slog(DEBUG, f'At {self.content}: Adding new child {c}')
|
2020-04-04 11:35:26 +02:00
|
|
|
self.children[name] = c
|
|
|
|
|
else:
|
2025-01-28 05:08:50 +01:00
|
|
|
self.children[name].__adopt_children(c)
|
2020-04-04 11:35:26 +02:00
|
|
|
|
2026-07-02 11:10:43 +02:00
|
|
|
def __set(
|
stree: Type annotate StringTree and serdes
The StringTree class and its companion serdes module have minimal
type annotations — only method signatures and a few instance
attributes were typed. This commit adds proper type annotations to
every parameter, return type, and local variable across both files.
cleanup_string() gains overloaded signatures so callers passing str
get str back, avoiding the need to widen the return type everywhere.
The matcher return type in __find is narrowed from Any to
Callable[[str, str], bool]. Casts are used sparingly where the
runtime guarantees a narrower type than the checker can infer (e.g.
self.value() always returns str when called without path, despite the
return annotation being str | None).
Variable annotations in StringTree.__set, get, __find, child_list, and
dump cover local bindings. serdes gains full parameter and return type
coverage plus annotations for all local variables in _cleanup_line,
parse, _read_lines_from_one_path, and _read_lines.
The get() default parameter remains Any because overriding
Mapping.get with a concrete type breaks the Liskov substitution
principle — Mapping.get is generic over the default type.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.80.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-13 13:19:10 +02:00
|
|
|
self,
|
|
|
|
|
path: str,
|
|
|
|
|
content: str | StringTree | None,
|
|
|
|
|
split: bool = True
|
2026-07-02 11:10:43 +02:00
|
|
|
) -> StringTree:
|
2026-06-10 13:28:35 +02:00
|
|
|
slog(
|
|
|
|
|
DEBUG,
|
|
|
|
|
('At "{}": '.format(str(self.content)) if hasattr(self, "content") else "")
|
2026-07-02 11:04:51 +02:00
|
|
|
+ f'Setting "{path}" -> "{content}"'
|
2026-06-10 13:28:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Not sure what the idea behind this was. It often goes off, and all
|
|
|
|
|
# works fine without.
|
|
|
|
|
#assert self.content != str(content)
|
|
|
|
|
|
|
|
|
|
if content is not None and type(content) not in [str, StringTree]:
|
2026-07-02 11:57:09 +02:00
|
|
|
raise self.Error(
|
2026-06-10 13:28:35 +02:00
|
|
|
"Tried to add content of unsupported type {}".format(
|
|
|
|
|
type(content).__name__
|
|
|
|
|
)
|
|
|
|
|
)
|
2026-07-02 11:04:51 +02:00
|
|
|
if path is None:
|
2020-04-04 11:35:26 +02:00
|
|
|
if isinstance(content, str):
|
|
|
|
|
self.content = cleanup_string(content)
|
|
|
|
|
elif isinstance(content, StringTree):
|
2025-01-28 05:08:50 +01:00
|
|
|
self.__adopt_children(content)
|
2020-04-04 11:35:26 +02:00
|
|
|
else:
|
2026-07-02 11:57:09 +02:00
|
|
|
raise self.Error(
|
2026-06-10 13:28:35 +02:00
|
|
|
"Tried to add content of unsupported type {}".format(
|
|
|
|
|
type(content).__name__
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
slog(
|
|
|
|
|
DEBUG,
|
|
|
|
|
" -- content = >" + str(content) + "<, self.content = >" +
|
|
|
|
|
str(self.content) + "<"
|
|
|
|
|
)
|
2017-11-01 13:26:10 +01:00
|
|
|
return self
|
stree: Type annotate StringTree and serdes
The StringTree class and its companion serdes module have minimal
type annotations — only method signatures and a few instance
attributes were typed. This commit adds proper type annotations to
every parameter, return type, and local variable across both files.
cleanup_string() gains overloaded signatures so callers passing str
get str back, avoiding the need to widen the return type everywhere.
The matcher return type in __find is narrowed from Any to
Callable[[str, str], bool]. Casts are used sparingly where the
runtime guarantees a narrower type than the checker can infer (e.g.
self.value() always returns str when called without path, despite the
return annotation being str | None).
Variable annotations in StringTree.__set, get, __find, child_list, and
dump cover local bindings. serdes gains full parameter and return type
coverage plus annotations for all local variables in _cleanup_line,
parse, _read_lines_from_one_path, and _read_lines.
The get() default parameter remains Any because overriding
Mapping.get with a concrete type breaks the Liskov substitution
principle — Mapping.get is generic over the default type.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.80.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-13 13:19:10 +02:00
|
|
|
_path: str = cleanup_string(path)
|
2026-07-13 12:52:21 +02:00
|
|
|
if not _path:
|
2026-06-10 13:28:35 +02:00
|
|
|
|
|
|
|
|
#assert self.content is None or (
|
|
|
|
|
# isinstance(content, StringTree) and content.content == self.content
|
|
|
|
|
#)
|
|
|
|
|
|
2020-04-04 11:35:26 +02:00
|
|
|
if isinstance(content, StringTree):
|
2026-06-10 13:28:35 +02:00
|
|
|
#assert isinstance(content, StringTree), (
|
|
|
|
|
# f'Type: {type(content).__name__ }'
|
|
|
|
|
#)
|
2025-01-28 05:08:50 +01:00
|
|
|
self.__adopt_children(content)
|
2020-04-04 11:35:26 +02:00
|
|
|
else:
|
2025-01-28 05:08:50 +01:00
|
|
|
if self.content != content:
|
|
|
|
|
#self.content = cleanup_string(content)
|
|
|
|
|
slog(DEBUG, f'Changing content: "{self.content}" ->"{content}"')
|
2026-06-10 13:28:35 +02:00
|
|
|
assert (content != '"[a-zA-Z0-9+_*/-]"')
|
2025-01-28 05:08:50 +01:00
|
|
|
self.content = content
|
|
|
|
|
#assert(content != "'antlr_doesnt_understand_vertical_tab'")
|
|
|
|
|
#self.children[content] = StringTree(None, content)
|
2017-11-01 13:26:10 +01:00
|
|
|
return self
|
|
|
|
|
|
stree: Type annotate StringTree and serdes
The StringTree class and its companion serdes module have minimal
type annotations — only method signatures and a few instance
attributes were typed. This commit adds proper type annotations to
every parameter, return type, and local variable across both files.
cleanup_string() gains overloaded signatures so callers passing str
get str back, avoiding the need to widen the return type everywhere.
The matcher return type in __find is narrowed from Any to
Callable[[str, str], bool]. Casts are used sparingly where the
runtime guarantees a narrower type than the checker can infer (e.g.
self.value() always returns str when called without path, despite the
return annotation being str | None).
Variable annotations in StringTree.__set, get, __find, child_list, and
dump cover local bindings. serdes gains full parameter and return type
coverage plus annotations for all local variables in _cleanup_line,
parse, _read_lines_from_one_path, and _read_lines.
The get() default parameter remains Any because overriding
Mapping.get with a concrete type breaks the Liskov substitution
principle — Mapping.get is generic over the default type.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.80.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-13 13:19:10 +02:00
|
|
|
components: list[str] = _path.split('.') if split else [_path]
|
|
|
|
|
length: int = len(components)
|
2026-07-02 11:04:51 +02:00
|
|
|
#assert self.content is not None, f'Tried to set empty content to "{path}"'
|
2017-11-01 13:26:10 +01:00
|
|
|
|
stree: Type annotate StringTree and serdes
The StringTree class and its companion serdes module have minimal
type annotations — only method signatures and a few instance
attributes were typed. This commit adds proper type annotations to
every parameter, return type, and local variable across both files.
cleanup_string() gains overloaded signatures so callers passing str
get str back, avoiding the need to widen the return type everywhere.
The matcher return type in __find is narrowed from Any to
Callable[[str, str], bool]. Casts are used sparingly where the
runtime guarantees a narrower type than the checker can infer (e.g.
self.value() always returns str when called without path, despite the
return annotation being str | None).
Variable annotations in StringTree.__set, get, __find, child_list, and
dump cover local bindings. serdes gains full parameter and return type
coverage plus annotations for all local variables in _cleanup_line,
parse, _read_lines_from_one_path, and _read_lines.
The get() default parameter remains Any because overriding
Mapping.get with a concrete type breaks the Liskov substitution
principle — Mapping.get is generic over the default type.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.80.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-13 13:19:10 +02:00
|
|
|
nibble: str = components[0]
|
|
|
|
|
rest: str = '.'.join(components[1:])
|
2017-11-01 13:26:10 +01:00
|
|
|
if nibble not in self.children:
|
2026-06-10 13:28:35 +02:00
|
|
|
self.children[nibble] = StringTree('', content = nibble, parent = self)
|
|
|
|
|
if length > 1:
|
2026-07-02 11:57:09 +02:00
|
|
|
if len(rest) <= 0:
|
|
|
|
|
raise self.Error(f'{nibble} has missing child name')
|
2026-06-10 13:28:35 +02:00
|
|
|
return self.children[nibble].__set(rest, content = content)
|
2025-01-28 07:18:50 +01:00
|
|
|
# last component, a.k.a. leaf
|
2017-11-01 13:26:10 +01:00
|
|
|
if content is not None:
|
stree: Type annotate StringTree and serdes
The StringTree class and its companion serdes module have minimal
type annotations — only method signatures and a few instance
attributes were typed. This commit adds proper type annotations to
every parameter, return type, and local variable across both files.
cleanup_string() gains overloaded signatures so callers passing str
get str back, avoiding the need to widen the return type everywhere.
The matcher return type in __find is narrowed from Any to
Callable[[str, str], bool]. Casts are used sparingly where the
runtime guarantees a narrower type than the checker can infer (e.g.
self.value() always returns str when called without path, despite the
return annotation being str | None).
Variable annotations in StringTree.__set, get, __find, child_list, and
dump cover local bindings. serdes gains full parameter and return type
coverage plus annotations for all local variables in _cleanup_line,
parse, _read_lines_from_one_path, and _read_lines.
The get() default parameter remains Any because overriding
Mapping.get with a concrete type breaks the Liskov substitution
principle — Mapping.get is generic over the default type.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.80.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-13 13:19:10 +02:00
|
|
|
gc: StringTree = content if isinstance(content, StringTree) else StringTree(
|
2026-06-10 13:28:35 +02:00
|
|
|
'', content = content, parent = self.children[nibble]
|
|
|
|
|
)
|
2026-07-02 11:10:43 +02:00
|
|
|
if gc.content is None:
|
2026-07-13 12:52:21 +02:00
|
|
|
raise self.Error(f'Grandchild {nibble} has no content')
|
2025-01-28 07:18:50 +01:00
|
|
|
# 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:
|
|
|
|
|
del self.children[nibble].children[gc.content]
|
2020-04-04 11:35:26 +02:00
|
|
|
self.children[nibble].children[gc.content] = gc
|
2017-11-01 13:26:10 +01:00
|
|
|
return self.children[nibble]
|
|
|
|
|
|
stree: Type annotate StringTree and serdes
The StringTree class and its companion serdes module have minimal
type annotations — only method signatures and a few instance
attributes were typed. This commit adds proper type annotations to
every parameter, return type, and local variable across both files.
cleanup_string() gains overloaded signatures so callers passing str
get str back, avoiding the need to widen the return type everywhere.
The matcher return type in __find is narrowed from Any to
Callable[[str, str], bool]. Casts are used sparingly where the
runtime guarantees a narrower type than the checker can infer (e.g.
self.value() always returns str when called without path, despite the
return annotation being str | None).
Variable annotations in StringTree.__set, get, __find, child_list, and
dump cover local bindings. serdes gains full parameter and return type
coverage plus annotations for all local variables in _cleanup_line,
parse, _read_lines_from_one_path, and _read_lines.
The get() default parameter remains Any because overriding
Mapping.get with a concrete type breaks the Liskov substitution
principle — Mapping.get is generic over the default type.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.80.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-13 13:19:10 +02:00
|
|
|
def __dump(self, prio: int, indent: int = 0, **kwargs: Any) -> None:
|
2017-11-22 09:34:26 +01:00
|
|
|
caller = kwargs['caller'] if 'caller' in kwargs.keys() else get_caller_pos(1)
|
2026-06-10 13:28:35 +02:00
|
|
|
slog(prio, '|' + (' ' * indent) + str(self.content), caller = caller)
|
2017-11-01 13:26:10 +01:00
|
|
|
indent += 2
|
2019-05-19 13:15:27 +00:00
|
|
|
for name, child in self.children.items():
|
2026-06-10 13:28:35 +02:00
|
|
|
child.__dump(prio, indent = indent, caller = caller)
|
2017-11-01 13:26:10 +01:00
|
|
|
|
2026-07-13 12:52:21 +02:00
|
|
|
def __find(
|
|
|
|
|
self,
|
|
|
|
|
key: str | None,
|
|
|
|
|
val: str | None,
|
|
|
|
|
m: Match,
|
|
|
|
|
depth_first: bool,
|
|
|
|
|
) -> list[StringTree]:
|
2025-05-29 12:05:10 +02:00
|
|
|
|
stree: Type annotate StringTree and serdes
The StringTree class and its companion serdes module have minimal
type annotations — only method signatures and a few instance
attributes were typed. This commit adds proper type annotations to
every parameter, return type, and local variable across both files.
cleanup_string() gains overloaded signatures so callers passing str
get str back, avoiding the need to widen the return type everywhere.
The matcher return type in __find is narrowed from Any to
Callable[[str, str], bool]. Casts are used sparingly where the
runtime guarantees a narrower type than the checker can infer (e.g.
self.value() always returns str when called without path, despite the
return annotation being str | None).
Variable annotations in StringTree.__set, get, __find, child_list, and
dump cover local bindings. serdes gains full parameter and return type
coverage plus annotations for all local variables in _cleanup_line,
parse, _read_lines_from_one_path, and _read_lines.
The get() default parameter remains Any because overriding
Mapping.get with a concrete type breaks the Liskov substitution
principle — Mapping.get is generic over the default type.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.80.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-13 13:19:10 +02:00
|
|
|
def __children() -> None:
|
2025-05-29 12:05:10 +02:00
|
|
|
for name, child in self.children.items():
|
2026-06-10 13:28:35 +02:00
|
|
|
ret.extend(child.__find(key, val, m, depth_first))
|
2025-05-29 12:05:10 +02:00
|
|
|
|
stree: Type annotate StringTree and serdes
The StringTree class and its companion serdes module have minimal
type annotations — only method signatures and a few instance
attributes were typed. This commit adds proper type annotations to
every parameter, return type, and local variable across both files.
cleanup_string() gains overloaded signatures so callers passing str
get str back, avoiding the need to widen the return type everywhere.
The matcher return type in __find is narrowed from Any to
Callable[[str, str], bool]. Casts are used sparingly where the
runtime guarantees a narrower type than the checker can infer (e.g.
self.value() always returns str when called without path, despite the
return annotation being str | None).
Variable annotations in StringTree.__set, get, __find, child_list, and
dump cover local bindings. serdes gains full parameter and return type
coverage plus annotations for all local variables in _cleanup_line,
parse, _read_lines_from_one_path, and _read_lines.
The get() default parameter remains Any because overriding
Mapping.get with a concrete type breaks the Liskov substitution
principle — Mapping.get is generic over the default type.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.80.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-13 13:19:10 +02:00
|
|
|
def __self() -> None:
|
|
|
|
|
_val: str = cast('str', self.value())
|
|
|
|
|
_content: str | None = self.content
|
2025-05-29 12:05:10 +02:00
|
|
|
try:
|
stree: Type annotate StringTree and serdes
The StringTree class and its companion serdes module have minimal
type annotations — only method signatures and a few instance
attributes were typed. This commit adds proper type annotations to
every parameter, return type, and local variable across both files.
cleanup_string() gains overloaded signatures so callers passing str
get str back, avoiding the need to widen the return type everywhere.
The matcher return type in __find is narrowed from Any to
Callable[[str, str], bool]. Casts are used sparingly where the
runtime guarantees a narrower type than the checker can infer (e.g.
self.value() always returns str when called without path, despite the
return annotation being str | None).
Variable annotations in StringTree.__set, get, __find, child_list, and
dump cover local bindings. serdes gains full parameter and return type
coverage plus annotations for all local variables in _cleanup_line,
parse, _read_lines_from_one_path, and _read_lines.
The get() default parameter remains Any because overriding
Mapping.get with a concrete type breaks the Liskov substitution
principle — Mapping.get is generic over the default type.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.80.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-13 13:19:10 +02:00
|
|
|
if ((key == _content and matcher(cast('str', val), _val))
|
|
|
|
|
or (key is None and matcher(cast('str', val), _val))
|
2026-06-10 13:28:35 +02:00
|
|
|
or (key == _content and val is None)):
|
2025-05-29 12:05:10 +02:00
|
|
|
ret.append(self)
|
|
|
|
|
except Exception as e:
|
2026-07-13 12:52:21 +02:00
|
|
|
raise self.Error(str(e)).with_traceback(e.__traceback__) from e
|
2025-05-29 12:05:10 +02:00
|
|
|
|
stree: Type annotate StringTree and serdes
The StringTree class and its companion serdes module have minimal
type annotations — only method signatures and a few instance
attributes were typed. This commit adds proper type annotations to
every parameter, return type, and local variable across both files.
cleanup_string() gains overloaded signatures so callers passing str
get str back, avoiding the need to widen the return type everywhere.
The matcher return type in __find is narrowed from Any to
Callable[[str, str], bool]. Casts are used sparingly where the
runtime guarantees a narrower type than the checker can infer (e.g.
self.value() always returns str when called without path, despite the
return annotation being str | None).
Variable annotations in StringTree.__set, get, __find, child_list, and
dump cover local bindings. serdes gains full parameter and return type
coverage plus annotations for all local variables in _cleanup_line,
parse, _read_lines_from_one_path, and _read_lines.
The get() default parameter remains Any because overriding
Mapping.get with a concrete type breaks the Liskov substitution
principle — Mapping.get is generic over the default type.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.80.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-13 13:19:10 +02:00
|
|
|
def __select_matcher(m: StringTree.Match) -> Callable[[str, str], bool]:
|
2026-06-10 13:28:35 +02:00
|
|
|
match m:
|
|
|
|
|
case self.Match.Equal:
|
|
|
|
|
return lambda x, y: x == y
|
|
|
|
|
case self.Match.RegExArg:
|
|
|
|
|
return lambda x, y: re.search(x, y) is not None
|
|
|
|
|
case self.Match.RegExConf:
|
|
|
|
|
return lambda x, y: re.search(y, x) is not None
|
|
|
|
|
case self.Match.GlobArg:
|
|
|
|
|
return lambda x, y: fnmatch.fnmatch(y, x)
|
|
|
|
|
case self.Match.GlobConf:
|
|
|
|
|
return lambda x, y: fnmatch.fnmatch(x, y)
|
|
|
|
|
case _:
|
|
|
|
|
raise NotImplementedError(f'Matcher {m} is not yet implemented')
|
|
|
|
|
|
stree: Type annotate StringTree and serdes
The StringTree class and its companion serdes module have minimal
type annotations — only method signatures and a few instance
attributes were typed. This commit adds proper type annotations to
every parameter, return type, and local variable across both files.
cleanup_string() gains overloaded signatures so callers passing str
get str back, avoiding the need to widen the return type everywhere.
The matcher return type in __find is narrowed from Any to
Callable[[str, str], bool]. Casts are used sparingly where the
runtime guarantees a narrower type than the checker can infer (e.g.
self.value() always returns str when called without path, despite the
return annotation being str | None).
Variable annotations in StringTree.__set, get, __find, child_list, and
dump cover local bindings. serdes gains full parameter and return type
coverage plus annotations for all local variables in _cleanup_line,
parse, _read_lines_from_one_path, and _read_lines.
The get() default parameter remains Any because overriding
Mapping.get with a concrete type breaks the Liskov substitution
principle — Mapping.get is generic over the default type.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.80.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-13 13:19:10 +02:00
|
|
|
def __debug_matcher(
|
|
|
|
|
matcher: Callable[[str, str], bool],
|
|
|
|
|
log_level: int = DEBUG
|
|
|
|
|
) -> Callable[[str, str], bool]:
|
2026-06-10 13:28:35 +02:00
|
|
|
|
stree: Type annotate StringTree and serdes
The StringTree class and its companion serdes module have minimal
type annotations — only method signatures and a few instance
attributes were typed. This commit adds proper type annotations to
every parameter, return type, and local variable across both files.
cleanup_string() gains overloaded signatures so callers passing str
get str back, avoiding the need to widen the return type everywhere.
The matcher return type in __find is narrowed from Any to
Callable[[str, str], bool]. Casts are used sparingly where the
runtime guarantees a narrower type than the checker can infer (e.g.
self.value() always returns str when called without path, despite the
return annotation being str | None).
Variable annotations in StringTree.__set, get, __find, child_list, and
dump cover local bindings. serdes gains full parameter and return type
coverage plus annotations for all local variables in _cleanup_line,
parse, _read_lines_from_one_path, and _read_lines.
The get() default parameter remains Any because overriding
Mapping.get with a concrete type breaks the Liskov substitution
principle — Mapping.get is generic over the default type.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.80.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-13 13:19:10 +02:00
|
|
|
def __matcher(x: str, y: str) -> bool:
|
2025-05-29 12:05:10 +02:00
|
|
|
slog(log_level, f'Comparing "{x}" ~ "{y}"')
|
|
|
|
|
return matcher(x, y)
|
2026-06-10 13:28:35 +02:00
|
|
|
|
2025-05-29 12:05:10 +02:00
|
|
|
return __matcher
|
|
|
|
|
|
|
|
|
|
if not self.children:
|
|
|
|
|
return []
|
|
|
|
|
|
2026-06-10 13:28:35 +02:00
|
|
|
matcher = __select_matcher(m)
|
2025-05-29 12:05:10 +02:00
|
|
|
|
2025-10-13 12:45:51 +02:00
|
|
|
ret: list[StringTree] = []
|
2025-05-29 12:05:10 +02:00
|
|
|
|
|
|
|
|
if depth_first:
|
|
|
|
|
__children()
|
|
|
|
|
__self()
|
|
|
|
|
else:
|
|
|
|
|
__self()
|
|
|
|
|
__children()
|
|
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
2026-07-13 13:48:44 +02:00
|
|
|
@staticmethod
|
|
|
|
|
def _dict_to_tree(
|
|
|
|
|
parent: StringTree,
|
|
|
|
|
data: dict[str, str | Any],
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Recursively populate *parent* with entries from a nested dict."""
|
|
|
|
|
for key, value in data.items():
|
|
|
|
|
if isinstance(value, dict):
|
|
|
|
|
StringTree._dict_to_tree(parent.add(key), value)
|
|
|
|
|
else:
|
|
|
|
|
parent.add(key, str(value))
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return 'st:"{}"'.format(self.content)
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
|
return self.content or ''
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
def __getitem__(self, path: str) -> StringTree:
|
|
|
|
|
ret = self.get(path)
|
|
|
|
|
if ret is None:
|
|
|
|
|
raise self.Error(path)
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
def __setitem__(self, key: str, value: str) -> None:
|
|
|
|
|
self.__set(key, value)
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
def __iter__(self) -> Iterator[str]:
|
|
|
|
|
return iter(self.children)
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
def __delitem__(self, key: str) -> None:
|
|
|
|
|
if key in self.children:
|
|
|
|
|
del self.children[key]
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
def __len__(self) -> int:
|
|
|
|
|
return len(self.children)
|
|
|
|
|
|
|
|
|
|
# -- Public API
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self, path: str, content: str, parent: StringTree | None = None
|
|
|
|
|
) -> None:
|
|
|
|
|
slog(DEBUG, f'Constructing StringTree(path="{path}", content="{content}")')
|
|
|
|
|
self.__parent: StringTree | None = parent
|
|
|
|
|
self.children: OrderedDict[str, StringTree] = OrderedDict()
|
|
|
|
|
self.content: str | None = None
|
|
|
|
|
self.__set(path, content)
|
|
|
|
|
#assert self.content is not None
|
|
|
|
|
|
|
|
|
|
# root (content = [ symbols ])
|
|
|
|
|
# symbols (content = [ regex ])
|
|
|
|
|
# regex ( content ='[ \n\t\r]+' )
|
|
|
|
|
|
|
|
|
|
# root (content = root, children = [ symbols ])
|
|
|
|
|
# symbols (content = symbols, children = [ regex ])
|
|
|
|
|
# regex ( content = regex, children = [ '[ \n\t\r]+' ] )
|
|
|
|
|
# '[ \n\t\r]+)' ( content = '\n\t\r]+)', children = [] )
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def path(self) -> str:
|
|
|
|
|
if self.__parent is None:
|
|
|
|
|
return ''
|
|
|
|
|
prefix = self.__parent.path
|
|
|
|
|
if len(prefix):
|
|
|
|
|
prefix += '.'
|
|
|
|
|
return prefix + cast('str', self.content)
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def parent(self) -> StringTree | None:
|
|
|
|
|
return self.__parent
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def root(self) -> StringTree:
|
|
|
|
|
if self.__parent is None:
|
|
|
|
|
return self
|
|
|
|
|
return self.__parent.root
|
|
|
|
|
|
|
|
|
|
def set_content(self, content: str) -> None:
|
|
|
|
|
if content is None:
|
|
|
|
|
raise self.Error("Tried to set none content")
|
|
|
|
|
content = cleanup_string(content)
|
|
|
|
|
if len(content) == 0:
|
|
|
|
|
raise self.Error("Tried to set empty content")
|
|
|
|
|
self.content = content
|
|
|
|
|
|
|
|
|
|
def add(
|
|
|
|
|
self,
|
|
|
|
|
path: str,
|
|
|
|
|
content: str | StringTree | None = None,
|
|
|
|
|
split: bool = True
|
|
|
|
|
) -> StringTree:
|
|
|
|
|
slog(DEBUG, f'-- At "{self.content}": Adding "{path}" -> "{content}"')
|
|
|
|
|
return self.__set(path, content, split)
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
def get(self, path: str, default: Any = None) -> StringTree | None:
|
|
|
|
|
slog(DEBUG, 'looking for "{}" in "{}"'.format(path, self.content))
|
|
|
|
|
assert not isinstance(path, int)
|
|
|
|
|
_path: str = cleanup_string(path)
|
|
|
|
|
if len(_path) == 0:
|
|
|
|
|
slog(DEBUG, "returning myself")
|
|
|
|
|
return self
|
|
|
|
|
if is_quoted(path):
|
|
|
|
|
if _path not in self.children.keys():
|
|
|
|
|
return default
|
|
|
|
|
return self.children[_path]
|
|
|
|
|
components: list[str] = _path.split('.')
|
|
|
|
|
if len(components) == 0:
|
|
|
|
|
return self
|
|
|
|
|
name: str = cleanup_string(components[0])
|
|
|
|
|
if not hasattr(self, "children"):
|
|
|
|
|
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 default
|
|
|
|
|
rel_path: str = '.'.join(components[1:])
|
|
|
|
|
return self.children[name].get(rel_path)
|
|
|
|
|
|
|
|
|
|
def value(self, path: str | None = None, default: str | None = None) -> str | None:
|
|
|
|
|
if path:
|
|
|
|
|
child = self.get(path)
|
|
|
|
|
if child is None:
|
|
|
|
|
if default:
|
|
|
|
|
return default
|
|
|
|
|
return None
|
|
|
|
|
return child.value()
|
|
|
|
|
if len(self.children) == 0:
|
|
|
|
|
raise self.Error('tried to get value from leaf "{}"'.format(self.content))
|
|
|
|
|
slog(DEBUG, f'Returning value from children {self.children}')
|
|
|
|
|
return self.children[next(reversed(self.children))].content
|
|
|
|
|
|
|
|
|
|
def child_list(self, depth_first: bool = True) -> list[StringTree]:
|
|
|
|
|
if not depth_first:
|
|
|
|
|
raise self.Error(
|
|
|
|
|
'Tried to retrieve child list with breadth-first '
|
|
|
|
|
'search, not yet implemented'
|
|
|
|
|
)
|
|
|
|
|
r: list[StringTree] = []
|
|
|
|
|
for name, c in self.children.items():
|
|
|
|
|
r.append(c)
|
|
|
|
|
r.extend(c.child_list())
|
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
def dump(self, prio: int, *args: str, **kwargs: Any) -> None:
|
|
|
|
|
caller = kwargs['caller'] if 'caller' in kwargs.keys() else get_caller_pos(1)
|
|
|
|
|
msg: str = ''
|
|
|
|
|
if args is not None:
|
|
|
|
|
msg = ' ' + ' '.join(args) + ' '
|
|
|
|
|
slog(prio, ",------------" + msg + "----------- >", caller = caller)
|
|
|
|
|
self.__dump(prio, indent = 0, caller = caller)
|
|
|
|
|
slog(prio, "`------------" + msg + "----------- <", caller = caller)
|
|
|
|
|
|
2026-06-10 13:28:35 +02:00
|
|
|
def find(
|
|
|
|
|
self,
|
|
|
|
|
key: str | None = None,
|
|
|
|
|
val: str | None = None,
|
|
|
|
|
match: Match = Match.Equal,
|
|
|
|
|
depth_first: bool = False
|
2026-07-02 11:00:58 +02:00
|
|
|
) -> list[str]:
|
2026-06-10 13:28:35 +02:00
|
|
|
ret: list[str] = []
|
|
|
|
|
for node in self.__find(key, val, m = match, depth_first = depth_first):
|
|
|
|
|
if node.parent is None:
|
2026-07-13 12:52:21 +02:00
|
|
|
continue
|
2026-06-10 13:28:35 +02:00
|
|
|
ret.append(node.parent.path)
|
|
|
|
|
return ret
|
2026-07-02 11:00:58 +02:00
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def read(
|
|
|
|
|
cls,
|
|
|
|
|
path: str,
|
|
|
|
|
root_content: str = 'root',
|
stree: Type annotate StringTree and serdes
The StringTree class and its companion serdes module have minimal
type annotations — only method signatures and a few instance
attributes were typed. This commit adds proper type annotations to
every parameter, return type, and local variable across both files.
cleanup_string() gains overloaded signatures so callers passing str
get str back, avoiding the need to widen the return type everywhere.
The matcher return type in __find is narrowed from Any to
Callable[[str, str], bool]. Casts are used sparingly where the
runtime guarantees a narrower type than the checker can infer (e.g.
self.value() always returns str when called without path, despite the
return annotation being str | None).
Variable annotations in StringTree.__set, get, __find, child_list, and
dump cover local bindings. serdes gains full parameter and return type
coverage plus annotations for all local variables in _cleanup_line,
parse, _read_lines_from_one_path, and _read_lines.
The get() default parameter remains Any because overriding
Mapping.get with a concrete type breaks the Liskov substitution
principle — Mapping.get is generic over the default type.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.80.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-13 13:19:10 +02:00
|
|
|
log_prio: int = INFO,
|
|
|
|
|
paths_buf: list[str] | None = None
|
2026-07-02 11:00:58 +02:00
|
|
|
) -> StringTree:
|
|
|
|
|
from .serdes import read
|
|
|
|
|
return read(
|
|
|
|
|
path,
|
|
|
|
|
root_content = root_content,
|
|
|
|
|
log_prio = log_prio,
|
|
|
|
|
paths_buf = paths_buf
|
|
|
|
|
)
|
2026-07-13 13:48:44 +02:00
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def from_dict(
|
|
|
|
|
cls,
|
|
|
|
|
data: dict[str, str | Any],
|
|
|
|
|
root_content: str = 'root',
|
|
|
|
|
) -> StringTree:
|
|
|
|
|
"""Build a StringTree from a nested dict.
|
|
|
|
|
|
|
|
|
|
Dict values that are dicts represent sub-trees. All other values
|
|
|
|
|
are converted to strings via ``str()``. Keys are path components;
|
|
|
|
|
dotted keys are expanded just as with :meth:`add`.
|
|
|
|
|
|
|
|
|
|
Example::
|
|
|
|
|
|
|
|
|
|
>>> tree = StringTree.from_dict({'section': {'key': 'value'}})
|
|
|
|
|
>>> tree.value('section.key')
|
|
|
|
|
'value'
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
tree: StringTree = cls('', content = root_content)
|
|
|
|
|
cls._dict_to_tree(tree, data)
|
|
|
|
|
return tree
|