Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 1 | # 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 Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 9 | # it under the terms of the GNU General Public License as |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 10 | # 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 Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 16 | # GNU General Public License for more details. |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 17 | # |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 18 | # You should have received a copy of the GNU General Public License |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 19 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | |
Holger Hans Peter Freyther | 9106723 | 2019-02-27 04:33:21 +0000 | [diff] [blame] | 21 | import os |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 22 | from mako.lookup import TemplateLookup |
| 23 | |
| 24 | from . import log |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 25 | from .util import dict2obj |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 26 | |
| 27 | _lookup = None |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 28 | _logger = log.Origin(log.C_CNF, 'no templates dir set') |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 29 | |
| 30 | def set_templates_dir(*templates_dirs): |
| 31 | global _lookup |
| 32 | global _logger |
| 33 | if not templates_dirs: |
| 34 | # default templates dir is relative to this source file |
| 35 | templates_dirs = [os.path.join(os.path.dirname(__file__), 'templates')] |
| 36 | for d in templates_dirs: |
| 37 | if not os.path.isdir(d): |
| 38 | raise RuntimeError('templates dir is not a dir: %r' |
| 39 | % os.path.abspath(d)) |
| 40 | _lookup = TemplateLookup(directories=templates_dirs) |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 41 | _logger = log.Origin(log.C_CNF, 'Templates') |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 42 | |
| 43 | def render(name, values): |
| 44 | '''feed values dict into template and return rendered result. |
| 45 | ".tmpl" is added to the name to look it up in the templates dir.''' |
| 46 | global _lookup |
| 47 | if _lookup is None: |
| 48 | set_templates_dir() |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 49 | tmpl_name = name + '.tmpl' |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 50 | log.ctx(tmpl_name) |
| 51 | template = _lookup.get_template(tmpl_name) |
| 52 | _logger.dbg('rendering', tmpl_name) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 53 | |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 54 | return template.render(**dict2obj(values)) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 55 | |
| 56 | # vim: expandtab tabstop=4 shiftwidth=4 |