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

Use contextdecorator from contextlib2. add logging for entering context.

This commit is contained in:
Ivan Malison
2014-09-18 15:25:42 -07:00
parent 4868a63876
commit 0c19acd74f
2 changed files with 12 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
'''The container for recorded requests and responses'''
import logging
import contextlib2
import functools
try:
from collections import Counter
except ImportError:
@@ -15,7 +16,10 @@ from .matchers import requests_match, uri, method
from .errors import UnhandledHTTPRequestError
class CassetteContextDecorator(object):
log = logging.getLogger(__name__)
class CassetteContextDecorator(contextlib2.ContextDecorator):
"""Context manager/decorator that handles installing the cassette and
removing cassettes.
@@ -38,7 +42,9 @@ class CassetteContextDecorator(object):
with contextlib2.ExitStack() as exit_stack:
for patcher in PatcherBuilder(cassette).build_patchers():
exit_stack.enter_context(patcher)
log.debug('Entered context for cassette at {0}.'.format(cassette._path))
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.
cassette._save()
@@ -52,13 +58,6 @@ class CassetteContextDecorator(object):
next(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):
'''A container for recorded requests and responses'''

View File

@@ -79,11 +79,13 @@ class PatcherBuilder(object):
return mock.patch.object(obj, patched_attribute, replacement_class)
def _get_cassette_subclass(self, klass):
if klass.cassette is not None:
return klass
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]
def _cassette_subclass(self, base_class):
def _build_cassette_subclass(self, base_class):
bases = (base_class,)
if not issubclass(base_class, object): # Check for old style class
bases += (object,)
@@ -144,7 +146,6 @@ class PatcherBuilder(object):
yield cpool, 'CertValidatingHTTPSConnection', VCRCertValidatingHTTPSConnection
def reset_patchers():
yield mock.patch.object(httplib, 'HTTPConnection', _HTTPConnection)
yield mock.patch.object(httplib, 'HTTPSConnection', _HTTPSConnection)