def get_logs(): try: mysql = MySql() logs = mysql.get_all(LogTaskSql.log_list) mysql.end(option='commit') return logs except Exception as e: Log.info(e)
def save_log(log_name, log_info): try: mysql = MySql() info_val = [log_name, log_info] mysql.update(LogTaskSql.log_insert, info_val) mysql.end(option='commit') except Exception as e: Log.info(e)
def clear_logs(): try: mysql = MySql() logs = mysql.remove(LogTaskSql.remove_list) mysql.end(option='commit') print("清空数据成功!") return logs except Exception as e: Log.info(e)
def get_value_and_key(file_path): """ 获取 xml 文件的 key - value :param file_path: 文件路径 :return: dic[key]-value """ if not file_path or not os.path.exists(file_path): Log.error("xml 文件不存在") return xml_doc = xml.dom.minidom.parse(file_path) nodes = xml_doc.getElementsByTagName('string') dic = collections.OrderedDict() for index, node in enumerate(nodes): if node is None or node.firstChild is None: continue if Constant.Config.export_apply_translatable: # ignore translatable translatable = node.getAttribute('translatable') if translatable is not None and translatable == "false": continue key = node.getAttribute("name") value = XMLParse.get_text_node_value(node) if not Constant.Config.export_only_zh: dic[key] = value else: # 仅导出中文,是中文,则保存 if is_chinese(value): dic[key] = value # Log.info("%s : %s" % (key, value)) array_nodes = xml_doc.getElementsByTagName("string-array") for array_node in array_nodes: key = array_node.getAttribute('name') child_nodes = array_node.getElementsByTagName('item') for idx, child_node in enumerate(child_nodes): newKey = key + "-INDEX-" + str(idx) value = XMLParse.get_text_node_value(child_node) if not Constant.Config.export_only_zh: dic[newKey] = value else: if is_chinese(value): dic[newKey] = value return dic
def addParser(): parser = OptionParser() parser.add_option("-i", "--input", help="excel file path") parser.add_option( "-f", "--targetFilePath", help="means target output is xml file and input the file path") parser.add_option( "-l", "--targetLanguage", help="target language shortname(just for output is file)") parser.add_option("-d", "--targetDirPath", help="means target output is dir contains xml file(s)") (options, args) = parser.parse_args() Log.info("options: %s, args: %s" % (options, args)) return options
def update_multi_xml_value(sub_dir_path, keys, values, modules): Log.info("\n" + sub_dir_path + "\n") ''' sub_dir_path: 目标子目录,比如 value-zh ''' if len(modules) == 0: return # 先排序,把 excel 中的统一 module 排到一起 # 排序,分块处理 current_module = modules[0] module_length_list = [] current_module_len = 0 modules_new = [] values_new = [] keys_new = [] for mid, module in enumerate(modules): if module is None or module == "": continue if current_module != module: module_length_list.append(current_module_len) current_module = module current_module_len = 0 modules_new.append(module) values_new.append(values[mid]) keys_new.append(keys[mid]) current_module_len += 1 module_length_list.append(current_module_len) start = 0 end = 0 for module_len in module_length_list: end += module_len subKeys = keys_new[start:end] subValues = values_new[start:end] module = modules_new[start] start += module_len filePath = sub_dir_path + module + ".xml" XMLParse.update_xml_value(filePath, subKeys, subValues)
def xls2xml(self, xls_path, file_path, target_language, target_dir_path): """ :param xls_path: 表格路径 :param file_path: 目标文件路径 :param target_language: 目标语言 :param target_dir_path: 目标文件目录 """ Log.info("--- xls2xml ---") # 输入 excel if not xls_path or not os.path.exists(xls_path): Log.error( Constant.Error(Constant.ERROR_EXCEL_NOT_EXIST).get_desc_en()) return Constant.Error(Constant.ERROR_EXCEL_NOT_EXIST) xlsPath = xls_path self.filePath = file_path self.targetLanguage = target_language self.dirPath = target_dir_path # 获取 xls 对象,以及目标 sheet(这里默认为第一张表,index 从0开始) xlsParse = XLSParse() xlsParse.open_excel(xlsPath) sheet = xlsParse.sheet_by_index(0) Log.info("name = %s, rows number = %s,clos number = %s" % (sheet.name, sheet.nrows, sheet.ncols)) return self.convert(sheet)
def update_xml_value(file_path, keys, values): Log.info("--- updating xml... \n%s" % file_path) if not os.path.exists(file_path): return # Log.info ("--- string ---") # 读取文档 xml_doc = xml.dom.minidom.parse(file_path) # filename nodes = xml_doc.getElementsByTagName('string') for node in nodes: xmlKey = node.getAttribute("name") xmlValue = "" # 改变量仅用于输出 if node.firstChild is None: continue xmlValue = XMLParse.get_text_node_value(node) for index, key in enumerate(keys): if key == xmlKey and len(values[index]) != 0: node.firstChild.data = values[index] Log.debug("%s : %s -- >%s " % (xmlKey, xmlValue, node.firstChild.data)) # Log.info("--- string end ---\n") # 数组 # Log.info("--- array ---") array_nodes = xml_doc.getElementsByTagName('string-array') for array_node in array_nodes: xmlKey = array_node.getAttribute('name') child_nodes = array_node.getElementsByTagName('item') for idx, child_node in enumerate(child_nodes): newKey = xmlKey + "-INDEX-" + str(idx) xmlValue = child_node.firstChild.data for index, key in enumerate(keys): if key == newKey and len(values[index]) != 0: child_node.firstChild.data = values[index] Log.debug("%s : %s --> %s" % (newKey, xmlValue, child_node.firstChild.data)) # Log.info("--- array end ---\n") writeFile = open(file_path, 'wb') writeFile.write(xml_doc.toxml('utf-8')) writeFile.close()
# This is a sample Python script. # Press ⌃R to execute it or replace it with your code. # Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings. from utils import LogUtils from utils.LogUtils import Log def print_hi(name): # Use a breakpoint in the code line below to debug your script. print(f'Hi, {name}') # Press ⌘F8 to toggle the breakpoint. # Press the green button in the gutter to run the script. if __name__ == '__main__': print_hi('PyCharm') Log.debug("test") # See PyCharm help at https://www.jetbrains.com/help/pycharm/
def convert(self, sheet): """ 真正转化部分 :param sheet: excel 的 sheet 对象 :return: ErrorConstant.Error """ Log.info("--- convert ---") keyIndex = -1 moduleIndex = -1 tempLanguageIndex = None # 返回由该行中所有单元格的数据组成的列表 try: firstRow = sheet.row_values(0) except Exception as e: Log.error( Constant.Error(Constant.EXCEPTION_EXL_FILE, e.message).get_desc_en()) return Constant.Error(Constant.EXCEPTION_EXL_FILE, e.message) if len(firstRow) == 0: Log.error( Constant.Error(Constant.ERROR_KEY_NOT_FOUND).get_desc_en()) return Constant.Error(Constant.ERROR_KEY_NOT_FOUND) for index in range(len(firstRow)): if firstRow[index] == self.keyTitle: keyIndex = index pass elif firstRow[index] == self.moduleTitle: moduleIndex = index pass elif firstRow[index] == self.targetLanguage: tempLanguageIndex = index pass if keyIndex == -1: Log.error( Constant.Error(Constant.ERROR_KEY_NOT_FOUND).get_desc_en()) return Constant.Error(Constant.ERROR_KEY_NOT_FOUND) # 获取 key 集合,并删除 title 项 xlsKeys = sheet.col_values(keyIndex) del xlsKeys[0] if self.filePath and tempLanguageIndex: # 输入是文件,指定目标语言 Log.debug("keyIndex = %s moduleIndex = %s languageIndex = %s" % (keyIndex, moduleIndex, tempLanguageIndex)) # 获取 value 集合,并删除 title 项 xlsValues = sheet.col_values(tempLanguageIndex) del xlsValues[0] XMLParse.update_xml_value(self.filePath, xlsKeys, xlsValues) Log.info(Constant.Error(Constant.SUCCESS).get_desc_en()) return Constant.Error(Constant.SUCCESS) Log.debug("Not a file") if moduleIndex == -1: Log.error( Constant.Error(Constant.ERROR_MODULE_NOT_FOUND).get_desc_en()) return Constant.Error(Constant.ERROR_MODULE_NOT_FOUND) if not self.dirPath: # 目录为空,返回 Log.error( Constant.Error(Constant.ERROR_IMPORT_INPUT).get_desc_en()) return Constant.Error(Constant.ERROR_IMPORT_INPUT) if not os.path.exists(self.dirPath): Log.error( Constant.Error(Constant.ERROR_DIR_NOT_EXIST).get_desc_en()) return Constant.Error(Constant.ERROR_DIR_NOT_EXIST) for index, title in enumerate(firstRow): if index < self.fromIndex: continue languageIndex = index targetLanguage = title # print languageIndex # print title xlsKeys = sheet.col_values(keyIndex) del xlsKeys[0] xlsModules = sheet.col_values(moduleIndex) del xlsModules[0] xlsValues = sheet.col_values(languageIndex) del xlsValues[0] # 文件路径(子目录) 比如; value-zh # ├── android # │ ├── values-zh # │ | ├── strings_device.xml # │ | ├── strings_me.xml # │ | ├── strings_moment.xml # │ ├── values-de # │ ├── values-ko sub_dir_path = covertTargetPath(self.dirPath, targetLanguage) # print sub_dir_path if os.path.exists(sub_dir_path): XMLParse.update_multi_xml_value(sub_dir_path, xlsKeys, xlsValues, xlsModules) Log.info(Constant.Error(Constant.SUCCESS).get_desc_en()) return Constant.Error(Constant.SUCCESS)