Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/pyramid/config/factories.py : 43%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from zope.interface import implementer
3from pyramid.interfaces import (
4 IDefaultRootFactory,
5 IExecutionPolicy,
6 IRequestFactory,
7 IResponseFactory,
8 IRequestExtensions,
9 IRootFactory,
10 ISessionFactory,
11)
13from pyramid.router import default_execution_policy
14from pyramid.traversal import DefaultRootFactory
16from pyramid.util import get_callable_name, InstancePropertyHelper
18from pyramid.config.actions import action_method
21class FactoriesConfiguratorMixin(object):
22 @action_method
23 def set_root_factory(self, factory):
24 """ Add a :term:`root factory` to the current configuration
25 state. If the ``factory`` argument is ``None`` a default root
26 factory will be registered.
28 .. note::
30 Using the ``root_factory`` argument to the
31 :class:`pyramid.config.Configurator` constructor can be used to
32 achieve the same purpose.
33 """
34 factory = self.maybe_dotted(factory)
35 if factory is None:
36 factory = DefaultRootFactory
38 def register():
39 self.registry.registerUtility(factory, IRootFactory)
40 self.registry.registerUtility(factory, IDefaultRootFactory) # b/c
42 intr = self.introspectable(
43 'root factories',
44 None,
45 self.object_description(factory),
46 'root factory',
47 )
48 intr['factory'] = factory
49 self.action(IRootFactory, register, introspectables=(intr,))
51 _set_root_factory = set_root_factory # bw compat
53 @action_method
54 def set_session_factory(self, factory):
55 """
56 Configure the application with a :term:`session factory`. If this
57 method is called, the ``factory`` argument must be a session
58 factory callable or a :term:`dotted Python name` to that factory.
60 .. note::
62 Using the ``session_factory`` argument to the
63 :class:`pyramid.config.Configurator` constructor can be used to
64 achieve the same purpose.
65 """
66 factory = self.maybe_dotted(factory)
68 def register():
69 self.registry.registerUtility(factory, ISessionFactory)
71 intr = self.introspectable(
72 'session factory',
73 None,
74 self.object_description(factory),
75 'session factory',
76 )
77 intr['factory'] = factory
78 self.action(ISessionFactory, register, introspectables=(intr,))
80 @action_method
81 def set_request_factory(self, factory):
82 """ The object passed as ``factory`` should be an object (or a
83 :term:`dotted Python name` which refers to an object) which
84 will be used by the :app:`Pyramid` router to create all
85 request objects. This factory object must have the same
86 methods and attributes as the
87 :class:`pyramid.request.Request` class (particularly
88 ``__call__``, and ``blank``).
90 See :meth:`pyramid.config.Configurator.add_request_method`
91 for a less intrusive way to extend the request objects with
92 custom methods and properties.
94 .. note::
96 Using the ``request_factory`` argument to the
97 :class:`pyramid.config.Configurator` constructor
98 can be used to achieve the same purpose.
99 """
100 factory = self.maybe_dotted(factory)
102 def register():
103 self.registry.registerUtility(factory, IRequestFactory)
105 intr = self.introspectable(
106 'request factory',
107 None,
108 self.object_description(factory),
109 'request factory',
110 )
111 intr['factory'] = factory
112 self.action(IRequestFactory, register, introspectables=(intr,))
114 @action_method
115 def set_response_factory(self, factory):
116 """ The object passed as ``factory`` should be an object (or a
117 :term:`dotted Python name` which refers to an object) which
118 will be used by the :app:`Pyramid` as the default response
119 objects. The factory should conform to the
120 :class:`pyramid.interfaces.IResponseFactory` interface.
122 .. note::
124 Using the ``response_factory`` argument to the
125 :class:`pyramid.config.Configurator` constructor
126 can be used to achieve the same purpose.
127 """
128 factory = self.maybe_dotted(factory)
130 def register():
131 self.registry.registerUtility(factory, IResponseFactory)
133 intr = self.introspectable(
134 'response factory',
135 None,
136 self.object_description(factory),
137 'response factory',
138 )
139 intr['factory'] = factory
140 self.action(IResponseFactory, register, introspectables=(intr,))
142 @action_method
143 def add_request_method(
144 self, callable=None, name=None, property=False, reify=False
145 ):
146 """ Add a property or method to the request object.
148 When adding a method to the request, ``callable`` may be any
149 function that receives the request object as the first
150 parameter. If ``name`` is ``None`` then it will be computed
151 from the name of the ``callable``.
153 When adding a property to the request, ``callable`` can either
154 be a callable that accepts the request as its single positional
155 parameter, or it can be a property descriptor. If ``callable`` is
156 a property descriptor, it has to be an instance of a class which is
157 a subclass of ``property``. If ``name`` is ``None``, the name of
158 the property will be computed from the name of the ``callable``.
160 If the ``callable`` is a property descriptor a ``ValueError``
161 will be raised if ``name`` is ``None`` or ``reify`` is ``True``.
163 See :meth:`pyramid.request.Request.set_property` for more
164 details on ``property`` vs ``reify``. When ``reify`` is
165 ``True``, the value of ``property`` is assumed to also be
166 ``True``.
168 In all cases, ``callable`` may also be a
169 :term:`dotted Python name` which refers to either a callable or
170 a property descriptor.
172 If ``callable`` is ``None`` then the method is only used to
173 assist in conflict detection between different addons requesting
174 the same attribute on the request object.
176 This is the recommended method for extending the request object
177 and should be used in favor of providing a custom request
178 factory via
179 :meth:`pyramid.config.Configurator.set_request_factory`.
181 .. versionadded:: 1.4
182 """
183 if callable is not None:
184 callable = self.maybe_dotted(callable)
186 property = property or reify
187 if property:
188 name, callable = InstancePropertyHelper.make_property(
189 callable, name=name, reify=reify
190 )
191 elif name is None:
192 name = callable.__name__
193 else:
194 name = get_callable_name(name)
196 def register():
197 exts = self.registry.queryUtility(IRequestExtensions)
199 if exts is None:
200 exts = _RequestExtensions()
201 self.registry.registerUtility(exts, IRequestExtensions)
203 plist = exts.descriptors if property else exts.methods
204 plist[name] = callable
206 if callable is None:
207 self.action(('request extensions', name), None)
208 elif property:
209 intr = self.introspectable(
210 'request extensions',
211 name,
212 self.object_description(callable),
213 'request property',
214 )
215 intr['callable'] = callable
216 intr['property'] = True
217 intr['reify'] = reify
218 self.action(
219 ('request extensions', name), register, introspectables=(intr,)
220 )
221 else:
222 intr = self.introspectable(
223 'request extensions',
224 name,
225 self.object_description(callable),
226 'request method',
227 )
228 intr['callable'] = callable
229 intr['property'] = False
230 intr['reify'] = False
231 self.action(
232 ('request extensions', name), register, introspectables=(intr,)
233 )
235 @action_method
236 def set_execution_policy(self, policy):
237 """
238 Override the :app:`Pyramid` :term:`execution policy` in the
239 current configuration. The ``policy`` argument must be an instance
240 of an :class:`pyramid.interfaces.IExecutionPolicy` or a
241 :term:`dotted Python name` that points at an instance of an
242 execution policy.
244 """
245 policy = self.maybe_dotted(policy)
246 if policy is None:
247 policy = default_execution_policy
249 def register():
250 self.registry.registerUtility(policy, IExecutionPolicy)
252 intr = self.introspectable(
253 'execution policy',
254 None,
255 self.object_description(policy),
256 'execution policy',
257 )
258 intr['policy'] = policy
259 self.action(IExecutionPolicy, register, introspectables=(intr,))
262@implementer(IRequestExtensions)
263class _RequestExtensions(object):
264 def __init__(self):
265 self.descriptors = {}
266 self.methods = {}