commit 197f0a626dc333a4956a541ce556abb87bda8b46
parent 456a61874e68847d1a7099831fa6247e6cbdc276
Author: Florian Dold <florian.dold@gmail.com>
Date: Tue, 14 Feb 2017 21:07:41 +0100
make template compilation a LOT faster
By not doing template parsing and starting the python interpreter every
time, templating only takes about a second on my machine now.
Diffstat:
3 files changed, 32 insertions(+), 46 deletions(-)
diff --git a/Makefile b/Makefile
@@ -33,4 +33,4 @@ locale: locale-update locale-compile
# Run the jinga2 templating engine to expand templates to HTML
# incorporating translations.
template: locale-compile
- ./template.sh
+ ./template.py
diff --git a/template.py b/template.py
@@ -8,45 +8,48 @@
# Note that the gettext files need to be prepared first. This script
# is thus to be invoked via the Makefile.
import os
+import os.path
import sys
import re
import gettext
import jinja2
+import glob
+import codecs
-if len(sys.argv) < 3:
- sys.exit("Usage: " + __file__ + " <template-file> <locale> <output-file>")
+env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
+ extensions=["jinja2.ext.i18n"],
+ autoescape=False)
-in_file = sys.argv[1]
-locale = sys.argv[2]
-name, ext = re.match(r"(.*)\.([^.]+)$", in_file.rstrip(".j2")).groups()
+for in_file in glob.glob("*.j2"):
+ name, ext = re.match(r"(.*)\.([^.]+)$", in_file.rstrip(".j2")).groups()
+ tmpl = env.get_template(in_file)
-tr = gettext.translation("messages",
- localedir="locale",
- languages=[locale])
+ def self_localized(other_locale):
+ """
+ Return URL for the current page in another locale.
+ """
+ return "../" + other_locale + "/" + in_file.rstrip(".j2")
-env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
- extensions=["jinja2.ext.i18n"],
- autoescape=False)
-env.install_gettext_translations(tr, newstyle=True)
+ def url_localized(filename):
+ return "../" + locale + "/" + filename
-tmpl = env.get_template(in_file)
+ def url(x):
+ # TODO: look at the app root environment variable
+ # TODO: check if file exists
+ return "../" + x
-def self_localized(other_locale):
- """
- Return URL for the current page in another locale.
- """
- return "../" + other_locale + "/" + in_file.rstrip(".j2")
+ for l in glob.glob("locale/*/"):
+ locale = os.path.basename(l[:-1])
-def url_localized(filename):
- return "../" + locale + "/" + filename
+ tr = gettext.translation("messages",
+ localedir="locale",
+ languages=[locale])
-def url(x):
- # TODO: look at the app root environment variable
- # TODO: check if file exists
- return "../" + x
+ env.install_gettext_translations(tr, newstyle=True)
-import codecs
-f = codecs.open("./" + locale + "/" + in_file.rstrip(".j2"), "w", "utf-8")
-f.write(tmpl.render(lang=locale, url=url, self_localized=self_localized, url_localized=url_localized))
-f.close()
+
+ content = tmpl.render(lang=locale, url=url, self_localized=self_localized, url_localized=url_localized)
+ out_name = "./" + locale + "/" + in_file.rstrip(".j2")
+ with codecs.open(out_name, "w", "utf-8") as f:
+ f.write(content)
diff --git a/template.sh b/template.sh
@@ -1,17 +0,0 @@
-#!/bin/sh
-# This file is in the public domain.
-#
-# Wrapper around 'template.py', running it on all
-# of our jinja2 input files for all languages for which
-# we have translations.
-#
-# Note that the gettext files need to be prepared first. This script
-# is thus to be invoked via the Makefile.
-for f in $(git ls-files *.j2); do
- for ld in locale/*/; do
- l=$(basename $ld)
- mkdir -p $(basename $l)
- echo "$f: $l"
- python template.py $f $l
- done
-done