2 # Copyright (c) 2014-2016 Intel Corporation
4 # Permission is hereby granted, free of charge, to any person obtaining a copy
5 # of this software and associated documentation files (the "Software"), to deal
6 # in the Software without restriction, including without limitation the rights
7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 # copies of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 """Provides a few simple helpers for working with mako templates."""
24 from __future__
import absolute_import
32 from mako
.template
import Template
33 from mako
.lookup
import TemplateLookup
36 # Based on a similar setup in framework/summary
37 if platform
.system() == "Windows":
38 MAKO_TEMP_DIR
= tempfile
.gettempdir()
40 MAKO_TEMP_DIR
= os
.path
.join(tempfile
.gettempdir(),
43 'python-{}'.format(sys
.version
.split()[0]),
44 'mako-{}'.format(mako
.__version
__),
47 TEMPLATE_DIR
= os
.path
.abspath(os
.path
.dirname(__file__
))
49 # Future functions to be used in all templates
50 _FUTURES
= ['division']
53 def template_file(generator
, template
):
54 """Get a single template file from the templates directory.
56 If the generator uses more than one template use template_dir instead.
59 generator -- the name of the generator, which corresponds to the subdir of
61 template -- the name of the template to get. If getting a single template
62 it's probably template.*.mako, where * is shader_test,
63 glsl_parser_test, vert, frag, geom, etc.
66 assert os
.path
.sep
not in generator
, \
67 'generator should be a filename, not a path'
68 assert not generator
.endswith('.py'), \
69 'generator should not have a file extension'
71 return Template(filename
=os
.path
.join(TEMPLATE_DIR
, generator
, template
),
72 module_directory
=os
.path
.join(MAKO_TEMP_DIR
, generator
),
73 future_imports
=_FUTURES
,
74 output_encoding
='utf-8')
77 def template_dir(generator
):
78 """Get a TemplateLookup object for a generator.
80 This is useful if a generator uses more than one template.
83 generator -- the name of the generator, which corresponds to the subdir of
87 assert os
.path
.sep
not in generator
, \
88 'generator should be a filename, not a path'
89 assert not generator
.endswith('.py'), \
90 'generator should not have a file extension'
92 return TemplateLookup(
93 directories
=[os
.path
.join(TEMPLATE_DIR
, generator
)],
94 module_directory
=os
.path
.join(MAKO_TEMP_DIR
, generator
),
95 future_imports
=_FUTURES
,
96 output_encoding
='utf-8')