-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.py
executable file
·102 lines (77 loc) · 2.63 KB
/
registry.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
#!/usr/bin/env python
"""This is an *actually* pythonic way to access (read from, add write
functionality later) the registry
"""
from _winreg import *
from os.path import join as pjoin
class _base:
def __init__(self):
pass
def get_subkey(self, path):
return Key(path, self)
class Value:
def __init__(self, name, content, vtype):
self.name = name.strip() or "@"
self.content = content
self.vtype = vtype
def __repr__(self):
return "<key value of %s is %s of type %s>" % (self.name,
self.content,
self.vtype)
class Key(_base):
""" Keys don't exist without a parent registry connection.
"""
def __init__(self, path, _parent):
self._parent = _parent
self.con = OpenKey(self._parent.con, path)
if "path" in _parent.__dict__:
self.path = pjoin(self._parent.path, path)
else:
self.path = path
self.name = path.split('\\')[-1]
self.num_keys, self.num_values, self.last_mod = \
QueryInfoKey(self.con)
def get_values(self):
for i in range(self.num_values):
yield Value(*EnumValue(self.con,i))
def get_value(self, name=None):
if name:
for i in range(self.num_values):
v = Value(*EnumValue(self.con,i))
if v.name == name:
return v
raise ValueError('Value named %s not found' % name)
return Value(*EnumValue(self.con,0))
def get_subkeys(self):
for i in range(self.num_keys):
subkey = EnumKey(self.con,i)
yield Key(subkey, self)
def create_subkey(self, subkey):
path = self.path + subkey
return CreateKey(self.con, path)
def set_value(self, subkey, vtype, value):
SetValue(self.con, subkey, vtype, value)
def delete_subkey(self, subkey):
DeleteKey(self.con, subkey)
def delete_value(self, value):
DeleteValue(self.con, value)
def flush(self):
FlushKey(self.con)
def close(self):
self.flush()
CloseKey(self.con)
def __del__(self):
try:
self.close()
except:
pass
def __repr__(self):
return "<Registry Key %s>" % self.path
class Registry(_base):
""" Connection to the registry.
"""
def __init__(self, root):
self.root = root
self.con = ConnectRegistry(None,root)
def __repr__(self):
return "<Registry connection to %s>" % self.root