ClientDistClass.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """ClientDistClass module: contains the ClientDistClass class"""
  2. from PandaModules import *
  3. import DirectNotifyGlobal
  4. import ClientDistUpdate
  5. class ClientDistClass:
  6. notify = DirectNotifyGlobal.directNotify.newCategory("ClientDistClass")
  7. def __init__(self, dcClass):
  8. self.number = dcClass.getNumber()
  9. self.name = dcClass.getName()
  10. self.allFields = self.parseFields(dcClass)
  11. self.allCDU = self.createAllCDU(self.allFields)
  12. self.number2CDU = self.createNumber2CDUDict(self.allCDU)
  13. self.name2CDU = self.createName2CDUDict(self.allCDU)
  14. self.broadcastRequiredCDU = self.listBroadcastRequiredCDU(self.allCDU)
  15. self.allRequiredCDU = self.listRequiredCDU(self.allCDU)
  16. # Import the class, and store the constructor
  17. try:
  18. exec("import " + self.name)
  19. self.constructor = eval(self.name + "." + self.name)
  20. except ImportError, e:
  21. self.notify.warning("Unable to import %s.py: %s" % (self.name, e))
  22. self.constructor = None
  23. except (NameError, AttributeError), e:
  24. self.notify.warning("%s.%s does not exist: %s" % (self.name, self.name, e))
  25. self.constructor = None
  26. return None
  27. def parseFields(self, dcClass):
  28. fields=[]
  29. for i in range(0,dcClass.getNumInheritedFields()):
  30. fields.append(dcClass.getInheritedField(i))
  31. return fields
  32. def createAllCDU(self, allFields):
  33. allCDU = []
  34. for i in allFields:
  35. allCDU.append(ClientDistUpdate.ClientDistUpdate(self, i))
  36. return allCDU
  37. def createNumber2CDUDict(self, allCDU):
  38. dict={}
  39. for i in allCDU:
  40. dict[i.number] = i
  41. return dict
  42. def createName2CDUDict(self, allCDU):
  43. dict={}
  44. for i in allCDU:
  45. dict[i.name] = i
  46. return dict
  47. def listBroadcastRequiredCDU(self, allCDU):
  48. requiredCDU = []
  49. for i in allCDU:
  50. atom = i.field.asAtomicField()
  51. if atom:
  52. if (atom.isRequired() and atom.isBroadcast()):
  53. requiredCDU.append(i)
  54. return requiredCDU
  55. def listRequiredCDU(self, allCDU):
  56. requiredCDU = []
  57. for i in allCDU:
  58. atom = i.field.asAtomicField()
  59. if atom:
  60. if (atom.isRequired()):
  61. requiredCDU.append(i)
  62. return requiredCDU
  63. def updateField(self, do, di):
  64. # Get the update field id
  65. fieldId = di.getArg(STUint16)
  66. # look up the CDU
  67. assert(self.number2CDU.has_key(fieldId))
  68. cdu = self.number2CDU[fieldId]
  69. # Let the cdu finish the job
  70. cdu.updateField(self, do, di)
  71. return None
  72. def sendUpdate(self, cr, do, fieldName, args, sendToId = None):
  73. # Look up the cdu
  74. assert(self.name2CDU.has_key(fieldName))
  75. cdu = self.name2CDU[fieldName]
  76. # Let the cdu finish the job
  77. cdu.sendUpdate(cr, do, args, sendToId)