lib-registry 3.2.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.2.0 → lib_registry-3.3.1}/CHANGELOG.md +31 -0
- {lib_registry-3.2.0 → lib_registry-3.3.1}/PKG-INFO +2 -2
- {lib_registry-3.2.0 → lib_registry-3.3.1}/docs/systemdesign/module_reference.md +12 -8
- {lib_registry-3.2.0 → lib_registry-3.3.1}/pyproject.toml +2 -2
- {lib_registry-3.2.0 → lib_registry-3.3.1}/src/lib_registry/__init__conf__.py +1 -1
- {lib_registry-3.2.0 → lib_registry-3.3.1}/src/lib_registry/_cli_helpers.py +136 -41
- {lib_registry-3.2.0 → lib_registry-3.3.1}/src/lib_registry/_winreg_setup.py +49 -39
- {lib_registry-3.2.0 → lib_registry-3.3.1}/src/lib_registry/cli.py +8 -9
- {lib_registry-3.2.0 → lib_registry-3.3.1}/src/lib_registry/registry.py +25 -5
- lib_registry-3.3.1/tests/test_remote.py +120 -0
- {lib_registry-3.2.0 → lib_registry-3.3.1}/tests/test_walks.py +99 -5
- {lib_registry-3.2.0 → lib_registry-3.3.1}/.gitignore +0 -0
- {lib_registry-3.2.0 → lib_registry-3.3.1}/LICENSE +0 -0
- {lib_registry-3.2.0 → lib_registry-3.3.1}/README.md +0 -0
- {lib_registry-3.2.0 → lib_registry-3.3.1}/src/lib_registry/__init__.py +0 -0
- {lib_registry-3.2.0 → lib_registry-3.3.1}/src/lib_registry/__main__.py +0 -0
- {lib_registry-3.2.0 → lib_registry-3.3.1}/src/lib_registry/_helpers.py +0 -0
- {lib_registry-3.2.0 → lib_registry-3.3.1}/src/lib_registry/exceptions.py +0 -0
- {lib_registry-3.2.0 → lib_registry-3.3.1}/src/lib_registry/py.typed +0 -0
- {lib_registry-3.2.0 → lib_registry-3.3.1}/src/lib_registry/records.py +0 -0
- {lib_registry-3.2.0 → lib_registry-3.3.1}/src/lib_registry/typed_click.py +0 -0
- {lib_registry-3.2.0 → lib_registry-3.3.1}/tests/conftest.py +0 -0
- {lib_registry-3.2.0 → lib_registry-3.3.1}/tests/test_cli.py +0 -0
- {lib_registry-3.2.0 → lib_registry-3.3.1}/tests/test_metadata.py +0 -0
- {lib_registry-3.2.0 → lib_registry-3.3.1}/tests/test_module_entry.py +0 -0
- {lib_registry-3.2.0 → lib_registry-3.3.1}/tests/test_packaging.py +0 -0
- {lib_registry-3.2.0 → lib_registry-3.3.1}/tests/test_platform_markers.py +0 -0
|
@@ -5,6 +5,37 @@ 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
|
+
|
|
18
|
+
## [3.3.0] - 2026-11-24
|
|
19
|
+
|
|
20
|
+
### Changed
|
|
21
|
+
|
|
22
|
+
- `fake_winreg>=1.11.0` (runtime, non-Windows); `uv.lock` refreshed.
|
|
23
|
+
- The exported lookup tables `main_key_hashed_by_name`, `hive_names_hashed_by_int` and
|
|
24
|
+
`reg_type_names_hashed_by_int` are now read-only `MappingProxyType` views, typed `Mapping`.
|
|
25
|
+
Every key parse in a process reads them, so a caller writing an entry changed how every later
|
|
26
|
+
key resolved. Reading, `in`, iteration, `.items()` and `dict(table)` work as before; item
|
|
27
|
+
assignment, `.update()`, `json.dumps(table)` and pickling no longer do (use `dict(table)`).
|
|
28
|
+
|
|
29
|
+
### Fixed
|
|
30
|
+
|
|
31
|
+
- Remote registry access did not reach the remote computer. `Registry(computer_name="X")` (and the
|
|
32
|
+
CLI's `--computer X`, which builds it that way) connected every hive to the LOCAL computer, so
|
|
33
|
+
reads and writes silently went to the local registry. `Registry("HKLM", computer_name="X")`
|
|
34
|
+
connected that one hive to X and then refused every further operation with
|
|
35
|
+
`RegistryError: can not connect to different Machines in the same scope`. Every connection now
|
|
36
|
+
goes to the computer the `Registry` was created for, and `create_key`, `delete_key` and
|
|
37
|
+
`delete_key_ex` work through that computer's connection instead of the bare local hive constant.
|
|
38
|
+
|
|
8
39
|
## [3.2.0] - 2026-11-24
|
|
9
40
|
|
|
10
41
|
### Changed
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: lib_registry
|
|
3
|
-
Version: 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
|
|
@@ -25,7 +25,7 @@ Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
|
25
25
|
Classifier: Topic :: System :: Systems Administration
|
|
26
26
|
Classifier: Typing :: Typed
|
|
27
27
|
Requires-Python: >=3.10
|
|
28
|
-
Requires-Dist: fake-winreg>=1.
|
|
28
|
+
Requires-Dist: fake-winreg>=1.11.0; sys_platform != 'win32'
|
|
29
29
|
Requires-Dist: rich-click>=1.9.9
|
|
30
30
|
Requires-Dist: rtoml>=0.14.0
|
|
31
31
|
Provides-Extra: dev
|
|
@@ -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,9 +139,9 @@ 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`
|
|
143
|
-
* Lookup
|
|
144
|
-
`reg_type_names_hashed_by_int`
|
|
142
|
+
* `l_hive_names` - `frozenset` for O(1) hive name lookups
|
|
143
|
+
* Lookup tables, read-only `MappingProxyType` views: `main_key_hashed_by_name`,
|
|
144
|
+
`hive_names_hashed_by_int`, `reg_type_names_hashed_by_int`
|
|
145
145
|
|
|
146
146
|
### CLI Commands
|
|
147
147
|
|
|
@@ -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:**
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "lib_registry"
|
|
3
|
-
version = "3.
|
|
3
|
+
version = "3.3.1"
|
|
4
4
|
description = "a more pythonic way to access the windows registry as winreg"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.10"
|
|
7
7
|
dependencies = [
|
|
8
8
|
"rich-click>=1.9.9",
|
|
9
9
|
"rtoml>=0.14.0",
|
|
10
|
-
"fake_winreg>=1.
|
|
10
|
+
"fake_winreg>=1.11.0; sys_platform!='win32'",
|
|
11
11
|
]
|
|
12
12
|
license = { text = "MIT" }
|
|
13
13
|
authors = [{ name = "bitranox", email = "bitranox@gmail.com" }]
|
|
@@ -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.
|
|
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,17 +2,20 @@
|
|
|
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
|
|
18
|
+
from types import MappingProxyType
|
|
16
19
|
from typing import NamedTuple
|
|
17
20
|
|
|
18
21
|
import rich_click as click
|
|
@@ -25,7 +28,7 @@ from .records import RegValue
|
|
|
25
28
|
from .registry import Registry
|
|
26
29
|
|
|
27
30
|
_TYPE_NAMES = list(reg_type_names_hashed_by_int.values())
|
|
28
|
-
_TYPE_NAME_TO_INT = {v: k for k, v in reg_type_names_hashed_by_int.items()}
|
|
31
|
+
_TYPE_NAME_TO_INT: Mapping[str, int] = MappingProxyType({v: k for k, v in reg_type_names_hashed_by_int.items()})
|
|
29
32
|
#: Largest value a REG_DWORD (unsigned 32-bit integer) can hold.
|
|
30
33
|
_REG_DWORD_MAX = 0xFFFFFFFF
|
|
31
34
|
|
|
@@ -88,14 +91,86 @@ def value_to_json(value: RegValue) -> dict[str, object]:
|
|
|
88
91
|
return {"name": value.name, "data": json_safe(value.data), "type": get_value_type_as_string(value.reg_type)}
|
|
89
92
|
|
|
90
93
|
|
|
91
|
-
|
|
92
|
-
|
|
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()
|
|
93
110
|
|
|
94
|
-
|
|
95
|
-
|
|
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.
|
|
96
116
|
|
|
97
117
|
Examples:
|
|
98
|
-
>>>
|
|
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]}])))
|
|
99
174
|
[
|
|
100
175
|
{
|
|
101
176
|
"a": 1
|
|
@@ -106,15 +181,18 @@ def echo_json_array(items: Iterable[Mapping[str, object]]) -> None:
|
|
|
106
181
|
]
|
|
107
182
|
}
|
|
108
183
|
]
|
|
109
|
-
>>>
|
|
184
|
+
>>> echo_json(JsonStream(iter([])))
|
|
110
185
|
[]
|
|
111
186
|
"""
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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))
|
|
118
196
|
|
|
119
197
|
|
|
120
198
|
@contextlib.contextmanager
|
|
@@ -161,21 +239,33 @@ def list_human(registry: Registry, key: str, *, show_keys: bool, show_values: bo
|
|
|
161
239
|
click.echo(f"{prefix}[{get_value_type_as_string(value.reg_type)}] {value.name} = {value.data}")
|
|
162
240
|
|
|
163
241
|
|
|
164
|
-
def
|
|
165
|
-
"""
|
|
166
|
-
|
|
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]:
|
|
167
257
|
with released_handles(registry, key):
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
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)
|
|
179
269
|
|
|
180
270
|
|
|
181
271
|
@dataclass(frozen=True)
|
|
@@ -228,17 +318,22 @@ def build_tree(registry: Registry, key: str, tree: Tree, max_depth: int, current
|
|
|
228
318
|
build_tree(registry, f"{key}\\{subkey}", branch, max_depth, current_depth + 1)
|
|
229
319
|
|
|
230
320
|
|
|
231
|
-
def
|
|
232
|
-
"""
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
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.
|
|
237
334
|
with released_handles(registry, key), contextlib.suppress(RegistryKeyNotFoundError):
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
result["children"] = children
|
|
241
|
-
return result
|
|
335
|
+
for subkey in registry.subkeys(key):
|
|
336
|
+
yield tree_document(registry, f"{key}\\{subkey}", max_depth, current_depth + 1)
|
|
242
337
|
|
|
243
338
|
|
|
244
339
|
def copy_values(registry: Registry, src_key: str, dst_key: str) -> None:
|
|
@@ -5,6 +5,8 @@ imported. All other modules import ``winreg`` from here.
|
|
|
5
5
|
"""
|
|
6
6
|
|
|
7
7
|
import sys
|
|
8
|
+
from collections.abc import Mapping
|
|
9
|
+
from types import MappingProxyType
|
|
8
10
|
|
|
9
11
|
is_platform_windows = sys.platform == "win32"
|
|
10
12
|
|
|
@@ -30,22 +32,26 @@ RegData = None | bytes | int | str | list[str]
|
|
|
30
32
|
# Constants
|
|
31
33
|
# ---------------------------------------------------------------------------
|
|
32
34
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
35
|
+
# The lookup tables are read-only views: every key parse in the process reads them, so one caller
|
|
36
|
+
# writing an entry would change how every later key is resolved.
|
|
37
|
+
main_key_hashed_by_name: Mapping[str, int] = MappingProxyType(
|
|
38
|
+
{
|
|
39
|
+
"hkey_classes_root": winreg.HKEY_CLASSES_ROOT,
|
|
40
|
+
"hkcr": winreg.HKEY_CLASSES_ROOT,
|
|
41
|
+
"hkey_current_config": winreg.HKEY_CURRENT_CONFIG,
|
|
42
|
+
"hkcc": winreg.HKEY_CURRENT_CONFIG,
|
|
43
|
+
"hkey_current_user": winreg.HKEY_CURRENT_USER,
|
|
44
|
+
"hkcu": winreg.HKEY_CURRENT_USER,
|
|
45
|
+
"hkey_dyn_data": winreg.HKEY_DYN_DATA,
|
|
46
|
+
"hkdd": winreg.HKEY_DYN_DATA,
|
|
47
|
+
"hkey_local_machine": winreg.HKEY_LOCAL_MACHINE,
|
|
48
|
+
"hklm": winreg.HKEY_LOCAL_MACHINE,
|
|
49
|
+
"hkey_performance_data": winreg.HKEY_PERFORMANCE_DATA,
|
|
50
|
+
"hkpd": winreg.HKEY_PERFORMANCE_DATA,
|
|
51
|
+
"hkey_users": winreg.HKEY_USERS,
|
|
52
|
+
"hku": winreg.HKEY_USERS,
|
|
53
|
+
}
|
|
54
|
+
)
|
|
49
55
|
|
|
50
56
|
l_hive_names: frozenset[str] = frozenset(
|
|
51
57
|
{
|
|
@@ -66,30 +72,34 @@ l_hive_names: frozenset[str] = frozenset(
|
|
|
66
72
|
}
|
|
67
73
|
)
|
|
68
74
|
|
|
69
|
-
hive_names_hashed_by_int:
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
hive_names_hashed_by_int: Mapping[int, str] = MappingProxyType(
|
|
76
|
+
{
|
|
77
|
+
winreg.HKEY_CLASSES_ROOT: "HKEY_CLASSES_ROOT",
|
|
78
|
+
winreg.HKEY_CURRENT_CONFIG: "HKEY_CURRENT_CONFIG",
|
|
79
|
+
winreg.HKEY_CURRENT_USER: "HKEY_CURRENT_USER",
|
|
80
|
+
winreg.HKEY_DYN_DATA: "HKEY_DYN_DATA",
|
|
81
|
+
winreg.HKEY_LOCAL_MACHINE: "HKEY_LOCAL_MACHINE",
|
|
82
|
+
winreg.HKEY_PERFORMANCE_DATA: "HKEY_PERFORMANCE_DATA",
|
|
83
|
+
winreg.HKEY_USERS: "HKEY_USERS",
|
|
84
|
+
}
|
|
85
|
+
)
|
|
78
86
|
|
|
79
|
-
reg_type_names_hashed_by_int:
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
87
|
+
reg_type_names_hashed_by_int: Mapping[int, str] = MappingProxyType(
|
|
88
|
+
{
|
|
89
|
+
winreg.REG_BINARY: "REG_BINARY",
|
|
90
|
+
winreg.REG_DWORD: "REG_DWORD",
|
|
91
|
+
winreg.REG_DWORD_BIG_ENDIAN: "REG_DWORD_BIG_ENDIAN",
|
|
92
|
+
winreg.REG_EXPAND_SZ: "REG_EXPAND_SZ",
|
|
93
|
+
winreg.REG_LINK: "REG_LINK",
|
|
94
|
+
winreg.REG_MULTI_SZ: "REG_MULTI_SZ",
|
|
95
|
+
winreg.REG_NONE: "REG_NONE",
|
|
96
|
+
winreg.REG_QWORD: "REG_QWORD",
|
|
97
|
+
winreg.REG_RESOURCE_LIST: "REG_RESOURCE_LIST",
|
|
98
|
+
winreg.REG_FULL_RESOURCE_DESCRIPTOR: "REG_FULL_RESOURCE_DESCRIPTOR",
|
|
99
|
+
winreg.REG_RESOURCE_REQUIREMENTS_LIST: "REG_RESOURCE_REQUIREMENTS_LIST",
|
|
100
|
+
winreg.REG_SZ: "REG_SZ",
|
|
101
|
+
}
|
|
102
|
+
)
|
|
93
103
|
|
|
94
104
|
# ---------------------------------------------------------------------------
|
|
95
105
|
# Windows error codes used in exception mapping
|
|
@@ -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:
|
|
@@ -9,6 +9,7 @@ See Also:
|
|
|
9
9
|
"""
|
|
10
10
|
|
|
11
11
|
import contextlib
|
|
12
|
+
import enum
|
|
12
13
|
import pathlib
|
|
13
14
|
from collections.abc import Iterator
|
|
14
15
|
from types import TracebackType
|
|
@@ -73,6 +74,15 @@ from .records import KeyInfo, RegValue, TypedValue
|
|
|
73
74
|
# ---------------------------------------------------------------------------
|
|
74
75
|
|
|
75
76
|
|
|
77
|
+
class _ThisRegistrysComputer(enum.Enum):
|
|
78
|
+
"""Default of ``_reg_connect``'s ``computer_name``: the computer this Registry is bound to.
|
|
79
|
+
|
|
80
|
+
A sentinel rather than ``None``, because ``None`` already means the local computer.
|
|
81
|
+
"""
|
|
82
|
+
|
|
83
|
+
SAME = enum.auto()
|
|
84
|
+
|
|
85
|
+
|
|
76
86
|
class Registry:
|
|
77
87
|
"""Pythonic interface for Windows registry access.
|
|
78
88
|
|
|
@@ -167,7 +177,11 @@ class Registry:
|
|
|
167
177
|
# Connection management
|
|
168
178
|
# ------------------------------------------------------------------
|
|
169
179
|
|
|
170
|
-
def _reg_connect(
|
|
180
|
+
def _reg_connect(
|
|
181
|
+
self,
|
|
182
|
+
key: str | int,
|
|
183
|
+
computer_name: str | _ThisRegistrysComputer | None = _ThisRegistrysComputer.SAME,
|
|
184
|
+
) -> winreg.HKEYType:
|
|
171
185
|
r"""Establish a connection to a predefined registry hive.
|
|
172
186
|
|
|
173
187
|
Connections are cached and reused. Users typically do not call this
|
|
@@ -175,7 +189,8 @@ class Registry:
|
|
|
175
189
|
|
|
176
190
|
Args:
|
|
177
191
|
key: Predefined HKEY_* constant or hive name string.
|
|
178
|
-
computer_name: Remote computer name or None for local.
|
|
192
|
+
computer_name: Remote computer name or None for local. Omitted, the
|
|
193
|
+
computer this Registry was created for (``self.computer_name``).
|
|
179
194
|
|
|
180
195
|
Returns:
|
|
181
196
|
Handle to the opened hive.
|
|
@@ -228,6 +243,8 @@ class Registry:
|
|
|
228
243
|
...
|
|
229
244
|
lib_registry.exceptions.RegistryNetworkConnectionError: ...
|
|
230
245
|
"""
|
|
246
|
+
if computer_name is _ThisRegistrysComputer.SAME:
|
|
247
|
+
computer_name = self.computer_name
|
|
231
248
|
try:
|
|
232
249
|
if self._is_computer_name_set and computer_name != self.computer_name:
|
|
233
250
|
raise RegistryError("can not connect to different Machines in the same scope")
|
|
@@ -239,6 +256,9 @@ class Registry:
|
|
|
239
256
|
|
|
240
257
|
hive_handle: winreg.HKEYType = winreg.ConnectRegistry(computer_name, hive_key)
|
|
241
258
|
self.reg_hive_connections[hive_key] = hive_handle
|
|
259
|
+
# The first connection binds the scope to its computer, so every later call that
|
|
260
|
+
# omits computer_name reaches the same one.
|
|
261
|
+
self.computer_name = computer_name
|
|
242
262
|
self._is_computer_name_set = True
|
|
243
263
|
return hive_handle
|
|
244
264
|
|
|
@@ -366,7 +386,7 @@ class Registry:
|
|
|
366
386
|
key_str = get_key_as_string(hive_key, hive_sub_key)
|
|
367
387
|
raise RegistryKeyCreateError(f'can not create key, the parent key to "{key_str}" does not exist')
|
|
368
388
|
|
|
369
|
-
new_key_handle: winreg.HKEYType = winreg.CreateKey(hive_key, hive_sub_key)
|
|
389
|
+
new_key_handle: winreg.HKEYType = winreg.CreateKey(self._reg_connect(hive_key), hive_sub_key)
|
|
370
390
|
self.reg_key_handles[(hive_key, hive_sub_key, winreg.KEY_ALL_ACCESS)] = new_key_handle
|
|
371
391
|
return new_key_handle
|
|
372
392
|
|
|
@@ -441,7 +461,7 @@ class Registry:
|
|
|
441
461
|
self.reg_key_handles[(hive_key, hive_sub_key, winreg.KEY_ALL_ACCESS)].Close()
|
|
442
462
|
|
|
443
463
|
try:
|
|
444
|
-
winreg.DeleteKey(hive_key, hive_sub_key)
|
|
464
|
+
winreg.DeleteKey(self._reg_connect(hive_key), hive_sub_key)
|
|
445
465
|
except OSError as e:
|
|
446
466
|
# WinError 1018: key marked for deletion
|
|
447
467
|
if hasattr(e, "winerror") and e.winerror == _WINERROR_KEY_MARKED_FOR_DELETION: # type: ignore[attr-defined]
|
|
@@ -1059,7 +1079,7 @@ class Registry:
|
|
|
1059
1079
|
"""
|
|
1060
1080
|
hive_key, hive_sub_key = resolve_key(key, sub_key)
|
|
1061
1081
|
try:
|
|
1062
|
-
winreg.DeleteKeyEx(hive_key, hive_sub_key, access, 0) # type: ignore[attr-defined]
|
|
1082
|
+
winreg.DeleteKeyEx(self._reg_connect(hive_key), hive_sub_key, access, 0) # type: ignore[attr-defined]
|
|
1063
1083
|
except FileNotFoundError:
|
|
1064
1084
|
key_str = get_key_as_string(key, sub_key)
|
|
1065
1085
|
raise RegistryKeyDeleteError(f'can not delete key "{key_str}"') from None
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"""A Registry bound to a remote computer sends every operation through that computer's connection.
|
|
2
|
+
|
|
3
|
+
fake_winreg cannot reach another machine: its ConnectRegistry raises for every computer name. These
|
|
4
|
+
tests replace ``ConnectRegistry`` - the one call that leaves the machine - with a recorder that
|
|
5
|
+
answers with the local hive, and record which handle the key-level calls receive. Everything below
|
|
6
|
+
that edge is the real code and the real fake registry.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import uuid
|
|
10
|
+
from collections.abc import Callable, Iterator
|
|
11
|
+
from dataclasses import dataclass, field
|
|
12
|
+
|
|
13
|
+
import pytest
|
|
14
|
+
from click.testing import CliRunner
|
|
15
|
+
|
|
16
|
+
import lib_registry.registry as registry_module
|
|
17
|
+
from lib_registry import Registry, RegistryError
|
|
18
|
+
from lib_registry import cli as cli_mod
|
|
19
|
+
|
|
20
|
+
_REMOTE = "remotehost"
|
|
21
|
+
_CURRENT_VERSION = "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass
|
|
25
|
+
class _Wire:
|
|
26
|
+
"""What crossed the edge: the computer names asked for and the handles the key calls got."""
|
|
27
|
+
|
|
28
|
+
computer_names: list[str | None] = field(default_factory=list[str | None])
|
|
29
|
+
connections: list[object] = field(default_factory=list[object])
|
|
30
|
+
key_call_bases: list[object] = field(default_factory=list[object])
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@pytest.fixture
|
|
34
|
+
def wire(monkeypatch: pytest.MonkeyPatch) -> _Wire:
|
|
35
|
+
"""Record ConnectRegistry and the first argument of CreateKey/DeleteKey/DeleteKeyEx."""
|
|
36
|
+
recorded = _Wire()
|
|
37
|
+
winreg = registry_module.winreg
|
|
38
|
+
real_connect = winreg.ConnectRegistry
|
|
39
|
+
|
|
40
|
+
def connect(computer_name: str | None, hive: int) -> object:
|
|
41
|
+
recorded.computer_names.append(computer_name)
|
|
42
|
+
handle = real_connect(None, hive)
|
|
43
|
+
recorded.connections.append(handle)
|
|
44
|
+
return handle
|
|
45
|
+
|
|
46
|
+
def recording(real: Callable[..., object]) -> Callable[..., object]:
|
|
47
|
+
def call(base: object, *args: object) -> object:
|
|
48
|
+
recorded.key_call_bases.append(base)
|
|
49
|
+
return real(base, *args)
|
|
50
|
+
|
|
51
|
+
return call
|
|
52
|
+
|
|
53
|
+
monkeypatch.setattr(winreg, "ConnectRegistry", connect)
|
|
54
|
+
for name in ("CreateKey", "DeleteKey", "DeleteKeyEx"):
|
|
55
|
+
monkeypatch.setattr(winreg, name, recording(getattr(winreg, name)))
|
|
56
|
+
return recorded
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@pytest.fixture
|
|
60
|
+
def remote_key() -> Iterator[str]:
|
|
61
|
+
"""Name a key private to this test under HKCU and remove whatever the test left there."""
|
|
62
|
+
key = f"HKCU\\Software\\lib_registry_remote_test_{uuid.uuid4().hex}"
|
|
63
|
+
yield key
|
|
64
|
+
with Registry() as local:
|
|
65
|
+
local.delete_key(key, delete_subkeys=True, missing_ok=True)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
@pytest.mark.os_agnostic
|
|
69
|
+
def test_a_registry_given_only_a_computer_name_reads_from_that_computer(wire: _Wire) -> None:
|
|
70
|
+
with Registry(computer_name=_REMOTE) as registry:
|
|
71
|
+
registry.get_value(_CURRENT_VERSION, "CurrentBuild")
|
|
72
|
+
|
|
73
|
+
assert wire.computer_names == [_REMOTE]
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@pytest.mark.os_agnostic
|
|
77
|
+
def test_a_registry_given_a_hive_and_a_computer_name_reads_from_that_computer(wire: _Wire) -> None:
|
|
78
|
+
with Registry("HKLM", computer_name=_REMOTE) as registry:
|
|
79
|
+
registry.get_value(_CURRENT_VERSION, "CurrentBuild")
|
|
80
|
+
list(registry.subkeys("HKEY_USERS"))
|
|
81
|
+
|
|
82
|
+
assert wire.computer_names == [_REMOTE, _REMOTE]
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@pytest.mark.os_agnostic
|
|
86
|
+
def test_creating_and_deleting_keys_on_a_remote_registry_uses_its_connection(wire: _Wire, remote_key: str) -> None:
|
|
87
|
+
with Registry(computer_name=_REMOTE) as registry:
|
|
88
|
+
registry.create_key(f"{remote_key}\\child", parents=True)
|
|
89
|
+
registry.set_value(f"{remote_key}\\child", "v", "x")
|
|
90
|
+
registry.delete_key(remote_key, delete_subkeys=True)
|
|
91
|
+
|
|
92
|
+
assert set(wire.computer_names) == {_REMOTE}
|
|
93
|
+
assert wire.key_call_bases
|
|
94
|
+
assert all(any(base is connection for connection in wire.connections) for base in wire.key_call_bases)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
@pytest.mark.os_agnostic
|
|
98
|
+
def test_the_first_remote_connection_binds_the_scope_to_that_computer(wire: _Wire) -> None:
|
|
99
|
+
with Registry() as registry:
|
|
100
|
+
registry._reg_connect("HKLM", computer_name=_REMOTE) # pyright: ignore[reportPrivateUsage] - the binding happens here
|
|
101
|
+
list(registry.subkeys("HKEY_USERS"))
|
|
102
|
+
|
|
103
|
+
assert wire.computer_names == [_REMOTE, _REMOTE]
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
@pytest.mark.os_agnostic
|
|
107
|
+
def test_a_registry_refuses_a_second_computer_in_the_same_scope(wire: _Wire) -> None:
|
|
108
|
+
with Registry(computer_name=_REMOTE) as registry:
|
|
109
|
+
registry.get_value(_CURRENT_VERSION, "CurrentBuild")
|
|
110
|
+
|
|
111
|
+
with pytest.raises(RegistryError, match="different Machines"):
|
|
112
|
+
registry._reg_connect("HKCU", computer_name="otherhost") # pyright: ignore[reportPrivateUsage] - the guard lives here
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
@pytest.mark.os_agnostic
|
|
116
|
+
def test_the_cli_computer_option_reads_from_that_computer(wire: _Wire) -> None:
|
|
117
|
+
result = CliRunner().invoke(cli_mod.cli, ["--computer", _REMOTE, "get", _CURRENT_VERSION, "CurrentBuild"])
|
|
118
|
+
|
|
119
|
+
assert result.exit_code == 0, result.output
|
|
120
|
+
assert wire.computer_names == [_REMOTE]
|
|
@@ -1,12 +1,15 @@
|
|
|
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
|
|
9
11
|
|
|
12
|
+
import lib_registry
|
|
10
13
|
from lib_registry import KeyInfo, Registry, RegValue, ResolvedKey, TypedValue, resolve_key, winreg
|
|
11
14
|
from lib_registry import _cli_helpers as helpers
|
|
12
15
|
|
|
@@ -34,8 +37,8 @@ def _handles_below(registry: Registry, key: str) -> list[tuple[int, str, int]]:
|
|
|
34
37
|
_WALKS: dict[str, Callable[[Registry, str], object]] = {
|
|
35
38
|
"search": lambda registry, key: list(helpers.search_recursive(registry, key, "*", None)),
|
|
36
39
|
"diff": lambda registry, key: list(helpers.diff_keys(registry, key, f"{key}\\a")),
|
|
37
|
-
"list": lambda registry, key: helpers.
|
|
38
|
-
"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))),
|
|
39
42
|
}
|
|
40
43
|
|
|
41
44
|
|
|
@@ -95,14 +98,76 @@ def test_the_registry_api_returns_named_records(tree_key: str) -> None:
|
|
|
95
98
|
assert type(resolve_key(tree_key)) is ResolvedKey
|
|
96
99
|
|
|
97
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
|
+
|
|
140
|
+
@pytest.mark.os_agnostic
|
|
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
|
+
|
|
98
163
|
@pytest.mark.os_agnostic
|
|
99
|
-
@pytest.mark.parametrize("count", [0, 1, 3])
|
|
100
|
-
def
|
|
164
|
+
@pytest.mark.parametrize("count", [0, 1, 3, 4000])
|
|
165
|
+
def test_echo_json_prints_what_json_dumps_prints(count: int) -> None:
|
|
101
166
|
items: list[dict[str, object]] = [{"key": f"k{i}", "nested": {"list": [i, "x\ny"]}} for i in range(count)]
|
|
102
167
|
|
|
103
168
|
@helpers.click.command()
|
|
104
169
|
def emit() -> None:
|
|
105
|
-
helpers.
|
|
170
|
+
helpers.echo_json(helpers.JsonStream(iter(items)))
|
|
106
171
|
|
|
107
172
|
result = CliRunner().invoke(emit)
|
|
108
173
|
|
|
@@ -125,3 +190,32 @@ def test_a_subkey_diff_refuses_a_value_kind() -> None:
|
|
|
125
190
|
def test_diff_kinds_keep_their_wire_values() -> None:
|
|
126
191
|
assert [kind.value for kind in helpers.DiffKind] == ["only_in_a", "only_in_b", "value_differs", "subkey_only_in_a", "subkey_only_in_b"]
|
|
127
192
|
assert json.dumps(helpers.DiffKind.ONLY_IN_A) == '"only_in_a"'
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
@pytest.mark.os_agnostic
|
|
196
|
+
@pytest.mark.parametrize("name", ["main_key_hashed_by_name", "hive_names_hashed_by_int", "reg_type_names_hashed_by_int"])
|
|
197
|
+
def test_the_exported_lookup_tables_refuse_writes(name: str) -> None:
|
|
198
|
+
table = getattr(lib_registry, name)
|
|
199
|
+
key = next(iter(table))
|
|
200
|
+
|
|
201
|
+
# A read-only mapping has no item assignment, so Python falls back to the sequence protocol:
|
|
202
|
+
# that raises TypeError, or IndexError first when the key is an HKEY constant too large to be
|
|
203
|
+
# an index. Either way nothing is written.
|
|
204
|
+
with pytest.raises((TypeError, IndexError)):
|
|
205
|
+
table[key] = table[key]
|
|
206
|
+
assert not hasattr(table, "__setitem__")
|
|
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
|