/
registry.py
52 lines (39 loc) · 1.78 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
from zope.component.registry import Components
from pyramid.interfaces import ISettings
class Registry(Components, dict):
""" A registry object is an :term:`application registry`. The existence
of a registry implementation detail of :app:`pyramid`. It is used by the
framework itself to perform mappings of URLs to view callables, as well
as servicing other various duties. Despite being an implementation detail
of the framework, it has a number of attributes that may be useful within
application code.
For information about the purpose and usage of the application registry,
see :ref:`zca_chapter`.
The application registry is usually accessed as ``request.registry`` in
application code.
"""
# for optimization purposes, if no listeners are listening, don't try
# to notify them
has_listeners = False
_settings = None
def registerSubscriptionAdapter(self, *arg, **kw):
result = Components.registerSubscriptionAdapter(self, *arg, **kw)
self.has_listeners = True
return result
def registerHandler(self, *arg, **kw):
result = Components.registerHandler(self, *arg, **kw)
self.has_listeners = True
return result
def notify(self, *events):
if self.has_listeners:
# iterating over subscribers assures they get executed
[ _ for _ in self.subscribers(events, None) ]
# backwards compatibility for code that wants to look up a settings
# object via ``registry.getUtility(ISettings)``
def _get_settings(self):
return self._settings
def _set_settings(self, settings):
self.registerUtility(settings, ISettings)
self._settings = settings
settings = property(_get_settings, _set_settings)
global_registry = Registry('global')