-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from pmbrull/feature/json
Add JSON rendering
- Loading branch information
Showing
10 changed files
with
116 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
## Rendering JSON Files | ||
|
||
`levy` supports JSON file as well as YAML. All the features remain the same, however, | ||
we need to make sure that the JSON file we write is a correctly formatted JSON | ||
after the render phase, which might be a bit tricky at times. | ||
|
||
> OBS: Note that the added difficulty is only for Jinja templating, as we need to | ||
take care about putting all the quotes and commas. | ||
|
||
Let's revisit the first example we saw, and how it would look like as a JSON file: | ||
|
||
```json | ||
{ | ||
"title": "Lévy the cat", | ||
"colors": ["black", "white"], | ||
"hobby": { | ||
"eating": { | ||
"what": "anything" | ||
} | ||
}, | ||
"friends": [ | ||
{% set friends = [ "cartman", "lima" ] %} | ||
{% for friend in friends %} | ||
{ | ||
"name": "${ friend }", | ||
"type": "cat" | ||
} | ||
{% if loop.index0 < friends|length - 1%} | ||
, | ||
{% endif %} | ||
{% endfor %} | ||
] | ||
} | ||
``` | ||
|
||
As you can see, most of it is the same. However, in the `friends` list, we need | ||
to add specific logic to add commas `,` if we have not reached the end of the loop. | ||
|
||
Afterwards, the API remains: | ||
|
||
```python | ||
from levy.config import Config | ||
|
||
cfg = Config.read_file("test.json") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
"""Dynamic python configuration parser""" | ||
|
||
__version__ = "0.5.1" | ||
__version__ = "0.6.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"title": "Lévy the cat", | ||
"colors": ["black", "white"], | ||
"hobby": { | ||
"eating": { | ||
"what": "anything" | ||
} | ||
}, | ||
"friends": [ | ||
{% set friends = [ "cartman", "lima" ] %} | ||
{% for friend in friends %} | ||
{ | ||
"name": "${ friend }", | ||
"type": "cat" | ||
} | ||
{% if loop.index0 < friends|length - 1%} | ||
, | ||
{% endif %} | ||
{% endfor %} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters