1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-09 09:13:23 +00:00

Add Path handling to use_cassette and to filesystem.py persister

* now it is possible to use path from pathlib
This commit is contained in:
Josef
2022-09-24 23:57:26 +02:00
committed by Jair Henrique
parent 511d0ab855
commit 526fdbb194
3 changed files with 28 additions and 15 deletions

View File

@@ -4,6 +4,7 @@ import inspect
import os
import types
from collections import abc as collections_abc
from pathlib import Path
import six
@@ -98,7 +99,7 @@ class VCR:
return matchers
def use_cassette(self, path=None, **kwargs):
if path is not None and not isinstance(path, str):
if path is not None and not isinstance(path, (str, Path)):
function = path
# Assume this is an attempt to decorate a function
return self._use_cassette(**kwargs)(function)

View File

@@ -1,6 +1,6 @@
# .. _persister_example:
import os
from pathlib import Path
from ..serialize import deserialize, serialize
@@ -8,19 +8,25 @@ from ..serialize import deserialize, serialize
class FilesystemPersister:
@classmethod
def load_cassette(cls, cassette_path, serializer):
try:
with open(cassette_path) as f:
cassette_content = f.read()
except OSError:
cassette_path = Path(cassette_path) # if cassette path is already Path this is no operation
if not cassette_path.is_file():
raise ValueError("Cassette not found.")
cassette = deserialize(cassette_content, serializer)
return cassette
try:
with cassette_path.open() as f:
data = f.read()
except UnicodeEncodeError as err:
raise ValueError("Can't read Cassette, Encoding is broken") from err
return deserialize(data, serializer)
@staticmethod
def save_cassette(cassette_path, cassette_dict, serializer):
data = serialize(cassette_dict, serializer)
dirname, filename = os.path.split(cassette_path)
if dirname and not os.path.exists(dirname):
os.makedirs(dirname)
with open(cassette_path, "w") as f:
cassette_path = Path(cassette_path) # if cassette path is already Path this is no operation
cassette_folder = cassette_path.parent
if not cassette_folder.exists():
cassette_folder.mkdir(parents=True)
with cassette_path.open("w") as f:
f.write(data)