Skip to content

Commit c2f028d

Browse files
kbroch-rivosincmattrobenolt
authored andcommitted
add support for hjson and json5 formats input data source
related to #106
1 parent 162a0f9 commit c2f028d

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ python:
88
- '3.6'
99
- '3.7'
1010

11-
install: pip install -e .[yaml,toml,tests,xml]
11+
install: pip install -e .[yaml,toml,tests,xml,hjson,json5]
1212

1313
script:
1414
- pytest -v

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ If `xmltodict` is present, you can use XML as an input data source.
4343

4444
`$ pip install jinja2-cli[xml]`
4545

46+
## Optional HJSON support
47+
If `hjson` is present, you can use HJSON as an input data source.
48+
49+
`$ pip install jinja2-cli[hjson]`
50+
51+
## Optional JSON5 support
52+
If `json5` is present, you can use JSON5 as an input data source.
53+
54+
`$ pip install jinja2-cli[json5]`
55+
4656
## TODO
4757
* Variable inheritance and overrides
4858
* Tests!

jinja2cli/cli.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ class MalformedEnv(InvalidDataFormat):
6969
pass
7070

7171

72+
class MalformedHJSON(InvalidDataFormat):
73+
pass
74+
75+
76+
class MalformedJSON5(InvalidDataFormat):
77+
pass
78+
79+
7280
def get_format(fmt):
7381
try:
7482
return formats[fmt]()
@@ -206,6 +214,18 @@ def _parse_env(data):
206214
return _parse_env, Exception, MalformedEnv
207215

208216

217+
def _load_hjson():
218+
import hjson
219+
220+
return hjson.loads, Exception, MalformedHJSON
221+
222+
223+
def _load_json5():
224+
import json5
225+
226+
return json5.loads, Exception, MalformedJSON5
227+
228+
209229
# Global list of available format parsers on your system
210230
# mapped to the callable/Exception to parse a string into a dict
211231
formats = {
@@ -217,6 +237,8 @@ def _parse_env(data):
217237
"toml": _load_toml,
218238
"xml": _load_xml,
219239
"env": _load_env,
240+
"hjson": _load_hjson,
241+
"json5": _load_json5,
220242
}
221243

222244

@@ -299,6 +321,10 @@ def cli(opts, args):
299321
raise InvalidDataFormat("toml: install toml to fix")
300322
if format == "xml":
301323
raise InvalidDataFormat("xml: install xmltodict to fix")
324+
if format == "hjson":
325+
raise InvalidDataFormat("hjson: install hjson to fix")
326+
if format == "json5":
327+
raise InvalidDataFormat("json5: install json5 to fix")
302328
raise
303329
try:
304330
data = fn(data) or {}

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
"toml": install_requires + ["toml"],
3434
"xml": install_requires + ["xmltodict"],
3535
"tests": install_requires + tests_requires,
36+
"hjson": install_requires + ["hjson"],
37+
"json5": install_requires + ["json5"],
3638
},
3739
tests_require=tests_requires,
3840
include_package_data=True,

0 commit comments

Comments
 (0)