From c405656690e8bf9c8c326b4cd0f3aa8a762be8a0 Mon Sep 17 00:00:00 2001 From: Kim Blomqvist Date: Sat, 24 Feb 2018 16:46:00 +0200 Subject: [PATCH 1/7] Fix whitespace control in nrf51.rs.jinja Jinja had change its behaviour on this. --- tests/fixtures/nrf51.rs.jinja | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fixtures/nrf51.rs.jinja b/tests/fixtures/nrf51.rs.jinja index c536008..76c3054 100644 --- a/tests/fixtures/nrf51.rs.jinja +++ b/tests/fixtures/nrf51.rs.jinja @@ -54,7 +54,7 @@ use core::ops::Drop; {{ register(r)|indent }} {% endfor %} } -{% endmacro %} +{%- endmacro %} {% for p in peripherals %} ioregs! ({{ p.name }} @ {{ "%#010x"|format(p.baseAddress) }} From 35e76ff992e2cc6e0a516043fecf2c689165eaea Mon Sep 17 00:00:00 2001 From: Kim Blomqvist Date: Sat, 24 Feb 2018 16:46:30 +0200 Subject: [PATCH 2/7] Skip build automation tests if scons/cmake does not exist --- tests/test_build_automation_c.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/test_build_automation_c.py b/tests/test_build_automation_c.py index bb1bd64..a4af44d 100644 --- a/tests/test_build_automation_c.py +++ b/tests/test_build_automation_c.py @@ -23,8 +23,8 @@ """ import sys +import subprocess from os import path, chdir, mkdir -from subprocess import check_output, check_call import pytest @@ -36,6 +36,22 @@ reason='Requires either Python 2.7 or >= 3.5' ) +def check_output(cmd): + try: + return subprocess.check_output(cmd) + except FileNotFoundError as e: + msg = str(e) + pytest.skip(msg) + + +def check_call(cmd): + try: + return subprocess.check_call(cmd) + except FileNotFoundError as e: + msg = str(e) + pytest.skip(msg) + + def setup_function(): chdir(SCRIPT_PATH + '/fixtures/c_project') From de4f6f8431450b7c3f0fed5e1df3c11edaac7f14 Mon Sep 17 00:00:00 2001 From: Kim Blomqvist Date: Sun, 25 Feb 2018 13:43:18 +0200 Subject: [PATCH 3/7] Rewrite scons builder The rewritten builder is not anymore dependent on changes made in yasha cli, e.g., in case when -v was decided to support multiple variable files. --- yasha/scons.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/yasha/scons.py b/yasha/scons.py index d18040c..c364bd8 100644 --- a/yasha/scons.py +++ b/yasha/scons.py @@ -32,30 +32,28 @@ class Builder(BuilderBase): - def __init__(self, action="yasha $SOURCE -o $TARGET"): + def __init__(self, action="yasha -o $TARGET $SOURCE"): def scan(node, env, path): src = str(node.srcnode()) src_dir = os.path.dirname(src) variant_dir = os.path.dirname(str(node)) - cli_command = [src, "-M"] - extensions = re.search(r"(-e|--extensions)\s*(.+)", action) - variables = re.search(r"(-v|--variables)\s*(.+)", action) + cmd = action.replace('$SOURCE', src) + cmd = ['-M'] + cmd.split()[1:] - if extensions: - cli_command += ["-e", extensions.group(2)] - if variables: - cli_command += ["-v", extensions.group(2)] - if re.match(r"--no-variables", action): - cli_command += ["--no-variable-file"] - if re.match(r"--no-extensions", action): - cli_command += ["--no-extension-file"] + try: # Remove $TARGET from action + index = cmd.index('-o') + del cmd[index] + del cmd[index] + except ValueError: + pass runner = CliRunner() - result = runner.invoke(cli.cli, cli_command) + result = runner.invoke(cli.cli, cmd) deps = result.output[:-1].split(" ")[2:] deps = [d.replace(src_dir, variant_dir) for d in deps] + return env.File(deps) def emit(target, source, env): From e441ee1354c5bc85c7e191c4422c4180fe2ed416 Mon Sep 17 00:00:00 2001 From: Kim Blomqvist Date: Sun, 25 Feb 2018 16:28:16 +0200 Subject: [PATCH 4/7] Remove unnecessary import of re --- yasha/scons.py | 1 - 1 file changed, 1 deletion(-) diff --git a/yasha/scons.py b/yasha/scons.py index c364bd8..5503d22 100644 --- a/yasha/scons.py +++ b/yasha/scons.py @@ -23,7 +23,6 @@ """ import os -import re from SCons.Builder import BuilderBase from click.testing import CliRunner From 4bb4c53684a9eb5d1c9fc19d23315c47fbd33beb Mon Sep 17 00:00:00 2001 From: Jeff Potter Date: Mon, 12 Mar 2018 02:19:49 -0400 Subject: [PATCH 5/7] Support for multiple variables files (#40) --- README.md | 11 +++++++++-- tests/test_cli.py | 20 ++++++++++++++++++++ yasha/cli.py | 14 ++++++++------ 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1a06bd1..82f2c2d 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,13 @@ Template variables can be defined in a separate file. [JSON](http://www.json.org yasha -v variables.yaml template.j2 ``` +Multiple variable files can be given: + +```bash +yasha -v variables.yaml -v settings.yaml template.j2 +``` +Note that variables redefined in later variable files will take precedence. + Additionally you may define variables as part of the command-line call, e.g. ```bash @@ -92,7 +99,7 @@ A variable defined via command-line will overwrite a variable defined in file. ### Automatic variable file look up -If the variable file is not explicitly given, Yasha will look for it by searching a file named in the same way than the corresponding template but with the file extension either `.json`, `.yaml`, `.yml`, `.toml`, or `.xml`. +If no variable file is explicitly given, Yasha will look for one by searching for a file named in the same way than the corresponding template but with the file extension either `.json`, `.yaml`, `.yml`, `.toml`, or `.xml`. For example, consider the following template and variable files @@ -107,7 +114,7 @@ Because of automatic variable file look up, the command-line call yasha template.j2 ``` -equals to +is equal to ```bash yasha -v template.yaml template.j2 diff --git a/tests/test_cli.py b/tests/test_cli.py index 47dca01..24d37d9 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -58,6 +58,26 @@ def test_explicit_variable_file(tmpdir, varfile): assert output.read() == '1' +def test_two_explicitly_given_variables_files(tmpdir): + # Template to calculate a + b + c: + tpl = tmpdir.join('template.j2') + tpl.write('{{ a + b + c }}') + + # First variable file defines a & b: + a = tmpdir.join('a.yaml') + a.write('a: 1\nb: 100') + + # Second variable file redefines b & defines c: + b = tmpdir.join('b.toml') + b.write('b = 2\nc = 3') + + errno = call(('yasha', '-v', str(a), '-v', str(b), str(tpl))) + assert errno == 0 + + output = tmpdir.join('template') + assert output.read() == '6' # a + b + c = 1 + 2 + 3 = 6 + + def test_variable_file_lookup(tmpdir, vartpl): # /cwd # /sub diff --git a/yasha/cli.py b/yasha/cli.py index dc0eb31..01c683c 100644 --- a/yasha/cli.py +++ b/yasha/cli.py @@ -120,7 +120,7 @@ def load_extensions(file): @click.argument("template_variables", nargs=-1, type=click.UNPROCESSED) @click.argument("template", type=click.File("rb")) @click.option("--output", "-o", type=click.File("wb"), help="Place the rendered template into FILENAME.") -@click.option("--variables", "-v", type=click.File("rb"), help="Read template variables from FILENAME. Built-in parsers are JSON, YAML, TOML and XML.") +@click.option("--variables", "-v", type=click.File("rb"), multiple=True, help="Read template variables from FILENAME. Built-in parsers are JSON, YAML, TOML and XML.") @click.option("--extensions", "-e", envvar='YASHA_EXTENSIONS', type=click.File("rb"), help="Read template extensions from FILENAME. A Python file is expected.") @click.option("--encoding", "-c", default=yasha.ENCODING, help="Default is UTF-8.") @click.option("--include_path", "-I", type=click.Path(exists=True, file_okay=False), multiple=True, help="Add DIRECTORY to the list of directories to be searched for the referenced templates.") @@ -144,7 +144,7 @@ def cli(template_variables, template, output, variables, extensions, encoding, i yasha --hello=world -o output.txt template.j2 defines a variable 'hello' for a template like: - + Hello {{ hello }} ! """ @@ -173,7 +173,7 @@ def cli(template_variables, template, output, variables, extensions, encoding, i if not variables and not no_variable_file: for file in template_companion: if file.endswith(tuple(PARSERS.keys())): - variables = click.open_file(file, "rb") + variables = (click.open_file(file, "rb"),) break if not output: @@ -185,8 +185,8 @@ def cli(template_variables, template, output, variables, extensions, encoding, i if m or md: deps = [os.path.relpath(template.name)] - if variables: - deps.append(os.path.relpath(variables.name)) + for file in variables: + deps.append(os.path.relpath(file.name)) if extensions: deps.append(os.path.relpath(extensions.name)) for d in yasha.find_referenced_templates(template, include_path): @@ -220,7 +220,9 @@ def cli(template_variables, template, output, variables, extensions, encoding, i t = jinja.get_template(os.path.basename(template.name)) # Parse variables - context = parse_variable_file(variables) + context = dict() + for file in variables: + context.update(parse_variable_file(file)) context.update(yasha.parse_cli_variables(template_variables)) # Finally render template and save it From 5a3ee45290d34320e7de75b3cd9978f5207857c4 Mon Sep 17 00:00:00 2001 From: Kim Blomqvist Date: Mon, 12 Mar 2018 08:26:43 +0200 Subject: [PATCH 6/7] Update CHANGES.md --- CHANGES.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 7173d7c..0067fcc 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,10 @@ +Version 4.2 +----------- + +Minor release, unreleased + +- Added support for multiple variables files. + Version 4.1 ----------- From 92e1dc98366b6186108b25a1a04f2554974f6e21 Mon Sep 17 00:00:00 2001 From: Kim Blomqvist Date: Sun, 18 Mar 2018 13:48:41 +0200 Subject: [PATCH 7/7] Prepare a release (version 4.2) --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 0067fcc..c9cff7b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,7 +1,7 @@ Version 4.2 ----------- -Minor release, unreleased +Minor release, released 18 March 2018 - Added support for multiple variables files.