warning for accepting an event already in the dict, made watching variables private, assert that the method is callable

This commit is contained in:
Joe Shochet 2006-03-22 20:18:16 +00:00
parent 76252c5316
commit ac0f6042e2

View File

@ -29,8 +29,8 @@ class Messenger:
self.__objectEvents = {} self.__objectEvents = {}
if __debug__: if __debug__:
self.isWatching=0 self.__isWatching=0
self.watching={} self.__watching={}
# I'd like this to be in the __debug__, but I fear that someone will # I'd like this to be in the __debug__, but I fear that someone will
# want this in a release build. If you're sure that that will not be # want this in a release build. If you're sure that that will not be
# then please remove this comment and put the quiet/verbose stuff # then please remove this comment and put the quiet/verbose stuff
@ -49,18 +49,22 @@ class Messenger:
to this event, otherwise it will respond only once. to this event, otherwise it will respond only once.
""" """
if Messenger.notify.getDebug(): if Messenger.notify.getDebug():
Messenger.notify.debug('object: ' + `object` Messenger.notify.debug("object: %s\n accepting: %s\n method: %s\n extraArgs: %s\n persistent: %s" %
+ '\n now accepting: ' + `event` (object, event, method, extraArgs, persistent))
+ '\n method: ' + `method`
+ '\n extraArgs: ' + `extraArgs` # Make the the method is callable
+ '\n persistent: ' + `persistent`) assert callable(method), ("method not callable in accept (ignoring): %s %s"%
if not callable(method):
self.notify.warning(
"method not callable in accept (ignoring): %s %s"%
(method, extraArgs)) (method, extraArgs))
return
acceptorDict = self.__callbacks.setdefault(event, {}) acceptorDict = self.__callbacks.setdefault(event, {})
# Make sure we are not inadvertently overwriting an existing event
# on this particular object.
# assert (not acceptorDict.has_key(object)), ("object already accepting %s" %
# (event))
if acceptorDict.has_key(object):
self.notify.warning("object: %s already accepting: %s" % (object.__class__.__name__, event))
acceptorDict[object] = [method, extraArgs, persistent] acceptorDict[object] = [method, extraArgs, persistent]
# Remember that this object is listening for this event # Remember that this object is listening for this event
@ -150,8 +154,8 @@ class Messenger:
Messenger.notify.debug('sent event: ' + event + ' sentArgs: ' + `sentArgs`) Messenger.notify.debug('sent event: ' + event + ' sentArgs: ' + `sentArgs`)
if __debug__: if __debug__:
foundWatch=0 foundWatch=0
if self.isWatching: if self.__isWatching:
for i in self.watching.keys(): for i in self.__watching.keys():
if str(event).find(i) >= 0: if str(event).find(i) >= 0:
foundWatch=1 foundWatch=1
break break
@ -205,12 +209,8 @@ class Messenger:
# we have cleaned up the accept hook, because the # we have cleaned up the accept hook, because the
# method itself might call accept() or acceptOnce() # method itself might call accept() or acceptOnce()
# again. # again.
if callable(method): assert callable(method)
apply(method, (extraArgs + sentArgs)) apply(method, (extraArgs + sentArgs))
else:
self.notify.warning(
"method not callable in send: %s %s %s"%
(method, extraArgs, sentArgs))
def clear(self): def clear(self):
""" """
@ -272,9 +272,9 @@ class Messenger:
See Also: unwatch See Also: unwatch
""" """
if not self.watching.get(needle): if not self.__watching.get(needle):
self.isWatching += 1 self.__isWatching += 1
self.watching[needle]=1 self.__watching[needle]=1
def unwatch(self, needle): def unwatch(self, needle):
""" """
@ -286,9 +286,9 @@ class Messenger:
See Also: watch See Also: watch
""" """
if self.watching.get(needle): if self.__watching.get(needle):
self.isWatching -= 1 self.__isWatching -= 1
del self.watching[needle] del self.__watching[needle]
def quiet(self, message): def quiet(self, message):
""" """