def getKeysAndValuesByPath(path): if path is None: Log.error('file path is None') return Log.info('parse %s' % path) keys = [] values = [] doc = minidom.parse(path) root = doc.documentElement for node in root.childNodes: if node.nodeType == Node.TEXT_NODE: pass elif node.nodeType == Node.COMMENT_NODE: keys.append(Contant.KEY_DEV_COMMENT) values.append(node.nodeValue) elif node.nodeType == Node.ELEMENT_NODE: retValue, ok = _stringElementKeyValue(node) if ok: keys.append(retValue[0]) values.append(retValue[1]) else: Log.error('not support node: %s %d %s' % (node.nodeName, node.nodeType, node.nodeValue)) return keys, values
def getKeysAndValuesByPath(path): if path is None: Log.error('file path is None') return Log.info('parse %s' % path) keys = [] values = [] file = codecs.open(path, mode='r', encoding='utf-8') while True: lines = file.readlines(sizehint=1000) if not lines: break for line in lines: retvalue = re.split(r'"[ ]*=[ ]*"', line, maxsplit=1) if len(retvalue) > 1: keys.append(retvalue[0].lstrip()[1:]) values.append(retvalue[1].rstrip().rstrip(' ;')[:-1]) else: result = re.split(r'/\*.+\*/\n', line, maxsplit=1) if len(result) == 2: keys.append(Contant.KEY_DEV_COMMENT) values.append(line.strip().replace('/*', '').replace( '*/', '')) return keys, values
def startConvert(xlsPath, targetFolder): if xlsPath is not None: if targetFolder is None: Log.error("targetFolder is None!use -h for help.") return Log.info("read xls file from" + xlsPath) reader = ExcelReader(xlsPath) table = reader.getTableByIndex(0) convertExcelTableToStringsXml(table, targetFolder) Log.info("Finished,go to see it -> " + targetFolder) else: Log.error("file path is None!use -h for help.")
def _elementKeyValue(elementNode): key = elementNode.getAttribute('name').strip() value = '' for node in elementNode.childNodes: if node.nodeType == Node.TEXT_NODE: v = node.nodeValue.strip() if v != None and v != '': value = v elif node.nodeType == Node.ELEMENT_NODE: k, v = _elementKeyValue(node) value = node.nodeName + Contant.ELEMENT_TAG_DEVIDER + v else: Log.error('not text node in %s: %s %d %s' % (key, node.nodeName, node.nodeType, node.nodeValue)) return key, value
def getKeyValueDictByPath(path): if path is None: Log.error('file path is None') return Log.info('parse %s' % path) keyvalue = {} file = codecs.open(path, mode='r', encoding='utf-8') while True: lines = file.readlines(sizehint=1000) if not lines: break for line in lines: retvalue = re.split(r'"[ ]*=[ ]*"', line, maxsplit=1) if len(retvalue) > 1: k = retvalue[0].lstrip()[1:] v = retvalue[1].rstrip().rstrip(' ;')[:-1] keyvalue[k] = v return keyvalue
def getKeyValueDictByPath(path): if path is None: Log.error('file path is None') return Log.info('parse %s' % path) languageDict = {} doc = minidom.parse(path) root = doc.documentElement nodeList = root.getElementsByTagName('string') for node in nodeList: if node.nodeType == Node.ELEMENT_NODE: retValue, ok = _stringElementKeyValue(node) if ok: languageDict[retValue[0]] = retValue[1] else: Log.error('not support node: %s %d %s' % (node.nodeName, node.nodeType, node.nodeValue)) return languageDict
def parseXMLToCheckKeyUniqueAt(filepath): if filepath is None or filepath == '': Log.error('filepath is empty') return Log.info('parsing %s' % filepath) doc = minidom.parse(filepath) root = doc.documentElement nodeList = root.getElementsByTagName('string') keyDict = dict() for node in nodeList: if node.nodeType == Node.ELEMENT_NODE: key = node.getAttribute('name').strip() count = keyDict.get(key, 0) keyDict[key] = count + 1 for k, v in keyDict.iteritems(): if v > 1: Log.error('%s appear %d!!!' % (k, v))
def parseStringsToCheckKeyUniqueAt(filepath): if filepath is None: Log.error('file path is None') return Log.info('parsing %s' % filepath) keyDict = {} file = codecs.open(filepath, mode='r', encoding='utf-8') while True: lines = file.readlines(sizehint=1000) if not lines: break for line in lines: retvalue = re.split(r'"[ ]*=[ ]*"', line, maxsplit=1) if len(retvalue) > 1: k = retvalue[0].lstrip()[1:] count = keyDict.get(k, 0) keyDict[k] = count + 1 for k, v in keyDict.iteritems(): if v > 1: Log.error('%s appear %d!!!' % (k, v))
def startConvert(sourceFolder, xlsFolder, apptype): if sourceFolder is not None: if xlsFolder is not None: workbook = pyExcelerator.Workbook() ws = workbook.add_sheet('Localizable.strings') # init keyName and standard language value (en) stKeys, stValues = _getStandardKeyValuesListFrom( sourceFolder, apptype) if len(stKeys) <= 0: Log.error('The language files is empty') return ws.write(0, 0, 'keyName') ws.write(0, 1, 'en') for row in range(len(stKeys)): ws.write(row + 1, 0, stKeys[row]) ws.write(row + 1, 1, stValues[row]) # append other language value index = 2 for parent, dirnames, filenames in os.walk(sourceFolder): for dirname in dirnames: conturyCode = _getCountryCode(dirname, apptype) if conturyCode == 'en': continue ws.write(0, index, conturyCode) otherLanguage = _getKeyValuesDictFrom( os.path.join(sourceFolder, dirname), apptype) for row in range(len(stKeys)): if stKeys[row] in otherLanguage: ws.write(row + 1, index, otherLanguage[stKeys[row]]) index += 1 filePath = os.path.join(xlsFolder, "Localizable.xls") workbook.save(filePath) Log.info("Convert successfully! you can see xls file in %s" % (filePath)) else: Log.error("xls folder path can not be empty! try -h for help.") else: Log.error( "strings or xml files filder can not be empty! try -h for help.")