/
extension_points.py
93 lines (72 loc) · 2.97 KB
/
extension_points.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
# -*- coding: utf-8 -*-
"""
Extension points for integrating with other applications,
frameworks, and libraries.
The normal extension point for this package is :mod:`zope.component`
and :mod:`zope.interface`. The particular extension points found here
are different for two reasons:
1. There is expected to be only one way that a given application will
want to configure the extension point.
2. They are believed to be so performance critical to normal
operations that the use of a component utility lookup would be
noticeably detrimental.
For those two reasons, these extension points are both developed with
:mod:`zope.hookable`, which provides a very low-overhead way to invoke
a function while allowing for it to be extended. Applications that
need to changed the behaviour of the built-in functions supplied here will
need to call their :func:`zope.hookable.hookable.sethook` method at
startup.
.. versionadded:: 1.0
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from zope.hookable import hookable as _base_hookable
from ._compat import to_unicode
from .oids import to_external_oid
from .interfaces import StandardExternalFields
class _hookable(_base_hookable):
# zope.hookable doesn't expose docstrings, so we need to do it
# manually.
# NOTE: You must manually list these with ..autofunction:: in the api
# document.
__doc__ = property(lambda self: self.original.__doc__)
# Unless the first line of the docstring for the function looks like
# a signature, we get a warning without these properties.
__bases__ = property(lambda _: ())
__dict__ = property(lambda _: None)
@_hookable
def get_current_request():
"""
get_current_request() -> request
In a request/response system like a WSGI server,
return an object representing the current request.
In some cases, this may be used to find adapters for objects.
It is also passed to the ``toExternalObject`` function of
each object as a keyword parameter.
In version 1.0, this will default to using Pyramid's
:func:`pyramid.threadlocal.get_current_request` if pyramid is
installed. However, in a future version, an application wishing
to use Pyramid's request will explicitly need to set the hook.
.. deprecated:: 1.0
The automatic fallback to Pyramid. It will be removed
in 1.1 or before.
"""
try:
from pyramid import threadlocal
except ImportError:
pass
else: # pragma: no cover
get_current_request.sethook(threadlocal.get_current_request)
del threadlocal
_StandardExternalFields_OID = StandardExternalFields.OID
_StandardExternalFields_NTIID = StandardExternalFields.NTIID
del StandardExternalFields
@_hookable
def set_external_identifiers(self, result):
# XXX: Document me
ntiid = oid = to_unicode(to_external_oid(self))
if ntiid:
result[_StandardExternalFields_OID] = oid
result[_StandardExternalFields_NTIID] = ntiid
return (oid, ntiid)