Skip to content

Commit

Permalink
feat(mako-render): multiple input-outputs per call
Browse files Browse the repository at this point in the history
That way, we read the data files only once, but produce all the outputs
we need. Together with a powerful makefile, we have a multi-invocation
with proper depedency tracking.
Everything will be regenerated though, even though just a single input
template file changed.

The alternative would be to have one dependency and invocation per
input dependency, but that will read the entire json each time.

Let's see what's faster/more useful during development.
  • Loading branch information
Byron committed Mar 1, 2015
1 parent c3d399e commit 087a076
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.pyenv
generated/
target
.api.deps
Cargo.lock
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ $(PYTHON):
$(MAKO_RENDER): $(PYTHON)

$(API_DEPS): $(API_SHARED_INFO) $(API_DEPS_TPL) $(MAKO_RENDER)
$(TPL) --data-files $(API_SHARED_INFO) --var SHARED_INFO_FILE=$(API_SHARED_INFO) $(API_DEPS_TPL) > $@
$(TPL) -io $(API_DEPS_TPL) --data-files $(API_SHARED_INFO) > $@

api-deps: $(API_DEPS)

Expand Down
2 changes: 2 additions & 0 deletions etc/api/shared.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ directories:
common: ../
# where are all the API meta files
api_base: ./etc/api
# all mako source files
mako_src: ./src/mako
api:
list:
- name: youtube
Expand Down
77 changes: 47 additions & 30 deletions etc/bin/mako-render
Original file line number Diff line number Diff line change
Expand Up @@ -257,47 +257,64 @@ def _exit():

def cmdline(argv=None):

parser = ArgumentParser("usage: %prog [FILENAME]")
parser.add_argument("--var", default=[], action="append",
help="variable (can be used multiple times, use name=value)")
parser.add_argument("--data-files", default=[], action="append",
parser = ArgumentParser("mako-render")
parser.add_argument("--var", nargs="*", default=[],
help="variable (can be used multiple times, use NAME=VALUE)")
parser.add_argument("--data-files", nargs="*", default=[],
help="data file (can be used multiple times, use path[=namespace])")
parser.add_argument("--template-dir", default=[], action="append",
parser.add_argument("--template-dir", nargs="*", default=[],
help="Directory to use for template lookup (multiple "
"directories may be provided). If not given then if the "
"template is read from stdin, the value defaults to be "
"the current directory, otherwise it defaults to be the "
"parent directory of the file provided.")
parser.add_argument('input', nargs='?', default='-')
parser.add_argument('-io', nargs="+",
help="input and ouptut pairs. can be used multiple times, use TEMPLATE_FILE_IN=[OUTPUT_FILE])")

options = parser.parse_args(argv)
if options.input == '-':
lookup_dirs = options.template_dir or ["."]
lookup = TemplateLookup(lookup_dirs)
try:
template = Template(sys.stdin.read(), lookup=lookup)
except:
_exit()
else:
filename = options.input
if not isfile(filename):
raise SystemExit("error: can't find %s" % filename)
lookup_dirs = options.template_dir or [dirname(filename)]
lookup = TemplateLookup(lookup_dirs)
try:
template = Template(filename=filename, lookup=lookup)
except:
_exit()
if len(options.io) == 0:
options.io.append('-')
options.io = [varsplit(v) for v in options.io]

datafiles = [varsplit(var) for var in options.data_files]
data = load_data(datafiles)
data = dict((k, DictObject(v)) for k, v in data.items())
data.update(dict([varsplit(var) for var in options.var]))

try:
print(template.render(**data))
except:
_exit()
data_converted = dict((k, DictObject(v)) for k, v in data.items() if isinstance(v, dict))
data_converted.update((k, v) for k, v in data.items() if not isinstance(v, dict))
data_converted.update(dict([varsplit(var) for var in options.var]))
del data

seen_stdin = False
for input_file, output_file in options.io:
if input_file == '-':
assert not seen_stdin, "STDIN (-) can only be named once"
seen_stdin = True
lookup_dirs = options.template_dir or ["."]
lookup = TemplateLookup(lookup_dirs)
try:
template = Template(sys.stdin.read(), lookup=lookup)
except:
_exit()
else:
if not isfile(input_file):
raise SystemExit("error: can't find %s" % input_file)
lookup_dirs = options.template_dir or [dirname(input_file)]
lookup = TemplateLookup(lookup_dirs)
try:
template = Template(filename=input_file, lookup=lookup)
except:
_exit()

try:
result = template.render(**data_converted)
if output_file:
fh = open(output_file, "wb")
fh.write(result)
fh.close()
else:
print(result)
except:
_exit()
# end for each input file


if __name__ == "__main__":
Expand Down
21 changes: 21 additions & 0 deletions src/mako/cargo.toml.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]

name = "youtube3-dev"
version = "0.0.1"
authors = ["Sebastian Thiel <byronimo@gmail.com>"]
description = "A library to facilitate interacting with your youtube account"
repository = "https://github.com/Byron/youtube-rs"
license = "MIT"
keywords = ["youtube", "google", "protocol"]

[dependencies]
# Just to get hyper to work !
openssl = "= 0.4.3"
# Just to get hyper to work !
cookie = "= 0.1.13"
hyper = "*"
rustc-serialize = "*"
yup-oauth2 = "*"

[dev-dependencies]
yup-hyper-mock = "*"
7 changes: 5 additions & 2 deletions src/mako/deps.mako
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
api_clean = api_name + '-clean'
api_info.append((api_name, api_clean, gen_root))
%>\
${gen_root}: ${directories.api_base}/${a.name}/${a.version}/${a.name}-api.json ${SHARED_INFO_FILE}
${gen_root}: ${directories.api_base}/${a.name}/${a.version}/${a.name}-api.json $(API_SHARED_INFO)
@mkdir -p $@
$(TPL) -io ${directories.mako_src}/cargo.toml.mako=$@/cargo.toml --data-files $^
${api_name}: ${gen_root}
@echo TODO ${api_name} command
${api_clean}:
-rm -Rf ${gen_root}
% endfor
Expand Down

0 comments on commit 087a076

Please sign in to comment.