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

Allow customising the jinja2.Environment #101

Merged
merged 11 commits into from
Jul 27, 2022

Conversation

joaopgrassi
Copy link
Member

@joaopgrassi joaopgrassi commented Jun 27, 2022

Sometimes it might be desired to customize the Jinja Environment. For example, it might be useful to enable the features in Jinja to strip/trim blocks to make it easier to control whitespace rendering in templates.

This PR adds a possibility to pass multiple parameters (similar as the params that are fed into the template)which are passed into the jinja2.Environment as is. provide a new flag --trim-whitespace which enables both trim_blocks and lstrip_blocks in the Jinja environment

I came into this, while working on using the semconv code tool to generate the code files for the conventions in .NET: open-telemetry/opentelemetry-dotnet#2069

It proved very challenging to manage the whites paces/new lines and make the generated .cs file look right. In the end it got complex with the {%- option in jinja. I tried leaving it as is and using dotnet format but that also brought a myriad of other problems.

Now with strip/trim enabled, actual white space/new lines are rendered as is and it is MUCH easier to read and understand what the template is doing and what to expect of an output.

The usage of it would be:

docker run --rm \
  -v ${SCRIPT_DIR}/opentelemetry-specification/semantic_conventions/trace:/source \
  -v ${SCRIPT_DIR}/templates:/templates \
  -v ${ROOT_DIR}/Trace:/output \
  semconvgen \
  -f /source code \
  --template /templates/SemanticConventions.cs.j2 \
  --output /output/TraceSemanticConventions.cs \
  --trim-whitespace \ -> new flag
  -Dclass=TraceSemanticConventions \
  -DschemaUrl=$SCHEMA_URL \
  -Dpkg=OpenTelemetry.Trace

Sometimes it might be desired to customise the Jinja Environment. For example, it might
be useful to enable the features in Jinja to strip/trim blocks to make it easier to control
whitespace rendering in templates.

Now it is possible to pass multiple parameters, which are fed into the `jinja2.Environment` as is.
@joaopgrassi joaopgrassi requested a review from a team June 27, 2022 22:18
Copy link
Member

@Oberon00 Oberon00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I forgot to send my review!

Restrict the params passed to a single one, only allowing to customize whitespace in templates.
@arminru arminru merged commit 9eb700b into open-telemetry:main Jul 27, 2022
@joaopgrassi joaopgrassi deleted the feature/customise_jinja_env branch July 27, 2022 08:07
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

Successfully merging this pull request may close these issues.

3 participants