~ubuntu-branches/ubuntu/precise/checkbox/precise

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#
# This file is part of Checkbox.
#
# Copyright 2008 Canonical Ltd.
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
#
import logging
import threading

from checkbox.lib.log import format_object


class EventID(object):

    def __init__(self, event_type, pair):
        self._event_type = event_type
        self._pair = pair


class StopException(Exception):

    pass


class StopAllException(Exception):

    pass


class Reactor(object):

    def __init__(self):
        self._event_handlers = {}
        self._local = threading.local()
        self._depth = 0

    def call_on(self, event_type, handler, priority=0):
        pair = (handler, priority)

        logging.debug("Calling %s on %s.", format_object(handler), event_type)
        handlers = self._event_handlers.setdefault(event_type, [])
        handlers.append(pair)

        return EventID(event_type, pair)

    def fire(self, event_type, *args, **kwargs):
        indent = "  " * self._depth
        self._depth += 1
        logging.debug("%sStarted firing %s.", indent, event_type)

        handlers = self._event_handlers.get(event_type, ())
        if not handlers:
            logging.debug("%sNo handlers found for event type: %s", indent, event_type)

        results = []
        handlers = sorted(handlers, key=lambda pair: pair[1])
        for handler, priority in handlers:
            try:
                logging.debug("%sCalling %s for %s with priority %d.",
                              indent, format_object(handler, *args, **kwargs),
                              event_type, priority)
                results.append(handler(*args, **kwargs))
            except StopException:
                break
            except StopAllException:
                raise
            except KeyboardInterrupt:
                logging.exception("Keyboard interrupt while running event "
                                  "handler %s for event type %r",
                                  format_object(handler, *args, **kwargs),
                                  event_type)
                self.stop_all()
            except:
                logging.exception("Error running event handler %s for "
                                  "event type %r",
                                  format_object(handler, *args, **kwargs),
                                  event_type)

        logging.debug("%sFinished firing %s.", indent, event_type)
        self._depth -= 1
        return results

    def has_call(self, event_type):
        return event_type in self._event_handlers

    def cancel_call(self, id):
        if type(id) is EventID:
            self._event_handlers[id._event_type].remove(id._pair)
        else:
            raise Exception, "EventID instance expected, received %r" % id

    def cancel_all_calls(self, event_type):
        del self._event_handlers[event_type]

    def run(self):
        try:
            self.fire("run")
        except StopAllException:
            pass
        self.fire("stop")

    def stop(self):
        raise StopException

    def stop_all(self):
        raise StopAllException