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 Aug 23, 2020
2 parents e128b7d + c914218 commit 9996d16
Show file tree
Hide file tree
Showing 20 changed files with 407 additions and 90 deletions.
24 changes: 24 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
Version 5.0
-----------

Major release, unreleased

- Dropped support for Python 2.7.
- The option `--keep-trailing-newline` was removed in favor of making
it default. The old behaviour can be achieved with the new option
`--remove-trailing-newline`.
- Fixed Jinja 2.11.x compatability issue (gh-60).

Version 4.4
-----------

Minor release (last with Python 2.7 support), unreleased

- Fixed an exit code in case of undefined variable from 0 to 1.
- Fixed a bug that caused extension classes not to load.
- Quoted string variable with commas is not converted to list anymore (gh-57).
- Implemented workaround for Jinja 2.11 compatability issue (gh-60)
- Added support for INI and CSV file parsing.
- Fixed a bug that caused Yasha to crash when loading file extensions
(regression likely caused by Click).

Version 4.3
-----------

Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015-2017 Kim Blomqvist
Copyright (c) 2015-2020 Kim Blomqvist

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
66 changes: 62 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Usage: yasha [OPTIONS] [TEMPLATE_VARIABLES]... TEMPLATE
Options:
-o, --output FILENAME Place the rendered template into FILENAME.
-v, --variables FILENAME Read template variables from FILENAME. Built-
in parsers are JSON, YAML, TOML and XML.
in parsers are JSON, YAML, TOML, INI, XML and CSV.
-e, --extensions FILENAME Read template extensions from FILENAME. A
Python file is expected.
-c, --encoding TEXT Default is UTF-8.
Expand All @@ -80,7 +80,7 @@ Options:

## Template variables

Template variables can be defined in a separate file. By default [JSON](http://www.json.org), [YAML](http://www.yaml.org/start.html), [TOML](https://github.com/toml-lang/toml) and [XML](https://github.com/martinblech/xmltodict) formats are supported.
Template variables can be defined in a separate file. By default [JSON](http://www.json.org), [YAML](http://www.yaml.org/start.html), [TOML](https://github.com/toml-lang/toml), [INI](https://docs.python.org/3/library/configparser.html#supported-ini-file-structure), [XML](https://github.com/martinblech/xmltodict) and [CSV](https://tools.ietf.org/html/rfc4180#section-2) formats are supported.

```bash
yasha -v variables.yaml template.j2
Expand All @@ -98,6 +98,58 @@ Additionally you may define variables as part of the command-line call. A variab
yasha --foo=bar -v variables.yaml template.j2
```

### CSV files

to use the data stored in a csv variable file in templates, the name of the variable in the template has to match the name of the csv variable file.

For example, consider the following template and variable files

```
template.j2
mydata.csv
```

And the following contents in `mydata.csv`

```csv
cell1,cell2,cell3
cell4,cell5,cell6
```

to access the rows of cells, you use the following syntax in your template (note that 'mydata' here matches the file name of the csv file)

```jinja2
{% for row in mydata %}
cell 1's value is {{row[0]}},
cell 2's value is {{row[1]}}
{% endfor %}
```

By default, each row in the csv file is accessed in the template as a list of values (`row[0]`, `row[1]`, etc).
You can make each row accessible instead as a mapping by adding a header to the csv file.

For example, consider the following contents of `mydata.csv`

```csv
first_column,column2,third column
cell1,cell2,cell3
cell4,cell5,cell6
```

and the following Jinja template

```jinja2
{% for row in mydata %}
cell 1's value is {{row.first_column}},
cell 2's value is {{row.column2}},
cell 3's value is {{row['third column']}}
{% endfor %}
```

As you can see, cells can be accessed by column name instead of column index.
If the column name has no spaces in it, the cell can be accessed with 'dotted notation' (ie `row.first_column`) or 'square-bracket notation' (ie `row['third column']`.
If the column name has a space in it, the cell can only be accessed with 'square-bracket notation'

### Automatic file variables look up

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`.
Expand Down Expand Up @@ -354,13 +406,19 @@ cat template.j2 | yasha -v variables.yaml -
Variables given as part of the command-line call can be Python literals, e.g. a list would be defined like this

```bash
yasha --foo="['foo', 'bar', 'baz']" template.j2
yasha --lst="['foo', 'bar', 'baz']" template.j2
```

The following is also interpreted as a list

```bash
yasha --foo=foo,bar,baz template.j2
yasha --lst=foo,bar,baz template.j2
```

Note that in case you like to pass a string with commas as a variable you have to quote it as

```bash
yasha --str='"foo,bar,baz"' template.j2
```

Other possible literals are:
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
include_package_data=True,
install_requires=[
"Click",
"Jinja2",
"Jinja2<2.11",
"pytoml",
"pyyaml",
"xmltodict",
'configparser;python_version<"3.5"'
],
entry_points='''
[console_scripts]
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
The MIT License (MIT)
Copyright (c) 2015-2017 Kim Blomqvist
Copyright (c) 2015-2020 Kim Blomqvist
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,10 +24,10 @@

import pytest
import os.path
import sys

SCRIPT_PATH = os.path.dirname(os.path.realpath(__file__))


@pytest.fixture
def fixtures_dir():
return os.path.join(SCRIPT_PATH, "fixtures")
2 changes: 1 addition & 1 deletion tests/fixtures/nrf51.rs.jinja
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{#- The MIT License (MIT)
Copyright (c) 2015-2017 Kim Blomqvist
Copyright (c) 2015-2020 Kim Blomqvist
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
5 changes: 4 additions & 1 deletion tests/test_build_automation_c.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
The MIT License (MIT)
Copyright (c) 2015-2017 Kim Blomqvist
Copyright (c) 2015-2020 Kim Blomqvist
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -28,6 +28,9 @@

import pytest

if sys.version_info[0] == 2:
FileNotFoundError = IOError

SCRIPT_PATH = path.dirname(path.realpath(__file__))

requires_py27_or_py35_or_greater = pytest.mark.skipif(
Expand Down
Loading

0 comments on commit 9996d16

Please sign in to comment.