diff options
Diffstat (limited to 'bitbake/lib/bb/event.py')
-rw-r--r-- | bitbake/lib/bb/event.py | 44 |
1 files changed, 28 insertions, 16 deletions
diff --git a/bitbake/lib/bb/event.py b/bitbake/lib/bb/event.py index cbe6d2a11a..b1d12177c4 100644 --- a/bitbake/lib/bb/event.py +++ b/bitbake/lib/bb/event.py | |||
@@ -44,7 +44,13 @@ class Event: | |||
44 | 44 | ||
45 | NotHandled = 0 | 45 | NotHandled = 0 |
46 | Handled = 1 | 46 | Handled = 1 |
47 | handlers = [] | 47 | |
48 | Registered = 10 | ||
49 | AlreadyRegistered = 14 | ||
50 | |||
51 | # Internal | ||
52 | _handlers = [] | ||
53 | _handlers_dict = {} | ||
48 | 54 | ||
49 | def tmpHandler(event): | 55 | def tmpHandler(event): |
50 | """Default handler for code events""" | 56 | """Default handler for code events""" |
@@ -57,7 +63,7 @@ def defaultTmpHandler(): | |||
57 | 63 | ||
58 | def fire(event): | 64 | def fire(event): |
59 | """Fire off an Event""" | 65 | """Fire off an Event""" |
60 | for h in handlers: | 66 | for h in _handlers: |
61 | if type(h).__name__ == "code": | 67 | if type(h).__name__ == "code": |
62 | exec(h) | 68 | exec(h) |
63 | if tmpHandler(event) == Handled: | 69 | if tmpHandler(event) == Handled: |
@@ -67,15 +73,22 @@ def fire(event): | |||
67 | return Handled | 73 | return Handled |
68 | return NotHandled | 74 | return NotHandled |
69 | 75 | ||
70 | def register(handler): | 76 | def register(name, handler): |
71 | """Register an Event handler""" | 77 | """Register an Event handler""" |
78 | |||
79 | # already registered | ||
80 | if name in _handlers_dict: | ||
81 | return AlreadyRegistered | ||
82 | |||
72 | if handler is not None: | 83 | if handler is not None: |
73 | # handle string containing python code | 84 | # handle string containing python code |
74 | if type(handler).__name__ == "str": | 85 | if type(handler).__name__ == "str": |
75 | return _registerCode(handler) | 86 | _registerCode(handler) |
76 | # prevent duplicate registration | 87 | else: |
77 | if not handler in handlers: | 88 | _handlers.append(handler) |
78 | handlers.append(handler) | 89 | |
90 | _handlers_dict[name] = 1 | ||
91 | return Registered | ||
79 | 92 | ||
80 | def _registerCode(handlerStr): | 93 | def _registerCode(handlerStr): |
81 | """Register a 'code' Event. | 94 | """Register a 'code' Event. |
@@ -88,24 +101,23 @@ def _registerCode(handlerStr): | |||
88 | tmp = "def tmpHandler(e):\n%s" % handlerStr | 101 | tmp = "def tmpHandler(e):\n%s" % handlerStr |
89 | comp = bb.utils.better_compile(tmp, "tmpHandler(e)", "bb.event._registerCode") | 102 | comp = bb.utils.better_compile(tmp, "tmpHandler(e)", "bb.event._registerCode") |
90 | # prevent duplicate registration | 103 | # prevent duplicate registration |
91 | if not comp in handlers: | 104 | _handlers.append(comp) |
92 | handlers.append(comp) | ||
93 | 105 | ||
94 | def remove(handler): | 106 | def remove(name, handler): |
95 | """Remove an Event handler""" | 107 | """Remove an Event handler""" |
96 | for h in handlers: | ||
97 | if type(handler).__name__ == "str": | ||
98 | return _removeCode(handler) | ||
99 | 108 | ||
100 | if handler is h: | 109 | _handlers_dict.pop(name) |
101 | handlers.remove(handler) | 110 | if type(handler).__name__ == "str": |
111 | return _removeCode(handler) | ||
112 | else: | ||
113 | _handlers.remove(handler) | ||
102 | 114 | ||
103 | def _removeCode(handlerStr): | 115 | def _removeCode(handlerStr): |
104 | """Remove a 'code' Event handler | 116 | """Remove a 'code' Event handler |
105 | Deprecated interface; call remove instead.""" | 117 | Deprecated interface; call remove instead.""" |
106 | tmp = "def tmpHandler(e):\n%s" % handlerStr | 118 | tmp = "def tmpHandler(e):\n%s" % handlerStr |
107 | comp = bb.utils.better_compile(tmp, "tmpHandler(e)", "bb.event._removeCode") | 119 | comp = bb.utils.better_compile(tmp, "tmpHandler(e)", "bb.event._removeCode") |
108 | handlers.remove(comp) | 120 | _handlers.remove(comp) |
109 | 121 | ||
110 | def getName(e): | 122 | def getName(e): |
111 | """Returns the name of a class or class instance""" | 123 | """Returns the name of a class or class instance""" |