Michael Merickel
2018-10-15 dd3cc81f75dcb5ff96e0751653071722a15f46c2
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
from pyramid.view import view_config
from pyramid.events import subscriber
 
class Yup(object):
    def __init__(self, val, config):
        self.val = val
 
    def text(self):
        return 'path_startswith = %s' % (self.val,)
 
    phash = text
 
    def __call__(self, event):
        return getattr(event.response, 'yup', False)
 
class Foo(object):
    def __init__(self, response):
        self.response = response
 
class Bar(object):
    pass
 
@subscriber(Foo)
def foo(event):
    event.response.text += 'foo '
 
@subscriber(Foo, yup=True)
def fooyup(event):
    event.response.text += 'fooyup '
    
@subscriber([Foo, Bar])
def foobar(event):
    event.response.text += 'foobar '
 
@subscriber([Foo, Bar])
def foobar2(event, context):
    event.response.text += 'foobar2 '
 
@subscriber([Foo, Bar], yup=True)
def foobaryup(event):
    event.response.text += 'foobaryup '
 
@subscriber([Foo, Bar], yup=True)
def foobaryup2(event, context):
    event.response.text += 'foobaryup2 '
 
@view_config(name='sendfoo')
def sendfoo(request):
    response = request.response
    response.yup = True
    request.registry.notify(Foo(response))
    return response
 
@view_config(name='sendfoobar')
def sendfoobar(request):
    response = request.response
    response.yup = True
    request.registry.notify(Foo(response), Bar())
    return response
 
def includeme(config):
    config.add_subscriber_predicate('yup', Yup)
    config.scan('tests.pkgs.eventonly')