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


Python aiohttp_jinja2.setup方法代码示例

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


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

示例1: load_plugins

# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import setup [as 别名]
def load_plugins(self, plugins):
        def trim(p):
            if p.startswith('.'):
                return False
            return True

        async def load(p):
            plugin = Plugin(name=p)
            if plugin.load_plugin():
                await self.get_service('data_svc').store(plugin)
            if plugin.name in self.get_config('plugins'):
                await plugin.enable(self.get_services())
                self.log.debug('Enabled plugin: %s' % plugin.name)
                if not plugin.version:
                    self._errors.append(Error(plugin.name, 'plugin code is not a release version'))

        for plug in filter(trim, plugins):
            if not os.path.isdir('plugins/%s' % plug) or not os.path.isfile('plugins/%s/hook.py' % plug):
                self.log.error('Problem locating the "%s" plugin. Ensure code base was cloned recursively.' % plug)
                exit(0)
            asyncio.get_event_loop().create_task(load(plug))

        templates = ['plugins/%s/templates' % p.lower() for p in self.get_config('plugins')]
        templates.append('templates')
        aiohttp_jinja2.setup(self.application, loader=jinja2.FileSystemLoader(templates)) 
开发者ID:mitre,项目名称:caldera,代码行数:27,代码来源:app_svc.py

示例2: test_url

# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import setup [as 别名]
def test_url(aiohttp_client):

    @aiohttp_jinja2.template('tmpl.jinja2')
    async def index(request):
        return {}

    async def other(request):
        return

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader(
        {'tmpl.jinja2':
         "{{ url('other', name='John_Doe')}}"}))

    app.router.add_route('GET', '/', index)
    app.router.add_route('GET', '/user/{name}', other, name='other')
    client = await aiohttp_client(app)

    resp = await client.get('/')
    assert 200 == resp.status
    txt = await resp.text()
    assert '/user/John_Doe' == txt 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:24,代码来源:test_jinja_globals.py

示例3: test_url_with_query

# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import setup [as 别名]
def test_url_with_query(aiohttp_client):

    @aiohttp_jinja2.template('tmpl.jinja2')
    async def index(request):
        return {}

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader(
        {'tmpl.jinja2':
         "{{ url('index', query_={'foo': 'bar'})}}"}))

    app.router.add_get('/', index, name='index')
    client = await aiohttp_client(app)

    resp = await client.get('/')
    assert 200 == resp.status
    txt = await resp.text()
    assert '/?foo=bar' == txt 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:20,代码来源:test_jinja_globals.py

示例4: test_url_int_param

# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import setup [as 别名]
def test_url_int_param(aiohttp_client):

    @aiohttp_jinja2.template('tmpl.jinja2')
    async def index(request):
        return {}

    async def other(request):
        return

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader(
        {'tmpl.jinja2':
         "{{ url('other', arg=1)}}"}))

    app.router.add_route('GET', '/', index)
    app.router.add_route('GET', '/uid/{arg}', other, name='other')
    client = await aiohttp_client(app)

    resp = await client.get('/')
    assert 200 == resp.status
    txt = await resp.text()
    assert '/uid/1' == txt 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:24,代码来源:test_jinja_globals.py

示例5: test_url_param_forbidden_type

# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import setup [as 别名]
def test_url_param_forbidden_type(aiohttp_client):

    async def index(request):
        with pytest.raises(TypeError,
                           match=(r"argument value should be str or int, "
                                  r"got arg -> \[<class 'bool'>\] True")):
            aiohttp_jinja2.render_template('tmpl.jinja2', request, {})
        return web.Response()

    async def other(request):
        return

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader(
        {'tmpl.jinja2':
         "{{ url('other', arg=True)}}"}))

    app.router.add_route('GET', '/', index)
    app.router.add_route('GET', '/uid/{arg}', other, name='other')
    client = await aiohttp_client(app)

    resp = await client.get('/')
    assert 200 == resp.status 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:25,代码来源:test_jinja_globals.py

示例6: test_helpers_disabled

# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import setup [as 别名]
def test_helpers_disabled(aiohttp_client):

    async def index(request):
        with pytest.raises(jinja2.UndefinedError,
                           match="'url' is undefined"):
            aiohttp_jinja2.render_template('tmpl.jinja2', request, {})
        return web.Response()

    app = web.Application()
    aiohttp_jinja2.setup(
        app,
        default_helpers=False,
        loader=jinja2.DictLoader(
            {'tmpl.jinja2': "{{ url('index')}}"})
    )

    app.router.add_route('GET', '/', index)
    client = await aiohttp_client(app)

    resp = await client.get('/')
    assert 200 == resp.status 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:23,代码来源:test_jinja_globals.py

示例7: test_static_var_missing

# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import setup [as 别名]
def test_static_var_missing(aiohttp_client, caplog):

    async def index(request):
        with pytest.raises(RuntimeError, match='static_root_url'):
            aiohttp_jinja2.render_template('tmpl.jinja2', request, {})
        return web.Response()

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader(
        {'tmpl.jinja2':
         "{{ static('whatever.js') }}"}))

    app.router.add_route('GET', '/', index)
    client = await aiohttp_client(app)

    resp = await client.get('/')
    assert 200 == resp.status  # static_root_url is not set 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:19,代码来源:test_jinja_globals.py

示例8: test_func

# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import setup [as 别名]
def test_func(aiohttp_client):

    @aiohttp_jinja2.template('tmpl.jinja2')
    async def func(request):
        return {'head': 'HEAD', 'text': 'text'}

    template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>'
    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({
        'tmpl.jinja2': template
    }))

    app.router.add_route('*', '/', func)

    client = await aiohttp_client(app)

    resp = await client.get('/')
    assert 200 == resp.status
    txt = await resp.text()
    assert '<html><body><h1>HEAD</h1>text</body></html>' == txt 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:22,代码来源:test_simple_renderer.py

示例9: test_render_class_based_view

# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import setup [as 别名]
def test_render_class_based_view(aiohttp_client):
    class MyView(web.View):
        @aiohttp_jinja2.template('tmpl.jinja2')
        async def get(self):
            return {'head': 'HEAD', 'text': 'text'}

    template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>'

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({
        'tmpl.jinja2': template
    }))

    app.router.add_route('*', '/', MyView)

    client = await aiohttp_client(app)

    resp = await client.get('/')

    assert 200 == resp.status
    txt = await resp.text()
    assert '<html><body><h1>HEAD</h1>text</body></html>' == txt 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:24,代码来源:test_simple_renderer.py

示例10: test_convert_func_to_coroutine

# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import setup [as 别名]
def test_convert_func_to_coroutine(aiohttp_client):

    @aiohttp_jinja2.template('tmpl.jinja2')
    async def func(request):
        return {'head': 'HEAD', 'text': 'text'}

    template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>'

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({
        'tmpl.jinja2': template
    }))

    app.router.add_route('*', '/', func)

    client = await aiohttp_client(app)

    resp = await client.get('/')

    txt = await resp.text()
    assert '<html><body><h1>HEAD</h1>text</body></html>' == txt 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:23,代码来源:test_simple_renderer.py

示例11: test_set_status

# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import setup [as 别名]
def test_set_status(aiohttp_client):

    @aiohttp_jinja2.template('tmpl.jinja2', status=201)
    async def func(request):
        return {'head': 'HEAD', 'text': 'text'}

    template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>'

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({
        'tmpl.jinja2': template
    }))

    app.router.add_route('*', '/', func)

    client = await aiohttp_client(app)

    resp = await client.get('/')

    assert 201 == resp.status
    txt = await resp.text()
    assert '<html><body><h1>HEAD</h1>text</body></html>' == txt 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:24,代码来源:test_simple_renderer.py

示例12: test_render_template

# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import setup [as 别名]
def test_render_template(aiohttp_client):

    async def func(request):
        return aiohttp_jinja2.render_template(
            'tmpl.jinja2', request,
            {'head': 'HEAD', 'text': 'text'})

    template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>'

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({
        'tmpl.jinja2': template
    }))

    app.router.add_route('*', '/', func)

    client = await aiohttp_client(app)

    resp = await client.get('/')

    assert 200 == resp.status
    txt = await resp.text()
    assert '<html><body><h1>HEAD</h1>text</body></html>' == txt 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:25,代码来源:test_simple_renderer.py

示例13: test_render_template_custom_status

# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import setup [as 别名]
def test_render_template_custom_status(aiohttp_client):

    async def func(request):
        return aiohttp_jinja2.render_template(
            'tmpl.jinja2', request,
            {'head': 'HEAD', 'text': 'text'}, status=404)

    template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>'

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({
        'tmpl.jinja2': template
    }))

    app.router.add_route('*', '/', func)

    client = await aiohttp_client(app)

    resp = await client.get('/')

    assert 404 == resp.status
    txt = await resp.text()
    assert '<html><body><h1>HEAD</h1>text</body></html>' == txt 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:25,代码来源:test_simple_renderer.py

示例14: test_template_not_found

# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import setup [as 别名]
def test_template_not_found():

    async def func(request):
        return aiohttp_jinja2.render_template('template', request, {})

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({}))

    app.router.add_route('GET', '/', func)

    req = make_mocked_request('GET', '/', app=app)

    with pytest.raises(web.HTTPInternalServerError) as ctx:
        await func(req)

    t = "Template 'template' not found"
    assert t == ctx.value.text
    assert t == ctx.value.reason 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:20,代码来源:test_simple_renderer.py

示例15: test_render_not_mapping

# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import setup [as 别名]
def test_render_not_mapping():

    @aiohttp_jinja2.template('tmpl.jinja2')
    async def func(request):
        return 123

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader(
        {'tmpl.jinja2': 'tmpl'}))

    app.router.add_route('GET', '/', func)

    req = make_mocked_request('GET', '/', app=app)
    msg = "context should be mapping, not <class 'int'>"
    with pytest.raises(web.HTTPInternalServerError) as ctx:
        await func(req)

    assert msg == ctx.value.text 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:20,代码来源:test_simple_renderer.py


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