mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 01:03:24 +00:00
Use contextdecorator from contextlib2. add logging for entering context.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
'''The container for recorded requests and responses'''
|
'''The container for recorded requests and responses'''
|
||||||
|
import logging
|
||||||
|
|
||||||
import contextlib2
|
import contextlib2
|
||||||
import functools
|
|
||||||
try:
|
try:
|
||||||
from collections import Counter
|
from collections import Counter
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -15,7 +16,10 @@ from .matchers import requests_match, uri, method
|
|||||||
from .errors import UnhandledHTTPRequestError
|
from .errors import UnhandledHTTPRequestError
|
||||||
|
|
||||||
|
|
||||||
class CassetteContextDecorator(object):
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class CassetteContextDecorator(contextlib2.ContextDecorator):
|
||||||
"""Context manager/decorator that handles installing the cassette and
|
"""Context manager/decorator that handles installing the cassette and
|
||||||
removing cassettes.
|
removing cassettes.
|
||||||
|
|
||||||
@@ -38,7 +42,9 @@ class CassetteContextDecorator(object):
|
|||||||
with contextlib2.ExitStack() as exit_stack:
|
with contextlib2.ExitStack() as exit_stack:
|
||||||
for patcher in PatcherBuilder(cassette).build_patchers():
|
for patcher in PatcherBuilder(cassette).build_patchers():
|
||||||
exit_stack.enter_context(patcher)
|
exit_stack.enter_context(patcher)
|
||||||
|
log.debug('Entered context for cassette at {0}.'.format(cassette._path))
|
||||||
yield cassette
|
yield cassette
|
||||||
|
log.debug('Exiting context for cassette at {0}.'.format(cassette._path))
|
||||||
# TODO(@IvanMalison): Hmmm. it kind of feels like this should be somewhere else.
|
# TODO(@IvanMalison): Hmmm. it kind of feels like this should be somewhere else.
|
||||||
cassette._save()
|
cassette._save()
|
||||||
|
|
||||||
@@ -52,13 +58,6 @@ class CassetteContextDecorator(object):
|
|||||||
next(self.__finish, None)
|
next(self.__finish, None)
|
||||||
self.__finish = None
|
self.__finish = None
|
||||||
|
|
||||||
def __call__(self, function):
|
|
||||||
@functools.wraps(function)
|
|
||||||
def wrapped(*args, **kwargs):
|
|
||||||
with self:
|
|
||||||
return function(*args, **kwargs)
|
|
||||||
return wrapped
|
|
||||||
|
|
||||||
|
|
||||||
class Cassette(object):
|
class Cassette(object):
|
||||||
'''A container for recorded requests and responses'''
|
'''A container for recorded requests and responses'''
|
||||||
|
|||||||
@@ -79,11 +79,13 @@ class PatcherBuilder(object):
|
|||||||
return mock.patch.object(obj, patched_attribute, replacement_class)
|
return mock.patch.object(obj, patched_attribute, replacement_class)
|
||||||
|
|
||||||
def _get_cassette_subclass(self, klass):
|
def _get_cassette_subclass(self, klass):
|
||||||
|
if klass.cassette is not None:
|
||||||
|
return klass
|
||||||
if klass not in self._class_to_cassette_subclass:
|
if klass not in self._class_to_cassette_subclass:
|
||||||
self._class_to_cassette_subclass[klass] = self._cassette_subclass(klass)
|
self._class_to_cassette_subclass[klass] = self._build_cassette_subclass(klass)
|
||||||
return self._class_to_cassette_subclass[klass]
|
return self._class_to_cassette_subclass[klass]
|
||||||
|
|
||||||
def _cassette_subclass(self, base_class):
|
def _build_cassette_subclass(self, base_class):
|
||||||
bases = (base_class,)
|
bases = (base_class,)
|
||||||
if not issubclass(base_class, object): # Check for old style class
|
if not issubclass(base_class, object): # Check for old style class
|
||||||
bases += (object,)
|
bases += (object,)
|
||||||
@@ -144,7 +146,6 @@ class PatcherBuilder(object):
|
|||||||
yield cpool, 'CertValidatingHTTPSConnection', VCRCertValidatingHTTPSConnection
|
yield cpool, 'CertValidatingHTTPSConnection', VCRCertValidatingHTTPSConnection
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def reset_patchers():
|
def reset_patchers():
|
||||||
yield mock.patch.object(httplib, 'HTTPConnection', _HTTPConnection)
|
yield mock.patch.object(httplib, 'HTTPConnection', _HTTPConnection)
|
||||||
yield mock.patch.object(httplib, 'HTTPSConnection', _HTTPSConnection)
|
yield mock.patch.object(httplib, 'HTTPSConnection', _HTTPSConnection)
|
||||||
|
|||||||
Reference in New Issue
Block a user