-
Notifications
You must be signed in to change notification settings - Fork 31
/
winreg.py
91 lines (68 loc) · 2.71 KB
/
winreg.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
#
# Copyright (C) 2016 Satoru SATOH <ssato @ redhat.com>
# License: MIT
#
# pylint: disable=unused-argument
"""Windows(TM) Registry files backend.
.. versionadded:: 0.6.99
- Format to support: Windows registry,
- https://support.microsoft.com/en-us/kb/256986
- http://pogostick.net/~pnh/ntpasswd/WinReg.txt
- Requirements: python-registry, http://www.williballenthin.com/registry/
- Limitations:
- Dump[s] APIs are not implemented / supported yet.
- All values are encoded into base64 encoded and the output may be larger
than original.
- I'm not sure but maybe some data might be lost after load; I don't have
hosts runing Windows and cannot test this module works as expected
personally in actual.
- Special options: None
"""
from __future__ import absolute_import
import base64
import Registry.Registry as winreg
import anyconfig.backend.base
def decode_0(rval, to_container=dict):
"""
Decode :class:`Registry.Registry.RegistryValue` object.
:param rval: :class:`Registry.Registry.RegistryValue` object
:param to_container: any callable to make container
:return: Dict or dict-like object represents this object
"""
vtype = rval.value_type()
if vtype == winreg.RegSZ:
return to_container(mimetype="text/plain", value_type=vtype,
value=rval.value())
else:
return to_container(mimetype="application/octet-stream",
value_type=vtype,
value=base64.encodestring(rval.value()))
def decode(rkey, to_container=dict):
"""
Decode :class:`Registry.Registry.RegistryKey` object.
:param rkey: :class:`Registry.Registry.RegistryKey` object
:param to_container: any callable to make container
:return: Dict or dict-like object represents this object
"""
skeys = [decode(sk, to_container) for sk in rkey.subkeys()]
vals = [decode_0(v, to_container) for v in rkey.values()]
val = to_container(subkeys=skeys, values=vals,
timestamp=str(rkey.timestamp()))
return to_container({rkey.path(): val})
class Parser(anyconfig.backend.base.FromStreamLoader):
"""
Windows registry files parser.
"""
_type = "registry"
_extensions = [".reg"]
_open_flags = ('rb', 'wb')
def load_from_stream(self, stream, to_container, **options):
"""
Load config from given file like object `stream`.
:param stream: Config file or file like object
:param to_container: callble to make a container object
:param options: optional keyword arguments
:return: Dict-like object holding config parameters
"""
return decode(winreg.Registry(stream).root(), to_container)
# vim:sw=4:ts=4:et: