Messenger.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. from PythonUtil import *
  2. from DirectNotifyGlobal import *
  3. class Messenger:
  4. notify = None
  5. def __init__(self):
  6. """ __init__(self)
  7. One dictionary does it all. It has the following structure:
  8. {event1 : {object1: [method, extraArgs, persistent],
  9. object2: [method, extraArgs, persistent]},
  10. event2 : {object1: [method, extraArgs, persistent],
  11. object2: [method, extraArgs, persistent]}}
  12. Or, for an example with more real data:
  13. {'mouseDown' : {avatar : [avatar.jump, [2.0], 1]}}
  14. """
  15. self.dict = {}
  16. if (Messenger.notify == None):
  17. Messenger.notify = directNotify.newCategory("Messenger")
  18. # Messenger.notify.setDebug(1)
  19. def accept(self, event, object, method, extraArgs=[], persistent=1):
  20. """ accept(self, string, DirectObject, Function, List, Boolean)
  21. Make this object accept this event. When the event is
  22. sent (using Messenger.send or from C++), method will be executed,
  23. optionally passing in extraArgs.
  24. If the persistent flag is set, it will continue to respond
  25. to this event, otherwise it will respond only once.
  26. """
  27. Messenger.notify.debug('object: ' + `object`
  28. + '\n accept: ' + `event`
  29. + '\n method: ' + `method`
  30. + '\n extraArgs: ' + `extraArgs`
  31. + '\n persistent: ' + `persistent`)
  32. acceptorDict = ifAbsentPut(self.dict, event, {})
  33. acceptorDict[object] = [method, extraArgs, persistent]
  34. def ignore(self, event, object):
  35. """ ignore(self, string, DirectObject)
  36. Make this object no longer respond to this event.
  37. It is safe to call even if it was not alread
  38. """
  39. Messenger.notify.debug(`object` + '\n ignore: ' + `event`)
  40. if self.dict.has_key(event):
  41. # Find the dictionary of all the objects accepting this event
  42. acceptorDict = self.dict[event]
  43. # If this object is there, delete it from the dictionary
  44. if acceptorDict.has_key(object):
  45. del acceptorDict[object]
  46. # If this dictionary is now empty, remove the event
  47. # entry from the Messenger alltogether
  48. if (len(acceptorDict) == 0):
  49. del self.dict[event]
  50. def isAccepting(self, event, object):
  51. """ isAccepting(self, string, DirectOject)
  52. Is this object accepting this event?
  53. """
  54. if self.dict.has_key(event):
  55. if self.dict[event].has_key(object):
  56. # Found it, return true
  57. return 1
  58. # If we looked in both dictionaries and made it here
  59. # that object must not be accepting that event.
  60. return 0
  61. def isIgnoring(self, event, object):
  62. """ isIgnorning(self, string, DirectObject)
  63. Is this object ignoring this event?
  64. """
  65. return (not self.isAccepting(event, object))
  66. def send(self, event, sentArgs=[]):
  67. """ send(self, string, [arg1, arg2,...])
  68. Send this event, optionally passing in arguments
  69. """
  70. # Do not print the new frame debug, it is too noisy!
  71. if (event != 'NewFrame'):
  72. Messenger.notify.debug('sent event: ' + event + ' sentArgs: ' + `sentArgs`)
  73. if self.dict.has_key(event):
  74. acceptorDict = self.dict[event]
  75. for object in acceptorDict.keys():
  76. method, extraArgs, persistent = acceptorDict[object]
  77. apply(method, (extraArgs + sentArgs))
  78. # If this object was only accepting this event once,
  79. # remove it from the dictionary
  80. if not persistent:
  81. del acceptorDict[object]
  82. # If this dictionary is now empty, remove the event
  83. # entry from the Messenger alltogether
  84. if (len(acceptorDict) == 0):
  85. del self.dict[event]
  86. def clear(self):
  87. """clear(self)
  88. Start fresh with a clear dict
  89. """
  90. self.dict.clear()
  91. def listAllEvents(self):
  92. str = 'Messenger\n'
  93. str = str + '='*50 + '\n'
  94. keys = self.dict.keys()
  95. keys.sort()
  96. for event in keys:
  97. str = str + 'Event: ' + event + '\n'
  98. acceptorDict = self.dict[event]
  99. for object in acceptorDict.keys():
  100. method, extraArgs, persistent = acceptorDict[object]
  101. className = object.__class__.__name__
  102. methodName = method.__name__
  103. str = str + '\t' + className + '.' + methodName + '('
  104. if extraArgs:
  105. str = str + `extraArgs` + ' + '
  106. str = str + 'sentArgs)\n'
  107. str = str + '\n'
  108. str = str + '='*50 + '\n'
  109. print str
  110. def __repr__(self):
  111. """__repr__(self)
  112. Print out the table in a readable format
  113. """
  114. import types
  115. str = 'Messenger\n'
  116. str = str + '='*50 + '\n'
  117. keys = self.dict.keys()
  118. keys.sort()
  119. for event in keys:
  120. acceptorDict = self.dict[event]
  121. str = str + 'Event: ' + event + '\n'
  122. for object in acceptorDict.keys():
  123. method, extraArgs, persistent = acceptorDict[object]
  124. if (type(object) == types.InstanceType):
  125. className = object.__class__.__name__
  126. else:
  127. className = "Not a class"
  128. methodName = method.__name__
  129. str = (str + '\t' +
  130. 'Acceptor: ' + className + ' instance' + '\n\t' +
  131. 'Method: ' + methodName + '\n\t' +
  132. 'Extra Args: ' + `extraArgs` + '\n\t' +
  133. 'Persistent: ' + `persistent` + '\n\n'
  134. )
  135. str = str + '='*50 + '\n'
  136. return str