Commit graph jw-python/src/python/jw/util/stree/StringTree.py
Author SHA1 Message Date
df5202f4dc
stree: Reorder StringTree class members
Reorder the StringTree class members to follow the project coding
style:

1. Nested public/helper types (Error, Match) at the top.
2. Instance attribute annotations so mypy can track them before
   __adopt_children runs in __init__.
3. Private methods (__adopt_children, __set, __dump, __find) grouped
   together, with _dict_to_tree moved from module scope into the
   class as a @staticmethod.
4. Dunder methods (__repr__ through __len__) between private and public.
5. Public API preceded by # -- Public API comment, with __init__
   moved from the top into this section.
6. _dict_to_tree recursive call updated to StringTree._dict_to_tree()
   to avoid the bare-name lookup that mypy rejects on a @staticmethod.

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-08-12 23:10:12 +00:00
b4f053c892
stree: Switch StringTree from Mapping to MutableMapping
StringTree declares itself as a Mapping but implements __setitem__
which returns StringTree — neither correct for Mapping nor for
MutableMapping. This commit changes the base class to MutableMapping
and fixes the signatures to match the ABC contract: __setitem__ now
returns None, and __delitem__ is implemented by delegating to the
underlying OrderedDict.

The __setitem__ return value change is an API break for callers that
rely on its StringTree return. Those callers should use add() instead.
This is acceptable because the previous type signature was silently
incorrect — it violated the Mapping abstract base class contract.

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-08-12 23:10:12 +00:00
4d49b508bc
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-08-12 23:10:12 +00:00
0e6276e30e
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>
2026-08-12 23:10:12 +00:00
01942ac5bf
stree.StringTree: Exception -> StringTree.Error
Raise a specialized StringTree.Error rather than a bare Exception to
allow callers to distinguish where it came from.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-12 23:10:12 +00:00
2a6814d125
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>
2026-08-12 23:10:12 +00:00
b26e709082
stree.StringTree.read(): Add classmethod
Add StringTree.read() which is a frontend for serdes.read(), saving
one import from the caller's perspective.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-12 23:10:12 +00:00
aec0b0a3f5
stree.StringTree: Add type hinting
Add some type hinting to the StringTree class in the hope to make
some call semanticas clearer.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-12 23:10:12 +00:00
b4041018d2
stree.StringTree.__set(): path_ -> path
Some methods take a "path_" argument, which is not nice for callers.
Rename it to "path" and fix the fallout in the method bodies.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-12 23:10:12 +00:00
e9845b5a1f
Makefile: Include py-topdir.mk
Include py-topdir.mk, which entails loads of fallout from make check.
Fix it.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-06-12 04:55:47 +02:00
a2684dd601
jwutils: Move to jwutils -> jw.util
Move all implementation source code from the jwutils module to
jw.util. For compatibility with existing Python modules, keep a thin,
autogenerated compatibility shim under jwutils.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-06-11 23:10:25 +00:00
Renamed from tools/python/jwutils/stree/StringTree.py (Previous history)