-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathdecorator.py
More file actions
37 lines (29 loc) · 1.11 KB
/
decorator.py
File metadata and controls
37 lines (29 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# coding=utf-8
from functools import wraps
from vcr import VCR
import os
def use_cassette(vcr=VCR(), path='fixtures/vcr_cassettes/', **kwargs):
"""
Usage:
@use_cassette() # the default path will be fixtures/vcr_cassettes/foo.yaml
def foo(self):
...
@use_cassette(path='fixtures/vcr_cassettes/synopsis.yaml', record_mode='one')
def foo(self):
...
"""
def decorator(func):
@wraps(func)
def inner_func(*args, **kw):
if os.path.isabs(path):
file_path = path
else:
fun_path, fun_filename = os.path.split(func.func_code.co_filename)
file_path = os.path.join(fun_path, path, os.path.splitext(fun_filename)[0])
if not os.path.splitext(file_path)[1]:
serializer = kwargs.get('serializer', vcr.serializer)
file_path = os.path.join(file_path, '{0}.{1}'.format(func.func_name.lower(), serializer))
with vcr.use_cassette(file_path, **kwargs):
return func(*args, **kw)
return inner_func
return decorator