-
Notifications
You must be signed in to change notification settings - Fork 5
Introduce support for jinja2-based templates. #210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
albu-diku
wants to merge
13
commits into
next
Choose a base branch
from
addition/object_type_template
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0daf967
Lay groundwork for splitting runtime and static configuration data.
albu-diku 955aef2
Introduce support for jinja2-based templates.
albu-diku bd6950f
pull in the Makefile priming invocation for clarity
albu-diku df0c8a2
pull in the changes to config generation
albu-diku 9715058
header name and date fixes
albu-diku e51ac74
expand unknown template error with what was missing
albu-diku cab96f0
spacing
albu-diku d9f697b
fixup
albu-diku 5753d80
fixup
albu-diku 8931154
fixup
albu-diku 9acc77c
fixup
albu-diku b04dbe9
fixup
albu-diku b7d579f
validate template output objects to enforce the presence of template_…
albu-diku File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| # Byte-compiled / optimized / DLL files | ||
| __jinja__/ | ||
| __pycache__/ | ||
| *.py[cod] | ||
| *$py.class | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,280 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # | ||
| # --- BEGIN_HEADER --- | ||
| # | ||
| # templates/__init__ - main logic for template support | ||
| # Copyright (C) 2003-2026 The MiG Project by the Science HPC Center at UCPH | ||
| # | ||
| # This file is part of MiG. | ||
| # | ||
| # MiG is free software: you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License as published by | ||
| # the Free Software Foundation; either version 2 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # MiG is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with this program; if not, write to the Free Software | ||
| # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, | ||
| # USA. | ||
| # | ||
| # -- END_HEADER --- | ||
| # | ||
|
|
||
| """ | ||
| Template support library code. | ||
| """ | ||
|
|
||
| import importlib | ||
| import os | ||
| from operator import itemgetter | ||
|
|
||
| from jinja2 import ( | ||
| Environment, | ||
| FileSystemBytecodeCache, | ||
| PackageLoader, | ||
| Template, | ||
| ) | ||
| from jinja2 import meta as jinja2_meta | ||
| from jinja2 import ( | ||
| select_autoescape, | ||
| ) | ||
|
|
||
|
|
||
| def _expand_base_packages(base_packages): | ||
| template_packages = [] | ||
| for package_name in base_packages: | ||
| try: | ||
| package = importlib.import_module(package_name) | ||
| except (ImportError, ModuleNotFoundError): | ||
| raise UnknownTemplateError(package_name) | ||
| template_packages.extend(package.TEMPLATE_PACKAGES) | ||
| return template_packages | ||
|
|
||
|
|
||
| def _strip_template_ext(template_name_with_ext): | ||
| return os.path.splitext(os.path.splitext(template_name_with_ext)[0])[0] | ||
|
|
||
|
|
||
| class _NoopContext: | ||
| """ | ||
| Adapter class to allow templates to be directly rendered. | ||
|
|
||
| Note that this is in contrast to further work making use of the | ||
| same provisions that allows the selection of translations. | ||
| """ | ||
|
|
||
| def __init__(self, *args): | ||
| self._tmpl = None | ||
| self._tmpl_args = None | ||
|
|
||
| def extend(self, template, template_args): | ||
| self._tmpl = template | ||
| self._tmpl_args = template_args | ||
| return self | ||
|
|
||
| def render(self): | ||
| return self._tmpl.render(**self._tmpl_args) | ||
|
|
||
|
|
||
| class TemplateStore: | ||
| """ | ||
| An abstraction for interacting with an enable series of template packages. | ||
| """ | ||
|
|
||
| def __init__(self, packages, cache_dir=None, extra_globals=None): | ||
| assert cache_dir is not None | ||
|
|
||
| self._packages = packages | ||
| self._cache_dir = cache_dir | ||
| self._template_globals = extra_globals | ||
| self._template_env_by_package = {} | ||
|
|
||
| @property | ||
| def cache_dir(self): | ||
| return self._cache_dir | ||
|
|
||
| @property | ||
| def context(self): | ||
| return self._template_globals | ||
|
|
||
| def _env_for_package(self, package_name): | ||
| """ | ||
| Direct access to a jinja2 Environment for a package exposing templates. | ||
| """ | ||
|
|
||
| if package_name not in self._packages: | ||
| raise UnknownTemplateError(package_name) | ||
|
|
||
| if package_name in self._template_env_by_package: | ||
| return self._template_env_by_package[package_name] | ||
|
|
||
| package_cache_dir = os.path.join(self.cache_dir, package_name) | ||
| template_env = Environment( | ||
| loader=PackageLoader(package_name), | ||
| bytecode_cache=FileSystemBytecodeCache(package_cache_dir, "%s"), | ||
| autoescape=select_autoescape(), | ||
| ) | ||
| self._template_env_by_package[package_name] = template_env | ||
| return template_env | ||
|
|
||
| def grab_template( | ||
| self, | ||
| template_name, | ||
| template_group, | ||
| output_format, | ||
| template_globals=None, | ||
| **kwargs | ||
| ): | ||
| """ | ||
| Directly access an enabled template. | ||
| """ | ||
|
|
||
| template_env = self._env_for_package(template_group) | ||
| template_fqname = "%s.%s.jinja" % (template_name, output_format) | ||
| try: | ||
| return template_env.get_template( | ||
| template_fqname, globals=template_globals | ||
| ) | ||
| except FileNotFoundError: | ||
| raise UnknownTemplateError(template_group, template_name) | ||
|
|
||
| def list_templates(self): | ||
| """ | ||
| Return a list of templates for all enabled packages. | ||
| """ | ||
|
|
||
| template_and_group_pairs = [] | ||
| for template_group in self._packages: | ||
| template_env = self._env_for_package(template_group) | ||
| pairs = ( | ||
| (_strip_template_ext(template), template_group) | ||
| for template in template_env.list_templates() | ||
| ) | ||
| template_and_group_pairs.extend(pairs) | ||
| template_and_group_pairs.sort(key=itemgetter(1, 0)) | ||
| return template_and_group_pairs | ||
|
|
||
| def list_templates_groups(self): | ||
| """ | ||
| Return the set of enabled packages that expose templates. | ||
| """ | ||
|
|
||
| nonunique_template_groups = ( | ||
| template_group for _, template_group in self.list_templates() | ||
| ) | ||
| return set(nonunique_template_groups) | ||
|
|
||
| def prime_templates(self): | ||
| """ | ||
| Precompile all templates across the enabled packages. | ||
| """ | ||
|
|
||
| os.makedirs(self.cache_dir, exist_ok=True) | ||
|
|
||
| for template_group in self.list_templates_groups(): | ||
| template_group_cache_dir = os.path.join( | ||
| self.cache_dir, template_group | ||
| ) | ||
| os.makedirs(template_group_cache_dir, exist_ok=True) | ||
|
|
||
| primed_count = 0 | ||
|
|
||
| for template_name, template_group in self.list_templates(): | ||
| primed_count += 1 | ||
| self.grab_template(template_name, template_group, "html") | ||
|
|
||
| return primed_count | ||
|
|
||
| def extract_variables( | ||
| self, | ||
| template_or_name, | ||
| template_group, | ||
| output_format=None, | ||
| template_globals=None, | ||
| ): | ||
| """ | ||
| Return the expected variables for a given template. | ||
| """ | ||
|
|
||
| template_env = self._env_for_package(template_group) | ||
| if isinstance(template_or_name, Template): | ||
| raise NotImplementedError() | ||
| else: | ||
| template = self.grab_template( | ||
| template_or_name, | ||
| template_group, | ||
| output_format, | ||
| globals=template_globals, | ||
| ) | ||
| with open(template.filename) as f: | ||
| template_source = f.read() | ||
| ast = template_env.parse(template_source) | ||
| return jinja2_meta.find_undeclared_variables(ast) | ||
|
|
||
| @staticmethod | ||
| def from_configuration(configuration): | ||
| """ | ||
| Create a TemplateStore instance for a specified configuration. | ||
| """ | ||
|
|
||
| template_division = configuration.division(section_name="TEMPLATES") | ||
|
|
||
| return TemplateStore.from_names( | ||
| template_division.base_packages, | ||
| cache_dir=template_division.cache_dir, | ||
| context=_NoopContext(configuration), | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def from_names(template_packages, *, cache_dir=None, context=None): | ||
| """ | ||
| Create a template store from a list of package names. | ||
| """ | ||
|
|
||
| assert cache_dir is not None | ||
| if context is None: | ||
| context = _NoopContext() | ||
|
|
||
| packages = _expand_base_packages(template_packages) | ||
|
|
||
| return TemplateStore( | ||
| packages, | ||
| cache_dir=cache_dir, | ||
| extra_globals=context, | ||
| ) | ||
|
|
||
|
|
||
| def init_global_templates(runtime_configuration): | ||
| """ | ||
| Make a TemplateStore available within the active request context. | ||
| """ | ||
|
|
||
| store = runtime_configuration.context_get("templates") | ||
| if store: | ||
| return store | ||
| store = TemplateStore.from_configuration(runtime_configuration) | ||
| runtime_configuration.context_set("templates", store) | ||
| return store | ||
|
|
||
|
|
||
| def render_html_template( | ||
| runtime_configuration, template_name, template_group, template_args | ||
| ): | ||
| """ | ||
| Render a template available within the active request context. | ||
| """ | ||
|
|
||
| store = init_global_templates(runtime_configuration) | ||
| template = store.grab_template(template_name, template_group, "html") | ||
| bound = store.context.extend(template, template_args) | ||
| return bound.render() | ||
|
|
||
|
|
||
| class UnknownTemplateError(KeyError): | ||
| def __init__(self, template_group, template_name="*"): | ||
| super().__init__("%s.%s" % (template_group, template_name)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| #!/usr/bin/python | ||
| # -*- coding: utf-8 -*- | ||
| # | ||
| # --- BEGIN_HEADER --- | ||
| # | ||
| # templates/__main__ - templates CLI | ||
| # Copyright (C) 2003-2026 The MiG Project by the Science HPC Center at UCPH | ||
| # | ||
| # This file is part of MiG. | ||
| # | ||
| # MiG is free software: you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License as published by | ||
| # the Free Software Foundation; either version 2 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # MiG is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with this program; if not, write to the Free Software | ||
| # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, | ||
| # USA. | ||
| # | ||
| # -- END_HEADER --- | ||
| # | ||
|
|
||
| """ | ||
| Template support CLI code. | ||
| """ | ||
|
|
||
| import sys | ||
|
|
||
| from mig.lib.templates import TemplateStore | ||
| from mig.shared.conf import get_configuration_object | ||
|
|
||
|
|
||
| def warn(message): | ||
| print(message, file=sys.stderr, flush=True) | ||
|
|
||
|
|
||
| def main(args, _print=print): | ||
| configuration = get_configuration_object( | ||
| config_file=args.config_file, skip_log=True, disable_auth_log=True | ||
| ) | ||
| template_store = TemplateStore.from_configuration(configuration) | ||
|
|
||
| command = args.command | ||
| if command == "cache": | ||
| templates_division = configuration.division(section_name="TEMPLATES") | ||
| _print(templates_division.cache_dir) | ||
| elif command == "show": | ||
| _print(template_store.list_templates()) | ||
| elif command == "prime": | ||
| primed_count = template_store.prime_templates() | ||
| if primed_count == 0: | ||
| _print("No templates were specified.") | ||
| elif command == "vars": | ||
| for template_name, template_group in template_store.list_templates(): | ||
| _print("<%s.%s>" % (template_group, template_name)) | ||
| for var in template_store.extract_variables( | ||
| template_name, template_group, "html" | ||
| ): | ||
| _print(" {{%s}}" % (var,)) | ||
| _print("</%s.%s>" % (template_group, template_name)) | ||
| else: | ||
| raise RuntimeError("unknown command: %s" % (command,)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("-c", dest="config_file", required=True) | ||
| parser.add_argument("command") | ||
| args = parser.parse_args() | ||
|
|
||
| try: | ||
| main(args) | ||
| sys.exit(0) | ||
| except Exception as exc: | ||
| warn(str(exc)) | ||
| sys.exit(1) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should properly be emphasized in the documentation or elsewhere that it is required that any
appsthat provide templates are required to use this format. Might even turn into [TEMPLATES] configuration option in the futureThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree on the documentation side, but I don''t quite follow the mention of the templates section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you be able to clarify your comment?