better DistObj importing

This commit is contained in:
Joe Shochet 2004-04-27 01:35:00 +00:00
parent d7871b69f2
commit 283bee6b35
2 changed files with 18 additions and 29 deletions

View File

@ -4,6 +4,7 @@ from PandaModules import *
import DirectNotifyGlobal import DirectNotifyGlobal
import ClientDistUpdate import ClientDistUpdate
import sys import sys
import ihooks
# These are stored here so that the distributed classes we load on the fly # These are stored here so that the distributed classes we load on the fly
# can be exec'ed in the module namespace as if we imported them normally. # can be exec'ed in the module namespace as if we imported them normally.
@ -24,27 +25,19 @@ class ClientDistClass:
self.broadcastRequiredCDU = self.listBroadcastRequiredCDU(self.allCDU) self.broadcastRequiredCDU = self.listBroadcastRequiredCDU(self.allCDU)
self.allRequiredCDU = self.listRequiredCDU(self.allCDU) self.allRequiredCDU = self.listRequiredCDU(self.allCDU)
# Import the class, and store the constructor stuff = ihooks.current_importer.get_loader().find_module(self.name)
try: if not stuff:
exec("import " + self.name, moduleGlobals, moduleLocals) self.notify.warning("Unable to import %s.py" % (self.name))
except ImportError, e:
self.notify.warning("Unable to import %s.py: %s" % (self.name, e))
self.constructor = None self.constructor = None
return return
try: module = __import__(self.name, moduleGlobals, moduleLocals)
self.constructor = eval(self.name + "." + self.name) self.constructor = getattr(module, self.name, None)
except (NameError, AttributeError), e:
self.notify.warning("%s.%s does not exist: %s" % (self.name, self.name, e))
self.constructor = None
# If this assertion fails, you probably had an import error in # If this assertion fails, you probably had an import error in
# a file named in your toon.dc file, or in some file included # a file named in your toon.dc file, or in some file included
# in a file named in your toon.dc file. # in a file named in your toon.dc file.
assert(self.constructor != None) assert(self.constructor != None)
return
def parseFields(self, dcClass): def parseFields(self, dcClass):
fields=[] fields=[]
for i in range(0,dcClass.getNumInheritedFields()): for i in range(0,dcClass.getNumInheritedFields()):

View File

@ -3,6 +3,7 @@
import DirectNotifyGlobal import DirectNotifyGlobal
from PyDatagram import PyDatagram from PyDatagram import PyDatagram
from MsgTypes import * from MsgTypes import *
import ihooks
# These are stored here so that the distributed classes we load on the fly # These are stored here so that the distributed classes we load on the fly
# can be exec'ed in the module namespace as if we imported them normally. # can be exec'ed in the module namespace as if we imported them normally.
@ -21,24 +22,19 @@ class ClientDistUpdate:
self.types = [] self.types = []
self.divisors = [] self.divisors = []
self.deriveTypesFromParticle(dcField) self.deriveTypesFromParticle(dcField)
# Figure out our function pointer
try: stuff = ihooks.current_importer.get_loader().find_module(cdc.name)
exec("import " + cdc.name, moduleGlobals, moduleLocals) if not stuff:
except ImportError, e: # This will be printed by ClientDistClass
# Don't bother reporting this error; the ClientDistClass # self.notify.warning("Unable to import %s.py" % (cdc.name))
# will catch it.
#self.notify.warning("Unable to import %s.py: %s" % (cdc.name, e))
self.func = None self.func = None
return return
try: module = __import__(cdc.name, moduleGlobals, moduleLocals)
self.func = eval(cdc.name + "." + cdc.name + "." + self.name) # If there is no class here, that is an error
except (NameError, AttributeError), e: classObj = getattr(module, cdc.name)
# Only catch name and attribute errors # If there is no func, it will just be None
# as all other errors are legit errors self.func = getattr(classObj, self.name, None)
#self.notify.warning(cdc.name + "." + self.name + " does not exist")
self.func = None
return
def deriveTypesFromParticle(self, dcField): def deriveTypesFromParticle(self, dcField):
dcFieldAtomic = dcField.asAtomicField() dcFieldAtomic = dcField.asAtomicField()