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.
This commit is contained in:
Sebastian Thiel
2015-03-02 11:52:15 +01:00
parent be938255bd
commit 2298601165
7 changed files with 33 additions and 10 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,5 @@
.pyenv
*.pyc
generated/
target
.api.deps

View File

@@ -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)

View File

@@ -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)

View File

@@ -1,3 +1,6 @@
# DO NOT EDIT !
# This file was generated automatically by '${self.uri}'
# DO NOT EDIT !
[package]
name = "${name}${version[1:]}"

View File

@@ -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}

9
src/mako/lib/util.mako Normal file
View File

@@ -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:]}\
</%def>

3
src/mako/lib/util.py Normal file
View File

@@ -0,0 +1,3 @@
def to_api_version(v):
assert len(v) >= 2 and v[0] == 'v'
return v[1:]