Add jinja2 command line script #291

Closed
wants to merge 3 commits into
from

Conversation

6 participants
@tebeka

tebeka commented Jan 10, 2014

See issue 258

I decided to go with the simple approach for now - just command line variables.

@douglarek

This comment has been minimized.

Show comment Hide comment
@douglarek

douglarek Jan 15, 2014

@tebeka please see the errors, your patch should support 2.6 and 3.3

@tebeka please see the errors, your patch should support 2.6 and 3.3

@tebeka

This comment has been minimized.

Show comment Hide comment
@tebeka

tebeka Jan 15, 2014

OK, will look and fix.

tebeka commented Jan 15, 2014

OK, will look and fix.

@tebeka

This comment has been minimized.

Show comment Hide comment
@tebeka

tebeka Feb 2, 2014

Took a bit, but now passes on 2.6 and 3.3 (at least on my machine :)

tebeka commented Feb 2, 2014

Took a bit, but now passes on 2.6 and 3.3 (at least on my machine :)

@tebeka

This comment has been minimized.

Show comment Hide comment
@tebeka

tebeka Feb 2, 2014

Another option (that I implemented for an internal tool) is to use py: prefix for values to evaluate them.
This was better than -e since then you need to "'double quote'" strings.

What do you think?

tebeka commented Feb 2, 2014

Another option (that I implemented for an internal tool) is to use py: prefix for values to evaluate them.
This was better than -e since then you need to "'double quote'" strings.

What do you think?

+
+def parse_value(value):
+ prefix = 'py:'
+ if value[:3] != prefix:

This comment has been minimized.

Show comment Hide comment
@indecisivemal

indecisivemal May 14, 2015

Use if not value.startswith(prefix):?

@indecisivemal

indecisivemal May 14, 2015

Use if not value.startswith(prefix):?

@indecisivemal

This comment has been minimized.

Show comment Hide comment
@indecisivemal

indecisivemal May 14, 2015

I'd also like to be able to read variables from the environment, for use like eRuby. I had written my own before thinking to search here: https://gist.github.com/mallegonian/09d7e005ddbc225852fc
This allows for easier usage from other scripts, eg:

export THING_A="beep"
export THING_B="boop"
python -m jinja2 /etc/braap.jinja >/etc/braap.conf

I'd also like to be able to read variables from the environment, for use like eRuby. I had written my own before thinking to search here: https://gist.github.com/mallegonian/09d7e005ddbc225852fc
This allows for easier usage from other scripts, eg:

export THING_A="beep"
export THING_B="boop"
python -m jinja2 /etc/braap.jinja >/etc/braap.conf
+ env = Environment(undefined=StrictUndefined)
+ template = env.from_string(args.template.read())
+
+ args.output.write(template.render(tvars))

This comment has been minimized.

Show comment Hide comment
@snoack

snoack Aug 6, 2015

Contributor

I think you should use template.generate() here, writing chunk by chunk to the file. This will improve performance and keeps memory usage low in case the output is huge.

@snoack

snoack Aug 6, 2015

Contributor

I think you should use template.generate() here, writing chunk by chunk to the file. This will improve performance and keeps memory usage low in case the output is huge.

+ tvars = parse_cmdline_vars(args.var or [])
+
+ # Fail on undefined
+ env = Environment(undefined=StrictUndefined)

This comment has been minimized.

Show comment Hide comment
@snoack

snoack Aug 6, 2015

Contributor

Why do you enforce this non-standard behavior? If anything this should be an option.

@snoack

snoack Aug 6, 2015

Contributor

Why do you enforce this non-standard behavior? If anything this should be an option.

extras_require={'i18n': ['Babel>=0.8']},
test_suite='jinja2.testsuite.suite',
include_package_data=True,
entry_points="""
[babel.extractors]
jinja2 = jinja2.ext:babel_extract[i18n]
+
+ [console_scripts]
+ jinja2 = jinja2.__main__:main

This comment has been minimized.

Show comment Hide comment
@snoack

snoack Aug 6, 2015

Contributor

I think this should be a standalone script rather than a __main__ module.

@snoack

snoack Aug 6, 2015

Contributor

I think this should be a standalone script rather than a __main__ module.

+ assert out.strip() == expected, 'bad output'
+
+
+def suite():

This comment has been minimized.

Show comment Hide comment
@snoack

snoack Aug 6, 2015

Contributor

This should probably be integrated in the pytest suite located in the tests folder.

@snoack

snoack Aug 6, 2015

Contributor

This should probably be integrated in the pytest suite located in the tests folder.

@auvipy

This comment has been minimized.

Show comment Hide comment
@auvipy

auvipy Aug 13, 2016

@tebeka are you still working on it?

auvipy commented Aug 13, 2016

@tebeka are you still working on it?

@tebeka

This comment has been minimized.

Show comment Hide comment
@tebeka

tebeka Aug 14, 2016

@auvipy Nope :)

tebeka commented Aug 14, 2016

@auvipy Nope :)

@davidism

This comment has been minimized.

Show comment Hide comment
@davidism

davidism Aug 14, 2016

Member

@davidism davidism closed this Aug 14, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment