Esempio n. 1
0
    def findLibResources(self, lib, filter=None):

        def getCache(lib):
            cacheId = "resinlib-%s" % lib._path
            liblist = self._genobj._cache.read(cacheId, dependsOn=None, memory=True)
            return liblist, cacheId

        def isSkipFile(f):
            if [x for x in map(lambda x: re.search(x, f), ignoredFiles) if x!=None]:
                return True
            else:
                return False

        # - Main --------------------------------------------------------------
        cacheList    = []  # to poss. populate cache
        cacheId      = ""  # will be filled in getCache()
        ignoredFiles = [r'\.meta$',]  # files not considered as resources

        # create wrapper object
        libObj = LibraryPath(lib, self._genobj._console)
        # retrieve list of library resources
        libList, cacheId = getCache(libObj)
        if libList:
            inCache = True
        else:
            libList = libObj.scanResourcePath()
            inCache = False

        # go through list of library resources and add suitable
        for resource in libList:
            # scanResourcePath() yields absolute paths to a resource, but
            # we only want to match against the 'resource' part of it
            resourcePart = Path.getCommonPrefix(libObj._resourcePath, resource)[2]
            if not inCache:
                cacheList.append(resource)
            if isSkipFile(resource):
                continue
            elif (filter and not filter(resourcePart)):
                continue
            else:
                yield resource

        if not inCache:
            # cache write
            self._genobj._cache.write(cacheId, cacheList, memory=True, writeToFile=False)

        return
Esempio n. 2
0
    def updateTranslations(self, namespace, translationDir, localesList=None):
        self._console.info("Updating namespace: %s" % namespace)
        self._console.indent()
        
        self._console.debug("Looking up relevant class files...")
        classList = []
        classes = self._classes
        for classId in classes:
            if classes[classId]["namespace"] == namespace:
                classList.append(classId)
                    
        self._console.debug("Compiling filter...")
        pot = self.getPotFile(classList)
        pot.sort()

        allfiles = self._translation[namespace]
        if localesList == None:
            filenames = allfiles.keys()
        else:
            filenames = localesList
            for name in filenames:
                if name not in allfiles:
                    path = os.path.join(translationDir, name + ".po")
                    f    = open(path, 'w')  # create stanza file
                    pof  = self.createPoFile()
                    f.write(str(pof))
                    f.close()
                    allfiles[name] = LibraryPath.translationEntry(name, path, namespace)

        self._console.info("Updating %d translations..." % len(filenames))
        self._console.indent()

        for name in filenames:
            self._console.debug("Processing: %s" % name)

            entry = allfiles[name]
            po = polib.pofile(entry["path"])
            po.merge(pot)
            po.sort()
            po.save(entry["path"])

        self._console.outdent()
        self._console.outdent()
    def findAllResources(self, libraries, filter=None):
        """Find relevant resources/assets, implementing shaddowing of resources.
           Returns a list of resources, each a pair of [file_path, uri]"""

        # - Helpers -----------------------------------------------------------

        def getCache(lib):
            cacheId = "resinlib-%s" % lib._path
            liblist = self._genobj._cache.read(cacheId, dependsOn=None, memory=True)
            return liblist, cacheId

        def isSkipFile(f):
            if [x for x in map(lambda x: re.search(x, f), ignoredFiles) if x!=None]:
                return True
            else:
                return False

        def resourceValue(r):
            # create a pair res = [path, uri] for this resource...
            rsource = os.path.normpath(r)  # normalize "./..."
            relpath = (Path.getCommonPrefix(libObj._resourcePath, rsource))[2]
            if relpath[0] == os.sep:  # normalize "/..."
                relpath = relpath[1:]
            ruri = (self._genobj._computeResourceUri(lib, relpath, rType='resource', 
                                                        appRoot=self._genobj.approot))

            return (rsource, ruri)

            
        # - Main --------------------------------------------------------------

        result       = []
        cacheList    = []  # to poss. populate cache
        cacheId      = ""  # will be filled in getCache()
        ignoredFiles = [r'\.meta$',]  # files not considered as resources
        libs         = libraries[:]
        #libs.reverse()     # this is to search the 'important' libs first

        # go through all libs (weighted) and collect necessary resources
        for lib in libs:
            # create wrapper object
            libObj = LibraryPath(lib, self._genobj._console)
            # retrieve list of library resources
            libList, cacheId = getCache(libObj)
            if libList:
                inCache = True
            else:
                libList = libObj.scanResourcePath()
                inCache = False

            # go through list of library resources and add suitable
            for resource in libList:
                # scanResourcePath() yields absolute paths to a resource, but
                # we only want to match against the 'resource' part of it
                resourcePart = Path.getCommonPrefix(libObj._resourcePath, resource)[2]
                if not inCache:
                    cacheList.append(resource)
                if isSkipFile(resource):
                    continue
                elif (filter and not filter(resourcePart)):
                    continue
                else:
                    result.append(resource)

            if not inCache:
                # cache write
                self._genobj._cache.write(cacheId, cacheList, memory=True, writeToFile=False)

        return result
Esempio n. 4
0
    def updateTranslations(self, namespace, translationDir, localesList=None):

        def parsePOEntryStrings(poset):
            for poentry in poset:
                poentry.msgid        = self.parseAsUnicodeString(poentry.msgid)
                poentry.msgid_plural = self.parseAsUnicodeString(poentry.msgid_plural)
                if poentry.msgstr_plural:
                    for pos in poentry.msgstr_plural:
                        poentry.msgstr_plural[pos] = self.parseAsUnicodeString(poentry.msgstr_plural[pos])

        def unescapeMsgIds(poset):
            for poentry in poset:
                if poentry.msgid.find(r'\\') > -1:
                    poentry.msgid = self.recoverBackslashEscapes(poentry.msgid)

        self._console.info("Updating namespace: %s" % namespace)
        self._console.indent()
        
        self._console.debug("Looking up relevant class files...")
        classList = []
        classes = self._classes
        for classId in classes:
            if classes[classId]["namespace"] == namespace:
                classList.append(classId)
                    
        self._console.debug("Compiling filter...")
        pot = self.getPotFile(classList)
        pot.sort()

        allLocales = self._translation[namespace]
        if localesList == None:
            selectedLocales = allLocales.keys()
        else:
            selectedLocales = localesList
            for locale in selectedLocales:
                if locale not in allLocales:
                    path = os.path.join(translationDir, locale + ".po")
                    f    = open(path, 'w')  # create stanza file
                    pof  = self.createPoFile()
                    f.write(str(pof))
                    f.close()
                    allLocales[locale] = LibraryPath.translationEntry(locale, path, namespace)

        self._console.info("Updating %d translations..." % len(selectedLocales))
        self._console.indent()

        for locale in selectedLocales:
            self._console.debug("Processing: %s" % locale)
            self._console.indent()

            entry = allLocales[locale]
            po = polib.pofile(entry["path"])
            po.merge(pot)
            po.sort()
            self._console.debug("Percent translated: %d" % (po.percent_translated(),))
            #po.save(entry["path"])
            poString = str(po)
            #poString = self.recoverBackslashEscapes(poString)
            filetool.save(entry["path"], poString)
            self._console.outdent()

        self._console.outdent()
        self._console.outdent()