qgc-lupdate-json.py 6.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#!/usr/bin/env python
import os
import json
from xml.dom.minidom import parse
import xml.dom.minidom
import codecs
import sys

qgcFileTypeKey =        "fileType"
translateKeysKey =      "translateKeys"
arrayIDKeysKey =        "arrayIDKeys"
disambiguationPrefix =  "#loc.disambiguation#"

def parseJsonObjectForTranslateKeys(jsonObjectHierarchy, jsonObject, translateKeys, arrayIDKeys, locStringDict):
    for translateKey in translateKeys:
        if (translateKey in jsonObject):
            locStr = jsonObject[translateKey]
            currentHierarchy = jsonObjectHierarchy + "." + translateKey
            if locStr in locStringDict:
                # Duplicate of an existing string
                locStringDict[locStr].append(currentHierarchy)
            else:
                # First time we are seeing this string
                locStringDict[locStr] = [ currentHierarchy ]
    for key in jsonObject:
        currentHierarchy = jsonObjectHierarchy + "." + key
        if (type(jsonObject[key]) == type({})):
            parseJsonObjectForTranslateKeys(currentHierarchy, jsonObject[key], translateKeys,arrayIDKeys, locStringDict)
        elif (type(jsonObject[key]) == type([])):
            parseJsonArrayForTranslateKeys(currentHierarchy, jsonObject[key], translateKeys, arrayIDKeys, locStringDict)

def parseJsonArrayForTranslateKeys(jsonObjectHierarchy, jsonArray, translateKeys, arrayIDKeys, locStringDict):
    for index in range(0, len(jsonArray)):
        jsonObject = jsonArray[index]
        arrayIndexStr = str(index)
        for arrayIDKey in arrayIDKeys:
            if arrayIDKey in jsonObject.keys():
                arrayIndexStr = jsonObject[arrayIDKey]
                break
        currentHierarchy = jsonObjectHierarchy + "[" + arrayIndexStr + "]"
        parseJsonObjectForTranslateKeys(currentHierarchy, jsonObject, translateKeys, arrayIDKeys, locStringDict)

def addLocKeysBasedOnQGCFileType(jsonPath, jsonDict):
    # Instead of having to add the same keys over and over again in a pile of files we add them here automatically based on file type
    if qgcFileTypeKey in jsonDict:
        qgcFileType = jsonDict[qgcFileTypeKey]
        translateKeyValue =         ""
        arrayIDKeysKeyValue =       ""
        if qgcFileType == "MavCmdInfo":
            translateKeyValue =         "label,enumStrings,friendlyName,description,category"
            arrayIDKeysKeyValue =       "rawName,comment"
        elif qgcFileType == "FactMetaData":
            translateKeyValue =         "shortDescription,longDescription,enumStrings"
            arrayIDKeysKeyValue =       "name"
        if translateKeysKey not in jsonDict and translateKeyValue != "":
            jsonDict[translateKeysKey] = translateKeyValue
        if arrayIDKeysKey not in jsonDict and arrayIDKeysKeyValue != "":
            jsonDict[arrayIDKeysKey] = arrayIDKeysKeyValue

def parseJson(jsonPath, locStringDict):
    jsonFile = open(jsonPath)
    jsonDict = json.load(jsonFile)
    if (type(jsonDict) != type({})):
        return
    addLocKeysBasedOnQGCFileType(jsonPath, jsonDict)
    if (not translateKeysKey in jsonDict):
        return
    translateKeys = jsonDict[translateKeysKey].split(",")
    arrayIDKeys = jsonDict.get(arrayIDKeysKey, "").split(",")
    parseJsonObjectForTranslateKeys("", jsonDict, translateKeys, arrayIDKeys, locStringDict)

def walkDirectoryTreeForJsonFiles(dir, multiFileLocArray):
    for filename in os.listdir(dir):
        path = os.path.join(dir, filename)
        if (os.path.isfile(path) and filename.endswith(".json")):
            #print "json",path
            singleFileLocStringDict = {}
            parseJson(path, singleFileLocStringDict)
            if len(singleFileLocStringDict.keys()):
                # Check for duplicate file names
                for entry in multiFileLocArray:
                    if entry[0] == filename:
                        print "Error: Duplicate filenames: %s paths: %s %s" % (filename, path, entry[1])
                        sys.exit(1)
                multiFileLocArray.append([filename, path, singleFileLocStringDict])
        if (os.path.isdir(path)):
            walkDirectoryTreeForJsonFiles(path, multiFileLocArray)

89 90
def writeJsonTSFile(multiFileLocArray):
    jsonTSFile = codecs.open('qgc-json.ts', 'w', "utf-8")
DonLakeFlyer's avatar
DonLakeFlyer committed
91 92 93
    jsonTSFile.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
    jsonTSFile.write("<!DOCTYPE TS>\n")
    jsonTSFile.write("<TS version=\"2.1\">\n")
94
    for entry in multiFileLocArray:
95 96
        jsonTSFile.write("<context>\n")
        jsonTSFile.write("    <name>%s</name>\n" % entry[0])
97 98 99 100 101 102 103 104 105 106 107
        singleFileLocStringDict = entry[2]
        for locStr in singleFileLocStringDict.keys():
            disambiguation = ""
            if locStr.startswith(disambiguationPrefix):
                workStr = locStr[len(disambiguationPrefix):]
                terminatorIndex = workStr.find("#")
                if terminatorIndex == -1:
                    print "Bad disambiguation %1 '%2'" % (entry[0], locStr)
                    sys.exit(1)
                disambiguation = workStr[:terminatorIndex]
                locStr = workStr[terminatorIndex+1:]
108
            jsonTSFile.write("    <message>\n")
109
            if len(disambiguation):
110
                jsonTSFile.write("        <comment>%s</comment>\n" % disambiguation)
111 112 113
            extraCommentStr = ""
            for jsonHierachy in singleFileLocStringDict[locStr]:
                extraCommentStr += "%s, " % jsonHierachy
114 115 116 117 118 119 120 121
            jsonTSFile.write("        <extracomment>%s</extracomment>\n" % extraCommentStr)
            jsonTSFile.write("        <location filename=\"%s\"/>\n" % entry[1])
            jsonTSFile.write(unicode("        <source>%s</source>\n") % locStr)
            jsonTSFile.write("        <translation type=\"unfinished\"></translation>\n")
            jsonTSFile.write("    </message>\n")
        jsonTSFile.write("</context>\n")
    jsonTSFile.write("</TS>\n")
    jsonTSFile.close()
122 123 124 125

def main():
    multiFileLocArray = []
    walkDirectoryTreeForJsonFiles("../src", multiFileLocArray)
126
    writeJsonTSFile(multiFileLocArray)
127 128 129

if __name__ == '__main__':
    main()