def parseFile(filename): chunk = list() with open_n_decode(filename, "r", "utf-8") as f: string = prepare(f) jsondata = dict() try: jsondata = loads(string) except: print("Cannot parse " + filename) return [] paths = list_field_paths(jsondata) dialog = dirname(filename).endswith("dialog") for path in paths: for k in files_of_interest.keys(): if filename.endswith(k) or k == "*": for roi in files_of_interest[k]: if roi.match(path) or dialog: val = field_by_path(jsondata, path) if not type(val) is str: print("File: " + filename) print("Type of " + path + " is not a string!") continue if val == "": continue for handler in textHandlers: res = handler(val, filename, '/' + path) if res: chunk += res break break return chunk
def parseFile(filename): chunk = list() if basename(filename) not in ignore_filelist: print(basename(filename)) with open_n_decode(filename, "r", "utf_8_sig") as f: try: if basename(filename).endswith('.patch'): chunk.append("patch") if basename(filename) in dict.keys(patch_serialization): string = trans_patch( f, patch_serialization[basename(filename)]) else: string = trans_patch(f) paths = to_a_list(string, 0) else: string = prepare(f) jsondata = loads(string) paths = list_field_paths(jsondata) except: print("Cannot parse " + filename) try: problem_file = open(error_list_file, 'a') problem_file.writelines( filename.replace(root_dir, '') + '\n') problem_file.close() except: pass return [] filename_base = filename if basename(filename).endswith('.patch'): filename = filename.replace('.patch', "") dialog = dirname(filename).endswith("dialog") for i, path in enumerate(paths): for k in files_of_interest.keys(): if filename.endswith(k) or k == "*": for roi in files_of_interest[k]: if roi.match(path) or dialog: if basename(filename_base).endswith('.patch'): val = to_a_list(string, 1)[i] else: val = field_by_path(jsondata, path) if not type(val) is str: print("File: " + filename) print("Type of " + path + " is not a string!") continue if val == "": continue for handler in textHandlers: res = handler(val, filename, '/' + path) if res: chunk += res break break return chunk
def trans_patch(jsons): string = prepare(jsons) json_text = json.loads(string) value_list = list() path_list = list() path_list_2 = list() value_list_2 = list() op_list = list() for i, value in enumerate(json_text): path_result = value['path'] op_result = value['op'] try: value_result = value['value'] except: value_result = '' value_list.append(value_result) path_list.append(path_result) op_list.append(op_result) dict_result = tuple(zip(op_list, path_list, value_list)) for i in dict_result: if i[0] == 'add' or i[0] == 'replace': path_1 = i[1] path_2 = list_field_paths(i[2]) if path_2 == []: path_2 = ['*'] else: pass for v in path_2: if path_2 == ['*']: value = i[2] path = path_1.replace('/', '', 1) value_list_2.append(value) path_list_2.append(path) else: value = field_by_path(i[2], v) path = (path_1+'/' + v).replace('/', '', 1) value_list_2.append(value) path_list_2.append(path) else: pass result = tuple(zip(path_list_2, value_list_2)) return result
def add_count(counter, path, value): thepath = normpath(relpath(path, translations_dir)) oldval = field_by_path(counter, thepath, sep=sep) field_by_path(counter, thepath, oldval + 1, sep)
def set_count(counter, path, value): ## Sets the count of the translated or total elements for specific path thepath = normpath(relpath(path, translations_dir)) field_by_path(counter, thepath, value, sep)
def set_count(counter, path, value): thepath = normpath(relpath(path, translations_dir)) field_by_path(counter, thepath, value, sep)
def trans_patch_spcial_1(jsons, ex): string = prepare(jsons) json_text = json.loads(string) value_list = list() path_list = list() path_list_2 = list() path_list_3 = list() value_list_2 = list() op_list = list() ex_list = ex for i, value in enumerate(json_text): path_result = value['path'] op_result = value['op'] try: value_result = value['value'] except: value_result = '' value_list.append(value_result) path_list.append(path_result) op_list.append(op_result) """ for i in ex_list: if i[2] == 1 : o = i[1] for text in path_list: if not re.search(i[0]+'/'+'-',text) == None : wait = text.replace(i[0]+'/'+'-',i[0]+'/'+str(o)) path_list_3.append(wait) o=o+1 else: path_list_3.append(text) else: for text in path_list: if not re.search(i[0]+'/'+'-',text) == None : wait = text.replace(i[0]+'/-',i[0]+'/'+str(i[1])) path_list_3.append(wait) else: path_list_3.append(text) path_list = path_list_3 """ for i in ex_list: path_list = replace_the_path(path_list, i) dict_result = tuple(zip(op_list, path_list, value_list)) for i in dict_result: if i[0] == 'add' or i[0] == 'replace': path_1 = i[1] path_2 = list_field_paths(i[2]) if path_2 == []: path_2 = ['*'] else: pass for v in path_2: if path_2 == ['*']: value = i[2] path = path_1.replace('/', '', 1) value_list_2.append(value) path_list_2.append(path) else: value = field_by_path(i[2], v) path = (path_1+'/' + v).replace('/', '', 1) value_list_2.append(value) path_list_2.append(path) else: pass result = tuple(zip(path_list_2, value_list_2)) return result
others_path = normpath(join(root_dir, "others")) for subdir, dirs, files in os.walk(mod_dir): for thefile in files: if not thefile.endswith(".patch"): # All non-patch files will be copied to others directory modpath = join(subdir, thefile) # File path in mod dir assetspath = normpath(relpath(modpath, mod_dir)) # File path in packed assets fwpath = normpath(join(others_path,assetspath)) # File path in framework if exists(fwpath): print(fwpath + " already exists! Replacing...") os.makedirs(dirname(fwpath), exist_ok = True) copy(modpath, fwpath) continue filename = join(subdir, thefile) # Patch file path # File path in packed assets fname, ext = splitext(filename) assetspath = normpath(relpath(fname, mod_dir)) replacements = parseFile(filename) for replacement in replacements: # We expect, that operation is always "replace". # If it isn't... Well, something strange will happen. newdata = replacement["value"] # Imported translated text jsonpath = replacement["path"] if type(newdata) is list: # Very special case if value is list paths = list_field_paths(newdata) for p in paths: # There we are restoring paths to leafs of structure in newdata process_replacement(join(jsonpath, p), field_by_path(newdata,p), assetspath) else: # All as expected, just perform replacement process_replacement(jsonpath, newdata, assetspath)
def add_count(counter, path, value): thepath = normpath(relpath(path, translations_dir)) oldval = field_by_path(counter, thepath, sep=sep) field_by_path(counter, thepath, oldval+1, sep)