def load(config_name, default_config, config_path='./config', encoding='utf-8'): """ 从指定路径读取配置,如果配置文件不存在,则使用缺省配置 并保存缺省配置到指定路径。 @param config_name: 配置文件名 @param default_config: 缺省配置 @param config_path: 配置路径 @param encoding: 文字编码 @return: 配置信息 """ if filecm.exists(config_path, config_name): # 文件存在,读取文件 logcm.print_info('配置文件存在:%s/%s' % (config_path, config_name)) cfg_content = filecm.read_str(config_path, config_name, encoding) cfg_info = json.loads(cfg_content) return cfg_info # 文件不存在,返回默认值并初始化配置文件。 logcm.print_info('配置文件不存在:%s/%s' % (config_path, config_name)) # 把对象转成JSON字符串,并格式化 cfg_content = json.dumps(default_config, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) # 保存到json文件 filecm.save_str(cfg_content, encoding, config_path, config_name) return default_config
def convert_freemarker_path(fm_path): """ 把指定目录下的模版文件中的Freemark语法,同一替换成Jinjia2语法 :param fm_path: 指定目录 :return:无 """ path_list = filecm.search_files(fm_path, ".tpl,.json,.ftl") for tpl_path in path_list: fm_str = filecm.read_str(file_name=tpl_path) jj_str = freemarker_to_jinjia2(fm_str) diffcm.diff_by_text(fm_str, jj_str) filecm.save_str(jj_str, file_name=tpl_path)
def save_html_url(page_url, encoding, local_path, file_name): """ 把网页URL保存到本地HTML文件 @param page_url: 网页URL @param encoding: 网页编码 @param local_path: 文件路径 @param file_name: 文件名 @return:无 """ logcm.print_info("Saved url as html. %s --> %s/%s" % (page_url, local_path, file_name)) # 读取HTML内容到文本 html = read_url(page_url, encoding) # 保存HTML内容到本地文件 filecm.save_str(html, encoding, local_path, file_name)
def make_by_tpl(self, tpl_path, out_file_tpl, cfg_obj): """ 按照指定模版文件,进行代码生成 :param tpl_path: 模版文件路径 :param out_file_tpl: 输出路径模版 :param cfg_obj: 配置数据 :return: 无 """ logcm.print_info("Making %s to %s ..." % (tpl_path, out_file_tpl)) # 读入模版 tpl_str = filecm.read_str(file_name=tpl_path) # 生成代码 out_str = Template(tpl_str, trim_blocks=True).render(**cfg_obj) # 生成输出路径 out_file = Template(out_file_tpl, trim_blocks=True).render(**cfg_obj) # 保存到文件 out_file = os.path.join(self.out_path, out_file) filecm.save_str(out_str, file_name=out_file)
# </#if> -> {% endif %} out_str = out_str.replace("</#if>", "{% endif %}") # <#if x_has_next> p = re.compile(r'<#if *[^<>]+has_next *>') out_str = re.sub(p, r'{% if not loop.last %}', out_str) # list转换 # <#list ... as .. > -> {% for .. in ... %} p = re.compile(r'<#list *([^ ]+) +as +([^ ]+) *>') out_str = re.sub(p, r'{% for \2 in \1 %}', out_str) # </#if> -> {% endif %} out_str = out_str.replace("</#list>", "{% endfor %}") # ?处理清除 # {{...?..}} -> {{...}} p = re.compile(r'\{\{([^\{\}\?]+)\?([^\{\}\?]+)\}\}') out_str = re.sub(p, r'{{\1}}', out_str) return out_str if __name__ == '__main__': # fm_str = filecm.read_str(file_name="./template/xls_to_interface/tpl_config.json") tpl_path = "./template/xls_to_interface/service/core_java_test_service.tpl" fm_str = filecm.read_str(file_name=tpl_path) jj_str = freemarker_to_jinjia2(fm_str) diffcm.diff_by_text(fm_str, jj_str) filecm.save_str(jj_str, file_name=tpl_path) # convert_freemarker_path("./template/xls_to_interface/") # core_java_test_service
This module provides classes and functions for comparing sequences. including HTML and context and unified diffs. difflib Document v7.411 add string kk pp """ text2 = """ text2: This module provides classes and functions for Comparing sequences. including HTML and context and unified diffs. difflib document v7.522 kk add2 pp dd...d.. """ diffcm.diff_by_text(text1, text2) # 文件对比 text_path1 = './temp/file/file_diff1.txt' text_path2 = './temp/file/file_diff2.txt' filecm.save_str(text1, file_name=text_path1) filecm.save_str(text2, file_name=text_path2) diffcm.diff_by_file(text_path1, text_path2) # 目录对比 dir1 = './temp/file/dir1' dir2 = './temp/file/dir2' diffcm.diff_by_dir(dir1, dir2)
r'\n[^\n]{0,15}天才一秒记住本站地址[^\n]+\n', r'\n[^\n]{0,15}手机版阅读网址[^\n]+\n', r'\(\)', r'()', r' ', r'\.\.cop>', ] str_org = filecm.read_str("./temp", "test.txt", "utf-8") str_new = str_org find_cnt = 0 for key in REPLACE_KEYS: # 匹配正则,匹配小写字母和大写字母的分界位置 p = re.compile(key, re.IGNORECASE) findList = p.findall(str_new) cnt = len(findList) find_cnt += cnt print("Found %d by key : %s" % (cnt, key)) if cnt > 0: print(findList) # 这里第二个参数使用了正则分组的后向引用 str_new = re.sub(p, r'', str_new) filecm.save_str(str_new, path="./temp", file_name="test2.txt") print("Replace %d String in total!" % find_cnt)
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Jinjia2 使用示例。 """ from jinja2 import Template from common import filecm tpl_str = filecm.read_str(file_name="./template/sample_01.tpl") out_str = Template(tpl_str, lstrip_blocks=True).render( config=[1, 2, 3], txt="aaaaaabbbbbb\ncccccccdddddd") filecm.save_str(out_str, file_name="./output/sample_01.txt")