Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def wrapper(view):
if handler is None:
handler_ = view
methods_ = methods or [METH_ANY]
if isfunction(handler_) or ismethod(handler_):
handler_ = Handler.from_view(view, *methods_, name=name)
handler_.bind(self, *paths, methods=methods_, name=name)
else:
view_name = view.__name__
if not hasattr(handler, view_name):
setattr(handler, view_name, to_coroutine(view))
name_ = name or view_name
handler.bind(self, *paths, methods=methods, name=name_, view=view_name)
return view
def ctx_provider(self, func):
""" Decorator for adding a context provider.
::
@jade.ctx_provider
def my_context():
return {...}
"""
func = to_coroutine(func)
self.providers.append(func)
return func
def middleware(self, func):
"""Register given middleware (v1)."""
self.middlewares.append(web.middleware(to_coroutine(func)))
def wrapper(method):
"""Store route params into method."""
method = to_coroutine(method)
setattr(method, ROUTE_PARAMS_ATTR, (paths, methods, name))
if handler and not hasattr(handler, method.__name__):
setattr(handler, method.__name__, method)
return method
return wrapper
# Ensure that the class methods are exist and iterable
if not cls.methods:
cls.methods = set(method for method in METH_ALL if method.lower() in cls.__dict__)
elif isinstance(cls.methods, str):
cls.methods = [cls.methods]
cls.methods = [method.upper() for method in cls.methods]
# Ensure that coroutine methods is coroutines
for name in coroutines:
method = getattr(cls, name, None)
if not method:
continue
setattr(cls, name, to_coroutine(method))
return cls
def wrapper(view):
view = to_coroutine(view)
@asyncio.coroutine
@functools.wraps(view)
def handler(request, *args, **kwargs):
yield from self.check_user(request, func, location, **rkwargs)
return (yield from view(request, *args, **kwargs))
return handler
def user_loader(self, func):
""" Register a function as user loader. """
self._user_loader = to_coroutine(func)
return self._user_loader
def from_view(cls, view, *methods, name=None):
"""Create a handler class from function or coroutine."""
docs = getattr(view, '__doc__', None)
view = to_coroutine(view)
methods = methods or ['GET']
if METH_ANY in methods:
methods = METH_ALL
def proxy(self, *args, **kwargs):
return view(*args, **kwargs)
params = {m.lower(): proxy for m in methods}
params['methods'] = methods
if docs:
params['__doc__'] = docs
return type(name or view.__name__, (cls,), params)