-
Notifications
You must be signed in to change notification settings - Fork 4
/
winregistry.py
328 lines (288 loc) · 6.48 KB
/
winregistry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import winreg
from abc import ABC, abstractmethod
from collections import namedtuple
from datetime import datetime, timedelta
from types import TracebackType
from typing import Any, Self
RawKeyInfo = namedtuple('RawKeyInfo', ['child_keys_count', 'values_count', 'modified_at'])
RawValueInfo = namedtuple('RawValueInfo', ['data', 'type'])
class RegEntity(
ABC,
):
def __init__(
self,
hkey: winreg.HKEYType,
auto_refresh: bool,
) -> None:
self._hkey = hkey
self._raw_info = None
self._is_auto_refreshable = auto_refresh
self._auto_refresh()
@abstractmethod
def refresh(
self,
) -> None:
raise NotImplementedError
def _auto_refresh(
self,
) -> None:
if self._is_auto_refreshable:
self.refresh()
@property
def raw_info(
self,
) -> tuple:
if not self._raw_info:
self.refresh()
return self._raw_info
class Value(
RegEntity,
):
raw_info: RawValueInfo
def __init__(
self,
hkey: winreg.HKEYType,
name: str,
auto_refresh: bool = True,
) -> None:
self._name = name
super().__init__(
hkey=hkey,
auto_refresh=auto_refresh,
)
@classmethod
def from_index(
cls,
key: winreg.HKEYType,
index: int,
) -> Self:
value_name, _, _ = winreg.EnumValue(key, index)
return Value(
key,
name=value_name,
)
def refresh(
self,
) -> None:
self._raw_info = RawValueInfo(
*winreg.QueryValueEx(
self._hkey,
self._name,
)
)
@property
def name(
self,
) -> str:
return self._name
@property
def data(
self,
) -> Any:
"""
The data of the registry value item
"""
return self.raw_info.data
@data.setter
def data(
self,
value: Any,
) -> None:
"""
Stores data in the value field of an open registry key
"""
winreg.SetValueEx(
self._hkey,
self._name,
0,
self.type,
value,
)
self._auto_refresh()
@property
def type(
self,
) -> int:
"""
An integer giving the registry type for this value
"""
return self.raw_info.type
class Key(
RegEntity,
):
raw_info: RawKeyInfo
def __init__(
self,
hkey: winreg.HKEYType,
auto_refresh: bool = True,
) -> None:
super().__init__(
hkey=hkey,
auto_refresh=auto_refresh,
)
def refresh(
self,
) -> None:
self._raw_info = RawKeyInfo(
*winreg.QueryInfoKey(
self._hkey,
)
)
@classmethod
def from_index(
cls,
hkey: winreg.HKEYType,
index: int,
) -> Self:
sub_key = winreg.EnumKey(hkey, index)
return Key(
winreg.OpenKey(
key=hkey,
sub_key=sub_key,
reserved=0,
access=winreg.KEY_READ,
),
)
@property
def child_keys_count(
self,
) -> int:
"""
Number of sub keys this key has
"""
return self.raw_info.child_keys_count
@property
def values_count(
self,
) -> int:
"""
Number of values this key has
"""
return self.raw_info.values_count
@property
def modified_at(
self,
) -> datetime:
return datetime(1601, 1, 1) + timedelta(microseconds=self._raw_info.modified_at / 10)
@property
def child_keys_names(
self,
):
for index in range(self.child_keys_count):
yield winreg.EnumKey(self._hkey, index)
@property
def child_keys(
self,
):
for index in range(self.child_keys_count):
yield self.from_index(
hkey=self._hkey,
index=index,
)
@property
def values(
self,
):
for index in range(0, self.values_count):
yield Value.from_index(self._hkey, index)
def open_key(
self,
sub_key: str,
) -> Self:
return Key(
winreg.OpenKey(
key=self._hkey,
sub_key=sub_key,
reserved=0,
access=winreg.KEY_READ,
),
)
def create_key(
self,
sub_key: str,
) -> None:
winreg.CreateKeyEx(
key=self._hkey,
sub_key=sub_key,
reserved=0,
access=winreg.KEY_CREATE_SUB_KEY,
)
self._auto_refresh()
def delete_key(
self,
sub_key: str,
) -> None:
"""
Deletes the specified key
"""
winreg.DeleteKey(self._hkey, sub_key)
self._auto_refresh()
def read_value(
self,
name: str,
) -> Value:
"""
Retrieves data for a specified value name
"""
return Value(
hkey=self._hkey,
name=name,
)
def set_value(
self,
name: str,
type: int,
value: Any,
) -> None:
"""
Associates a value with a specified key
"""
winreg.SetValueEx(self._hkey, name, 0, type, value)
self._auto_refresh()
def delete_value(
self,
name: str,
) -> None:
"""
Removes a named value from a registry key
"""
winreg.DeleteValue(self._hkey, name)
self._auto_refresh()
def close(
self,
) -> None:
"""
Closes a previously opened registry key
"""
self._hkey.Close()
def __enter__(
self,
) -> Self:
return self
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
) -> None:
self.close()
def connect_registry(
key: int,
computer_name: str | None = None,
auto_refresh: bool = True,
) -> Key:
"""
Establishes a connection with registry
"""
return Key(
hkey=winreg.ConnectRegistry(computer_name, key),
auto_refresh=auto_refresh,
)
__all__ = [
'connect_registry',
'Key',
'Value',
'RawKeyInfo',
'RawValueInfo',
]
__version__ = '0.0.0'