forked from progrium/ginkgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
209 lines (166 loc) · 6.46 KB
/
Copy pathconfig.py
File metadata and controls
209 lines (166 loc) · 6.46 KB
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
import os.path
import runpy
from peak.util.proxies import ObjectWrapper
class Config(object):
"""Represents a collection of settings
Provides access to a collection of settings that can be loaded from a
Python module or file, or a dictionary. It allows classic-style classes to
be used to indicate namespaces or groups, which can be nested.
"""
_settings = {}
_descriptors = []
_last_file = None
def _normalize_path(self, path):
return path.lower().lstrip(".")
def get(self, path, default=None):
return self._settings.get(self._normalize_path(path), default)
def set(self, path, value):
self._settings[self._normalize_path(path)] = value
def group(self, path=''):
return Group(self, path)
def setting(self, *args, **kwargs):
descriptor = _Setting(self, *args, **kwargs)
self._descriptors.append(descriptor)
return descriptor
def load_module(self, module_path):
return self.load(runpy.run_module(module_path))
def load_file(self, file_path):
file_path = os.path.abspath(os.path.expanduser(file_path))
config_dict = runpy.run_path(file_path)
self._last_file = file_path
return self.load(config_dict)
def reload_file(self):
return self.load_file(self._last_file)
def load(self, config_dict):
def _load(d, prefix=''):
"""
Recursively loads configuration from a dictionary, putting
configuration under classic-style classes in a namespace.
"""
items = (i for i in d.iteritems() if not i[0].startswith("_"))
for key, value in items:
path = ".".join((prefix, key))
if type(value).__name__ == 'classobj':
_load(value.__dict__, path)
else:
self.set(path, value)
_load(config_dict)
return self._settings
def print_help(self, only_default=False):
print "config settings:"
for d in sorted(self._descriptors, key=lambda d: d.path):
if d.help:
value = d.default if only_default else self.get(d.path,
d.default)
print " %- 14s %s [%s]" % (d.path, d.help, value)
class Group(object):
"""Provides read-only access to a group of config data
These objects represent a 'view' into a particular scope of the entire
config, whether the global group or a group created using classic-style
classes in the config file. They're not created directly. Use the `group()`
method on `Config`.
c = Config()
g1 = c.group() # global scope group
g1.foo # gives you the setting named "foo"
g2 = c.group("bar.baz") # bar.baz scoped group
g2.qux # give you the setting named "bar.baz.qux"
They are not intended to be the primary interface to settings. However, in
some cases it is more convenient. You should usually use the `setting`
function to embed config settings for particular values on relevant classes.
"""
def __init__(self, config, name):
self._config = config
self._name = name
def __getattr__(self, name):
path = self._config._normalize_path(".".join((self._name, name)))
try:
return self._config._settings[path]
except KeyError:
group_path = path + "."
keys = self._config._settings.keys()
if any(1 for k in keys if k.startswith(group_path)):
return Group(self._config, path)
return None
def __repr__(self):
return 'Group[{}]'.format(self._name)
class _Setting(object):
"""Setting descriptor for embedding in component classes.
Do not use this object directly, instead use `Config.setting()`.
This is a descriptor for your component classes to define what settings
your application uses and provides a way to access that setting. By
accessing with a descriptor, if the configuration changes you
will always have the current value. Example:
class MyService(Service):
foo = config.setting('foo', default='bar',
help="This lets us set foo for MyService")
def do_start(self):
print self.foo
"""
_init = object()
def __init__(self, config, path, default=None, monitored=False, help=''):
self._last_value = self._init
self.config = config
self.path = path
self.default = default
self.monitored = monitored
self.help = self.__doc__ = help
def __get__(self, instance, type):
if self.monitored:
return SettingProxy(self.value, self)
else:
return self.value
@property
def value(self):
return self.config.get(self.path, self.default)
@property
def changed(self):
""" True if the value has changed since the last time accessing
this property. False on first access.
"""
old, self._last_value = self._last_value, self.value
return self.value != old and old is not self._init
class SettingProxy(ObjectWrapper):
"""Wraps an object returned by a `Setting` descriptor
Primarily it gives any object that comes from `Setting` a `changed`
property that will determine if the value has been changed, such as when
configuration is reloaded.
"""
descriptor = None
def __init__(self, obj, descriptor):
ObjectWrapper.__init__(self, obj)
self.descriptor = descriptor
@property
def changed(self):
return self.descriptor.changed
@property
def value(self):
return self.__subject__
if __name__ == '__main__':
c = Config()
c.load({
"foo": "bar",
"bar.foo": "qux",
"bar.boo": "bar",
"bar.baz.foo": "bar",
"bar.baz.bar": "bar"})
assert c.get("foo") == "bar"
class MyClass(object):
foo = c.setting("foo", doc="This is foo.")
def override(self):
self.foo = "qux"
o = MyClass()
assert o.foo == "bar"
assert o.foo.changed == False
assert o.foo.changed == False
c.set("foo", "BAZ")
assert o.foo.changed == True
assert o.foo.changed == False
assert MyClass.foo == "BAZ"
o.override()
assert MyClass.foo == "BAZ"
assert o.foo == "qux"
g = c.group()
assert g.foo == "BAZ"
assert g.bar.__class__ == Group
assert g.bar.boo == "bar"
assert g.bar.tree == None