forked from morepath/morepath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtween.py
More file actions
55 lines (43 loc) · 1.92 KB
/
Copy pathtween.py
File metadata and controls
55 lines (43 loc) · 1.92 KB
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
"""
Tweens are lightweight middleware using webob.
A tween is a function that takes a :class:`morepath.Request` and returns
a :class:`morepath.Response`. A tween factory is a function that given
an application instance and tween constructs another tween that wraps it.
Used by :meth:`morepath.App.tween_factory`.
See also :class:`morepath.directive.TweenRegistry`
"""
from .toposort import toposorted, Info
from .publish import publish
class TweenRegistry(object):
"""Registry for tweens.
"""
def __init__(self):
self._tween_infos = []
def register_tween_factory(self, tween_factory, over, under):
"""Register a tween factory.
:param tween_factory: a function that constructs a tween given
a :class:`morepath.App` instance and a function that takes a
:class:`morepath.Request` argument and returns a
:class:`morepath.Response` (or a :class:`webob.response.Response`).
:over: this tween factory wraps the tween created by the ``over``
factory (possibly indirectly).
:under: the ``under`` factory wraps the tween created by this one
(possibly indirectly).
"""
self._tween_infos.append(Info(tween_factory, over, under))
def sorted_tween_factories(self):
"""Sort tween factories topologically by over and under.
:return: a sorted list of tween infos.
"""
return [info.key for info in toposorted(self._tween_infos)]
def wrap(self, app):
"""Wrap app with tweens.
This wraps :func:`morepath.publish.publish` with tweens.
:param app: an instance of :class:`morepath.App`.
:return: the application wrapped with tweens. This is a function
that takes request and returns a a response.
"""
result = publish
for tween_factory in reversed(self.sorted_tween_factories()):
result = tween_factory(app, result)
return result