Michael Merickel
2018-10-19 d579f2104de139e0b0fc5d6c81aabb2f826e5e54
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
from zope.interface import implementer
 
from pyramid.interfaces import (
    IDefaultRootFactory,
    IExecutionPolicy,
    IRequestFactory,
    IResponseFactory,
    IRequestExtensions,
    IRootFactory,
    ISessionFactory,
)
 
from pyramid.router import default_execution_policy
from pyramid.traversal import DefaultRootFactory
 
from pyramid.util import get_callable_name, InstancePropertyHelper
 
from pyramid.config.actions import action_method
 
 
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 set_response_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` as the default response
        objects. The factory should conform to the
        :class:`pyramid.interfaces.IResponseFactory` interface.
 
        .. note::
 
           Using the ``response_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, IResponseFactory)
 
        intr = self.introspectable(
            'response factory',
            None,
            self.object_description(factory),
            'response factory',
        )
        intr['factory'] = factory
        self.action(IResponseFactory, 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 ``callable`` is
        a property descriptor, it has to be an instance of a class which is
        a subclass of ``property``. 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 = InstancePropertyHelper.make_property(
                callable, name=name, reify=reify
            )
        elif name is None:
            name = callable.__name__
        else:
            name = get_callable_name(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
    def set_execution_policy(self, policy):
        """
        Override the :app:`Pyramid` :term:`execution policy` in the
        current configuration.  The ``policy`` argument must be an instance
        of an :class:`pyramid.interfaces.IExecutionPolicy` or a
        :term:`dotted Python name` that points at an instance of an
        execution policy.
 
        """
        policy = self.maybe_dotted(policy)
        if policy is None:
            policy = default_execution_policy
 
        def register():
            self.registry.registerUtility(policy, IExecutionPolicy)
 
        intr = self.introspectable(
            'execution policy',
            None,
            self.object_description(policy),
            'execution policy',
        )
        intr['policy'] = policy
        self.action(IExecutionPolicy, register, introspectables=(intr,))
 
 
@implementer(IRequestExtensions)
class _RequestExtensions(object):
    def __init__(self):
        self.descriptors = {}
        self.methods = {}