mirror of
https://github.com/OMGeeky/google-apis-rs.git
synced 2026-02-23 15:49:49 +01:00
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:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,5 @@
|
||||
.pyenv
|
||||
*.pyc
|
||||
generated/
|
||||
target
|
||||
.api.deps
|
||||
|
||||
10
Makefile
10
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)
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
# DO NOT EDIT !
|
||||
# This file was generated automatically by '${self.uri}'
|
||||
# DO NOT EDIT !
|
||||
[package]
|
||||
|
||||
name = "${name}${version[1:]}"
|
||||
|
||||
@@ -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
9
src/mako/lib/util.mako
Normal 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
3
src/mako/lib/util.py
Normal file
@@ -0,0 +1,3 @@
|
||||
def to_api_version(v):
|
||||
assert len(v) >= 2 and v[0] == 'v'
|
||||
return v[1:]
|
||||
Reference in New Issue
Block a user