Skip to content

Commit

Permalink
Merge branch 'master' into 4.x-maintenance
Browse files Browse the repository at this point in the history
  • Loading branch information
kblomqvist committed Mar 18, 2018
2 parents 31c91ef + 92e1dc9 commit 3aabe38
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 24 deletions.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Version 4.2
-----------

Minor release, released 18 March 2018

- Added support for multiple variables files.

Version 4.1
-----------

Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/nrf51.rs.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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) }}
Expand Down
18 changes: 17 additions & 1 deletion tests/test_build_automation_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"""

import sys
import subprocess
from os import path, chdir, mkdir
from subprocess import check_output, check_call

import pytest

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

Expand Down
20 changes: 20 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions yasha/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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 }} !
"""

Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down
25 changes: 11 additions & 14 deletions yasha/scons.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"""

import os
import re
from SCons.Builder import BuilderBase
from click.testing import CliRunner

Expand All @@ -32,30 +31,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):
Expand Down

0 comments on commit 3aabe38

Please sign in to comment.