1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- # -*- coding: utf-8 -*-
- #!/usr/bin/env python
- import os
- import re
- import sys
- from functools import wraps
- class inject(object):
- def __init__(self, **kwargs):
- self.kwargs = kwargs
- def __call__(self, fn):
- @wraps(fn)
- def wrapped(*args, **kwargs):
- old_trace = sys.gettrace()
- def trace_fn(frame, event, arg):
- # define a new trace_fn each time, since it needs to
- # call the *current* trace_fn if they're in debug mode
- frame.f_locals.update(self.kwargs)
- if old_trace:
- return old_trace(frame, event, arg)
- else:
- return None
- sys.settrace(trace_fn)
- try:
- retval = fn(*args, **kwargs)
- finally:
- sys.settrace(old_trace)
- return retval
- return wrapped
- def into(self, fn, *args, **kwargs):
- return self(fn)(*args, **kwargs)
- def not_allowed_in_prod(func):
- @wraps(func)
- def wrapper(*args, **kwargs):
- if re.match(os.environ['MY_ENV'], 'production'):
- raise RuntimeError('cannot import mock views in production env')
- return func(*args, **kwargs)
- return wrapper
|