1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
|
from mako import exceptions
from mako import lookup
from mako.template import Template
from mako.testing.assertions import assert_raises
from mako.testing.assertions import assert_raises_message_with_given_cause
from mako.testing.assertions import eq_
from mako.testing.fixtures import TemplateTest
from mako.testing.helpers import flatten_result
from mako.testing.helpers import result_lines
class NamespaceTest(TemplateTest):
def test_inline_crossreference(self):
self._do_memory_test(
"""
<%namespace name="x">
<%def name="a()">
this is x a
</%def>
<%def name="b()">
this is x b, and heres ${a()}
</%def>
</%namespace>
${x.a()}
${x.b()}
""",
"this is x a this is x b, and heres this is x a",
filters=flatten_result,
)
def test_inline_assignment(self):
self._do_memory_test(
"""
<%namespace name="x">
<%def name="a()">
<%
x = 5
%>
this is x: ${x}
</%def>
</%namespace>
${x.a()}
""",
"this is x: 5",
filters=flatten_result,
)
def test_inline_arguments(self):
self._do_memory_test(
"""
<%namespace name="x">
<%def name="a(x, y)">
<%
result = x * y
%>
result: ${result}
</%def>
</%namespace>
${x.a(5, 10)}
""",
"result: 50",
filters=flatten_result,
)
def test_inline_not_duped(self):
self._do_memory_test(
"""
<%namespace name="x">
<%def name="a()">
foo
</%def>
</%namespace>
<%
assert x.a is not UNDEFINED, "namespace x.a wasn't defined"
assert a is UNDEFINED, "name 'a' is in the body locals"
%>
""",
"",
filters=flatten_result,
)
def test_dynamic(self):
collection = lookup.TemplateLookup()
collection.put_string(
"a",
"""
<%namespace name="b" file="${context['b_def']}"/>
a. b: ${b.body()}
""",
)
collection.put_string(
"b",
"""
b.
""",
)
eq_(
flatten_result(collection.get_template("a").render(b_def="b")),
"a. b: b.",
)
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"
)
def test_module(self):
collection = lookup.TemplateLookup()
collection.put_string(
"main.html",
"""
<%namespace name="comp" module="test.sample_module_namespace"/>
this is main. ${comp.foo1()}
${comp.foo2("hi")}
""",
)
assert (
flatten_result(collection.get_template("main.html").render())
== "this is main. this is foo1. this is foo2, x is hi"
)
def test_module_2(self):
collection = lookup.TemplateLookup()
collection.put_string(
"main.html",
"""
<%namespace name="comp" module="test.foo.test_ns"/>
this is main. ${comp.foo1()}
${comp.foo2("hi")}
""",
)
assert (
flatten_result(collection.get_template("main.html").render())
== "this is main. this is foo1. this is foo2, x is hi"
)
def test_module_imports(self):
collection = lookup.TemplateLookup()
collection.put_string(
"main.html",
"""
<%namespace import="*" 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"
)
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"
)
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"
)
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"
)
def test_getattr(self):
collection = lookup.TemplateLookup()
collection.put_string(
"main.html",
"""
<%namespace name="foo" file="ns.html"/>
<%
if hasattr(foo, 'lala'):
foo.lala()
if not hasattr(foo, 'hoho'):
context.write('foo has no hoho.')
%>
""",
)
collection.put_string(
"ns.html",
"""
<%def name="lala()">this is lala.</%def>
""",
)
assert (
flatten_result(collection.get_template("main.html").render())
== "this is lala.foo has no hoho."
)
def test_in_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>
""",
)
assert result_lines(collection.get_template("main.html").render()) == [
"this is main.",
"this is bar, foo is",
"this is ns.html->bar",
]
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"]
def test_dont_pollute_self(self):
# test that get_namespace() doesn't modify the original context
# incompatibly
collection = lookup.TemplateLookup()
collection.put_string(
"base.html",
"""
<%def name="foo()">
<%
foo = local.get_namespace("foo.html")
%>
</%def>
name: ${self.name}
name via bar: ${bar()}
${next.body()}
name: ${self.name}
name via bar: ${bar()}
<%def name="bar()">
${self.name}
</%def>
""",
)
collection.put_string(
"page.html",
"""
<%inherit file="base.html"/>
${self.foo()}
hello world
""",
)
collection.put_string("foo.html", """<%inherit file="base.html"/>""")
assert result_lines(collection.get_template("page.html").render()) == [
"name: self:page.html",
"name via bar:",
"self:page.html",
"hello world",
"name: self:page.html",
"name via bar:",
"self:page.html",
]
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"]
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"]
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",
]
def test_attr_raise(self):
l = lookup.TemplateLookup()
l.put_string(
"foo.html",
"""
<%def name="foo()">
</%def>
""",
)
l.put_string(
"bar.html",
"""
<%namespace name="foo" file="foo.html"/>
${foo.notfoo()}
""",
)
assert_raises(AttributeError, l.get_template("bar.html").render)
def test_custom_tag_1(self):
template = Template(
"""
<%def name="foo(x, y)">
foo: ${x} ${y}
</%def>
<%self:foo x="5" y="${7+8}"/>
"""
)
assert result_lines(template.render()) == ["foo: 5 15"]
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"]
def test_custom_tag_3(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
<%self.foo:bar>
call body
</%self.foo:bar>
""",
)
assert result_lines(
collection.get_template("index.html").render()
) == [
"this is index",
"this is ns.html->bar",
"caller body:",
"call body",
]
def test_custom_tag_case_sensitive(self):
t = Template(
"""
<%def name="renderPanel()">
panel ${caller.body()}
</%def>
<%def name="renderTablePanel()">
<%self:renderPanel>
hi
</%self:renderPanel>
</%def>
<%self:renderTablePanel/>
"""
)
assert result_lines(t.render()) == ["panel", "hi"]
def test_expr_grouping(self):
"""test that parenthesis are placed around string-embedded
expressions."""
template = Template(
"""
<%def name="bar(x, y)">
${x}
${y}
</%def>
<%self:bar x=" ${foo} " y="x${g and '1' or '2'}y"/>
""",
input_encoding="utf-8",
)
# the concat has to come out as "x + (g and '1' or '2') + y"
assert result_lines(template.render(foo="this is foo", g=False)) == [
"this is foo",
"x2y",
]
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",
]
def test_ccall_2(self):
collection = lookup.TemplateLookup()
collection.put_string(
"base.html",
"""
<%namespace name="foo" file="ns1.html" inheritable="True"/>
${next.body()}
""",
)
collection.put_string(
"ns1.html",
"""
<%namespace name="foo2" file="ns2.html"/>
<%def name="bar()">
<%call expr="foo2.ns2_bar()">
this is ns1.html->bar
caller body: ${caller.body()}
</%call>
</%def>
""",
)
collection.put_string(
"ns2.html",
"""
<%def name="ns2_bar()">
this is ns2.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 ns2.html->bar",
"caller body:",
"this is ns1.html->bar",
"caller body:",
"call body",
]
def test_import(self):
collection = lookup.TemplateLookup()
collection.put_string(
"functions.html",
"""
<%def name="foo()">
this is foo
</%def>
<%def name="bar()">
this is bar
</%def>
<%def name="lala()">
this is lala
</%def>
""",
)
collection.put_string(
"func2.html",
"""
<%def name="a()">
this is a
</%def>
<%def name="b()">
this is b
</%def>
""",
)
collection.put_string(
"index.html",
"""
<%namespace file="functions.html" import="*"/>
<%namespace file="func2.html" import="a, b"/>
${foo()}
${bar()}
${lala()}
${a()}
${b()}
${x}
""",
)
assert result_lines(
collection.get_template("index.html").render(
bar="this is bar", x="this is x"
)
) == [
"this is foo",
"this is bar",
"this is lala",
"this is a",
"this is b",
"this is x",
]
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"
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"]
def test_import_local(self):
t = Template(
"""
<%namespace import="*">
<%def name="foo()">
this is foo
</%def>
</%namespace>
${foo()}
"""
)
assert flatten_result(t.render()) == "this is foo"
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",
]
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,
)
|