PageRenderTime 23ms CodeModel.GetById 17ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Languages/IronPython/Public/Tutorial/pyevent.py

#
Python | 85 lines | 49 code | 14 blank | 22 comment | 16 complexity | 9598071f52f36870c460c5a113863711 MD5 | raw file
 1#####################################################################################
 2#
 3#  Copyright (c) Microsoft Corporation. All rights reserved.
 4#
 5# This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
 6# copy of the license can be found in the License.html file at the root of this distribution. If 
 7# you cannot locate the  Apache License, Version 2.0, please send an email to 
 8# ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 9# by the terms of the Apache License, Version 2.0.
10#
11# You must not remove this notice, or any other, from this software.
12#
13#
14#####################################################################################
15
16class event(object):
17    """Provides CLR event-like functionality for Python.  This is a public
18    event helper that allows adding and removing handlers."""
19    __slots__ = ['handlers']
20        
21    def __init__(self):
22        self.handlers = []
23    
24    def __iadd__(self, other):
25        if isinstance(other, event):
26            self.handlers.extend(other.handlers)
27        elif isinstance(other, event_caller):
28            self.handlers.extend(other.event.handlers)
29        else:
30            if not callable(other):
31                raise TypeError, "cannot assign to event unless value is callable"
32            self.handlers.append(other)
33        return self
34        
35    def __isub__(self, other):
36        if isinstance(other, event):
37            newEv = []
38            for x in self.handlers:
39                if not other.handlers.contains(x):
40                    newEv.append(x)
41            self.handlers = newEv
42        elif isinstance(other, event_caller):
43            newEv = []
44            for x in self.handlers:
45                if not other.event.handlers.contains(x):
46                    newEv.append(x)
47            self.handlers = newEv
48        else:
49            if other in self.handlers:
50                self.handlers.remove(other)
51        return self
52
53    def make_caller(self):
54        return event_caller(self)
55
56class event_caller(object):
57    """Provides CLR event-like functionality for Python.  This is the
58    protected event caller that allows the owner to raise the event"""
59    __slots__ = ['event']
60    
61    def __init__(self, event):
62        self.event = event
63            
64    def __call__(self, *args):
65        for ev in self.event.handlers:
66            ev(*args)
67
68    def __set__(self, val):
69        raise ValueError, "cannot assign to an event, can only add or remove handlers"
70    
71    def __delete__(self, val):
72        raise ValueError, "cannot delete an event, can only add or remove handlers"
73
74    def __get__(self, instance, owner):
75        return self
76
77
78def make_event():
79    """Creates an event object tuple.  The first value in the tuple can be
80    exposed to allow external code to hook and unhook from the event.  The
81    second value can be used to raise the event and can be stored in a
82    private variable."""
83    res = event()
84    
85    return (res, res.make_caller())