data_hanle中增加对RequestsCookieJar的处理,避免source里面存在RequestsCookieJar, 处理完成后,数据格式不正确的问题.
This commit is contained in:
@@ -7,16 +7,21 @@
|
||||
|
||||
# 标准库导入
|
||||
import random
|
||||
import json
|
||||
import re, uuid
|
||||
from datetime import datetime, date, timedelta
|
||||
# 第三方库导入
|
||||
from loguru import logger
|
||||
from faker import Faker
|
||||
from string import Template
|
||||
from requests.cookies import RequestsCookieJar
|
||||
from requests.utils import dict_from_cookiejar
|
||||
|
||||
|
||||
class FakerData:
|
||||
"""
|
||||
测试数据生成类
|
||||
官方文档:https://faker.readthedocs.io/en/master/index.html
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
@@ -85,7 +90,7 @@ class FakerData:
|
||||
|
||||
def generate_name(self, lan="en") -> str:
|
||||
"""
|
||||
|
||||
生成人名
|
||||
:return: 人名
|
||||
"""
|
||||
if lan == "zh":
|
||||
@@ -94,6 +99,66 @@ class FakerData:
|
||||
name = self.faker.name()
|
||||
return name
|
||||
|
||||
def generate_company_name(self, lan: str = "en", fix: str = None) -> str:
|
||||
"""
|
||||
生成公司名
|
||||
:param lan: 语言类型,可选:en, zh; zh表示中文,en表示英文,默认是en
|
||||
:param fix: 前后缀,可选pre, suf; pre表示公司前缀,suf标识公司后缀
|
||||
:return: 公司名
|
||||
"""
|
||||
if lan == "zh":
|
||||
if fix == "pre":
|
||||
name = self.fk_zh.company_prefix()
|
||||
elif fix == "suf":
|
||||
name = self.fk_zh.company_suffix()
|
||||
else:
|
||||
name = self.fk_zh.company()
|
||||
else:
|
||||
if fix == "pre":
|
||||
name = self.faker.company_prefix()
|
||||
elif fix == "suf":
|
||||
name = self.faker.company_suffix()
|
||||
else:
|
||||
name = self.faker.company()
|
||||
|
||||
return name
|
||||
|
||||
def generate_paragraph(self, lan: str = "en", nb: int = 3) -> str:
|
||||
"""
|
||||
生成段落
|
||||
:param lan: 语言类型,可选:en, zh; zh表示中文,en表示英文,默认是en
|
||||
:param nb: 段落个数,默认是3个
|
||||
"""
|
||||
if lan == "zh":
|
||||
text = self.fk_zh.paragraph(nb_sentences=nb, variable_nb_sentences=True, ext_word_list=None)
|
||||
else:
|
||||
text = self.faker.paragraph(nb_sentences=nb, variable_nb_sentences=True, ext_word_list=None)
|
||||
|
||||
return text
|
||||
|
||||
def generate_words(self, lan: str = "en", nb: int = 1) -> str:
|
||||
|
||||
"""
|
||||
生成词语
|
||||
:param lan: 语言类型,可选:en, zh; zh表示中文,en表示英文,默认是en
|
||||
:param nb: 词语个数,默认是1个
|
||||
"""
|
||||
if lan == "zh":
|
||||
if nb == 1 or nb < 1:
|
||||
text = self.fk_zh.word(ext_word_list=None)
|
||||
else:
|
||||
res = self.fk_zh.words(nb=nb, ext_word_list=None)
|
||||
text = "-".join(res)
|
||||
|
||||
else:
|
||||
if nb == 1 or nb < 1:
|
||||
text = self.faker.word(ext_word_list=None)
|
||||
else:
|
||||
res = self.faker.words(nb=nb, ext_word_list=None)
|
||||
text = "-".join(res)
|
||||
|
||||
return text
|
||||
|
||||
def generate_email(self, lan="en") -> str:
|
||||
"""
|
||||
|
||||
@@ -184,6 +249,21 @@ class DataHandle:
|
||||
except Exception:
|
||||
return data
|
||||
|
||||
def process_cookie_jar(self, data):
|
||||
"""
|
||||
将任意数据里的RequestsCookieJar,转成dict,再转换成JSON 格式的字符串(序列化)
|
||||
:param data: 待处理的数据
|
||||
"""
|
||||
if isinstance(data, dict):
|
||||
for key, value in data.items():
|
||||
data[key] = self.process_cookie_jar(value)
|
||||
elif isinstance(data, list):
|
||||
for i, item in enumerate(data):
|
||||
data[i] = self.process_cookie_jar(item)
|
||||
elif isinstance(data, RequestsCookieJar):
|
||||
data = json.dumps(dict_from_cookiejar(data))
|
||||
return data
|
||||
|
||||
def replace_and_store_placeholders(self, pattern, text):
|
||||
"""
|
||||
提取字符串中符合正则表达式的元素,同时用一个唯一的uuid来替换原有字符串
|
||||
@@ -213,8 +293,13 @@ class DataHandle:
|
||||
func = {}
|
||||
keys = {}
|
||||
if not source or not isinstance(source, dict):
|
||||
# print("source为空或者source不是字典格式,都将认为是:{}")
|
||||
logger.debug("source为空或者source不是字典格式,都将认为是:{}")
|
||||
source = {}
|
||||
|
||||
# 处理一下source,检测到里面存在RequestsCookieJar,转成dict,再转换成JSON 格式的字符串(序列化)。
|
||||
# 避免传递过来一个RequestsCookieJar,替换后变成了'RequestsCookieJar',导致cookies无法使用的问题
|
||||
source = self.process_cookie_jar(data=source)
|
||||
|
||||
# 如果进来的是字符串,先将各种类型的表达式处理完
|
||||
if isinstance(obj, str):
|
||||
# 先把python表达式找出来存着,这里会漏掉一些诸如1+1的表达式
|
||||
@@ -275,15 +360,12 @@ class DataHandle:
|
||||
else: # 不是FakerData类方法,但有可能是 1+1 这样的
|
||||
obj = obj.replace(key, str(eval(func)))
|
||||
except:
|
||||
print("Warn: --------函数:%s 无法调用成功, 请检查是否存在该函数-------" % func)
|
||||
logger.warning("Warn: --------函数:%s 无法调用成功, 请检查是否存在该函数-------" % func)
|
||||
obj = obj.replace(key, funcs[0])
|
||||
pass
|
||||
|
||||
return obj
|
||||
|
||||
|
||||
|
||||
|
||||
# 声明data_handle方法,这样外部就可以直接import data_handle来使用了
|
||||
data_handle = DataHandle().data_handle
|
||||
|
||||
|
||||
Reference in New Issue
Block a user