-
Notifications
You must be signed in to change notification settings - Fork 3
/
product.py
78 lines (56 loc) · 1.95 KB
/
product.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
"""Access to product-specific configuration."""
import ZConfig
import os.path
_configs = {}
_schema = None
try:
import zope.testing.cleanup
except ImportError:
pass
else:
zope.testing.cleanup.addCleanUp(_configs.clear)
def getProductConfiguration(name):
"""Return the product configuration for product `name`.
If there is no configuration for `name`, None is returned.
"""
return _configs.get(name)
def setProductConfigurations(configs):
"""Initialize product configuration from ZConfig data."""
pconfigs = {}
for pconfig in configs:
pconfigs[pconfig.getSectionName()] = pconfig.mapping
_configs.clear()
_configs.update(pconfigs)
def setProductConfiguration(name, mapping):
"""Set the configuration for a single product."""
if mapping is None:
if name in _configs:
del _configs[name]
else:
_configs[name] = mapping
def saveConfiguration():
"""Retrieve a shallow copy of the configuration state."""
return _configs.copy()
def restoreConfiguration(state):
"""Restore the configuration state based on a state value."""
_configs.clear()
_configs.update(state)
def loadConfiguration(file, url=None):
global _schema
if _schema is None:
here = os.path.dirname(os.path.abspath(__file__))
path = os.path.join(here, "schema", "productconfig.xml")
_schema = ZConfig.loadSchema(path)
data, handlers = ZConfig.loadConfigFile(_schema, file, url=url)
return dict((sect.getSectionName(), sect.mapping)
for sect in data.product_config)
class FauxConfiguration(object):
"""Configuration object that can be use from tests.
An instance is of this is similar to a <product-config> section from a
zope.conf file in all the ways this module cares about.
"""
def __init__(self, name, mapping):
self.name = name
self.mapping = dict(mapping)
def getSectionName(self):
return self.name