예제 #1
0
    def test_uri_adjust(self):
        tl = lookup.TemplateLookup(directories=["/foo/bar"])
        assert (tl.filename_to_uri("/foo/bar/etc/lala/index.html") ==
                "/etc/lala/index.html")

        tl = lookup.TemplateLookup(directories=["./foo/bar"])
        assert (tl.filename_to_uri("./foo/bar/etc/index.html") ==
                "/etc/index.html")
예제 #2
0
    def test_uri_adjust(self):
        tl = lookup.TemplateLookup(directories=['/foo/bar'])
        assert tl.filename_to_uri('/foo/bar/etc/lala/index.html') == \
                        '/etc/lala/index.html'

        tl = lookup.TemplateLookup(directories=['./foo/bar'])
        assert tl.filename_to_uri('./foo/bar/etc/index.html') == \
                        '/etc/index.html'
예제 #3
0
    def test_custom_tag_2(self):
        collection = lookup.TemplateLookup()
        collection.put_string("base.html", """
            <%def name="foo(x, y)">
                foo: ${x} ${y}
            </%def>
 
            <%def name="bat(g)"><%
                return "the bat! %s" % g
            %></%def>
 
            <%def name="bar(x)">
                ${caller.body(z=x)}
            </%def>
        """)
 
        collection.put_string("index.html", """
            <%namespace name="myns" file="base.html"/>
 
            <%myns:foo x="${'some x'}" y="some y"/>
 
            <%myns:bar x="${myns.bat(10)}" args="z">
                record: ${z}
            </%myns:bar>
 
        """)
 
        assert result_lines(collection.get_template("index.html").render()) == [
            'foo: some x some y', 
            'record: the bat! 10'
        ]
예제 #4
0
    def test_inheritance_two(self):
        collection = lookup.TemplateLookup()
        collection.put_string("base.html", """
            <%def name="foo()">
                base.foo
            </%def>
 
            <%def name="bat()">
                base.bat
            </%def>
""")
        collection.put_string("lib.html", """
            <%inherit file="base.html"/>
            <%def name="bar()">
                lib.bar
                ${parent.foo()}
                ${self.foo()}
                ${parent.bat()}
                ${self.bat()}
            </%def>
 
            <%def name="foo()">
                lib.foo
            </%def>
 
        """)

        collection.put_string("front.html", """
            <%namespace name="lib" file="lib.html"/>
            ${lib.bar()}
        """)

        assert result_lines(collection.get_template("front.html").render()) == ['lib.bar', 'base.foo', 'lib.foo', 'base.bat', 'base.bat']
예제 #5
0
    def test_inheritance(self):
        """test namespace initialization in a base inherited template that doesnt otherwise access the namespace"""
        collection = lookup.TemplateLookup()
        collection.put_string("base.html", """
            <%namespace name="foo" file="ns.html" inheritable="True"/>
 
            ${next.body()}
""")
        collection.put_string("ns.html", """
            <%def name="bar()">
                this is ns.html->bar
            </%def>
        """)

        collection.put_string("index.html", """
            <%inherit file="base.html"/>
 
            this is index
            ${self.foo.bar()}
        """)
 
        assert result_lines(collection.get_template("index.html").render()) == [
            "this is index",
            "this is ns.html->bar"
        ]
예제 #6
0
    def test_includes(self):
        """test that an included template also has its full hierarchy invoked."""
        collection = lookup.TemplateLookup()

        collection.put_string(
            "base", """
        <%def name="a()">base_a</%def>
        This is the base.
        ${next.body()}
        End base.
""")

        collection.put_string(
            "index", """
        <%inherit file="base"/>
        this is index.
        a is: ${self.a()}
        <%include file="secondary"/>
""")

        collection.put_string(
            "secondary", """
        <%inherit file="base"/>
        this is secondary.
        a is: ${self.a()}
""")

        assert result_lines(collection.get_template("index").render()) == [
            'This is the base.', 'this is index.', 'a is: base_a',
            'This is the base.', 'this is secondary.', 'a is: base_a',
            'End base.', 'End base.'
        ]
예제 #7
0
 def test_dynamic(self):
     collection = lookup.TemplateLookup()
     collection.put_string(
         "base", """
         this is the base.
         ${next.body()}
     """)
     collection.put_string(
         "index", """
         <%!
             def dyn(context):
                 if context.get('base', None) is not None:
                     return 'base'
                 else:
                     return None
         %>
         <%inherit file="${dyn(context)}"/>
         this is index.
     """)
     assert result_lines(
         collection.get_template('index').render()) == ['this is index.']
     assert result_lines(
         collection.get_template('index').render(base=True)) == [
             'this is the base.', 'this is index.'
         ]
예제 #8
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.look_up = lookup.TemplateLookup([self.get_template_path()],
                                          input_encoding='utf-8',
                                          output_encoding='utf-8')
     self.con = None
     self.time1 = time.time()
예제 #9
0
    def test_ccall_import(self):
        collection = lookup.TemplateLookup()
        collection.put_string("functions.html","""
            <%def name="foo()">
                this is foo
            </%def>
 
            <%def name="bar()">
                this is bar.
                ${caller.body()}
                ${caller.lala()}
            </%def>
        """)
 
        collection.put_string("index.html", """
            <%namespace name="func" file="functions.html" import="*"/>
            <%call expr="bar()">
                this is index embedded
                foo is ${foo()}
                <%def name="lala()">
                     this is lala ${foo()}
                </%def>
            </%call>
        """)
        #print collection.get_template("index.html").code
        #print collection.get_template("functions.html").code
        assert result_lines(collection.get_template("index.html").render()) == [
            "this is bar.",
            "this is index embedded",
            "foo is",
            "this is foo",
            "this is lala",
            "this is foo"
        ]
예제 #10
0
파일: test_cache.py 프로젝트: wdmchaft/hue
    def test_fileargs_implicit(self):
        l = lookup.TemplateLookup(module_directory=module_base)
        l.put_string(
            "test", """
                <%!
                    callcount = [0]
                %>
                <%def name="foo()" cached="True" cache_type='dbm'>
                    this is foo
                    <%
                    callcount[0] += 1
                    %>
                </%def>

                ${foo()}
                ${foo()}
                ${foo()}
                callcount: ${callcount}
        """)

        m = self._install_mock_cache(l.get_template('test'))
        assert result_lines(l.get_template('test').render()) == [
            'this is foo',
            'this is foo',
            'this is foo',
            'callcount: [1]',
        ]
        eq_(m.kwargs, {'type': 'dbm'})
예제 #11
0
    def test_fileargs_lookup(self):
        l = lookup.TemplateLookup(cache_dir=module_base, cache_type="file")
        l.put_string(
            "test",
            """
                <%!
                    callcount = [0]
                %>
                <%def name="foo()" cached="True">
                    this is foo
                    <%
                    callcount[0] += 1
                    %>
                </%def>

                ${foo()}
                ${foo()}
                ${foo()}
                callcount: ${callcount}
        """,
        )

        t = l.get_template("test")
        m = self._install_mock_cache(t)
        assert result_lines(l.get_template("test").render()) == [
            "this is foo",
            "this is foo",
            "this is foo",
            "callcount: [1]",
        ]
        eq_(m.kwargs, {"dir": module_base, "type": "file"})
예제 #12
0
    def test_nonexistent_namespace_uri(self):
        collection = lookup.TemplateLookup()
        collection.put_string(
            "main.html",
            """
            <%namespace name="defs" file="eefs.html"/>

            this is main.  ${defs.def1("hi")}
            ${defs.def2("there")}
""",
        )

        collection.put_string(
            "defs.html",
            """
        <%def name="def1(s)">
            def1: ${s}
        </%def>

        <%def name="def2(x)">
            def2: ${x}
        </%def>
""",
        )

        assert_raises_message_with_given_cause(
            exceptions.TemplateLookupException,
            "Can't locate template for uri 'eefs.html",
            exceptions.TopLevelLookupException,
            collection.get_template("main.html").render,
        )
예제 #13
0
    def test_import_calledfromdef(self):
        l = lookup.TemplateLookup()
        l.put_string(
            "a",
            """
        <%def name="table()">
            im table
        </%def>
        """,
        )

        l.put_string(
            "b",
            """
        <%namespace file="a" import="table"/>

        <%
            def table2():
                table()
                return ""
        %>

        ${table2()}
        """,
        )

        t = l.get_template("b")
        assert flatten_result(t.render()) == "im table"
예제 #14
0
    def test_context(self):
        """test that namespace callables get access to the current context"""
        collection = lookup.TemplateLookup()

        collection.put_string(
            "main.html",
            """
        <%namespace name="comp" file="defs.html"/>

        this is main.  ${comp.def1()}
        ${comp.def2("there")}
""",
        )

        collection.put_string(
            "defs.html",
            """
        <%def name="def1()">
            def1: x is ${x}
        </%def>

        <%def name="def2(x)">
            def2: x is ${x}
        </%def>
""",
        )

        assert (flatten_result(
            collection.get_template("main.html").render(x="context x")) ==
                "this is main. def1: x is context x def2: x is there")
예제 #15
0
    def test_template(self):
        collection = lookup.TemplateLookup()

        collection.put_string(
            "main.html",
            """
        <%namespace name="comp" file="defs.html"/>

        this is main.  ${comp.def1("hi")}
        ${comp.def2("there")}
""",
        )

        collection.put_string(
            "defs.html",
            """
        <%def name="def1(s)">
            def1: ${s}
        </%def>

        <%def name="def2(x)">
            def2: ${x}
        </%def>
""",
        )

        assert (flatten_result(collection.get_template("main.html").render())
                == "this is main. def1: hi def2: there")
예제 #16
0
    def test_closure_import(self):
        collection = lookup.TemplateLookup()
        collection.put_string("functions.html","""
            <%def name="foo()">
                this is foo
            </%def>
 
            <%def name="bar()">
                this is bar
            </%def>
        """)
 
        collection.put_string("index.html", """
            <%namespace file="functions.html" import="*"/>
            <%def name="cl1()">
                ${foo()}
            </%def>
 
            <%def name="cl2()">
                ${bar()}
            </%def>
 
            ${cl1()}
            ${cl2()}
        """)
        assert result_lines(collection.get_template("index.html").render(bar="this is bar", x="this is x")) == [
            "this is foo",
            "this is bar",
        ]
예제 #17
0
    def test_pageargs(self):
        collection = lookup.TemplateLookup()
        collection.put_string(
            "base",
            """
            this is the base.

            <%
            sorted_ = pageargs.items()
            sorted_ = sorted(sorted_)
            %>
            pageargs: (type: ${type(pageargs)}) ${sorted_}
            <%def name="foo()">
                ${next.body(**context.kwargs)}
            </%def>

            ${foo()}
        """,
        )
        collection.put_string(
            "index",
            """
            <%inherit file="base"/>
            <%page args="x, y, z=7"/>
            print ${x}, ${y}, ${z}
        """,
        )

        assert result_lines(
            collection.get_template("index").render_unicode(x=5, y=10)) == [
                "this is the base.",
                "pageargs: (type: <class 'dict'>) [('x', 5), ('y', 10)]",
                "print 5, 10, 7",
            ]
예제 #18
0
    def test_fileargs_lookup(self):
        l = lookup.TemplateLookup(cache_dir=module_base, cache_type='file')
        l.put_string(
            "test", """
                <%!
                    callcount = [0]
                %>
                <%def name="foo()" cached="True">
                    this is foo
                    <%
                    callcount[0] += 1
                    %>
                </%def>

                ${foo()}
                ${foo()}
                ${foo()}
                callcount: ${callcount}
        """)

        t = l.get_template('test')
        m = self._install_mock_cache(t)
        assert result_lines(l.get_template('test').render()) == [
            'this is foo',
            'this is foo',
            'this is foo',
            'callcount: [1]',
        ]
        assert m.kwargs == {'data_dir': module_base, 'type': 'file'}
예제 #19
0
    def test_overload(self):
        collection = lookup.TemplateLookup()

        collection.put_string('main.html', """
        <%namespace name="comp" file="defs.html">
            <%def name="def1(x, y)">
                overridden def1 ${x}, ${y}
            </%def>
        </%namespace>

        this is main.  ${comp.def1("hi", "there")}
        ${comp.def2("there")}
    """)

        collection.put_string('defs.html', """
        <%def name="def1(s)">
            def1: ${s}
        </%def>

        <%def name="def2(x)">
            def2: ${x}
        </%def>
    """)

        assert flatten_result(collection.get_template('main.html').render()) == "this is main. overridden def1 hi, there def2: there"
예제 #20
0
    def test_pageargs_2(self):
        collection = lookup.TemplateLookup()
        collection.put_string(
            "base", """
            this is the base.
            
            ${next.body(**context.kwargs)}
            
            <%def name="foo(**kwargs)">
                ${next.body(**kwargs)}
            </%def>

            <%def name="bar(**otherargs)">
                ${next.body(z=16, **context.kwargs)}
            </%def>

            ${foo(x=12, y=15, z=8)}
            ${bar(x=19, y=17)}
        """)
        collection.put_string(
            "index", """
            <%inherit file="base"/>
            <%page args="x, y, z=7"/>
            pageargs: ${x}, ${y}, ${z}
        """)
        assert result_lines(
            collection.get_template('index').render(x=5, y=10)) == [
                "this is the base.", "pageargs: 5, 10, 7",
                "pageargs: 12, 15, 8", "pageargs: 5, 10, 16"
            ]
예제 #21
0
    def test_in_remote_def(self):
        collection = lookup.TemplateLookup()
        collection.put_string("main.html", """
            <%namespace name="foo" file="ns.html"/>

            this is main.  ${bar()}
            <%def name="bar()">
                this is bar, foo is ${foo.bar()}
            </%def>
        """)

        collection.put_string("ns.html", """
            <%def name="bar()">
                this is ns.html->bar
            </%def>
        """)
 
        collection.put_string("index.html", """
            <%namespace name="main" file="main.html"/>
 
            this is index
            ${main.bar()}
        """)

        assert result_lines(collection.get_template("index.html").render()) == [ 
            "this is index",
            "this is bar, foo is" ,
            "this is ns.html->bar"
        ]
예제 #22
0
    def test_basic(self):
        collection = lookup.TemplateLookup()

        collection.put_string(
            'main', """
<%inherit file="base"/>

<%def name="header()">
    main header.
</%def>

this is the content.
""")

        collection.put_string(
            'base', """
This is base.

header: ${self.header()}

body: ${self.body()}

footer: ${self.footer()}

<%def name="footer()">
    this is the footer. header again ${next.header()}
</%def>
""")

        assert result_lines(collection.get_template('main').render()) == [
            'This is base.', 'header:', 'main header.', 'body:',
            'this is the content.', 'footer:',
            'this is the footer. header again', 'main header.'
        ]
예제 #23
0
    def test_ccall(self):
        collection = lookup.TemplateLookup()
        collection.put_string("base.html", """
            <%namespace name="foo" file="ns.html" inheritable="True"/>

            ${next.body()}
    """)
        collection.put_string("ns.html", """
            <%def name="bar()">
                this is ns.html->bar
                caller body: ${caller.body()}
            </%def>
        """)

        collection.put_string("index.html", """
            <%inherit file="base.html"/>

            this is index
            <%call expr="self.foo.bar()">
                call body
            </%call>
        """)

        assert result_lines(collection.get_template("index.html").render()) == [
            "this is index",
            "this is ns.html->bar",
            "caller body:",
            "call body"
        ]
예제 #24
0
 def _load_templates(self, config):
     """ load templates
     @dict: configuration of ldapcherry
     """
     # definition of the template directory
     self.template_dir = self._get_param('resources', 'templates.dir',
                                         config)
     cherrypy.log.error(msg="loading templates from dir '%(dir)s'" %
                        {'dir': self.template_dir},
                        severity=logging.DEBUG)
     # preload templates
     self.temp_lookup = lookup.TemplateLookup(
         directories=self.template_dir,
         input_encoding='utf-8',
         default_filters=['unicode', 'h'])
     # load each template
     self.temp = {}
     for t in (
             'index.tmpl',
             'error.tmpl',
             'login.tmpl',
             '404.tmpl',
             'searchadmin.tmpl',
             'searchuser.tmpl',
             'adduser.tmpl',
             'roles.tmpl',
             'groups.tmpl',
             'form.tmpl',
             'selfmodify.tmpl',
             'modify.tmpl',
             'service_unavailable.tmpl',
             'reset_password_req.tmpl',
             'reset_password_set.tmpl',
     ):
         self.temp[t] = self.temp_lookup.get_template(t)
예제 #25
0
    def test_scope_nine(self):
        """test that 'enclosing scope' doesnt
        get exported to other templates"""

        l = lookup.TemplateLookup()
        l.put_string(
            "main",
            """
        <%
            x = 5
        %>
        this is main.  <%include file="secondary"/>
""",
        )

        l.put_string(
            "secondary",
            """
        this is secondary.  x is ${x}
""",
        )

        eq_(
            flatten_result(l.get_template("main").render(x=2)),
            "this is main. this is secondary. x is 2",
        )
예제 #26
0
    def test_dont_accept_relative_outside_of_root(self):
        """test the mechanics of an include where
        the include goes outside of the path"""
        tl = lookup.TemplateLookup(
            directories=[os.path.join(config.template_base, "subdir")])
        index = tl.get_template("index.html")

        ctx = runtime.Context(FastEncodingBuffer())
        ctx._with_template = index

        assert_raises_message(
            exceptions.TemplateLookupException,
            'Template uri "../index.html" is invalid - it '
            "cannot be relative outside of the root path",
            runtime._lookup_template,
            ctx,
            "../index.html",
            index.uri,
        )

        assert_raises_message(
            exceptions.TemplateLookupException,
            'Template uri "../othersubdir/foo.html" is invalid - it '
            "cannot be relative outside of the root path",
            runtime._lookup_template,
            ctx,
            "../othersubdir/foo.html",
            index.uri,
        )

        # this is OK since the .. cancels out
        runtime._lookup_template(ctx, "foo/../index.html", index.uri)
예제 #27
0
def tpl_txt_gen_iter(tpl_path,
                     count=None,
                     environ=None,
                     func_factory_map=None):
    if environ is None:
        environ = TplTxtGenEnviron()

    environ.tpl_path = tpl_path
    environ.count = count

    environ.root_dir = os.path.dirname(environ.tpl_path)
    environ.tpl_name = os.path.basename(environ.tpl_path)
    environ.tpl_lookup = mako_lookup.TemplateLookup(
        directories=(environ.root_dir, ))
    environ.tpl = environ.tpl_lookup.get_template(environ.tpl_name)

    if func_factory_map is None:
        func_factory_map = DEFAULT_FUNC_FACTORY_MAP

    func_factories = {
        func_name: func_factory_map[func_name](weakref.ref(environ))
        for func_name in func_factory_map
    }

    for i in count_iter(environ.count):
        tpl_kwargs = {
            func_name: func_factories[func_name]()
            for func_name in func_factories
        }

        yield environ.tpl.render(**tpl_kwargs)
예제 #28
0
    def test_attr(self):
        l = lookup.TemplateLookup()

        l.put_string(
            "foo.html",
            """
        <%!
            foofoo = "foo foo"
            onlyfoo = "only foo"
        %>
        <%inherit file="base.html"/>
        <%def name="setup()">
            <%
            self.attr.foolala = "foo lala"
            %>
        </%def>
        ${self.attr.basefoo}
        ${self.attr.foofoo}
        ${self.attr.onlyfoo}
        ${self.attr.lala}
        ${self.attr.foolala}
        """,
        )

        l.put_string(
            "base.html",
            """
        <%!
            basefoo = "base foo 1"
            foofoo = "base foo 2"
        %>
        <%
            self.attr.lala = "base lala"
        %>

        ${self.attr.basefoo}
        ${self.attr.foofoo}
        ${self.attr.onlyfoo}
        ${self.attr.lala}
        ${self.setup()}
        ${self.attr.foolala}
        body
        ${self.body()}
        """,
        )

        assert result_lines(l.get_template("foo.html").render()) == [
            "base foo 1",
            "foo foo",
            "only foo",
            "base lala",
            "foo lala",
            "body",
            "base foo 1",
            "foo foo",
            "only foo",
            "base lala",
            "foo lala",
        ]
예제 #29
0
 def test_check_not_found(self):
     tl = lookup.TemplateLookup()
     tl.put_string("foo", "this is a template")
     f = tl.get_template("foo")
     assert f.uri in tl._collection
     f.filename = "nonexistent"
     self.assertRaises(exceptions.TemplateLookupException, tl.get_template,
                       "foo")
     assert f.uri not in tl._collection
예제 #30
0
    def test_module_imports_2(self):
        collection = lookup.TemplateLookup()

        collection.put_string('main.html', """
        <%namespace import="foo1, foo2" module="test.foo.test_ns"/>

        this is main.  ${foo1()}
        ${foo2("hi")}
""")

        assert flatten_result(collection.get_template('main.html').render()) == "this is main. this is foo1. this is foo2, x is hi"