Example #1
0
    def __init__(self, entityTypeModule):
        self.entTypeModule = entityTypeModule
        hv = HashVal()
        import EntityTypes
        reload(EntityTypes)
        reload(self.entTypeModule)

        def getPyExtVersion(filename):
            base, ext = os.path.splitext(filename)
            if ext == '.pyc' or ext == '.pyo':
                filename = base + '.py'
            return filename

        fileLines = file(getPyExtVersion(EntityTypes.__file__)).readlines()
        hv.hashString(string.join(fileLines, ''))
        s = str(hv.asHex())
        s += '.'
        fileLines = file(getPyExtVersion(self.entTypeModule.__file__)).readlines()
        hv.hashString(string.join(fileLines, ''))
        s += str(hv.asHex())
        self.hashStr = s
        getPyExtVersion = None
        classes = []
        for key, value in entityTypeModule.__dict__.items():
            if type(value) is types.ClassType:
                if issubclass(value, EntityTypeDesc.EntityTypeDesc):
                    classes.append(value)

        self.entTypeName2typeDesc = {}
        mostDerivedLast(classes)
        for c in classes:
            if c.__dict__.has_key('type'):
                if self.entTypeName2typeDesc.has_key(c.type):
                    EntityTypeRegistry.notify.debug("replacing %s with %s for entity type '%s'" % (self.entTypeName2typeDesc[c.type].__class__, c, c.type))
                self.entTypeName2typeDesc[c.type] = c()

        self.output2typeNames = {}
        for typename, typeDesc in self.entTypeName2typeDesc.items():
            if typeDesc.isConcrete():
                if hasattr(typeDesc, 'output'):
                    outputType = typeDesc.output
                    self.output2typeNames.setdefault(outputType, [])
                    self.output2typeNames[outputType].append(typename)

        self.permanentTypeNames = []
        for typename, typeDesc in self.entTypeName2typeDesc.items():
            if typeDesc.isPermanent():
                self.permanentTypeNames.append(typename)

        self.typeName2derivedTypeNames = {}
        for typename, typeDesc in self.entTypeName2typeDesc.items():
            typenames = []
            for tn, td in self.entTypeName2typeDesc.items():
                if td.isConcrete():
                    if issubclass(td.__class__, typeDesc.__class__):
                        typenames.append(tn)

            self.typeName2derivedTypeNames[typename] = typenames

        return
    def __init__(self, entityTypeModule):
        self.entTypeModule = entityTypeModule
        hv = HashVal()
        from . import EntityTypes
        importlib.reload(EntityTypes)
        importlib.reload(self.entTypeModule)

        def getPyExtVersion(filename):
            base, ext = os.path.splitext(filename)
            if ext == '.pyc' or ext == '.pyo':
                filename = base + '.py'
            return filename

        fileLines = file(getPyExtVersion(EntityTypes.__file__)).readlines()
        hv.hashString(string.join(fileLines, ''))
        s = str(hv.asHex())
        s += '.'
        fileLines = file(getPyExtVersion(self.entTypeModule.__file__)).readlines()
        hv.hashString(string.join(fileLines, ''))
        s += str(hv.asHex())
        self.hashStr = s
        getPyExtVersion = None
        classes = []
        for key, value in list(entityTypeModule.__dict__.items()):
            if type(value) is type:
                if issubclass(value, EntityTypeDesc.EntityTypeDesc):
                    classes.append(value)

        self.entTypeName2typeDesc = {}
        mostDerivedLast(classes)
        for c in classes:
            if 'type' in c.__dict__:
                if c.type in self.entTypeName2typeDesc:
                    EntityTypeRegistry.notify.debug("replacing %s with %s for entity type '%s'" % (self.entTypeName2typeDesc[c.type].__class__, c, c.type))
                self.entTypeName2typeDesc[c.type] = c()

        self.output2typeNames = {}
        for typename, typeDesc in list(self.entTypeName2typeDesc.items()):
            if typeDesc.isConcrete():
                if hasattr(typeDesc, 'output'):
                    outputType = typeDesc.output
                    self.output2typeNames.setdefault(outputType, [])
                    self.output2typeNames[outputType].append(typename)

        self.permanentTypeNames = []
        for typename, typeDesc in list(self.entTypeName2typeDesc.items()):
            if typeDesc.isPermanent():
                self.permanentTypeNames.append(typename)

        self.typeName2derivedTypeNames = {}
        for typename, typeDesc in list(self.entTypeName2typeDesc.items()):
            typenames = []
            for tn, td in list(self.entTypeName2typeDesc.items()):
                if td.isConcrete():
                    if issubclass(td.__class__, typeDesc.__class__):
                        typenames.append(tn)

            self.typeName2derivedTypeNames[typename] = typenames

        return
Example #3
0
    def privCompileAttribDescs(entTypeClass):
        if entTypeClass.__dict__.has_key('_attribDescs'):
            return None

        c = entTypeClass
        EntityTypeDesc.notify.debug('compiling attrib descriptors for %s' %
                                    c.__name__)
        for base in c.__bases__:
            EntityTypeDesc.privCompileAttribDescs(base)

        blockAttribs = c.__dict__.get('blockAttribs', [])
        baseADs = []
        bases = list(c.__bases__)
        mostDerivedLast(bases)
        for base in bases:
            for desc in base._attribDescs:
                if desc.getName() in blockAttribs:
                    continue

                for d in baseADs:
                    if desc.getName() == d.getName():
                        EntityTypeDesc.notify.warning(
                            '%s inherits attrib %s from multiple bases' %
                            (c.__name__, desc.getName()))
                        break
                        continue

        attribDescs = []
        if c.__dict__.has_key('attribs'):
            for attrib in c.attribs:
                desc = AttribDesc.AttribDesc(*attrib)
                if desc.getName(
                ) == 'type' and entTypeClass.__name__ != 'Entity':
                    EntityTypeDesc.notify.error(
                        "(%s): '%s' is a reserved attribute name" %
                        (entTypeClass.__name__, desc.getName()))

                for ad in baseADs:
                    if ad.getName() == desc.getName():
                        baseADs.remove(ad)
                        break
                        continue

                attribDescs.append(desc)

        c._attribDescs = baseADs + attribDescs
 def privCompileAttribDescs(entTypeClass):
     if entTypeClass.__dict__.has_key('_attribDescs'):
         return None
     
     c = entTypeClass
     EntityTypeDesc.notify.debug('compiling attrib descriptors for %s' % c.__name__)
     for base in c.__bases__:
         EntityTypeDesc.privCompileAttribDescs(base)
     
     blockAttribs = c.__dict__.get('blockAttribs', [])
     baseADs = []
     bases = list(c.__bases__)
     mostDerivedLast(bases)
     for base in bases:
         for desc in base._attribDescs:
             if desc.getName() in blockAttribs:
                 continue
             
             for d in baseADs:
                 if desc.getName() == d.getName():
                     EntityTypeDesc.notify.warning('%s inherits attrib %s from multiple bases' % (c.__name__, desc.getName()))
                     break
                     continue
             
         
     
     attribDescs = []
     if c.__dict__.has_key('attribs'):
         for attrib in c.attribs:
             desc = AttribDesc.AttribDesc(*attrib)
             if desc.getName() == 'type' and entTypeClass.__name__ != 'Entity':
                 EntityTypeDesc.notify.error("(%s): '%s' is a reserved attribute name" % (entTypeClass.__name__, desc.getName()))
             
             for ad in baseADs:
                 if ad.getName() == desc.getName():
                     baseADs.remove(ad)
                     break
                     continue
             
             attribDescs.append(desc)
         
     
     c._attribDescs = baseADs + attribDescs
Example #5
0
    def privCompileAttribDescs(entTypeClass):
        """this compiles an ordered list of attribDescs for the Entity class
        passed in. The attribute descriptors describe the properties of each
        of the Entity type's attributes"""
        # has someone already compiled the info?
        if ('_attribDescs' in entTypeClass.__dict__) or (entTypeClass.__name__ == 'object'):
            return

        c = entTypeClass
        EntityTypeDesc.notify.debug('compiling attrib descriptors for %s' %
                                    c.__name__)


        bases = list(c.__bases__)
        if object in bases:
            print("Removing built-in `object` from base list")
            bases.remove(object)

        # make sure all of our base classes have their complete list of
        # attribDescs
        for base in bases:
            EntityTypeDesc.privCompileAttribDescs(base)

        # aggregate the attribute descriptors from our direct base classes
        blockAttribs = c.__dict__.get('blockAttribs', [])
        baseADs = []

        # make sure base-class attribs show up before derived-class attribs
        mostDerivedLast(bases)

        for base in bases:
            for desc in base._attribDescs:
                # are we blocking this attribute?
                if desc.getName() in blockAttribs:
                    continue
                    
                # make sure we haven't already picked up this attribute
                # from an earlier base class
                for d in baseADs:
                    if desc.getName() == d.getName():
                        EntityTypeDesc.notify.warning(
                            '%s inherits attrib %s from multiple bases' %
                            (c.__name__, desc.getName()))
                        break
                else:
                    baseADs.append(desc)

        # now that we have all of the descriptors from our base classes,
        # add the descriptors from this class
        attribDescs = []
        if 'attribs' in c.__dict__:
            for attrib in c.attribs:
                desc = AttribDesc.AttribDesc(*attrib)
                
                if (desc.getName() == 'type' and
                    entTypeClass.__name__ != 'Entity'):
                    EntityTypeDesc.notify.error(
                        '(%s): \'%s\' is a reserved attribute name' % (
                        entTypeClass.__name__, desc.getName()))

                # if we picked up an attribute with the same name from a base
                # class, this overrides it
                for ad in baseADs:
                    if ad.getName() == desc.getName():
                        baseADs.remove(ad)
                        # there ought to be no more than one desc with
                        # this name from the base classes
                        assert ad not in baseADs
                        break
                    
                attribDescs.append(desc)

        c._attribDescs = baseADs + attribDescs
Example #6
0
    def __init__(self, entityTypeModule):
        """pass in a module that contains EntityTypeDesc classes"""
        self.entTypeModule = entityTypeModule

        # compute the hash of the source modules as of the time of creation
        hv = HashVal()
        from . import EntityTypes
        importlib.reload(EntityTypes)
        importlib.reload(self.entTypeModule)

        # Convert a pyc or pyo to a py
        # If the client runs genPyCode -n then ihooks will not be installed
        # and you will get a pyc file instead of a py file. Then the AI and
        # client will be mismatched because the AI automatically installs
        # ihooks and thus will hash the py instead of the pyc. Then the
        # hashes do not match and everybody is sad. Let's just make sure
        # for once and for all that we use the .py file and not a .pyc or a
        # .pyo in case that ever happened.
        def getPyExtVersion(filename):
            base, ext = os.path.splitext(filename)
            if (ext == ".pyc") or (ext == ".pyo"):
                filename = base + ".py"
            return filename

        # avoid CR/LF issues by reading the file line by line
        # readlines() converts \r\n to \n
        entType = open(getPyExtVersion(EntityTypes.__file__), 'r')
        fileLines = entType.readlines()
        entType.close()
        hv.hashString(''.join(fileLines))
        s = str(hv.asHex())
        s += '.'
        # avoid CR/LF issues by reading the file line by line
        # readlines() converts \r\n to \n
        entType = open(getPyExtVersion(self.entTypeModule.__file__), 'r')
        fileLines = entType.readlines()
        entType.close()
        hv.hashString(''.join(fileLines))
        s += str(hv.asHex())
        self.hashStr = s

        getPyExtVersion = None

        # get a list of the EntityTypeDesc classes in the type module
        classes = []
        for key, value in list(entityTypeModule.__dict__.items()):
            if type(value) is type:
                if issubclass(value, EntityTypeDesc.EntityTypeDesc):
                    classes.append(value)

        self.entTypeName2typeDesc = {}

        # create an instance of each EntityType class with a typename
        # make sure that derived classes come after bases
        mostDerivedLast(classes)
        for c in classes:
            if 'type' in c.__dict__:
                if c.type in self.entTypeName2typeDesc:
                    # a more-derived class is replacing a less-derived class
                    # to implement a particular entity type
                    EntityTypeRegistry.notify.debug(
                        "replacing %s with %s for entity type '%s'" %
                        (self.entTypeName2typeDesc[c.type].__class__, c,
                         c.type))
                self.entTypeName2typeDesc[c.type] = c()

        # create mapping of entity output types to list of concrete entity
        # typenames with that output type
        self.output2typeNames = {}
        for typename, typeDesc in list(self.entTypeName2typeDesc.items()):
            if typeDesc.isConcrete():
                if hasattr(typeDesc, 'output'):
                    outputType = typeDesc.output
                    self.output2typeNames.setdefault(outputType, [])
                    self.output2typeNames[outputType].append(typename)

        # create list of permanent entity typenames (entity types that cannot
        # be inserted or removed in the editor)
        self.permanentTypeNames = []
        for typename, typeDesc in list(self.entTypeName2typeDesc.items()):
            if typeDesc.isPermanent():
                assert typeDesc.isConcrete()
                self.permanentTypeNames.append(typename)

        # create mapping of entity typename (abstract or concrete) to list
        # of entity typenames are concrete and are of that type or derive
        # from that type
        self.typeName2derivedTypeNames = {}
        for typename, typeDesc in list(self.entTypeName2typeDesc.items()):
            typenames = []
            for tn, td in list(self.entTypeName2typeDesc.items()):
                if td.isConcrete():
                    if issubclass(td.__class__, typeDesc.__class__):
                        typenames.append(tn)
            self.typeName2derivedTypeNames[typename] = typenames