def replace_chinese(file_path): lang_path = get_lang_path(file_path) file_var = get_file_line(lang_path) with open(file_path, 'r', encoding='utf-8') as file: content = file.read() result = re.sub(chineseReg, lambda ch: get_var(ch.group(), file_var, file_path), content) IO_util.write_to_file(file_path, result)
def capitalize_file(file_path): line_arr = [] with open(file_path, 'r', encoding='utf-8') as file: for line in file.readlines(): result = re.search(lineReg, line) if result: line_arr.append(capitalize_line(line)) else: line_arr.append(line) IO_util.write_to_file(file_path, ''.join(line_arr))
def to_img(): with open(base64FilePath, 'r', encoding='utf-8') as file: content = file.read() base64_arr = content.split('###') for i, base64str in enumerate(base64_arr): if i < 6: dir_path = os.path.join(writePath2, str(i + 1000001)) else: dir_path = os.path.join(writePath2, str(i - 5 + 2000000)) IO_util.base64_to_img(base64str[22:], os.path.join(dir_path, 'countdown_arc_bg.png'))
def en_to_ch(file_path): """ 把 en下的文件 转成 ch 文件 """ for f in IO_util.get_all_file(file_path): with open(f, 'r', encoding='utf-8') as r_file: ch_lines = [] for line in r_file.readlines(): l = Line(line) ch_lines.append(l.to_ch()) IO_util.write_to_file(os.path.join(zhPath, os.path.basename(f)), '\n'.join(ch_lines))
def del_file_var(file_name, var_set, find_global=False): del_lines = [] file_path = os.path.join(enPath, 'l_%s.ftl' % file_name) with open(file_path, 'r', encoding='utf-8') as f: for i, line in enumerate(f.readlines()): result = re.search(var_reg, line) if result and result.group() in var_set: if find_global: if is_same_global_var(line): del_lines.append(i + 1) else: del_lines.append(i+1) IO_util.del_file_lines(file_path, del_lines)
def del_file_var(file_name, var_set, find_global=False): del_lines = [] file_path = os.path.join(enPath, 'l_%s.ftl' % file_name) with open(file_path, 'r', encoding='utf-8') as f: for i, line in enumerate(f.readlines()): result = re.search(var_reg, line) if result and result.group() in var_set: if find_global: if is_same_global_var(line): del_lines.append(i + 1) else: del_lines.append(i + 1) IO_util.del_file_lines(file_path, del_lines)
def write_word_dict(word_dict): for k, v in word_dict.items(): assign_index = -1 language_path = [os.path.join(enPath, 'l_%s.ftl' % k), os.path.join(zhPath, 'l_%s.ftl' % k)] key_arr = ['en', 'zh'] for j, path in enumerate(language_path): with open(path, 'r', encoding='utf-8') as f: readlines = f.readlines() for i, l in enumerate(readlines): if re.search(assignReg, l): assign_index = i + 1 break if assign_index != -1: write_content = ''.join(readlines[:assign_index] + ['\n'.join(v[key_arr[j]]) + '\n'] + readlines[assign_index:]) IO_util.write_to_file(path, write_content)
def write_word_dict(word_dict): for k, v in word_dict.items(): assign_index = -1 language_path = [ os.path.join(enPath, 'l_%s.ftl' % k), os.path.join(zhPath, 'l_%s.ftl' % k) ] key_arr = ['en', 'zh'] for j, path in enumerate(language_path): with open(path, 'r', encoding='utf-8') as f: readlines = f.readlines() for i, l in enumerate(readlines): if re.search(assignReg, l): assign_index = i + 1 break if assign_index != -1: write_content = ''.join(readlines[:assign_index] + ['\n'.join(v[key_arr[j]]) + '\n'] + readlines[assign_index:]) IO_util.write_to_file(path, write_content)
def get_theme_new(): file_path = r'E:\SHT\project\sas-web\src\main\webapp\src\saishi\scss' theme_color = [] for path in IO_util.get_all_file(file_path): if skinFileReg.search(path): with open(path, 'r', encoding='utf-8') as file: for line in file.readlines(): result = skinReg.search(line) if result: theme_color.append(result.groups()[0]) break return theme_color
def get_theme_old(): # 获取theme4 file_path = r'E:\SHT\project\sas-web\src\main\webapp\src\default\scss' theme_color = [] for path in IO_util.get_all_file(file_path): if path.find('0000') != -1: # if path.find('theme200000') != -1 or path.find('theme100000') != -1 or path.find('theme2000010') != -1: with open(path, 'r', encoding='utf-8') as file: for line in file.readlines(): result = theme4Reg.search(line) if result: theme_color.append(result.groups()[0]) break return theme_color
import os from py.utils import IO_util # writePath = r'E:\git\pythonCode\src\theme' writePath2 = r'E:\SHT\project\sas-web\src\main\webapp\res\img\theme' base64FilePath = r'E:\git\pythonCode\src\base64.txt' def to_img(): with open(base64FilePath, 'r', encoding='utf-8') as file: content = file.read() base64_arr = content.split('###') for i, base64str in enumerate(base64_arr): if i < 6: dir_path = os.path.join(writePath2, str(i + 1000001)) else: dir_path = os.path.join(writePath2, str(i - 5 + 2000000)) IO_util.base64_to_img(base64str[22:], os.path.join(dir_path, 'countdown_arc_bg.png')) IO_util.clip(IO_util.img_to_base64(r'E:\chrome\double_arrow.png'))
from py.utils import IO_util appsPath = r'G:\code\MxOnline\apps' models = filter(lambda path: path.endswith('models.py'), IO_util.get_all_file(appsPath))
import os import http.client import hashlib import urllib.parse import random import json from py.utils import IO_util """ 一个翻译makerdown的小程序 主要应用于翻译GitHub上那些没有翻译的文档 本程序相对于浏览器的全文翻译 优点在于 会避开一些专有名词(如React, Vue等)的强制翻译 """ mdReg1 = re.compile('\[.+?\]\s*\(.+?\)') cfg = IO_util.get_json('../../private_config/baidu_translate_key.json') appid = cfg['appid'] secretKey = cfg['secretKey'] class Translator(object): needless = [mdReg1] placeholder = '♏' wordReg = '\w+' def __init__(self, cfg): self.config = cfg with open(cfg['filePath'], 'r', encoding='utf-8') as file: self.task = map(self.parse, file.readlines())
def get_lang_dict(): temp_dict = {} for p in IO_util.get_all_file(enPath): key, value = get_language_var(p) temp_dict[key] = value return temp_dict
enPath = r'E:\SHT\project\sas-web\src\main\webapp\WEB-INF\views\lang\en' zhPath = r'E:\SHT\project\sas-web\src\main\webapp\WEB-INF\views\lang\zh' testFile = r'E:\SHT\project\sas-web\src\main\webapp\WEB-INF\views\lang\zh\l_event.ftl' varReg = re.compile(r'(l_\w+)\s?=\s?".+?"') def del_var(file_path): with open(file_path, 'r', encoding='utf-8') as file: varArr = [] delArr = [] lines = file.readlines() lines.reverse() length = len(lines) for i, l in enumerate(lines): result = re.search(varReg, l) if result: var = result.groups()[0] if var in varArr: delArr.append(str(length - i)) else: varArr.append(var) # IO_util.del_file_lines(file_path, delArr) print(file_path, '-'.join(delArr)) for path in [enPath, zhPath]: for f in IO_util.get_all_file(path): del_var(f) # del_var(testFile)
import re import os import http.client import hashlib import urllib.parse import random import json from py.utils import IO_util """ 一个翻译makerdown的小程序 主要应用于翻译GitHub上那些没有翻译的文档 本程序相对于浏览器的全文翻译 优点在于 会避开一些专有名词(如React, Vue等)的强制翻译 """ mdReg1 = re.compile('\[.+?\]\s*\(.+?\)') cfg = IO_util.get_json('../../private_config/baidu_translate_key.json') appid = cfg['appid'] secretKey = cfg['secretKey'] class Translator(object): needless = [mdReg1] placeholder = '♏' wordReg = '\w+' def __init__(self, cfg): self.config = cfg with open(cfg['filePath'], 'r', encoding='utf-8') as file: self.task = map(self.parse, file.readlines())
import os import re from py.utils import IO_util os.rename(r'E:\git\pythonCode\src\new 1.txt', r'E:\git\pythonCode\src\new2.txt') def rename1(file_path, reg1, reg2): pass d = input('目录:') filePaths = IO_util.get_all_file(d) print(filePaths) reg1 = re.compile(input('正则输入:')) re.sub()
def get_icon_from_file_path(paths): temp = set() for path in paths: for file in IO_util.get_all_file(path): temp |= get_icon_from_file(file) return temp
from py.translate import line as L enPath = r'E:\SHT\project\sas-web\src\main\webapp\WEB-INF\views\lang\en' zhPath = r'E:\SHT\project\sas-web\src\main\webapp\WEB-INF\views\lang\zh' testFile = r'E:\SHT\project\sas-web\src\main\webapp\WEB-INF\views\lang\zh\l_event.ftl' varReg = re.compile(r'(l_\w+)\s?=\s?".+?"') def del_var(file_path): with open(file_path, 'r', encoding='utf-8') as file: varArr = [] delArr = [] lines = file.readlines() lines.reverse() length = len(lines) for i, l in enumerate(lines): result = re.search(varReg, l) if result: var = result.groups()[0] if var in varArr: delArr.append(str(length - i)) else: varArr.append(var) # IO_util.del_file_lines(file_path, delArr) print(file_path, '-'.join(delArr)) for path in [enPath, zhPath]: for f in IO_util.get_all_file(path): del_var(f) # del_var(testFile)