Example #1
0
    def modelsInit(cls, initPath, initName):
        out = dict()
        zipIndex = initPath[0].find(PyGlassModelUtils._ZIP_FIND)
        if  zipIndex == -1:
            moduleList = os.listdir(initPath[0])
        else:
            splitIndex = zipIndex+len(PyGlassModelUtils._ZIP_FIND)
            zipPath    = initPath[0][:splitIndex-1]
            modulePath = initPath[0][splitIndex:]
            z          = zipfile.ZipFile(zipPath)
            moduleList = []
            for item in z.namelist():
                item = os.sep.join(item.split('/'))
                if item.startswith(modulePath):
                    moduleList.append(item.rsplit(os.sep, 1)[-1])

        items = []
        for module in moduleList:
            if module.startswith('__init__.py') or module.find('_') == -1:
                continue

            parts    = module.rsplit('.', 1)
            parts[0] = parts[0].rsplit(os.sep, 1)[-1]
            if not parts[-1].startswith('py') or parts[0] in items:
                continue
            items.append(parts[0])

            m = None
            n = None
            r = None
            c = None
            try:
                n = module.rsplit('.', 1)[0]
                m = initName + '.' + n
                r = __import__(m, locals(), globals(), [n])
                c = getattr(r, n)
                out[n] = c
                if not c.__table__.exists(c.ENGINE):
                    c.__table__.create(c.ENGINE, True)

                    from pyglass.alembic.AlembicUtils import AlembicUtils
                    if AlembicUtils.hasAlembic:
                        AlembicUtils.stampDatabase(c.DATABASE_URL)
                        cls._logger.write(
                            u'CREATED: ' + unicode(c) + u' ' + unicode(c.__table__)
                            + u' [STAMPED head]'
                        )
            except Exception, err:
                cls._logger.writeError([
                    u'MODEL INITIALIZATION FAILURE:',
                    u'INIT PATH: ' + unicode(initPath),
                    u'INIT NAME: ' + unicode(initName),
                    u'MODULE IMPORT: ' + unicode(m),
                    u'IMPORT CLASS: ' + unicode(n),
                    u'IMPORT RESULT: ' + unicode(r),
                    u'CLASS RESULT: ' + unicode(c)
                ], err)