mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-10 09:35:34 +00:00
Add global toggle to use_cassette.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import logging
|
||||
from .config import VCR
|
||||
from . import cassette
|
||||
|
||||
# Set default logging handler to avoid "No handler found" warnings.
|
||||
try: # Python 2.7+
|
||||
@@ -9,6 +10,9 @@ except ImportError:
|
||||
def emit(self, record):
|
||||
pass
|
||||
|
||||
def global_toggle(enabled=True):
|
||||
cassette.use_cassette._enabled = enabled
|
||||
|
||||
|
||||
logging.getLogger(__name__).addHandler(NullHandler())
|
||||
|
||||
|
||||
@@ -14,15 +14,33 @@ from .matchers import requests_match, uri, method
|
||||
from .errors import UnhandledHTTPRequestError
|
||||
|
||||
|
||||
class NullContextDecorator(object):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def __enter__(self, *args):
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
pass
|
||||
|
||||
def __call__(self, function):
|
||||
return function
|
||||
|
||||
|
||||
class use_cassette(object):
|
||||
|
||||
_enabled = True
|
||||
|
||||
def __init__(self, cls, *args, **kwargs):
|
||||
self.args = args
|
||||
self.kwargs = kwargs
|
||||
self.cls = cls
|
||||
|
||||
def __enter__(self):
|
||||
self._cassette = self.cls.load(*self.args, **self.kwargs)
|
||||
self._cassette = self.cls.load(*self.args, **self.kwargs) if self._enabled \
|
||||
else NullContextDecorator()
|
||||
return self._cassette.__enter__()
|
||||
|
||||
def __exit__(self, *args):
|
||||
|
||||
Reference in New Issue
Block a user