-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce support for jinja2-based templates.
- Loading branch information
Showing
12 changed files
with
309 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,139 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# --- BEGIN_HEADER --- | ||
# | ||
# base - shared base helper functions | ||
# Copyright (C) 2003-2024 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 --- | ||
# | ||
|
||
from collections import ChainMap | ||
from jinja2 import meta as jinja2_meta, select_autoescape, Environment, \ | ||
FileSystemLoader, FileSystemBytecodeCache | ||
import os | ||
import weakref | ||
|
||
from mig.shared.defaults import MIG_BASE | ||
|
||
TEMPLATES_DIR = os.path.abspath(os.path.dirname(__file__)) | ||
TEMPLATES_CACHE_DIR = os.path.join(TEMPLATES_DIR, '__jinja__') | ||
|
||
_all_template_dirs = [ | ||
os.path.join(TEMPLATES_DIR, 'pages'), | ||
os.path.join(TEMPLATES_DIR, 'partials') | ||
] | ||
_global_store = None | ||
|
||
def cache_dir(): | ||
return TEMPLATES_CACHE_DIR | ||
|
||
|
||
def template_dirs(): | ||
return _all_template_dirs | ||
|
||
|
||
class _FormatContext: | ||
def __init__(self, configuration): | ||
self.output_format = None | ||
self.configuration = configuration | ||
self.conf_map = ChainMap(configuration) | ||
self.script_map = {} | ||
self.style_map = {} | ||
|
||
def __getitem__(self, key): | ||
return self.__dict__[key] | ||
|
||
def __iter__(self): | ||
return iter(self.__dict__) | ||
|
||
def extend(self, **kwargs): | ||
return ChainMap(kwargs, self) | ||
|
||
|
||
class TemplateStore: | ||
def __init__(self, template_dirs, cache_dir=None, extra_globals=None): | ||
assert cache_dir is not None | ||
|
||
self._template_globals = extra_globals | ||
self._template_environment = Environment( | ||
loader=FileSystemLoader(template_dirs), | ||
bytecode_cache=FileSystemBytecodeCache(cache_dir, '%s'), | ||
autoescape=select_autoescape() | ||
) | ||
|
||
@property | ||
def context(self): | ||
return self._template_globals | ||
|
||
def _get_template(self, template_fqname): | ||
return self._template_environment.get_template(template_fqname) | ||
|
||
def _grab_template(template_name, template_group, output_format): | ||
template_fqname = "%s_%s.%s.jinja" % (template_group, template_name, output_format) | ||
return _template_environment.get_template(template_fqname) | ||
|
||
|
||
def grab_template(self, template_name, template_group, output_format, template_globals=None, **kwargs): | ||
template_fqname = "%s_%s.%s.jinja" % (template_group, template_name, output_format) | ||
return self._template_environment.get_template(template_fqname, globals=template_globals) | ||
|
||
|
||
def list_templates(self): | ||
return self._template_environment.list_templates() | ||
|
||
|
||
def extract_variables(self, template_fqname): | ||
template = self._template_environment.get_template(template_fqname) | ||
with open(template.filename) as f: | ||
template_source = f.read() | ||
ast = self._template_environment.parse(template_source) | ||
return jinja2_meta.find_undeclared_variables(ast) | ||
|
||
@staticmethod | ||
def populated(template_dirs, cache_dir=None, context=None): | ||
assert cache_dir is not None | ||
|
||
try: | ||
os.mkdir(cache_dir) | ||
except FileExistsError: | ||
pass | ||
|
||
store = TemplateStore(template_dirs, cache_dir=cache_dir, extra_globals=context) | ||
|
||
for template_fqname in store.list_templates(): | ||
store._get_template(template_fqname) | ||
|
||
return store | ||
|
||
|
||
def init_global_templates(configuration): | ||
global _global_store | ||
|
||
if _global_store is not None: | ||
return _global_store | ||
|
||
_global_store = TemplateStore.populated( | ||
template_dirs(), | ||
cache_dir=cache_dir(), | ||
context=_FormatContext(configuration) | ||
) | ||
|
||
return _global_store |
This file contains 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,74 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# --- BEGIN_HEADER --- | ||
# | ||
# base - shared base helper functions | ||
# Copyright (C) 2003-2024 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 --- | ||
# | ||
|
||
from types import SimpleNamespace | ||
import os | ||
import sys | ||
|
||
from mig.shared.templates import cache_dir, \ | ||
_get_template, _grab_template_vars, _template_environment | ||
|
||
|
||
def warn(message): | ||
print(message, file=sys.stderr, flush=True) | ||
|
||
|
||
def main(args, _print=print): | ||
command = args.command | ||
|
||
if command == 'show': | ||
print(_template_environment.list_templates()) | ||
elif command == 'prime': | ||
try: | ||
os.mkdir(cache_dir()) | ||
except FileExistsError: | ||
pass | ||
|
||
for template_name in _list_templates(): | ||
_get_template(template_name) | ||
elif command == 'vars': | ||
for template_ref in _template_environment.list_templates(): | ||
_print("<%s>" % (template_ref,)) | ||
for var in _grab_template_vars(template_ref): | ||
_print(" %s" % (var,)) | ||
else: | ||
raise RuntimeError("unknown command: %s" % (command,)) | ||
|
||
|
||
if __name__ == '__main__': | ||
if len(sys.argv) == 2: | ||
command = sys.argv[1] | ||
else: | ||
command = 'show' | ||
args = SimpleNamespace(command=command) | ||
|
||
try: | ||
main(args) | ||
sys.exit(0) | ||
except Exception as exc: | ||
warn(str(exc)) | ||
sys.exit(1) |
This file contains 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 @@ | ||
<q>{{ other }}</q> |
This file contains 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 @@ | ||
<aside>{{ content }}</aside> |
This file contains 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 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 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 @@ | ||
<aside>here!!</aside> |
This file contains 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,32 @@ | ||
from __future__ import print_function | ||
|
||
from tests.support import MigTestCase, testmain | ||
|
||
from mig.shared.templates import TemplateStore, cache_dir, template_dirs | ||
|
||
|
||
class TestMigSharedTemplates(MigTestCase): | ||
def _provide_configuration(self): | ||
return 'testconfig' | ||
|
||
def test_the_creation_of_a_template_store(self): | ||
store = TemplateStore.populated(template_dirs(), cache_dir=cache_dir()) | ||
self.assertIsInstance(store, TemplateStore) | ||
|
||
def test_a_listing_all_templates(self): | ||
store = TemplateStore.populated(template_dirs(), cache_dir=cache_dir()) | ||
self.assertEqual(len(store.list_templates()), 2) | ||
|
||
def test_grab_template(self): | ||
store = TemplateStore.populated(template_dirs(), cache_dir=cache_dir()) | ||
template = store.grab_template('other', 'partial', 'html') | ||
pass | ||
|
||
def test_variables_for_remplate_ref(self): | ||
store = TemplateStore.populated(template_dirs(), cache_dir=cache_dir()) | ||
template_vars = store.extract_variables('partial_something.html.jinja') | ||
self.assertEqual(template_vars, set(['content'])) | ||
|
||
|
||
if __name__ == '__main__': | ||
testmain() |
This file contains 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