import os
import re
import yaml
from vcr import VCR
from vcr.request import Request
from vcr.serialize import CASSETTE_FORMAT_VERSION, _warn_about_old_cassette_format, _looks_like_an_old_cassette
from vcr.serializers import compat
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
class BinarySerializer(object):
@classmethod
def deserialize(cls, cassette_string):
try:
data = yaml.load(cassette_string, Loader=Loader)
except (ImportError, yaml.constructor.ConstructorError):
_warn_about_old_cassette_format()
if _looks_like_an_old_cassette(data):
_warn_about_old_cassette_format()
requests = [Request._from_dict(r["request"]) for r in data["interactions"]]
responses = [compat.convert_to_bytes(r["response"]) for r in data["interactions"] if r.get('response')]
return requests, responses
@classmethod
def serialize(cls, cassette_dict):
interactions = [
{
"id": str(i),
"request": compat.convert_to_unicode(r[0]._to_dict()),
"response": compat.convert_to_unicode(r[1]),
}
for i, r in enumerate(zip(cassette_dict["requests"], cassette_dict["responses"]))
]
data = {"version": CASSETTE_FORMAT_VERSION, "interactions": interactions}
return yaml.dump(data, Dumper=Dumper)
class CustomPersister:
@classmethod
def load_cassette(cls, cassette_path, serializer=BinarySerializer):
try:
with open(cassette_path) as f:
cassette_content = f.read()
except OSError:
raise ValueError("Cassette not found.")
cassette = serializer.deserialize(cassette_content)
return cassette
@staticmethod
def save_cassette(cassette_path, cassette_dict,
serializer=BinarySerializer):
data = serializer.serialize(cassette_dict)
dirname, filename = os.path.split(cassette_path)
if dirname and not os.path.exists(dirname):
os.makedirs(dirname)
with open(cassette_path, "wb") as f:
f.write(data)
#f.write(data.encode('utf-8'))
def filter_cassette(
interactions, id_=None, status=None, uri=None, method=None
):
filters = []
def id_filter(item):
return item["id"] == id_
def status_filter(item):
return item["status"].upper() == status.upper()
def uri_filter(item):
return bool(re.search(uri, item["request"]["uri"]))
def method_filter(item):
return bool(re.search(method, item["request"]["method"]))
if id_ is not None:
filters.append(id_filter)
if status is not None:
filters.append(status_filter)
if uri is not None:
filters.append(uri_filter)
if method is not None:
filters.append(method_filter)
def is_match(interaction):
return all(filter_(interaction) for filter_ in filters)
return filter(is_match, interactions)
d_vcr = VCR(record_mode='new_episodes')
d_vcr.register_serializer(name='binary', serializer=BinarySerializer())
d_vcr.serializer = 'binary'
d_vcr.register_persister(CustomPersister)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
资源分类:Python库 所属语言:Python 资源全名:response_differ-0.1.16.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
response_differ-0.1.16.tar.gz (9个子文件)
response_differ-0.1.16
PKG-INFO 493B
pyproject.toml 459B
response_differ
cheks.py 1KB
response_diff
cli.py 2KB
__init__.py 0B
play.py 2KB
cassettes.py 3KB
exceptions.py 1KB
setup.py 857B
共 9 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 14w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- exp4_2.c.sln
- [雷军]美妙的爱情......福的味道。.mp3
- 2023-04-06-项目笔记 - 第三百二十阶段 - 4.4.2.318全局变量的作用域-318 -2025.11.17
- 2023-04-06-项目笔记 - 第三百二十阶段 - 4.4.2.318全局变量的作用域-318 -2025.11.17
- java资源异步IO框架 Cindy
- java资源业务流程管理(BPM)和工作流系统 Activiti
- java资源高性能内存消息和事件驱动库 Chronicle
- 哋它亢技术应用2慕课自动化学习
- java资源高性能的JSON处理 Jackson
- java资源高性能的Java 3D引擎 Xith3D
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功