kblomqvist / yasha

A command-line tool to render Jinja templates for great good
MIT License
246 stars 23 forks source link

Yasha .json template error #34

Closed alan-cugler closed 7 years ago

alan-cugler commented 7 years ago

I have a valid .json file that works if I use it as is. This .json file has {{ .foo }} variables in it, which are internally processed ( these are not jinja variables) .

I am attempting to take this .json file and run it through the Yasha template command to create more .json files using jinja {{ bar }} to replace other hard coded variable values in the .json file.

If I run the .json file as is, it complains about the .json {{ .foo }} variables as expected. These variables can not be replaced or removed so I used the {% raw %} as described here to omit them.

My syntax looked like this: {% raw %} ...code line...{{ .foo } }...more code...{% endraw %}.

This should have allowed Yasha to ignore the .json variables with no complaints. Instead I receive the following error:

Traceback (most recent call last):
  File "/usr/local/bin/yasha", line 11, in <module>
    load_entry_point('yasha==3.1', 'console_scripts', 'yasha')()
  File "/usr/local/lib/python3.5/dist-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.5/dist-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.5/dist-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/yasha/scripts/yasha.py", line 156, in cli
    vardict.update(parse_variables(variables, ex["variable_parsers"]))
  File "/usr/local/lib/python3.5/dist-packages/yasha/scripts/yasha.py", line 35, in parse_variables
    return parser.parse(file)
  File "/usr/local/lib/python3.5/dist-packages/yasha/parsers/json.py", line 35, in parse
    return json.loads(file.read().decode('utf8'))
  File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.5/json/decoder.py", line 355, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

I think this is because a json validator is being run on the file first, before jinja is used. I didn't expect any json validating.

If I change the file extension from .json to .txt it has no complaints and completes its jinja parsing correctly. I think because there is no json validating in this file extension.

nixjdm commented 7 years ago

I didn't realize this happened either. Being new to Yasha, I'd have expected only jinja by default. Validating json (et al.) variable files makes sense, so they can be used. Validating the templates seems a bit out of scope though, again, at least by default. Sounds like nice icing on the cake though as an option. I'm still new though and I'm sure I don't know all Yasha's use cases. Thanks for the project :)

kblomqvist commented 7 years ago

I'm not sure if this is Yasha related. According to traceback you are using older version. Does this happen with Yasha 4.0 too?

kblomqvist commented 7 years ago

Yasha does not validate the template.

kblomqvist commented 7 years ago

How does your command-line call looks like?

nixjdm commented 7 years ago

It looks like it's trying to load a variable file and dying on that.

kblomqvist commented 7 years ago

Exactly. That's what I was thinking too. Workaround could be to use --no-variable-file, or to rename the template to foo.json.j2.

alan-cugler commented 7 years ago

My command line code was: yasha -v --SSD=20480 ~/test.json -o ~/test_exit.json

alan-cugler commented 7 years ago

I tested the command line code against yasha 4.0 and observed the same problem. I then took on your suggestion of --no-variable-file and added it in to my command line code: yasha --no-variable-file --SSD=20480 ~/test.json -o ~/test_exit.json The code worked without a hitch!

Analysis: The expectation of a variable file is annoying when you only need to pass in a couple variable values. It'd make sense for it to be an optional tag for feeding yasha a variable file path. At least there should be some check to give you a error feedback that is useful. ERROR: variable-file expected

kblomqvist commented 7 years ago

This should be fixed in 4.1. Thanks for reporting.