blob: 8178c1350be55a57fd0263ab3c30515f499b32d8 [file] [log] [blame]
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +02001# osmo_gsm_tester: automated cellular network hardware tests
2# Proxy to templating engine to handle files
3#
4# Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH
5#
6# Author: Neels Hofmeyr <neels@hofmeyr.de>
7#
8# This program is free software: you can redistribute it and/or modify
Harald Welte27205342017-06-03 09:51:45 +02009# it under the terms of the GNU General Public License as
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020010# published by the Free Software Foundation, either version 3 of the
11# License, or (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte27205342017-06-03 09:51:45 +020016# GNU General Public License for more details.
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020017#
Harald Welte27205342017-06-03 09:51:45 +020018# You should have received a copy of the GNU General Public License
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020019# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
Holger Hans Peter Freyther91067232019-02-27 04:33:21 +000021import os
Pau Espin Pedrol6ed30122020-02-27 17:03:15 +010022from mako.lookup import TemplateLookup, Template
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020023
24from . import log
Neels Hofmeyr3531a192017-03-28 14:30:28 +020025from .util import dict2obj
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020026
27_lookup = None
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020028_logger = log.Origin(log.C_CNF, 'no templates dir set')
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020029
Pau Espin Pedrol4ddcdaf2020-06-04 17:23:23 +020030def default_templates_dir():
31 return os.path.join(os.path.dirname(os.path.dirname(__file__)), 'templates')
32
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020033def set_templates_dir(*templates_dirs):
Pau Espin Pedrol166dc102020-06-04 18:44:42 +020034 '''Set a lit of directories to look for templates. It must be called
35 everytime a template file is updated.'''
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020036 global _lookup
37 global _logger
38 if not templates_dirs:
Pau Espin Pedrol4ddcdaf2020-06-04 17:23:23 +020039 raise RuntimeError('templates dir list is empty!')
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020040 for d in templates_dirs:
41 if not os.path.isdir(d):
42 raise RuntimeError('templates dir is not a dir: %r'
43 % os.path.abspath(d))
44 _lookup = TemplateLookup(directories=templates_dirs)
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020045 _logger = log.Origin(log.C_CNF, 'Templates')
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020046
47def render(name, values):
48 '''feed values dict into template and return rendered result.
49 ".tmpl" is added to the name to look it up in the templates dir.'''
50 global _lookup
51 if _lookup is None:
Pau Espin Pedrol4ddcdaf2020-06-04 17:23:23 +020052 set_templates_dir(default_templates_dir())
Neels Hofmeyr3531a192017-03-28 14:30:28 +020053 tmpl_name = name + '.tmpl'
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020054 log.ctx(tmpl_name)
55 template = _lookup.get_template(tmpl_name)
56 _logger.dbg('rendering', tmpl_name)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020057
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020058 return template.render(**dict2obj(values))
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020059
Pau Espin Pedrol6ed30122020-02-27 17:03:15 +010060def render_strbuf_inline(strbuf, values):
61 '''Receive a string containing template syntax, and generate output using
62 passed values.'''
63 mytemplate = Template(strbuf)
64 return mytemplate.render(**dict2obj(values))
65
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020066# vim: expandtab tabstop=4 shiftwidth=4