/
settings.py
373 lines (275 loc) · 10.7 KB
/
settings.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
""" settings """
import logging
import os.path
import sqlalchemy as sqla
from collections import OrderedDict
from zope import interface
from zope.interface.interface import InterfaceClass
from pyramid.compat import configparser
import ptah.form as form
import ptah
from ptah import uri, config
from ptah.sqlautils import JsonType
from ptah.config import StopException
log = logging.getLogger('ptah')
SETTINGS_ID = 'settings'
SETTINGS_OB_ID = 'ptah:settings'
ID_SETTINGS_GROUP = 'ptah:settings-group'
_marker = object()
def get_settings(grp, registry=None):
"""Get settings group by group id. Also there is `ptah_get_settins`
pyramid configurator directive.
.. code-block:: python
config = Configurator()
config.include('ptah')
config.commit()
# get settings with pyramid directive
ptah_settings = config.ptah_get_settings('ptah')
# get settings with `get_settings`
ptah_settings = ptah.get_settings(ptah.CFG_ID_PTAH, config.registry)
"""
return config.get_cfg_storage(ID_SETTINGS_GROUP, registry)[grp]
@uri.resolver('settings')
def settings_resolver(uri):
""" Ptah settings resolver """
return config.get_cfg_storage(ID_SETTINGS_GROUP)[uri[9:]]
def pyramid_get_settings(config, grp):
""" pyramid configurator directive for getting settings group::
config = Configurator()
config.include('ptah')
PTAH_CFG = config.get_settings(ptah.CFG_ID_PTAH)
"""
return config.get_cfg_storage(ID_SETTINGS_GROUP)[grp]
def load_dbsettings(registry=None):
session = ptah.get_session()
if not (session.bind and SettingRecord.__table__.exists()):
return
# load db settings
s_ob = config.get_cfg_storage(
SETTINGS_OB_ID, registry, default_factory=Settings)
s_ob.load_fromdb()
def init_settings(pconfig, cfg=None, section=configparser.DEFAULTSECT):
"""Initialize settings management system. This function available
as pyramid configurator directive. You should call it during
application configuration process.
.. code-block:: python
config = Configurator()
config.include('ptah')
# initialize ptah setting management system
config.ptah_init_settings()
"""
settings = config.get_cfg_storage(SETTINGS_OB_ID, pconfig.registry,Settings)
if settings.initialized:
raise RuntimeError(
"initialize_settings has been called more than once.")
log.info('Initializing ptah settings')
settings.initialized = True
if cfg is None:
cfg = pconfig.registry.settings
here = cfg.get('here', './')
include = cfg.get('include', '')
for f in include.split('\n'):
f = f.strip()
if f and os.path.exists(f):
parser = configparser.SafeConfigParser()
parser.read(f)
if section == configparser.DEFAULTSECT or \
parser.has_section(section):
cfg.update(parser.items(section, vars={'here': here}))
pconfig.begin()
try:
settings.init(pconfig, cfg)
pconfig.registry.notify(
ptah.events.SettingsInitializing(pconfig, pconfig.registry))
pconfig.registry.notify(
ptah.events.SettingsInitialized(pconfig, pconfig.registry))
except Exception as e:
raise StopException(e)
finally:
pconfig.end()
def register_settings(name, *fields, **kw):
"""Register settings group.
:param name: Name of settings group
:param fields: List of :py:class:`ptah.form.Field` objects
"""
iname = name
for ch in ('.', '-'):
iname = iname.replace(ch, '_')
category = InterfaceClass(
'SettingsGroup:%s' % iname.upper(), (),
__doc__='Settings group: %s' % name,
__module__='ptah.config.settings')
for field in fields:
field.required = False
field.missing = field.default
if field.default is form.null:
raise StopException(
'Default value is required for "{0}.{1}"'.format(name,field.name))
group = Group(name=name, *fields, **kw)
interface.directlyProvides(Group, category)
info = config.DirectiveInfo()
discr = (ID_SETTINGS_GROUP, name)
intr = config.Introspectable(
ID_SETTINGS_GROUP, discr, group.__title__, 'ptah-settingsgroup')
intr['name'] = name
intr['group'] = group
intr['codeinfo'] = info.codeinfo
info.attach(
config.Action(
lambda config, group: config.get_cfg_storage(ID_SETTINGS_GROUP)\
.update({group.__name__: group.clone(config.registry)}),
(group,), discriminator=discr, introspectables=(intr,))
)
class Settings(object):
""" settings management system """
initialized = False
def init(self, config, defaults=None):
groups = config.get_cfg_storage(ID_SETTINGS_GROUP).items()
for name, group in groups:
data = {}
for field in group.__fields__.values():
if field.default is not form.null:
data[field.name] = field.default
group.update(data)
if defaults is None: # pragma: no cover
return
self.load(defaults, True)
def load(self, rawdata, setdefaults=False):
groups = config.get_cfg_storage(ID_SETTINGS_GROUP).items()
try:
rawdata = dict((k.lower(), v) for k, v in rawdata.items())
except Exception as e:
raise StopException(e)
for name, group in groups:
data, errors = group.extract(rawdata)
if errors:
log.error(errors.msg)
raise StopException(errors)
for k, v in data.items():
if v is not form.null:
if setdefaults:
group.__fields__[k].default = v
group.update(data)
def load_fromdb(self):
records = dict(ptah.get_session().
query(SettingRecord.name, SettingRecord.value))
self.load(records)
# load non defined fields
groups = config.get_cfg_storage(ID_SETTINGS_GROUP).items()
for name, group in groups:
name = '%s.'%name
for attr, val in records.items():
if attr.startswith(name):
fname = attr[len(name):]
if fname not in group.__fields__:
try:
group[fname] = JsonType.serializer.loads(val)
except ValueError:
group[fname] = val
def export(self, default=False):
groups = config.get_cfg_storage(ID_SETTINGS_GROUP).items()
result = {}
for name, group in groups:
for field in group.__fields__.values():
fname = field.name
if group[fname] == field.default and not default:
continue
result['{0}.{1}'.format(name,fname)] = \
ptah.json.dumps(group[fname])
return result
class Group(OrderedDict):
""" Settings group """
def __init__(self, *args, **kwargs):
super(Group, self).__init__()
fields = form.Fieldset(*args, **kwargs)
self.__uri__ = 'settings:{0}'.format(fields.name)
self.__name__ = fields.name
self.__title__ = fields.title
self.__description__ = fields.description
self.__fields__ = fields
self.__ttw__ = kwargs.get('ttw', False)
self.__ttw_skip_fields__ = kwargs.get('ttw_skip_fields', ())
def clone(self, registry):
clone = self.__class__.__new__(self.__class__)
clone.__dict__.update(self.__dict__)
clone.__registry__ = registry
return clone
def extract(self, rawdata):
fieldset = self.__fields__
name = fieldset.name
data = {}
errors = form.FieldsetErrors(fieldset)
for field in fieldset.fields():
value = rawdata.get('{0}.{1}'.format(name, field.name), _marker)
if value is _marker:
value = self.get(field.name)
else:
try:
try:
value = ptah.json.loads(value)
except:
if not value.startswith('"'):
value = '"{0}"'.format(value)
value = value.replace('\n', '\\n')
value = ptah.json.loads(value)
field.validate(value)
if field.preparer is not None:
value = field.preparer(value)
except form.Invalid as e:
errors.append(e)
value = field.default
data[field.name] = value
if not errors:
try:
fieldset.validate(data)
except form.Invalid as e:
errors.append(e)
return data, errors
def get(self, name, default=None):
try:
return super(Group, self).__getitem__(name)
except (KeyError, AttributeError):
pass
if name in self.__fields__:
return self.__fields__[name].default
return default
def keys(self):
return [node.name for node in self.__fields__.values()]
def items(self):
return [(key, self.get(key)) for key in self.__fields__.keys()]
def __getitem__(self, name):
res = self.get(name, _marker)
if res is _marker:
raise KeyError(name)
return res
def updatedb(self, **data):
self.update(data)
name = self.__name__
fields = self.__fields__
Session = ptah.get_session()
# remove old data
keys = tuple('{0}.{1}'.format(name, key) for key in data.keys())
if keys:
Session.query(SettingRecord)\
.filter(SettingRecord.name.in_(keys)).delete(False)
# insert new data
for fname in data.keys():
if fname in fields:
field = fields[fname]
value = self[fname]
if value == field.default:
continue
rec = SettingRecord(name='{0}.{1}'.format(name, fname),
value = ptah.json.dumps(value))
else:
rec = SettingRecord(
name='{0}.{1}'.format(name, fname),
value = JsonType.serializer.dumps(data[fname]))
Session.add(rec)
Session.flush()
self.__registry__.notify(ptah.events.SettingsGroupModified(self))
self.__registry__.notify(ptah.events.UriInvalidateEvent(self.__uri__))
class SettingRecord(ptah.get_base()):
__tablename__ = 'ptah_settings'
name = sqla.Column(sqla.String(128), primary_key=True)
value = sqla.Column(sqla.UnicodeText)