当前位置: 首页>>代码示例>>Python>>正文


Python UrlDispatcher.add_resource方法代码示例

本文整理汇总了Python中aiohttp.web.UrlDispatcher.add_resource方法的典型用法代码示例。如果您正苦于以下问题:Python UrlDispatcher.add_resource方法的具体用法?Python UrlDispatcher.add_resource怎么用?Python UrlDispatcher.add_resource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在aiohttp.web.UrlDispatcher的用法示例。


在下文中一共展示了UrlDispatcher.add_resource方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ParentResource

# 需要导入模块: from aiohttp.web import UrlDispatcher [as 别名]
# 或者: from aiohttp.web.UrlDispatcher import add_resource [as 别名]
class ParentResource(Resource):

    def __init__(self, path, *, name=None):
        super(ParentResource, self).__init__(name=name)
        self._path = path.rstrip('/')
        self.router = UrlDispatcher()

    @asyncio.coroutine
    def resolve(self, method, path):
        allowed_methods = set()
        if not path.startswith(self._path + '/'):
            return None, allowed_methods

        path = path[len(self._path):]

        for resource in self.router._resources:
            match_dict, allowed = yield from resource.resolve(method, path)
            if match_dict is not None:
                return match_dict, allowed_methods
            else:
                allowed_methods |= allowed
        return None, allowed_methods

    def add_resource(self, path, *, name=None):
        """Add resource."""
        return self.router.add_resource(path, name=name)

    def get_info(self):
        return {'path': self._path}

    def url(self, name=None, **kwargs):
        if name:
            return self._path + self.router[name].url(**kwargs)
        return self._path + '/'
开发者ID:hugosenari,项目名称:muffin,代码行数:36,代码来源:urls.py

示例2: TestUrlDispatcher

# 需要导入模块: from aiohttp.web import UrlDispatcher [as 别名]
# 或者: from aiohttp.web.UrlDispatcher import add_resource [as 别名]

#.........这里部分代码省略.........
    def test_resource_adapter_resolve_not_math(self):
        route = PlainRoute('GET', lambda req: None, None, '/path')
        self.router.register_route(route)
        resource = route.resource
        self.assertEqual((None, set()),
                         self.loop.run_until_complete(
                             resource.resolve('GET', '/another/path')))

    def test_resource_adapter_resolve_bad_method(self):
        route = PlainRoute('POST', lambda req: None, None, '/path')
        self.router.register_route(route)
        resource = route.resource
        self.assertEqual((None, {'POST'}),
                         self.loop.run_until_complete(
                         resource.resolve('GET', '/path')))

    def test_resource_adapter_resolve_wildcard(self):
        route = PlainRoute('*', lambda req: None, None, '/path')
        self.router.register_route(route)
        resource = route.resource
        match_info, allowed = self.loop.run_until_complete(
            resource.resolve('GET', '/path'))
        self.assertEqual(allowed, {'*'})  # TODO: expand wildcard
        self.assertIsNotNone(match_info)

    def test_resource_adapter_iter(self):
        route = PlainRoute('GET', lambda req: None, None, '/path')
        self.router.register_route(route)
        resource = route.resource
        self.assertEqual(1, len(resource))
        self.assertEqual([route], list(resource))

    def test_resource_iter(self):
        resource = self.router.add_resource('/path')
        r1 = resource.add_route('GET', lambda req: None)
        r2 = resource.add_route('POST', lambda req: None)
        self.assertEqual(2, len(resource))
        self.assertEqual([r1, r2], list(resource))

    def test_deprecate_bare_generators(self):
        resource = self.router.add_resource('/path')

        def gen(request):
            yield

        with self.assertWarns(DeprecationWarning):
            resource.add_route('GET', gen)

    def test_view_route(self):
        resource = self.router.add_resource('/path')

        route = resource.add_route('GET', View)
        self.assertIs(View, route.handler)

    def test_resource_route_match(self):
        resource = self.router.add_resource('/path')
        route = resource.add_route('GET', lambda req: None)
        self.assertEqual({}, route.resource._match('/path'))

    def test_plain_route_url(self):
        route = PlainRoute('GET', lambda req: None, None, '/path')
        self.router.register_route(route)
        self.assertEqual('/path?arg=1', route.url(query={'arg': 1}))

    def test_dynamic_route_url(self):
        route = DynamicRoute('GET', lambda req: None, None,
开发者ID:1st1,项目名称:aiohttp,代码行数:70,代码来源:test_urldispatch.py


注:本文中的aiohttp.web.UrlDispatcher.add_resource方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。