From 2298601165f5b65f76c86f4542139965c2486e58 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 2 Mar 2015 11:52:15 +0100 Subject: [PATCH] feat(mako): can now use custom libraries in pycode Namespaces can exclusively be used during rendering, which is fine if you remind yourself of the newline rules. However, I also need some utiltiies that convert input data. These are now within their own libraries, which can be used from python blocks like the ordinary python functions they are. Quite neat. In future, most of the functionality will be in separate namespaces, the top-level will just assemble the main library file, usnig the provided %defs. That way, the main file is kept clean. --- .gitignore | 1 + Makefile | 10 +++++++--- etc/bin/mako-render | 2 +- src/mako/cargo.toml.mako | 3 +++ src/mako/deps.mako | 15 +++++++++------ src/mako/lib/util.mako | 9 +++++++++ src/mako/lib/util.py | 3 +++ 7 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 src/mako/lib/util.mako create mode 100644 src/mako/lib/util.py diff --git a/.gitignore b/.gitignore index 689fed0fd7..6fe38ff5b4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .pyenv +*.pyc generated/ target .api.deps diff --git a/Makefile b/Makefile index d836a2f89e..5ffec9d978 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ .PHONY: json-to-xml clean help api-deps +.SUFFIXES: include Makefile.helpers @@ -9,10 +10,13 @@ PIP := $(VENV_DIR)/bin/pip MAKO_RENDER := ./etc/bin/mako-render TPL := $(PYTHON) $(MAKO_RENDER) -API_DEPS_TPL = src/mako/deps.mako +MAKO_SRC = src/mako +API_DEPS_TPL = $(MAKO_SRC)/deps.mako API_DEPS = .api.deps API_SHARED_INFO = ./etc/api/shared.yaml API_JSON_FILES = $(shell find ./etc -type f -name '*-api.json') +MAKO_LIB_DIR = $(MAKO_SRC)/lib +MAKO_LIB_FILES = $(shell find $(MAKO_LIB_DIR) -type f -name '*.mako' -or -name '*.py') help: $(info using template engine: '$(TPL)') @@ -28,8 +32,8 @@ $(PYTHON): $(MAKO_RENDER): $(PYTHON) -$(API_DEPS): $(API_SHARED_INFO) $(API_DEPS_TPL) $(MAKO_RENDER) - $(TPL) -io $(API_DEPS_TPL) --data-files $(API_SHARED_INFO) > $@ +$(API_DEPS): $(API_SHARED_INFO) $(API_DEPS_TPL) $(MAKO_LIB_FILES) $(MAKO_RENDER) + PYTHONPATH=$(MAKO_LIB_DIR) $(TPL) --template-dir '.' -io $(API_DEPS_TPL) --data-files $(API_SHARED_INFO) > $@ api-deps: $(API_DEPS) diff --git a/etc/bin/mako-render b/etc/bin/mako-render index e4d62f1a59..2dc01bc1a5 100644 --- a/etc/bin/mako-render +++ b/etc/bin/mako-render @@ -288,7 +288,7 @@ def cmdline(argv=None): if input_file == '-': assert not seen_stdin, "STDIN (-) can only be named once" seen_stdin = True - lookup_dirs = options.template_dir or ["."] + lookup_dirs = options.template_dir or ['.'] lookup = TemplateLookup(lookup_dirs) try: template = Template(sys.stdin.read(), lookup=lookup) diff --git a/src/mako/cargo.toml.mako b/src/mako/cargo.toml.mako index e0c5cc80ef..5f5977864d 100644 --- a/src/mako/cargo.toml.mako +++ b/src/mako/cargo.toml.mako @@ -1,3 +1,6 @@ +# DO NOT EDIT ! +# This file was generated automatically by '${self.uri}' +# DO NOT EDIT ! [package] name = "${name}${version[1:]}" diff --git a/src/mako/deps.mako b/src/mako/deps.mako index 2324c81008..76e464361f 100644 --- a/src/mako/deps.mako +++ b/src/mako/deps.mako @@ -1,23 +1,26 @@ # DO NOT EDIT ! # This file was generated automatically by '${self.uri}' # DO NOT EDIT ! - +## ${util.to_api_version('v3')} +## here <%util:to_api_version v="v3"/> <%api_info=[]%>\ % for a in api.list: <% - gen_root = directories.output + '/' + a.name + a.version[1:] - api_name = a.name + a.version + import util + version = util.to_api_version(a.version) + gen_root = directories.output + '/' + a.name + version + api_name = a.name + version api_clean = api_name + '-clean' - # source, destination + # source, destination of individual output files sds = [(directories.mako_src + '/' + i.source + '.mako', gen_root + i.get('output_dir', '') + '/' + i.source) for i in api.templates] api_json = directories.api_base + '/' + a.name + '/' + a.version + '/' + a.name + '-api.json' api_json_inputs = api_json + " $(API_SHARED_INFO)" api_info.append((api_name, api_clean, gen_root)) %>\ -${gen_root}: ${' '.join(i[0] for i in sds)} ${api_json_inputs} +${gen_root}: ${' '.join(i[0] for i in sds)} ${api_json_inputs} $(MAKO_LIB_FILES) $(MAKO_RENDER) @mkdir -p $@ - $(TPL) --var OUTPUT_DIR=$@ -io ${' '.join("%s=%s" % (s, d) for s, d in sds)} --data-files ${api_json_inputs} + PYTHONPATH=$(MAKO_LIB_DIR) $(TPL) --template-dir '.' --var OUTPUT_DIR=$@ -io ${' '.join("%s=%s" % (s, d) for s, d in sds)} --data-files ${api_json_inputs} ${api_name}: ${gen_root} diff --git a/src/mako/lib/util.mako b/src/mako/lib/util.mako new file mode 100644 index 0000000000..be499f38c5 --- /dev/null +++ b/src/mako/lib/util.mako @@ -0,0 +1,9 @@ + + + +## This will only work within a substitution, not within python code +<%def name="to_api_version(v)">\ + <% assert len(v) >= 2 and v[0] == 'v'%>\ + ## convert it once to int, just to be sure it is an int +${v[1:]}\ + \ No newline at end of file diff --git a/src/mako/lib/util.py b/src/mako/lib/util.py new file mode 100644 index 0000000000..ac9d21348f --- /dev/null +++ b/src/mako/lib/util.py @@ -0,0 +1,3 @@ +def to_api_version(v): + assert len(v) >= 2 and v[0] == 'v' + return v[1:]