mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-10 09:35:34 +00:00
Change use_cassette to pass a function to CassetteContextDecorator so that changes to the default settings on the vcr properly propogate.
This commit is contained in:
@@ -24,13 +24,17 @@ class CassetteContextDecorator(object):
|
||||
from interfering with another.
|
||||
"""
|
||||
|
||||
def __init__(self, cls, *args, **kwargs):
|
||||
self.args = args
|
||||
self.kwargs = kwargs
|
||||
@classmethod
|
||||
def from_args(cls, cassette_class, path, **kwargs):
|
||||
return cls(cassette_class, lambda: (path, kwargs))
|
||||
|
||||
def __init__(self, cls, args_getter):
|
||||
self.cls = cls
|
||||
self._args_getter = args_getter
|
||||
|
||||
def __enter__(self):
|
||||
self._cassette = self.cls.load(*self.args, **self.kwargs)
|
||||
path, kwargs = self._args_getter()
|
||||
self._cassette = self.cls.load(path, **kwargs)
|
||||
return self._cassette.__enter__()
|
||||
|
||||
def __exit__(self, *args):
|
||||
@@ -55,10 +59,12 @@ class Cassette(object):
|
||||
return new_cassette
|
||||
|
||||
@classmethod
|
||||
def use(cls, *args, **kwargs):
|
||||
return CassetteContextDecorator(cls, *args, **kwargs)
|
||||
def use_arg_getter(cls, arg_getter):
|
||||
return CassetteContextDecorator(cls, arg_getter)
|
||||
|
||||
use_cassette = use
|
||||
@classmethod
|
||||
def use(cls, *args, **kwargs):
|
||||
return CassetteContextDecorator.from_args(cls, *args, **kwargs)
|
||||
|
||||
def __init__(self,
|
||||
path,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import functools
|
||||
import os
|
||||
from .cassette import Cassette
|
||||
from .serializers import yamlserializer, jsonserializer
|
||||
@@ -74,13 +75,16 @@ class VCR(object):
|
||||
return matchers
|
||||
|
||||
def use_cassette(self, path, **kwargs):
|
||||
args_getter = functools.partial(self.get_path_and_merged_config, path, **kwargs)
|
||||
return Cassette.use_arg_getter(args_getter)
|
||||
|
||||
def get_path_and_merged_config(self, path, **kwargs):
|
||||
serializer_name = kwargs.get('serializer', self.serializer)
|
||||
matcher_names = kwargs.get('match_on', self.match_on)
|
||||
cassette_library_dir = kwargs.get(
|
||||
'cassette_library_dir',
|
||||
self.cassette_library_dir
|
||||
)
|
||||
|
||||
if cassette_library_dir:
|
||||
path = os.path.join(cassette_library_dir, path)
|
||||
|
||||
@@ -107,8 +111,7 @@ class VCR(object):
|
||||
'ignore_localhost', self.ignore_localhost
|
||||
),
|
||||
}
|
||||
|
||||
return Cassette.use(path, **merged_config)
|
||||
return path, merged_config
|
||||
|
||||
def register_serializer(self, name, serializer):
|
||||
self.serializers[name] = serializer
|
||||
|
||||
Reference in New Issue
Block a user