Ejemplo n.º 1
0
def retrieveSavedExportsList(mainControl, wikiData, continuousExport):
    unifNames = wikiData.getDataBlockUnifNamesStartingWith(u"savedexport/")

    result = []
    suppExTypes = PluginManager.getSupportedExportTypes(mainControl,
                None, continuousExport)

    for un in unifNames:
        name = un[12:]
        content = wikiData.retrieveDataBlock(un)
        xmlDoc = minidom.parseString(content)
        xmlNode = xmlDoc.firstChild
        etype = Serialization.serFromXmlUnicode(xmlNode, u"exportTypeName")
        if etype not in suppExTypes:
            # Export type of saved export not supported
            continue

        result.append((name, xmlNode))

    mainControl.getCollator().sortByFirst(result)

    return result
Ejemplo n.º 2
0
def getI18nXrcData(appDir, globalConfigSubDir, baseXrcName):
    """
    Returns the XML data of translated XRC file.
    This function assumes that loadI18nDict() was previously
    called with same appDir.
    """
    global i18nPoPath, i18nLocale
    
    if i18nPoPath is None:
        # No translation
        return loadEntireFile(os.path.join(appDir, baseXrcName + ".xrc"), True)

    # Retrieve modification time of .po and .xrc file to compare with cache
    # Cache is only valid if newer than both
    poModTime = os.stat(pathEnc(i18nPoPath)).st_mtime
    poModTime2 = os.stat(pathEnc(os.path.join(appDir, baseXrcName + ".xrc"))).st_mtime

    poModTime = max(poModTime, poModTime2)

    # First test for cache in appDir
    try:
        cachePath = os.path.join(appDir,
                baseXrcName + "_" + i18nLocale + ".xrc")

        if os.path.exists(pathEnc(cachePath)) and \
                os.stat(pathEnc(cachePath)).st_mtime > poModTime:
            data = _stripI18nXrcCache(loadEntireFile(cachePath, True))
            if data is not None:
                # Valid cache found
                return data
    except:
        traceback.print_exc()  # Really?


    # then test for cache in globalConfigSubDir
    try:
        cachePath = os.path.join(globalConfigSubDir,
                baseXrcName + "_" + i18nLocale + ".xrc")

        if os.path.exists(pathEnc(cachePath)) and \
                os.stat(pathEnc(cachePath)).st_mtime > poModTime:
            # Valid cache found
            data = _stripI18nXrcCache(loadEntireFile(cachePath, True))
            if data is not None:
                # Valid cache found
                return data
    except:
        traceback.print_exc()  # Really?


    # No valid cache -> build content

    untranslated = loadEntireFile(
            os.path.join(appDir, baseXrcName + ".xrc"), True)

    xmlDoc = minidom.parseString(untranslated)
    elementsContainingText = xmlDoc.getElementsByTagName("label") + \
            xmlDoc.getElementsByTagName("title") + \
            xmlDoc.getElementsByTagName("item")

    for le in elementsContainingText:
        childs = le.childNodes
        if len(childs) != 1:
            continue

        child = childs[0]
        if child.nodeType != child.TEXT_NODE:
            continue

        child.data = _(child.data)

    translated = xmlDoc.toxml("utf-8")

    xmlDoc.unlink()


    # The following conversion is only needed when running a Windows
    # binary created by py2exe with wxPython 2.6.
    # Otherwise some mysterious unicode error may ocurr.

    translated = translated.decode("utf-8")
    result = []
    for c in translated:
        o = ord(c)
        if o > 127:
            result.append("&#%i;" % o)
        else:
            result.append(chr(o))

    translated = "".join(result)


    # Add version tag to ensure valid cache
    toCache = Consts.VERSION_STRING + "\n" + translated

    # Try to store content as cache in appDir
    try:
        cachePath = os.path.join(appDir,
                baseXrcName + "_" + i18nLocale + ".xrc")
        
        writeEntireFile(cachePath, toCache, True)
        
        return translated
    except:
        pass
    
    # Try to store content as cache in globalConfigSubDir
    try:
        cachePath = os.path.join(globalConfigSubDir,
                baseXrcName + "_" + i18nLocale + ".xrc")
        
        writeEntireFile(cachePath, toCache, True)

        return translated
    except:
        pass
    
    
    # Cache saving failed

    return translated
def getI18nXrcData(appDir, globalConfigSubDir, baseXrcName):
    """
    Returns the XML data of translated XRC file.
    This function assumes that loadI18nDict() was previously
    called with same appDir.
    """
    global i18nPoPath, i18nLocale

    if i18nPoPath is None:
        # No translation
        return loadEntireFile(os.path.join(appDir, baseXrcName + ".xrc"), True)

    # Retrieve modification time of .po and .xrc file to compare with cache
    # Cache is only valid if newer than both
    poModTime = os.stat(pathEnc(i18nPoPath)).st_mtime
    poModTime2 = os.stat(pathEnc(os.path.join(appDir,
                                              baseXrcName + ".xrc"))).st_mtime

    poModTime = max(poModTime, poModTime2)

    # First test for cache in appDir
    try:
        cachePath = os.path.join(appDir,
                                 baseXrcName + "_" + i18nLocale + ".xrc")

        if os.path.exists(pathEnc(cachePath)) and \
                os.stat(pathEnc(cachePath)).st_mtime > poModTime:
            data = _stripI18nXrcCache(loadEntireFile(cachePath, True))
            if data is not None:
                # Valid cache found
                return data
    except:
        traceback.print_exc()  # Really?

    # then test for cache in globalConfigSubDir
    try:
        cachePath = os.path.join(globalConfigSubDir,
                                 baseXrcName + "_" + i18nLocale + ".xrc")

        if os.path.exists(pathEnc(cachePath)) and \
                os.stat(pathEnc(cachePath)).st_mtime > poModTime:
            # Valid cache found
            data = _stripI18nXrcCache(loadEntireFile(cachePath, True))
            if data is not None:
                # Valid cache found
                return data
    except:
        traceback.print_exc()  # Really?

    # No valid cache -> build content

    untranslated = loadEntireFile(os.path.join(appDir, baseXrcName + ".xrc"),
                                  True)

    xmlDoc = minidom.parseString(untranslated)
    elementsContainingText = xmlDoc.getElementsByTagName("label") + \
            xmlDoc.getElementsByTagName("title") + \
            xmlDoc.getElementsByTagName("item")

    for le in elementsContainingText:
        childs = le.childNodes
        if len(childs) != 1:
            continue

        child = childs[0]
        if child.nodeType != child.TEXT_NODE:
            continue

        child.data = _(child.data)

    translated = xmlDoc.toxml("utf-8")

    xmlDoc.unlink()

    # The following conversion is only needed when running a Windows
    # binary created by py2exe with wxPython 2.6.
    # Otherwise some mysterious unicode error may ocurr.

    translated = translated.decode("utf-8")
    result = []
    for c in translated:
        o = ord(c)
        if o > 127:
            result.append("&#%i;" % o)
        else:
            result.append(chr(o))

    translated = "".join(result)

    # Add version tag to ensure valid cache
    toCache = Consts.VERSION_STRING + "\n" + translated

    # Try to store content as cache in appDir
    try:
        cachePath = os.path.join(appDir,
                                 baseXrcName + "_" + i18nLocale + ".xrc")

        writeEntireFile(cachePath, toCache, True)

        return translated
    except:
        pass

    # Try to store content as cache in globalConfigSubDir
    try:
        cachePath = os.path.join(globalConfigSubDir,
                                 baseXrcName + "_" + i18nLocale + ".xrc")

        writeEntireFile(cachePath, toCache, True)

        return translated
    except:
        pass

    # Cache saving failed

    return translated