/
factories.py
203 lines (169 loc) · 7.76 KB
/
factories.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
from zope.deprecation import deprecate
from zope.interface import implementer
from pyramid.interfaces import (
IDefaultRootFactory,
IRequestFactory,
IRequestExtensions,
IRootFactory,
ISessionFactory,
)
from pyramid.traversal import DefaultRootFactory
from pyramid.util import (
action_method,
InstancePropertyMixin,
)
class FactoriesConfiguratorMixin(object):
@action_method
def set_root_factory(self, factory):
""" Add a :term:`root factory` to the current configuration
state. If the ``factory`` argument is ``None`` a default root
factory will be registered.
.. note::
Using the ``root_factory`` argument to the
:class:`pyramid.config.Configurator` constructor can be used to
achieve the same purpose.
"""
factory = self.maybe_dotted(factory)
if factory is None:
factory = DefaultRootFactory
def register():
self.registry.registerUtility(factory, IRootFactory)
self.registry.registerUtility(factory, IDefaultRootFactory) # b/c
intr = self.introspectable('root factories',
None,
self.object_description(factory),
'root factory')
intr['factory'] = factory
self.action(IRootFactory, register, introspectables=(intr,))
_set_root_factory = set_root_factory # bw compat
@action_method
def set_session_factory(self, factory):
"""
Configure the application with a :term:`session factory`. If this
method is called, the ``factory`` argument must be a session
factory callable or a :term:`dotted Python name` to that factory.
.. note::
Using the ``session_factory`` argument to the
:class:`pyramid.config.Configurator` constructor can be used to
achieve the same purpose.
"""
factory = self.maybe_dotted(factory)
def register():
self.registry.registerUtility(factory, ISessionFactory)
intr = self.introspectable('session factory', None,
self.object_description(factory),
'session factory')
intr['factory'] = factory
self.action(ISessionFactory, register, introspectables=(intr,))
@action_method
def set_request_factory(self, factory):
""" The object passed as ``factory`` should be an object (or a
:term:`dotted Python name` which refers to an object) which
will be used by the :app:`Pyramid` router to create all
request objects. This factory object must have the same
methods and attributes as the
:class:`pyramid.request.Request` class (particularly
``__call__``, and ``blank``).
See :meth:`pyramid.config.Configurator.add_request_method`
for a less intrusive way to extend the request objects with
custom methods and properties.
.. note::
Using the ``request_factory`` argument to the
:class:`pyramid.config.Configurator` constructor
can be used to achieve the same purpose.
"""
factory = self.maybe_dotted(factory)
def register():
self.registry.registerUtility(factory, IRequestFactory)
intr = self.introspectable('request factory', None,
self.object_description(factory),
'request factory')
intr['factory'] = factory
self.action(IRequestFactory, register, introspectables=(intr,))
@action_method
def add_request_method(self,
callable=None,
name=None,
property=False,
reify=False):
""" Add a property or method to the request object.
When adding a method to the request, ``callable`` may be any
function that receives the request object as the first
parameter. If ``name`` is ``None`` then it will be computed
from the name of the ``callable``.
When adding a property to the request, ``callable`` can either
be a callable that accepts the request as its single positional
parameter, or it can be a property descriptor. If ``name`` is
``None``, the name of the property will be computed from the
name of the ``callable``.
If the ``callable`` is a property descriptor a ``ValueError``
will be raised if ``name`` is ``None`` or ``reify`` is ``True``.
See :meth:`pyramid.request.Request.set_property` for more
details on ``property`` vs ``reify``. When ``reify`` is
``True``, the value of ``property`` is assumed to also be
``True``.
In all cases, ``callable`` may also be a
:term:`dotted Python name` which refers to either a callable or
a property descriptor.
If ``callable`` is ``None`` then the method is only used to
assist in conflict detection between different addons requesting
the same attribute on the request object.
This is the recommended method for extending the request object
and should be used in favor of providing a custom request
factory via
:meth:`pyramid.config.Configurator.set_request_factory`.
.. versionadded:: 1.4
"""
if callable is not None:
callable = self.maybe_dotted(callable)
property = property or reify
if property:
name, callable = InstancePropertyMixin._make_property(
callable, name=name, reify=reify)
elif name is None:
name = callable.__name__
def register():
exts = self.registry.queryUtility(IRequestExtensions)
if exts is None:
exts = _RequestExtensions()
self.registry.registerUtility(exts, IRequestExtensions)
plist = exts.descriptors if property else exts.methods
plist[name] = callable
if callable is None:
self.action(('request extensions', name), None)
elif property:
intr = self.introspectable('request extensions', name,
self.object_description(callable),
'request property')
intr['callable'] = callable
intr['property'] = True
intr['reify'] = reify
self.action(('request extensions', name), register,
introspectables=(intr,))
else:
intr = self.introspectable('request extensions', name,
self.object_description(callable),
'request method')
intr['callable'] = callable
intr['property'] = False
intr['reify'] = False
self.action(('request extensions', name), register,
introspectables=(intr,))
@action_method
@deprecate('set_request_propery() is deprecated as of Pyramid 1.5; use '
'add_request_method() with the property=True argument instead')
def set_request_property(self, callable, name=None, reify=False):
""" Add a property to the request object.
.. deprecated:: 1.5
:meth:`pyramid.config.Configurator.add_request_method` should be
used instead. (This method was docs-deprecated in 1.4 and
issues a real deprecation warning in 1.5).
.. versionadded:: 1.3
"""
self.add_request_method(
callable, name=name, property=not reify, reify=reify)
@implementer(IRequestExtensions)
class _RequestExtensions(object):
def __init__(self):
self.descriptors = {}
self.methods = {}