jinja2-cli icon indicating copy to clipboard operation
jinja2-cli copied to clipboard

output file is scratched --strict and variable not found

Open ollofx opened this issue 4 years ago • 2 comments

echo "hello {{ toto }}, I'm {{ titi }}" > input.file
jinja2 --strict -D "titi=test" -o output.file input.file

then jinja2cli issues exception which is the expected behavior, but instead of leaving the output file as it is, it's already scratched to zero

will it be possible to only flush the output file if the jinja2cli succeeded.

this will be a bonus, replacing the content of file:

echo "hello {{ toto }}, I'm {{ titi }}" > same.file
jinja2 --strict -D "titi=test" -o same.file same.file

could the input file and the output file be the same file as sed -i option allows to replace within the same file

the behavior is that the file is scratched to zero and there is no exception triggered.

the input file could be read, processed then if everything is fine flushed back into the same file to be closed.

my aim is to replace into all my scripts the sed -i by jinja2cli

Thanks a lot already for this smart package

ollofx avatar Apr 28 '21 06:04 ollofx

I have this same issue, would prefer the file wasn't created rather than creating a blank file. The exit status is non-zero.

I'm using it with make and would like it to fail without creating a file, as the next run of make sees a file exists even though it's an empty file.

$
$ ls -la output.file
ls: output.file: No such file or directory
$ echo "hello {{ toto }}, I'm {{ titi }}" > input.file
$ jinja2 --strict -D "titi=test" -o output.file input.file
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/bin/jinja2", line 8, in <module>
    sys.exit(main())
  File "/Users/rdmarsh/Library/Python/3.9/lib/python/site-packages/jinja2cli/cli.py", line 449, in main
    sys.exit(cli(opts, args))
  File "/Users/rdmarsh/Library/Python/3.9/lib/python/site-packages/jinja2cli/cli.py", line 339, in cli
    out.write(render(template_path, data, extensions, opts.strict))
  File "/Users/rdmarsh/Library/Python/3.9/lib/python/site-packages/jinja2cli/cli.py", line 254, in render
    return env.get_template(os.path.basename(template_path)).render(data)
  File "/Users/rdmarsh/Library/Python/3.9/lib/python/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/Users/rdmarsh/Library/Python/3.9/lib/python/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "/Users/rdmarsh/tmp/input.file", line 1, in top-level template code
    hello {{ toto }}, I'm {{ titi }}
jinja2.exceptions.UndefinedError: 'toto' is undefined
$ echo $?
1
$ ls -la output.file
-rw-r--r--  1 rdmarsh  staff  0  6 May 15:15 output.file
$ 
$ 
$ jinja2 --version
jinja2-cli v0.8.2
 - Jinja2 v3.1.2
$ 

Other programs that use -o won't create a file if there's an error.

rdmarsh avatar May 06 '24 05:05 rdmarsh

Yeah. Makes sense. I haven't touched this software in a while, I need to give it some love.

mattrobenolt avatar May 06 '24 06:05 mattrobenolt