lib-registry 3.3.0__tar.gz → 3.3.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {lib_registry-3.3.0 → lib_registry-3.3.1}/CHANGELOG.md +10 -0
- {lib_registry-3.3.0 → lib_registry-3.3.1}/PKG-INFO +1 -1
- {lib_registry-3.3.0 → lib_registry-3.3.1}/docs/systemdesign/module_reference.md +10 -6
- {lib_registry-3.3.0 → lib_registry-3.3.1}/pyproject.toml +1 -1
- {lib_registry-3.3.0 → lib_registry-3.3.1}/src/lib_registry/__init__conf__.py +1 -1
- {lib_registry-3.3.0 → lib_registry-3.3.1}/src/lib_registry/_cli_helpers.py +134 -40
- {lib_registry-3.3.0 → lib_registry-3.3.1}/src/lib_registry/cli.py +8 -9
- {lib_registry-3.3.0 → lib_registry-3.3.1}/tests/test_walks.py +83 -5
- {lib_registry-3.3.0 → lib_registry-3.3.1}/.gitignore +0 -0
- {lib_registry-3.3.0 → lib_registry-3.3.1}/LICENSE +0 -0
- {lib_registry-3.3.0 → lib_registry-3.3.1}/README.md +0 -0
- {lib_registry-3.3.0 → lib_registry-3.3.1}/src/lib_registry/__init__.py +0 -0
- {lib_registry-3.3.0 → lib_registry-3.3.1}/src/lib_registry/__main__.py +0 -0
- {lib_registry-3.3.0 → lib_registry-3.3.1}/src/lib_registry/_helpers.py +0 -0
- {lib_registry-3.3.0 → lib_registry-3.3.1}/src/lib_registry/_winreg_setup.py +0 -0
- {lib_registry-3.3.0 → lib_registry-3.3.1}/src/lib_registry/exceptions.py +0 -0
- {lib_registry-3.3.0 → lib_registry-3.3.1}/src/lib_registry/py.typed +0 -0
- {lib_registry-3.3.0 → lib_registry-3.3.1}/src/lib_registry/records.py +0 -0
- {lib_registry-3.3.0 → lib_registry-3.3.1}/src/lib_registry/registry.py +0 -0
- {lib_registry-3.3.0 → lib_registry-3.3.1}/src/lib_registry/typed_click.py +0 -0
- {lib_registry-3.3.0 → lib_registry-3.3.1}/tests/conftest.py +0 -0
- {lib_registry-3.3.0 → lib_registry-3.3.1}/tests/test_cli.py +0 -0
- {lib_registry-3.3.0 → lib_registry-3.3.1}/tests/test_metadata.py +0 -0
- {lib_registry-3.3.0 → lib_registry-3.3.1}/tests/test_module_entry.py +0 -0
- {lib_registry-3.3.0 → lib_registry-3.3.1}/tests/test_packaging.py +0 -0
- {lib_registry-3.3.0 → lib_registry-3.3.1}/tests/test_platform_markers.py +0 -0
- {lib_registry-3.3.0 → lib_registry-3.3.1}/tests/test_remote.py +0 -0
|
@@ -5,6 +5,16 @@ the [Keep a Changelog](https://keepachangelog.com/) format.
|
|
|
5
5
|
|
|
6
6
|
## [Unreleased]
|
|
7
7
|
|
|
8
|
+
## [3.3.1] - 2026-11-24
|
|
9
|
+
|
|
10
|
+
### Fixed
|
|
11
|
+
|
|
12
|
+
- `list --recursive --json` and `tree --json` built the whole subtree as one document in memory
|
|
13
|
+
before printing it. They now print it while they walk, through the same streaming JSON writer
|
|
14
|
+
`search` and `diff` use, with byte-identical output. Measured over 1,000 and 8,000 keys (warm
|
|
15
|
+
runs, output to /dev/null): `list -r --json` peaked at 1,698 and 12,453 KiB before, 723 and
|
|
16
|
+
739 KiB now; `tree --json` at 625 and 3,140 KiB before, 588 and 764 KiB now.
|
|
17
|
+
|
|
8
18
|
## [3.3.0] - 2026-11-24
|
|
9
19
|
|
|
10
20
|
### Changed
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: lib_registry
|
|
3
|
-
Version: 3.3.
|
|
3
|
+
Version: 3.3.1
|
|
4
4
|
Summary: a more pythonic way to access the windows registry as winreg
|
|
5
5
|
Project-URL: Homepage, https://github.com/bitranox/lib_registry
|
|
6
6
|
Project-URL: Repository, https://github.com/bitranox/lib_registry.git
|
|
@@ -117,7 +117,7 @@ winreg / fake_winreg
|
|
|
117
117
|
* `_WINERROR_FILE_NOT_FOUND`, `_WINERROR_ACCESS_DENIED`,
|
|
118
118
|
`_WINERROR_INVALID_HANDLE`, `_WINERROR_NETWORK_PATH_NOT_FOUND`,
|
|
119
119
|
`_WINERROR_NO_MORE_DATA`, `_WINERROR_KEY_MARKED_FOR_DELETION`,
|
|
120
|
-
`_WINERROR_NETWORK_ADDRESS_INVALID`
|
|
120
|
+
`_WINERROR_NETWORK_ADDRESS_INVALID` - replace magic numbers in error handling
|
|
121
121
|
|
|
122
122
|
### Type Alias
|
|
123
123
|
|
|
@@ -139,7 +139,7 @@ compares like the tuple `winreg` returns.
|
|
|
139
139
|
* All `HKEY_*` hive constants (7 hives with short aliases)
|
|
140
140
|
* All `REG_*` type constants (12 types)
|
|
141
141
|
* All `KEY_*` access constants (12 constants including WOW64)
|
|
142
|
-
* `l_hive_names`
|
|
142
|
+
* `l_hive_names` - `frozenset` for O(1) hive name lookups
|
|
143
143
|
* Lookup tables, read-only `MappingProxyType` views: `main_key_hashed_by_name`,
|
|
144
144
|
`hive_names_hashed_by_int`, `reg_type_names_hashed_by_int`
|
|
145
145
|
|
|
@@ -222,8 +222,12 @@ overflow validation for REG_DWORD (0..4294967295).
|
|
|
222
222
|
* The CLI's recursive walkers (`list -r`, `search`, `tree`, `diff`, `copy -r`)
|
|
223
223
|
close the handles they open for a key once they leave it
|
|
224
224
|
(`_cli_helpers.released_handles`), so a walk holds one handle per level of
|
|
225
|
-
depth.
|
|
226
|
-
|
|
225
|
+
depth. Every `--json` output of these commands is written while it is read
|
|
226
|
+
(`_cli_helpers.echo_json`): `list` and `tree` describe their document with
|
|
227
|
+
`JsonStream` arrays that read the registry only as they are written, and
|
|
228
|
+
`search` and `diff` stream their results the same way. The output is the same
|
|
229
|
+
text `json.dumps(..., indent=2)` would print, and memory stays bounded by the
|
|
230
|
+
depth and the width of one key, not by the size of the subtree.
|
|
227
231
|
|
|
228
232
|
---
|
|
229
233
|
|
|
@@ -240,7 +244,7 @@ overflow validation for REG_DWORD (0..4294967295).
|
|
|
240
244
|
|
|
241
245
|
**Automated Tests:**
|
|
242
246
|
|
|
243
|
-
* `tests/test_cli.py`
|
|
247
|
+
* `tests/test_cli.py` - 30+ tests covering all 12 CLI commands including
|
|
244
248
|
forward-slash paths, error cases (missing values, overflow, invalid types),
|
|
245
249
|
and round-trip create/set/get/delete/export/import. Uses `temp_key` and
|
|
246
250
|
`temp_file` pytest fixtures for guaranteed teardown cleanup.
|
|
@@ -260,7 +264,7 @@ overflow validation for REG_DWORD (0..4294967295).
|
|
|
260
264
|
* `registry.py` and `cli.py` are excluded from pyright strict checking because
|
|
261
265
|
`fake_winreg` lacks type stubs.
|
|
262
266
|
* Coverage threshold at 70% due to Windows-only error handling branches.
|
|
263
|
-
* `save_key` / `load_key` parameter order is asymmetric (intentional
|
|
267
|
+
* `save_key` / `load_key` parameter order is asymmetric (intentional - see
|
|
264
268
|
CLAUDE.md Code Quality section).
|
|
265
269
|
|
|
266
270
|
**Future Enhancements:**
|
|
@@ -24,7 +24,7 @@ name = "lib_registry"
|
|
|
24
24
|
#: Human-readable summary shown in CLI help output.
|
|
25
25
|
title = "a more pythonic way to access the windows registry as winreg"
|
|
26
26
|
#: Current release version pulled from ``pyproject.toml`` by automation.
|
|
27
|
-
version = "3.3.
|
|
27
|
+
version = "3.3.1"
|
|
28
28
|
#: Repository homepage presented to users.
|
|
29
29
|
homepage = "https://github.com/bitranox/lib_registry"
|
|
30
30
|
#: Author attribution surfaced in CLI output.
|
|
@@ -2,15 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
The recursive walkers release the key handles they open once they leave a key
|
|
4
4
|
(see ``released_handles``), so a walk over a large subtree holds one handle per
|
|
5
|
-
level of depth instead of one per visited key.
|
|
6
|
-
|
|
5
|
+
level of depth instead of one per visited key. Every JSON output goes through
|
|
6
|
+
``echo_json``, which writes a document while its arrays are still being
|
|
7
|
+
produced (``JsonStream``), so list, tree, search and diff hold one path from
|
|
8
|
+
the root to the current item, never the whole result.
|
|
7
9
|
"""
|
|
8
10
|
|
|
9
11
|
import contextlib
|
|
10
12
|
import fnmatch
|
|
13
|
+
import itertools
|
|
11
14
|
import json
|
|
12
|
-
import
|
|
13
|
-
from collections.abc import Iterable, Iterator, Mapping
|
|
15
|
+
from collections.abc import Iterator, Mapping
|
|
14
16
|
from dataclasses import dataclass
|
|
15
17
|
from enum import Enum
|
|
16
18
|
from types import MappingProxyType
|
|
@@ -89,14 +91,86 @@ def value_to_json(value: RegValue) -> dict[str, object]:
|
|
|
89
91
|
return {"name": value.name, "data": json_safe(value.data), "type": get_value_type_as_string(value.reg_type)}
|
|
90
92
|
|
|
91
93
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
+
@dataclass(frozen=True)
|
|
95
|
+
class JsonStream:
|
|
96
|
+
"""A JSON array whose items are produced while ``echo_json`` writes it.
|
|
97
|
+
|
|
98
|
+
With ``omit_when_empty`` the key holding it is left out of its object when
|
|
99
|
+
the array has no item, as ``tree`` does for a key without subkeys.
|
|
100
|
+
"""
|
|
101
|
+
|
|
102
|
+
items: Iterator[object]
|
|
103
|
+
omit_when_empty: bool = False
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
_JSON_INDENT = " "
|
|
107
|
+
#: Size of the pieces ``echo_json`` hands to click: one write per piece, not one per token.
|
|
108
|
+
_JSON_PIECE_CHARS = 64 * 1024
|
|
109
|
+
_NO_ITEM = object()
|
|
94
110
|
|
|
95
|
-
|
|
96
|
-
|
|
111
|
+
|
|
112
|
+
def iter_json(value: object, level: int = 0) -> Iterator[str]:
|
|
113
|
+
"""Yield ``value`` as JSON text, the same characters as ``json.dumps(value, indent=2)``.
|
|
114
|
+
|
|
115
|
+
A ``JsonStream`` is written as an array whose items are pulled one at a time.
|
|
97
116
|
|
|
98
117
|
Examples:
|
|
99
|
-
>>>
|
|
118
|
+
>>> "".join(iter_json({"a": JsonStream(iter([1, {"b": []}])), "c": {}}))
|
|
119
|
+
'{\\n "a": [\\n 1,\\n {\\n "b": []\\n }\\n ],\\n "c": {}\\n}'
|
|
120
|
+
"""
|
|
121
|
+
if isinstance(value, JsonStream):
|
|
122
|
+
yield from _iter_json_array(value.items, level)
|
|
123
|
+
elif isinstance(value, list):
|
|
124
|
+
yield from _iter_json_array(iter(value), level)
|
|
125
|
+
elif isinstance(value, Mapping):
|
|
126
|
+
yield from _iter_json_object(value, level)
|
|
127
|
+
else:
|
|
128
|
+
yield json.dumps(value)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _iter_json_array(items: Iterator[object], level: int) -> Iterator[str]:
|
|
132
|
+
inner = _JSON_INDENT * (level + 1)
|
|
133
|
+
opened = False
|
|
134
|
+
for item in items:
|
|
135
|
+
yield f",\n{inner}" if opened else f"[\n{inner}"
|
|
136
|
+
opened = True
|
|
137
|
+
yield from iter_json(item, level + 1)
|
|
138
|
+
yield f"\n{_JSON_INDENT * level}]" if opened else "[]"
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _iter_json_object(mapping: Mapping[object, object], level: int) -> Iterator[str]:
|
|
142
|
+
inner = _JSON_INDENT * (level + 1)
|
|
143
|
+
opened = False
|
|
144
|
+
for key, value in mapping.items():
|
|
145
|
+
member = _present_member(value)
|
|
146
|
+
if member is _NO_ITEM:
|
|
147
|
+
continue
|
|
148
|
+
yield f",\n{inner}" if opened else f"{{\n{inner}"
|
|
149
|
+
opened = True
|
|
150
|
+
yield f"{json.dumps(key)}: "
|
|
151
|
+
yield from iter_json(member, level + 1)
|
|
152
|
+
yield f"\n{_JSON_INDENT * level}}}" if opened else "{}"
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def _present_member(value: object) -> object:
|
|
156
|
+
"""Return ``value`` to write, or ``_NO_ITEM`` for an empty ``omit_when_empty`` stream.
|
|
157
|
+
|
|
158
|
+
Finding out whether a stream is empty takes its first item, so a non-empty
|
|
159
|
+
one comes back as a new stream that starts with that item again.
|
|
160
|
+
"""
|
|
161
|
+
if not (isinstance(value, JsonStream) and value.omit_when_empty):
|
|
162
|
+
return value
|
|
163
|
+
first = next(value.items, _NO_ITEM)
|
|
164
|
+
if first is _NO_ITEM:
|
|
165
|
+
return _NO_ITEM
|
|
166
|
+
return JsonStream(itertools.chain([first], value.items))
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def echo_json(value: object) -> None:
|
|
170
|
+
"""Print ``value`` as ``json.dumps(value, indent=2)`` would, while it is produced.
|
|
171
|
+
|
|
172
|
+
Examples:
|
|
173
|
+
>>> echo_json(JsonStream(iter([{"a": 1}, {"b": [2]}])))
|
|
100
174
|
[
|
|
101
175
|
{
|
|
102
176
|
"a": 1
|
|
@@ -107,15 +181,18 @@ def echo_json_array(items: Iterable[Mapping[str, object]]) -> None:
|
|
|
107
181
|
]
|
|
108
182
|
}
|
|
109
183
|
]
|
|
110
|
-
>>>
|
|
184
|
+
>>> echo_json(JsonStream(iter([])))
|
|
111
185
|
[]
|
|
112
186
|
"""
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
187
|
+
piece: list[str] = []
|
|
188
|
+
size = 0
|
|
189
|
+
for chunk in iter_json(value):
|
|
190
|
+
piece.append(chunk)
|
|
191
|
+
size += len(chunk)
|
|
192
|
+
if size >= _JSON_PIECE_CHARS:
|
|
193
|
+
click.echo("".join(piece), nl=False)
|
|
194
|
+
piece, size = [], 0
|
|
195
|
+
click.echo("".join(piece))
|
|
119
196
|
|
|
120
197
|
|
|
121
198
|
@contextlib.contextmanager
|
|
@@ -162,21 +239,33 @@ def list_human(registry: Registry, key: str, *, show_keys: bool, show_values: bo
|
|
|
162
239
|
click.echo(f"{prefix}[{get_value_type_as_string(value.reg_type)}] {value.name} = {value.data}")
|
|
163
240
|
|
|
164
241
|
|
|
165
|
-
def
|
|
166
|
-
"""
|
|
167
|
-
|
|
242
|
+
def list_document(registry: Registry, key: str, *, show_keys: bool, show_values: bool, recursive: bool) -> dict[str, object]:
|
|
243
|
+
"""Describe the JSON document for ``list``: the key, its subkeys and its values.
|
|
244
|
+
|
|
245
|
+
The subkeys and values are ``JsonStream`` arrays, read from the registry
|
|
246
|
+
only while ``echo_json`` writes them.
|
|
247
|
+
"""
|
|
248
|
+
document: dict[str, object] = {"key": key}
|
|
249
|
+
if show_keys:
|
|
250
|
+
document["subkeys"] = JsonStream(_list_subkeys(registry, key, show_keys=show_keys, show_values=show_values, recursive=recursive))
|
|
251
|
+
if show_values:
|
|
252
|
+
document["values"] = JsonStream(_list_values(registry, key))
|
|
253
|
+
return document
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def _list_subkeys(registry: Registry, key: str, *, show_keys: bool, show_values: bool, recursive: bool) -> Iterator[object]:
|
|
168
257
|
with released_handles(registry, key):
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
258
|
+
for subkey in registry.subkeys(key):
|
|
259
|
+
if recursive:
|
|
260
|
+
yield list_document(registry, f"{key}\\{subkey}", show_keys=show_keys, show_values=show_values, recursive=recursive)
|
|
261
|
+
else:
|
|
262
|
+
yield subkey
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def _list_values(registry: Registry, key: str) -> Iterator[object]:
|
|
266
|
+
with released_handles(registry, key):
|
|
267
|
+
for value in registry.values(key):
|
|
268
|
+
yield value_to_json(value)
|
|
180
269
|
|
|
181
270
|
|
|
182
271
|
@dataclass(frozen=True)
|
|
@@ -229,17 +318,22 @@ def build_tree(registry: Registry, key: str, tree: Tree, max_depth: int, current
|
|
|
229
318
|
build_tree(registry, f"{key}\\{subkey}", branch, max_depth, current_depth + 1)
|
|
230
319
|
|
|
231
320
|
|
|
232
|
-
def
|
|
233
|
-
"""
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
321
|
+
def tree_document(registry: Registry, key: str, max_depth: int, current_depth: int = 0) -> dict[str, object]:
|
|
322
|
+
"""Describe the JSON document for ``tree``: the key's name and, below ``max_depth``, its children.
|
|
323
|
+
|
|
324
|
+
``children`` is a ``JsonStream`` left out of the output when the key has no subkey.
|
|
325
|
+
"""
|
|
326
|
+
document: dict[str, object] = {"name": key.rsplit("\\", 1)[-1] if "\\" in key else key}
|
|
327
|
+
if current_depth < max_depth:
|
|
328
|
+
document["children"] = JsonStream(_tree_children(registry, key, max_depth, current_depth), omit_when_empty=True)
|
|
329
|
+
return document
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def _tree_children(registry: Registry, key: str, max_depth: int, current_depth: int) -> Iterator[object]:
|
|
333
|
+
# A key that vanishes during the walk ends its own children, not the whole tree.
|
|
238
334
|
with released_handles(registry, key), contextlib.suppress(RegistryKeyNotFoundError):
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
result["children"] = children
|
|
242
|
-
return result
|
|
335
|
+
for subkey in registry.subkeys(key):
|
|
336
|
+
yield tree_document(registry, f"{key}\\{subkey}", max_depth, current_depth + 1)
|
|
243
337
|
|
|
244
338
|
|
|
245
339
|
def copy_values(registry: Registry, src_key: str, dst_key: str) -> None:
|
|
@@ -21,18 +21,19 @@ from rich.tree import Tree
|
|
|
21
21
|
from . import __init__conf__
|
|
22
22
|
from ._cli_helpers import (
|
|
23
23
|
_TYPE_NAMES,
|
|
24
|
+
JsonStream,
|
|
24
25
|
build_tree,
|
|
25
26
|
copy_recursive,
|
|
26
27
|
copy_values,
|
|
27
28
|
diff_keys,
|
|
28
29
|
diff_to_human,
|
|
29
|
-
|
|
30
|
+
echo_json,
|
|
30
31
|
echo_search_hit,
|
|
32
|
+
list_document,
|
|
31
33
|
list_human,
|
|
32
|
-
list_to_dict,
|
|
33
34
|
parse_value_and_type,
|
|
34
35
|
search_recursive,
|
|
35
|
-
|
|
36
|
+
tree_document,
|
|
36
37
|
value_to_json,
|
|
37
38
|
)
|
|
38
39
|
from ._helpers import get_key_as_string, get_value_type_as_string
|
|
@@ -231,8 +232,7 @@ def cli_list(ctx: click.Context, *, key: str, show_keys: bool, show_values: bool
|
|
|
231
232
|
registry = _make_registry(ctx)
|
|
232
233
|
show_all = not show_keys and not show_values
|
|
233
234
|
if _is_json(ctx):
|
|
234
|
-
|
|
235
|
-
click.echo(json.dumps(result, indent=2))
|
|
235
|
+
echo_json(list_document(registry, key, show_keys=show_all or show_keys, show_values=show_all or show_values, recursive=recursive))
|
|
236
236
|
else:
|
|
237
237
|
list_human(registry, key, show_keys=show_all or show_keys, show_values=show_all or show_values, recursive=recursive, indent=0)
|
|
238
238
|
|
|
@@ -295,7 +295,7 @@ def cli_search(ctx: click.Context, key: str, name_pattern: str | None, data_patt
|
|
|
295
295
|
registry = _make_registry(ctx)
|
|
296
296
|
hits = search_recursive(registry, key, name_pattern, data_pattern)
|
|
297
297
|
if _is_json(ctx):
|
|
298
|
-
|
|
298
|
+
echo_json(JsonStream(hit.to_json() for hit in hits))
|
|
299
299
|
else:
|
|
300
300
|
for hit in hits:
|
|
301
301
|
echo_search_hit(hit)
|
|
@@ -352,8 +352,7 @@ def cli_tree(ctx: click.Context, key: str, depth: int) -> None:
|
|
|
352
352
|
"""Display a visual tree of registry keys."""
|
|
353
353
|
registry = _make_registry(ctx)
|
|
354
354
|
if _is_json(ctx):
|
|
355
|
-
|
|
356
|
-
click.echo(json.dumps(result, indent=2))
|
|
355
|
+
echo_json(tree_document(registry, key, depth))
|
|
357
356
|
else:
|
|
358
357
|
label = get_key_as_string(key)
|
|
359
358
|
tree = Tree(f"[bold]{label}[/bold]")
|
|
@@ -401,7 +400,7 @@ def cli_diff(ctx: click.Context, key_a: str, key_b: str) -> None:
|
|
|
401
400
|
registry = _make_registry(ctx)
|
|
402
401
|
diffs = diff_keys(registry, key_a, key_b)
|
|
403
402
|
if _is_json(ctx):
|
|
404
|
-
|
|
403
|
+
echo_json(JsonStream(diff.to_json() for diff in diffs))
|
|
405
404
|
return
|
|
406
405
|
found = False
|
|
407
406
|
for diff in diffs:
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"""Recursive CLI walks release their key handles; the registry API returns named records."""
|
|
2
2
|
|
|
3
|
+
import io
|
|
3
4
|
import json
|
|
4
5
|
import uuid
|
|
5
6
|
from collections.abc import Callable, Iterator
|
|
7
|
+
from typing import cast
|
|
6
8
|
|
|
7
9
|
import pytest
|
|
8
10
|
from click.testing import CliRunner
|
|
@@ -35,8 +37,8 @@ def _handles_below(registry: Registry, key: str) -> list[tuple[int, str, int]]:
|
|
|
35
37
|
_WALKS: dict[str, Callable[[Registry, str], object]] = {
|
|
36
38
|
"search": lambda registry, key: list(helpers.search_recursive(registry, key, "*", None)),
|
|
37
39
|
"diff": lambda registry, key: list(helpers.diff_keys(registry, key, f"{key}\\a")),
|
|
38
|
-
"list": lambda registry, key: helpers.
|
|
39
|
-
"tree": lambda registry, key: helpers.
|
|
40
|
+
"list": lambda registry, key: "".join(helpers.iter_json(helpers.list_document(registry, key, show_keys=True, show_values=True, recursive=True))),
|
|
41
|
+
"tree": lambda registry, key: "".join(helpers.iter_json(helpers.tree_document(registry, key, 10))),
|
|
40
42
|
}
|
|
41
43
|
|
|
42
44
|
|
|
@@ -96,14 +98,76 @@ def test_the_registry_api_returns_named_records(tree_key: str) -> None:
|
|
|
96
98
|
assert type(resolve_key(tree_key)) is ResolvedKey
|
|
97
99
|
|
|
98
100
|
|
|
101
|
+
_JSON_DOCUMENTS: list[object] = [
|
|
102
|
+
[],
|
|
103
|
+
{},
|
|
104
|
+
[{}, [], [[]], {"a": {}}],
|
|
105
|
+
{"key": "k", "nested": {"list": [1, "x\ny", None, True, False, -0.5, 1e300]}},
|
|
106
|
+
{"unicode": "\u00e4\u4e2d\U0001f600", "quote": '"\\/', "ctrl": "\x00\x1f"},
|
|
107
|
+
[{"key": f"k{i}", "values": [{"name": "n", "data": i}]} for i in range(3)],
|
|
108
|
+
]
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
@pytest.mark.os_agnostic
|
|
112
|
+
@pytest.mark.parametrize("document", _JSON_DOCUMENTS)
|
|
113
|
+
def test_iter_json_writes_what_json_dumps_writes(document: object) -> None:
|
|
114
|
+
assert "".join(helpers.iter_json(document)) == json.dumps(document, indent=2)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _streamed(value: object) -> object:
|
|
118
|
+
"""Turn every list in ``value`` into a JsonStream, so the writer gets it item by item."""
|
|
119
|
+
if isinstance(value, list):
|
|
120
|
+
items: list[object] = [_streamed(item) for item in cast("list[object]", value)]
|
|
121
|
+
return helpers.JsonStream(iter(items))
|
|
122
|
+
if isinstance(value, dict):
|
|
123
|
+
return {key: _streamed(item) for key, item in cast("dict[str, object]", value).items()}
|
|
124
|
+
return value
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
@pytest.mark.os_agnostic
|
|
128
|
+
@pytest.mark.parametrize("document", _JSON_DOCUMENTS)
|
|
129
|
+
def test_a_streamed_array_is_written_like_a_list(document: object) -> None:
|
|
130
|
+
assert "".join(helpers.iter_json(_streamed(document))) == json.dumps(document, indent=2)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
@pytest.mark.os_agnostic
|
|
134
|
+
def test_an_empty_omit_when_empty_stream_leaves_its_key_out() -> None:
|
|
135
|
+
document = {"name": "x", "children": helpers.JsonStream(iter([]), omit_when_empty=True), "after": 1}
|
|
136
|
+
|
|
137
|
+
assert "".join(helpers.iter_json(document)) == json.dumps({"name": "x", "after": 1}, indent=2)
|
|
138
|
+
|
|
139
|
+
|
|
99
140
|
@pytest.mark.os_agnostic
|
|
100
|
-
|
|
101
|
-
|
|
141
|
+
def test_a_non_empty_omit_when_empty_stream_keeps_every_item() -> None:
|
|
142
|
+
document = {"children": helpers.JsonStream(iter([1, 2, 3]), omit_when_empty=True)}
|
|
143
|
+
|
|
144
|
+
assert "".join(helpers.iter_json(document)) == json.dumps({"children": [1, 2, 3]}, indent=2)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
@pytest.mark.os_agnostic
|
|
148
|
+
def test_the_writer_pulls_an_item_only_after_writing_the_one_before() -> None:
|
|
149
|
+
closed_items_seen_when_pulled: list[int] = []
|
|
150
|
+
written = io.StringIO()
|
|
151
|
+
|
|
152
|
+
def items() -> Iterator[object]:
|
|
153
|
+
for index in range(3):
|
|
154
|
+
closed_items_seen_when_pulled.append(written.getvalue().count("}"))
|
|
155
|
+
yield {"item": index}
|
|
156
|
+
|
|
157
|
+
for chunk in helpers.iter_json(helpers.JsonStream(items())):
|
|
158
|
+
written.write(chunk)
|
|
159
|
+
|
|
160
|
+
assert closed_items_seen_when_pulled == [0, 1, 2]
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
@pytest.mark.os_agnostic
|
|
164
|
+
@pytest.mark.parametrize("count", [0, 1, 3, 4000])
|
|
165
|
+
def test_echo_json_prints_what_json_dumps_prints(count: int) -> None:
|
|
102
166
|
items: list[dict[str, object]] = [{"key": f"k{i}", "nested": {"list": [i, "x\ny"]}} for i in range(count)]
|
|
103
167
|
|
|
104
168
|
@helpers.click.command()
|
|
105
169
|
def emit() -> None:
|
|
106
|
-
helpers.
|
|
170
|
+
helpers.echo_json(helpers.JsonStream(iter(items)))
|
|
107
171
|
|
|
108
172
|
result = CliRunner().invoke(emit)
|
|
109
173
|
|
|
@@ -141,3 +205,17 @@ def test_the_exported_lookup_tables_refuse_writes(name: str) -> None:
|
|
|
141
205
|
table[key] = table[key]
|
|
142
206
|
assert not hasattr(table, "__setitem__")
|
|
143
207
|
assert dict(table)[key] == table[key]
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
@pytest.mark.os_agnostic
|
|
211
|
+
@pytest.mark.parametrize("build", ["list", "tree"])
|
|
212
|
+
def test_a_json_document_reads_the_registry_only_while_it_is_written(tree_key: str, build: str) -> None:
|
|
213
|
+
with Registry() as registry:
|
|
214
|
+
if build == "list":
|
|
215
|
+
document = helpers.list_document(registry, tree_key, show_keys=True, show_values=True, recursive=True)
|
|
216
|
+
else:
|
|
217
|
+
document = helpers.tree_document(registry, tree_key, 10)
|
|
218
|
+
registry.create_key(f"{tree_key}\\added_after_build")
|
|
219
|
+
written = "".join(helpers.iter_json(document))
|
|
220
|
+
|
|
221
|
+
assert "added_after_build" in written
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|