DirectObject.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """Undocumented Module"""
  2. __all__ = ['DirectObject']
  3. from direct.directnotify.DirectNotifyGlobal import directNotify
  4. from MessengerGlobal import messenger
  5. from direct.showbase.PythonUtil import ClassTree
  6. class DirectObject:
  7. """
  8. This is the class that all Direct/SAL classes should inherit from
  9. """
  10. def __init__(self):
  11. pass
  12. #def __del__(self):
  13. # This next line is useful for debugging leaks
  14. #print "Destructing: ", self.__class__.__name__
  15. # Wrapper functions to have a cleaner, more object oriented approach to
  16. # the messenger functionality.
  17. def accept(self, event, method, extraArgs=[]):
  18. return messenger.accept(event, self, method, extraArgs, 1)
  19. def acceptOnce(self, event, method, extraArgs=[]):
  20. return messenger.accept(event, self, method, extraArgs, 0)
  21. def ignore(self, event):
  22. return messenger.ignore(event, self)
  23. def ignoreAll(self):
  24. return messenger.ignoreAll(self)
  25. def isAccepting(self, event):
  26. return messenger.isAccepting(event, self)
  27. def getAllAccepting(self):
  28. return messenger.getAllAccepting(self)
  29. def isIgnoring(self, event):
  30. return messenger.isIgnoring(event, self)
  31. def classTree(self):
  32. return ClassTree(self)
  33. #This function must be used if you want a managed task
  34. def addTask(self, *args, **kwargs):
  35. if(not hasattr(self,"_taskList")):
  36. self._taskList = {}
  37. kwargs['owner']=self
  38. task = taskMgr.add(*args, **kwargs)
  39. return task
  40. def doMethodLater(self, *args, **kwargs):
  41. if(not hasattr(self,"_taskList")):
  42. self._taskList ={}
  43. kwargs['owner']=self
  44. task = taskMgr.doMethodLater(*args, **kwargs)
  45. return task
  46. def removeTask(self, taskOrName):
  47. if type(taskOrName) == type(''):
  48. # we must use a copy, since task.remove will modify self._taskList
  49. if hasattr(self, '_taskList'):
  50. taskListValues = self._taskList.values()
  51. for task in taskListValues:
  52. if task.name == taskOrName:
  53. task.remove()
  54. else:
  55. taskOrName.remove()
  56. def removeAllTasks(self):
  57. if hasattr(self,'_taskList'):
  58. for task in self._taskList.values():
  59. task.remove()
  60. def _addTask(self, task):
  61. self._taskList[task.id] = task
  62. def _clearTask(self, task):
  63. del self._taskList[task.id]
  64. def detectLeaks(self):
  65. # call this after the DirectObject instance has been destroyed
  66. # if it's leaking, will notify user
  67. # make sure we're not still listening for messenger events
  68. events = messenger.getAllAccepting(self)
  69. # make sure we're not leaking tasks
  70. # TODO: include tasks that were added directly to the taskMgr
  71. tasks = []
  72. if hasattr(self, '_taskList'):
  73. tasks = [task.name for task in self._taskList.values()]
  74. if len(events) or len(tasks):
  75. estr = choice(len(events), 'listening to events: %s' % events, '')
  76. andStr = choice(len(events) and len(tasks), ' and ', '')
  77. tstr = choice(len(tasks), '%srunning tasks: %s' % (andStr, tasks), '')
  78. notify = directNotify.newCategory('LeakDetect')
  79. func = choice(getRepository()._crashOnProactiveLeakDetect,
  80. self.notify.error, self.notify.warning)
  81. func('destroyed %s instance is still %s%s' % (self.__class__.__name__, estr, tstr))