From 087a0762ac936f40bc4cec6f2281db34d9cab95b Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sun, 1 Mar 2015 17:44:02 +0100 Subject: [PATCH] feat(mako-render): multiple input-outputs per call 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. --- .gitignore | 1 + Makefile | 2 +- etc/api/shared.yaml | 2 ++ etc/bin/mako-render | 77 ++++++++++++++++++++++++---------------- src/mako/cargo.toml.mako | 21 +++++++++++ src/mako/deps.mako | 7 ++-- 6 files changed, 77 insertions(+), 33 deletions(-) create mode 100644 src/mako/cargo.toml.mako diff --git a/.gitignore b/.gitignore index 2d0cfb0716f..689fed0fd79 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .pyenv +generated/ target .api.deps Cargo.lock diff --git a/Makefile b/Makefile index 94ed3ba6c96..d836a2f89e1 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/etc/api/shared.yaml b/etc/api/shared.yaml index 7980e44b26c..93f404d5896 100644 --- a/etc/api/shared.yaml +++ b/etc/api/shared.yaml @@ -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 diff --git a/etc/bin/mako-render b/etc/bin/mako-render index bcb82a1ce49..e4d62f1a598 100644 --- a/etc/bin/mako-render +++ b/etc/bin/mako-render @@ -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__": diff --git a/src/mako/cargo.toml.mako b/src/mako/cargo.toml.mako new file mode 100644 index 00000000000..6344f349918 --- /dev/null +++ b/src/mako/cargo.toml.mako @@ -0,0 +1,21 @@ +[package] + +name = "youtube3-dev" +version = "0.0.1" +authors = ["Sebastian Thiel "] +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 = "*" diff --git a/src/mako/deps.mako b/src/mako/deps.mako index 544707d499e..32acf2556cd 100644 --- a/src/mako/deps.mako +++ b/src/mako/deps.mako @@ -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