本文整理匯總了Python中aiohttp.abc.AbstractView方法的典型用法代碼示例。如果您正苦於以下問題:Python abc.AbstractView方法的具體用法?Python abc.AbstractView怎麽用?Python abc.AbstractView使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類aiohttp.abc
的用法示例。
在下文中一共展示了abc.AbstractView方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: template
# 需要導入模塊: from aiohttp import abc [as 別名]
# 或者: from aiohttp.abc import AbstractView [as 別名]
def template(
template_name: str,
*,
app_key: str = APP_KEY,
encoding: str = 'utf-8',
status: int = 200
) -> Any:
def wrapper(func: Any) -> Any:
@functools.wraps(func)
async def wrapped(*args: Any) -> web.StreamResponse:
if asyncio.iscoroutinefunction(func):
coro = func
else:
warnings.warn("Bare functions are deprecated, "
"use async ones", DeprecationWarning)
coro = asyncio.coroutine(func)
context = await coro(*args)
if isinstance(context, web.StreamResponse):
return context
# Supports class based views see web.View
if isinstance(args[0], AbstractView):
request = args[0].request
else:
request = args[-1]
response = render_template(template_name, request, context,
app_key=app_key, encoding=encoding)
response.set_status(status)
return response
return wrapped
return wrapper
示例2: template
# 需要導入模塊: from aiohttp import abc [as 別名]
# 或者: from aiohttp.abc import AbstractView [as 別名]
def template(template_name, *, app_key=APP_KEY, encoding='utf-8', status=200):
"""
Decorator compatible with aiohttp_apiset router
"""
def wrapper(func):
@functools.wraps(func)
async def wrapped(*args, **kwargs):
if asyncio.iscoroutinefunction(func):
coro = func
else:
coro = asyncio.coroutine(func)
context = await coro(*args, **kwargs)
if isinstance(context, web.StreamResponse):
return context
if 'request' in kwargs:
request = kwargs['request']
elif not args:
request = None
warnings.warn("Request not detected")
elif isinstance(args[0], AbstractView):
request = args[0].request
else:
request = args[-1]
response = render_template(template_name, request, context,
app_key=app_key, encoding=encoding)
response.set_status(status)
return response
return wrapped
return wrapper
示例3: __init__
# 需要導入模塊: from aiohttp import abc [as 別名]
# 或者: from aiohttp.abc import AbstractView [as 別名]
def __init__(self, method, handler, *,
expect_handler=None,
resource=None):
if expect_handler is None:
expect_handler = _defaultExpectHandler
assert asyncio.iscoroutinefunction(expect_handler), \
'Coroutine is expected, got {!r}'.format(expect_handler)
method = method.upper()
if not HTTP_METHOD_RE.match(method):
raise ValueError("{} is not allowed HTTP method".format(method))
assert callable(handler), handler
if asyncio.iscoroutinefunction(handler):
pass
elif inspect.isgeneratorfunction(handler):
warnings.warn("Bare generators are deprecated, "
"use @coroutine wrapper", DeprecationWarning)
elif isinstance(handler, type) and issubclass(handler, AbstractView):
pass
else:
@functools.wraps(handler)
async def handler_wrapper(*args, **kwargs):
result = old_handler(*args, **kwargs)
if asyncio.iscoroutine(result):
result = await result
return result
old_handler = handler
handler = handler_wrapper
self._method = method
self._handler = handler
self._expect_handler = expect_handler
self._resource = resource
示例4: add_view
# 需要導入模塊: from aiohttp import abc [as 別名]
# 或者: from aiohttp.abc import AbstractView [as 別名]
def add_view(
self, path: str, handler: Type[AbstractView], **kwargs: Any
) -> web.AbstractRoute:
return self.add_route(hdrs.METH_ANY, path, handler, **kwargs)
示例5: add_route
# 需要導入模塊: from aiohttp import abc [as 別名]
# 或者: from aiohttp.abc import AbstractView [as 別名]
def add_route(
self,
method: str,
path: str,
handler: Union[_SwaggerHandler, Type[AbstractView]],
*,
name: Optional[str] = None,
expect_handler: Optional[ExpectHandler] = None,
validate: Optional[bool] = None,
) -> web.AbstractRoute:
if validate is None:
need_validation: bool = self.validate
else:
need_validation = False if not self.validate else validate
if need_validation and path in self.spec["paths"]:
if isinstance(handler, type) and issubclass(handler, AbstractView):
for meth in hdrs.METH_ALL:
meth = meth.lower()
if meth not in self.spec["paths"][path]:
continue
handler_ = getattr(handler, meth, None)
if handler_ is None:
continue
route = SwaggerRoute(meth, path, handler_, swagger=self)
setattr(
handler,
meth,
functools.partialmethod(
self._handle_swagger_method_call, route
),
)
else:
method_lower = method.lower()
if method_lower in self.spec["paths"][path]:
route = SwaggerRoute(method_lower, path, handler, swagger=self)
handler = functools.partial(self._handle_swagger_call, route)
return self._app.router.add_route(
method, path, handler, name=name, expect_handler=expect_handler
)
示例6: _get_request
# 需要導入模塊: from aiohttp import abc [as 別名]
# 或者: from aiohttp.abc import AbstractView [as 別名]
def _get_request(args):
# Supports class based views see web.View
if isinstance(args[0], AbstractView):
return args[0].request
return args[-1]
示例7: _import_handler
# 需要導入模塊: from aiohttp import abc [as 別名]
# 或者: from aiohttp.abc import AbstractView [as 別名]
def _import_handler(cls, path: str):
parts = path.rsplit('.', 2)
if len(parts) == 3:
p, v, h = parts
if v == v.lower():
p = '.'.join((p, v))
v = ''
elif len(parts) == 2:
p, h = parts
v = ''
else:
raise ValueError(path)
package = importlib.import_module(p)
if not v:
return cls._wrap_handler(getattr(package, h))
View = getattr(package, v)
if issubclass(View, AbstractView):
return cls._wrap_handler(View)
handler = getattr(View, h)
signature = inspect.signature(getattr(View(), h))
handler_kwargs = dict(signature.parameters)
if hasattr(View, 'init'):
async def init(request):
vi = View()
await vi.init(request)
return vi
else:
async def init(request):
vi = View()
vi.request = request
return vi
if not asyncio.iscoroutinefunction(handler):
handler = asyncio.coroutine(handler)
if 'request' in handler_kwargs:
@functools.wraps(handler)
async def wrap_handler(request, *args, **kwargs):
vi = await init(request)
return await handler(vi, request, *args, **kwargs)
else:
@functools.wraps(handler) # type: ignore
async def wrap_handler(request, *args, **kwargs):
vi = await init(request)
return await handler(vi, *args, **kwargs)
handler_kwargs['request'] = None # type: ignore
wrap_handler.__signature__ = signature # type: ignore
return wrap_handler, handler_kwargs
示例8: add_route
# 需要導入模塊: from aiohttp import abc [as 別名]
# 或者: from aiohttp.abc import AbstractView [as 別名]
def add_route(
self,
method: str,
path: str,
handler: Union[_SwaggerHandler, Type[AbstractView]],
*,
name: Optional[str] = None,
expect_handler: Optional[ExpectHandler] = None,
validate: Optional[bool] = None,
) -> web.AbstractRoute:
if validate is None:
need_validation: bool = self.validate
else:
need_validation = False if not self.validate else validate
if isinstance(handler, type) and issubclass(handler, AbstractView):
for meth in hdrs.METH_ALL:
meth = meth.lower()
handler_ = getattr(handler, meth, None)
if handler_ is not None:
setattr(
handler,
meth,
self._wrap_handler(
meth,
path,
handler_,
is_method=True,
validate=need_validation,
),
)
else:
if method == hdrs.METH_ANY:
for meth in (
hdrs.METH_GET,
hdrs.METH_POST,
hdrs.METH_PUT,
hdrs.METH_PATCH,
hdrs.METH_DELETE,
):
meth = meth.lower()
handler = self._wrap_handler(
meth, path, handler, is_method=False, validate=need_validation,
)
else:
handler = self._wrap_handler(
method.lower(),
path,
handler,
is_method=False,
validate=need_validation,
)
return self._app.router.add_route(
method, path, handler, name=name, expect_handler=expect_handler
)