aiohttp_utils/routing.py
def add_route_context(
    app: web.Application, module=None, url_prefix: str=None, name_prefix: str=None
):
    """Context manager which yields a function for adding multiple routes from a given module.

    Example:

    .. code-block:: python

        # myapp/articles/views.py
        async def list_articles(request):
            return web.Response(b'article list...')

        async def create_article(request):
            return web.Response(b'created article...')

    .. code-block:: python

        # myapp/app.py
        from myapp.articles import views

        with add_route_context(app, url_prefix='/api/', name_prefix='articles') as route:
            route('GET', '/articles/', views.list_articles)
            route('POST', '/articles/', views.create_article)

        app.router['articles.list_articles'].url()  # /api/articles/

    If you prefer, you can also pass module and handler names as strings.

    .. code-block:: python

        with add_route_context(app, module='myapp.articles.views',
                               url_prefix='/api/', name_prefix='articles') as route:
            route('GET', '/articles/', 'list_articles')
            route('POST', '/articles/', 'create_article')

    :param app: Application to add routes to.
    :param module: Import path to module (str) or module object which contains the handlers.
    :param url_prefix: Prefix to prepend to all route paths.
    :param name_prefix: Prefix to prepend to all route names.
    """
    if isinstance(module, (str, bytes)):
        module = importlib.import_module(module)

    def add_route(method, path, handler, name=None):
        """
        :param str method: HTTP method.
        :param str path: Path for the route.
        :param handler: A handler function or a name of a handler function contained
            in `module`.
        :param str name: Name for the route. If `None`, defaults to the handler's
            function name.
        """
        if isinstance(handler, (str, bytes)):
            if not module:
                raise ValueError(
                    'Must pass module to add_route_context if passing handler name strings.'
                )
            name = name or handler
            handler = getattr(module, handler)
        else:
            name = name or handler.__name__
        path = make_path(path, url_prefix)
        name = '.'.join((name_prefix, name)) if name_prefix else name
        return app.router.add_route(method, path, handler, name=name)
    yield add_route
def add_route_context(
    app: web.Application, module=None, url_prefix: str=None, name_prefix: str=None
):
    if isinstance(module, (str, bytes)):
        module = importlib.import_module(module)

    def add_route(method, path, handler, name=None):
        """
        :param str method: HTTP method.
        :param str path: Path for the route.
        :param handler: A handler function or a name of a handler function contained
            in `module`.
        :param str name: Name for the route. If `None`, defaults to the handler's
            function name.
        """
        if isinstance(handler, (str, bytes)):
            if not module:
                raise ValueError(
                    'Must pass module to add_route_context if passing handler name strings.'
                )
            name = name or handler
            handler = getattr(module, handler)
        else:
            name = name or handler.__name__
        path = make_path(path, url_prefix)
        name = '.'.join((name_prefix, name)) if name_prefix else name
        return app.router.add_route(method, path, handler, name=name)
    yield add_route
def add_route_context ( app : web . Application , module = None , url_prefix : str = None , name_prefix : str = None ) : if isinstance ( module , ( str , bytes ) ) : module = importlib . import_module ( module ) def add_route ( method , path , handler , name = None ) : """
        :param str method: HTTP method.
        :param str path: Path for the route.
        :param handler: A handler function or a name of a handler function contained
            in `module`.
        :param str name: Name for the route. If `None`, defaults to the handler's
            function name.
        """ if isinstance ( handler , ( str , bytes ) ) : if not module : raise ValueError ( 'Must pass module to add_route_context if passing handler name strings.' ) name = name or handler handler = getattr ( module , handler ) else : name = name or handler . __name__ path = make_path ( path , url_prefix ) name = '.' . join ( ( name_prefix , name ) ) if name_prefix else name return app . router . add_route ( method , path , handler , name = name ) yield add_route
Context manager which yields a function for adding multiple routes from a given module.

    Example:

    .. code-block:: python

        # myapp/articles/views.py
        async def list_articles(request):
            return web.Response(b'article list...')

        async def create_article(request):
            return web.Response(b'created article...')

    .. code-block:: python

        # myapp/app.py
        from myapp.articles import views

        with add_route_context(app, url_prefix='/api/', name_prefix='articles') as route:
            route('GET', '/articles/', views.list_articles)
            route('POST', '/articles/', views.create_article)

        app.router['articles.list_articles'].url()  # /api/articles/

    If you prefer, you can also pass module and handler names as strings.

    .. code-block:: python

        with add_route_context(app, module='myapp.articles.views',
                               url_prefix='/api/', name_prefix='articles') as route:
            route('GET', '/articles/', 'list_articles')
            route('POST', '/articles/', 'create_article')

    :param app: Application to add routes to.
    :param module: Import path to module (str) or module object which contains the handlers.
    :param url_prefix: Prefix to prepend to all route paths.
    :param name_prefix: Prefix to prepend to all route names.
Context manager which yields a function for adding multiple routes from a given module .