Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

output file is scratched --strict and variable not found #93

Open
ollofx opened this issue Apr 28, 2021 · 2 comments
Open

output file is scratched --strict and variable not found #93

ollofx opened this issue Apr 28, 2021 · 2 comments

Comments

@ollofx
Copy link

ollofx commented Apr 28, 2021

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

@rdmarsh
Copy link

rdmarsh commented May 6, 2024

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.

@mattrobenolt
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants