You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "Laura Lorenz (JIRA)" <ji...@apache.org> on 2016/10/11 19:04:20 UTC

[jira] [Commented] (AIRFLOW-436) Incorrect templating when used with .format

    [ https://issues.apache.org/jira/browse/AIRFLOW-436?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15566287#comment-15566287 ] 

Laura Lorenz commented on AIRFLOW-436:
--------------------------------------

By definition the default Jinja templating and string.format() are incompatible; {noformat}{{{noformat} and {noformat}}}{noformat} escapes a single bracket, e.g.: {noformat}{{noformat}  by `string.format` (see [string.format|https://docs.python.org/2/library/string.html#format-string-syntax]).

But {noformat}{{{noformat} and {noformat}}}{noformat} are the default delimiters for the Jinja template renderer (see [Jinja docs|http://jinja.pocoo.org/docs/dev/templates/#synopsis]). 

In your case the `string.format` is run first, escaping the {noformat}{{{noformat} to {noformat}{{noformat} which no longer matches the delimiter {noformat}{{{noformat} that Jinja expects, so Jinja doesn't resolve the `ds` variable. 

In your second case, the use of the modulo on strings does not consider braces special at all so will not try and escape them (see [string % modulo docs|https://docs.python.org/2/library/stdtypes.html#string-formatting]) and thus Jinja rendering proceeds as expected. 

I suggest to use `string.Template` class instead of `string.format()` method to not conflict with the Jinja renderer; `string.Template` uses `$` as a delimiter by default, and is also configurable on your side ([see docs |https://docs.python.org/2/library/string.html#template-strings]).

Ironically braces are also reserved characters in JIRA so I had to escape them here too, thus the odd formatting :)

> Incorrect templating when used with .format
> -------------------------------------------
>
>                 Key: AIRFLOW-436
>                 URL: https://issues.apache.org/jira/browse/AIRFLOW-436
>             Project: Apache Airflow
>          Issue Type: Bug
>            Reporter: Sumit Maheshwari
>
> Found that when {{.format}} is used in a templatable field, it produces weird results. For example:
> {code:none}
> t3 = BashOperator(
>     task_id='wget',
>     bash_command="wget {0}/{{ ds }}".format('google.com'),
>     dag=dag)
> {code}
> produces following result:
> {code:none}
> {bash_operator.py:79} INFO - Running command: wget google.com/{ ds }
> {code}
> But if we change {{bash_command}} to following, it works as expected:
> {code:none}
> bash_command="wget %s/{{ ds }}"%('google.com')
> {code}
> {code:none}
> {bash_operator.py:79} INFO - Running command: wget google.com/2016-08-05
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)