source: trunk/IronPython/src/Tutorial/pyevent.py @ 157

Revision 157, 3.1 KB checked in by tyler, 13 years ago (diff)
  • Add (useless) MacPorts Portfile
  • Also add basic IronPython 1.1 source with my modified Makefile

(located at monkeypox.org/ports/ironpython-1.1.tar.gz)

Line 
1#####################################################################################
2
3#  Copyright (c) Microsoft Corporation. All rights reserved.
4#
5#  This source code is subject to terms and conditions of the Shared Source License
6#  for IronPython. A copy of the license can be found in the License.html file
7#  at the root of this distribution. If you can not locate the Shared Source License
8#  for IronPython, please send an email to ironpy@microsoft.com.
9#  By using this source code in any fashion, you are agreeing to be bound by
10#  the terms of the Shared Source License for IronPython.
11#
12#  You must not remove this notice, or any other, from this software.
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 issubclass(other.__class__, event):
26            self.handlers.extend(other.handlers)
27        elif issubclass(other.__class__, 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 issubclass(other.__class__, 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 issubclass(other.__class__, event_caller):
43            newEv = []
44            for x in self.event.handlers:
45                if not other.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())
Note: See TracBrowser for help on using the repository browser.