示例#1
0
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
示例#2
0
 def __init__(self, test_config):
     self.cfg = test_config
     # 公私钥字符串
     self.pub_key_str = filecm.read_str('./cache/crypt', 'public.key',
                                        "utf-8")
     self.token = None
     self.client = BrowserClient("Chrome")
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)
示例#4
0
def read_file(path, file_name, encoding):
    """
    通过本地文件得到网页内容
    @param path: 文件路径
    @param file_name: 文件名
    @param encoding: 网页编码
    @return: 网页文本
    """

    # 从本地HTML文件读取文本
    html = filecm.read_str(path, file_name, encoding)
    return html
示例#5
0
 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
示例#7
0
"""
加解密使用示例。
"""

from common import cryptcm
from common import filecm
from common import checkcm

# 缓存目录
cache_path = './cache/crypt'
pub_key_file = 'public.key'
prv_key_file = 'private.key'
input_str = "{key:1234}"

# 公私钥字符串
pub_key_str = filecm.read_str(cache_path, pub_key_file, "utf-8")
prv_key_str = filecm.read_str(cache_path, prv_key_file, "utf-8")

# RSA加密
input_bytes = input_str.encode("utf-8")
encrypt_bytes = cryptcm.rsa_encrypt(pub_key_str, input_bytes)

# RSA解密
decrypt_bytes = cryptcm.rsa_decrypt(prv_key_str, encrypt_bytes)

# 确定解密后和加密前是否一致
checkcm.check_equal(input_bytes, decrypt_bytes, "decrypt bytes")

# MD5加密
md5_str = cryptcm.md5_encrypt(input_bytes)
示例#8
0
    r'[^\n]{0,15}记住手机版网址[^\n]{0,50}',

    r'求月票',
    r'[^\n]{0,15}有月票吗[^\n]{0,50}',
    r'\nps[^\n]+\n',
    r'\n[^\n]{0,15}天才一秒记住本站地址[^\n]+\n',
    r'\n[^\n]{0,15}手机版阅读网址[^\n]+\n',

    r'\(\)',
    r'()',
    r'&nbsp',
    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)
        # 这里第二个参数使用了正则分组的后向引用
#!/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")