|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# --- BEGIN_HEADER --- |
| 5 | +# |
| 6 | +# base - shared base helper functions |
| 7 | +# Copyright (C) 2003-2024 The MiG Project by the Science HPC Center at UCPH |
| 8 | +# |
| 9 | +# This file is part of MiG. |
| 10 | +# |
| 11 | +# MiG is free software: you can redistribute it and/or modify |
| 12 | +# it under the terms of the GNU General Public License as published by |
| 13 | +# the Free Software Foundation; either version 2 of the License, or |
| 14 | +# (at your option) any later version. |
| 15 | +# |
| 16 | +# MiG is distributed in the hope that it will be useful, |
| 17 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +# GNU General Public License for more details. |
| 20 | +# |
| 21 | +# You should have received a copy of the GNU General Public License |
| 22 | +# along with this program; if not, write to the Free Software |
| 23 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 24 | +# |
| 25 | +# -- END_HEADER --- |
| 26 | +# |
| 27 | + |
| 28 | +from collections import ChainMap |
| 29 | +import errno |
| 30 | +from jinja2 import meta as jinja2_meta, select_autoescape, Environment, \ |
| 31 | + FileSystemLoader, FileSystemBytecodeCache |
| 32 | +import os |
| 33 | +import weakref |
| 34 | + |
| 35 | +from mig.shared.defaults import MIG_BASE |
| 36 | + |
| 37 | +TEMPLATES_DIR = os.path.abspath(os.path.dirname(__file__)) |
| 38 | +TEMPLATES_CACHE_DIR = os.path.join(TEMPLATES_DIR, '__jinja__') |
| 39 | + |
| 40 | +_all_template_dirs = [ |
| 41 | + os.path.join(TEMPLATES_DIR, 'pages'), |
| 42 | + os.path.join(TEMPLATES_DIR, 'partials') |
| 43 | +] |
| 44 | +_global_store = None |
| 45 | + |
| 46 | + |
| 47 | +def cache_dir(): |
| 48 | + return TEMPLATES_CACHE_DIR |
| 49 | + |
| 50 | + |
| 51 | +def template_dirs(): |
| 52 | + return _all_template_dirs |
| 53 | + |
| 54 | + |
| 55 | +class _FormatContext: |
| 56 | + def __init__(self, configuration): |
| 57 | + self.output_format = None |
| 58 | + self.configuration = configuration |
| 59 | + self.conf_map = ChainMap(configuration) |
| 60 | + self.script_map = {} |
| 61 | + self.style_map = {} |
| 62 | + |
| 63 | + def __getitem__(self, key): |
| 64 | + return self.__dict__[key] |
| 65 | + |
| 66 | + def __iter__(self): |
| 67 | + return iter(self.__dict__) |
| 68 | + |
| 69 | + def extend(self, **kwargs): |
| 70 | + return ChainMap(kwargs, self) |
| 71 | + |
| 72 | + |
| 73 | +class TemplateStore: |
| 74 | + def __init__(self, template_dirs, cache_dir=None, extra_globals=None): |
| 75 | + assert cache_dir is not None |
| 76 | + |
| 77 | + self._template_globals = extra_globals |
| 78 | + self._template_environment = Environment( |
| 79 | + loader=FileSystemLoader(template_dirs), |
| 80 | + bytecode_cache=FileSystemBytecodeCache(cache_dir, '%s'), |
| 81 | + autoescape=select_autoescape() |
| 82 | + ) |
| 83 | + |
| 84 | + @property |
| 85 | + def context(self): |
| 86 | + return self._template_globals |
| 87 | + |
| 88 | + def _get_template(self, template_fqname): |
| 89 | + return self._template_environment.get_template(template_fqname) |
| 90 | + |
| 91 | + def grab_template(self, template_name, template_group, output_format, template_globals=None, **kwargs): |
| 92 | + template_fqname = "%s_%s.%s.jinja" % ( |
| 93 | + template_group, template_name, output_format) |
| 94 | + return self._template_environment.get_template(template_fqname, globals=template_globals) |
| 95 | + |
| 96 | + def list_templates(self): |
| 97 | + return self._template_environment.list_templates() |
| 98 | + |
| 99 | + def extract_variables(self, template_fqname): |
| 100 | + template = self._template_environment.get_template(template_fqname) |
| 101 | + with open(template.filename) as f: |
| 102 | + template_source = f.read() |
| 103 | + ast = self._template_environment.parse(template_source) |
| 104 | + return jinja2_meta.find_undeclared_variables(ast) |
| 105 | + |
| 106 | + @staticmethod |
| 107 | + def populated(template_dirs, cache_dir=None, context=None): |
| 108 | + assert cache_dir is not None |
| 109 | + |
| 110 | + try: |
| 111 | + os.mkdir(cache_dir) |
| 112 | + except OSError as direxc: |
| 113 | + if direxc.errno != errno.EEXIST: # FileExistsError |
| 114 | + raise |
| 115 | + |
| 116 | + store = TemplateStore( |
| 117 | + template_dirs, cache_dir=cache_dir, extra_globals=context) |
| 118 | + |
| 119 | + for template_fqname in store.list_templates(): |
| 120 | + store._get_template(template_fqname) |
| 121 | + |
| 122 | + return store |
| 123 | + |
| 124 | + |
| 125 | +def init_global_templates(configuration): |
| 126 | + global _global_store |
| 127 | + |
| 128 | + if _global_store is not None: |
| 129 | + return _global_store |
| 130 | + |
| 131 | + _global_store = TemplateStore.populated( |
| 132 | + template_dirs(), |
| 133 | + cache_dir=cache_dir(), |
| 134 | + context=_FormatContext(configuration) |
| 135 | + ) |
| 136 | + |
| 137 | + return _global_store |
0 commit comments