1111
1212
1313
14+ class DoesNotExistError (KeyError ):
15+ """Raised when binding to an :class:`Event` or :class:`~.properties.Property`
16+ that does not exist
17+
18+ .. versionadded:: 0.2.2
19+ """
20+ def __init__ (self , name ):
21+ self .name = name
22+
23+ def __str__ (self ):
24+ return f'Event "{ self .name } " not registered'
25+
26+
27+ class ExistsError (RuntimeError ):
28+ """Raised when registering an event name that already exists
29+ as either a normal :class:`Event` or :class:`~.properies.Property`
30+
31+ .. versionadded:: 0.2.2
32+ """
33+ def __init__ (self , name ):
34+ self .name = name
35+
36+ def __str__ (self ):
37+ return f'"{ self .name } " already exists'
38+
39+ class EventExistsError (ExistsError ):
40+ """Raised when registering an event name that already exists
41+ as an :class:`Event`
42+
43+ .. versionadded:: 0.2.2
44+ """
45+
46+
47+ class PropertyExistsError (ExistsError ):
48+ """Raised when registering an event name that already exists
49+ as a :class:`~.properies.Property`
50+
51+ .. versionadded:: 0.2.2
52+ """
53+
54+
1455class Event (object ):
1556 """Holds references to event names and subscribed listeners
1657
@@ -108,10 +149,20 @@ def register_event(self, *names):
108149
109150 Args:
110151 *names (str): Name or names of the events to register
152+
153+ Raises:
154+ EventExistsError: If an event with the given name already exists
155+ PropertyExistsError: If a property with the given name already exists
156+
157+ .. versionchanged:: 0.2.2
158+ :class:`ExistsError` exceptions are raised when attempting to
159+ register an event or property that already exists
111160 """
112161 for name in names :
113162 if name in self .__events :
114- continue
163+ raise EventExistsError (name )
164+ elif name in self .__property_events :
165+ raise PropertyExistsError (name )
115166 self .__events [name ] = Event (name )
116167 def bind (self , ** kwargs ):
117168 """Subscribes to events or to :class:`~pydispatch.properties.Property` updates
@@ -164,6 +215,14 @@ class Foo(Dispatcher):
164215
165216 This can also be done using :meth:`bind_async`.
166217
218+ Raises:
219+ DoesNotExistError: If attempting to bind to an event or
220+ property that has not been registered
221+
222+ .. versionchanged:: 0.2.2
223+ :class:`DoesNotExistError` is now raised when binding to
224+ non-existent events or properties
225+
167226 .. versionadded:: 0.1.0
168227
169228 """
@@ -174,7 +233,10 @@ class Foo(Dispatcher):
174233 if name in props :
175234 e = props [name ]
176235 else :
177- e = events [name ]
236+ try :
237+ e = events [name ]
238+ except KeyError :
239+ raise DoesNotExistError (name )
178240 e .add_listener (cb , __aio_loop__ = aio_loop )
179241 def unbind (self , * args ):
180242 """Unsubscribes from events or :class:`~pydispatch.properties.Property` updates
@@ -224,10 +286,21 @@ def emit(self, name, *args, **kwargs):
224286 name (str): The name of the :class:`Event` to dispatch
225287 *args (Optional): Positional arguments to be sent to listeners
226288 **kwargs (Optional): Keyword arguments to be sent to listeners
289+
290+ Raises:
291+ DoesNotExistError: If attempting to emit an event or
292+ property that has not been registered
293+
294+ .. versionchanged:: 0.2.2
295+ :class:`DoesNotExistError` is now raised if the event or property
296+ does not exist
227297 """
228298 e = self .__property_events .get (name )
229299 if e is None :
230- e = self .__events [name ]
300+ try :
301+ e = self .__events [name ]
302+ except KeyError :
303+ raise DoesNotExistError (name )
231304 return e (* args , ** kwargs )
232305 def get_dispatcher_event (self , name ):
233306 """Retrieves an Event object by name
0 commit comments