Skip to content

Commit

Permalink
feat(mako): can now use custom libraries in pycode
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Byron committed Mar 2, 2015
1 parent be93825 commit 2298601
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.pyenv
*.pyc
generated/
target
.api.deps
Expand Down
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.PHONY: json-to-xml clean help api-deps
.SUFFIXES:

include Makefile.helpers

Expand All @@ -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)')
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion etc/bin/mako-render
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/mako/cargo.toml.mako
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# DO NOT EDIT !
# This file was generated automatically by '${self.uri}'
# DO NOT EDIT !
[package]

name = "${name}${version[1:]}"
Expand Down
15 changes: 9 additions & 6 deletions src/mako/deps.mako
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
9 changes: 9 additions & 0 deletions src/mako/lib/util.mako
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions src/mako/lib/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def to_api_version(v):
assert len(v) >= 2 and v[0] == 'v'
return v[1:]

0 comments on commit 2298601

Please sign in to comment.