Skip to content

Commit

Permalink
Merge pull request #33282 [YAML] Make datetime available for jinja te…
Browse files Browse the repository at this point in the history
…mplatization.
  • Loading branch information
robertwb authored Dec 16, 2024
2 parents 4f70ca1 + f080be0 commit 0bd6639
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
## New Features / Improvements
* The datetime module is now available for use in jinja templatization for yaml.
* X feature added (Java/Python) ([#X](https://github.com/apache/beam/issues/X)).
## Breaking Changes
Expand Down
13 changes: 13 additions & 0 deletions sdks/python/apache_beam/yaml/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# limitations under the License.
#

import datetime
import glob
import logging
import os
Expand Down Expand Up @@ -100,6 +101,18 @@ def test_preparse_jinja_flags(self):
'pos_arg',
])

def test_jinja_datetime(self):
with tempfile.TemporaryDirectory() as tmpdir:
out_path = os.path.join(tmpdir, 'out.txt')
main.run([
'--yaml_pipeline',
TEST_PIPELINE.replace('PATH', out_path).replace(
'ELEMENT', '"{{datetime.datetime.now().strftime("%Y-%m-%d")}}"'),
])
with open(glob.glob(out_path + '*')[0], 'rt') as fin:
self.assertEqual(
fin.read().strip(), datetime.datetime.now().strftime("%Y-%m-%d"))


if __name__ == '__main__':
logging.getLogger().setLevel(logging.INFO)
Expand Down
3 changes: 2 additions & 1 deletion sdks/python/apache_beam/yaml/yaml_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#

import collections
import datetime
import functools
import json
import logging
Expand Down Expand Up @@ -992,7 +993,7 @@ def expand_jinja(
jinja2.Environment(
undefined=jinja2.StrictUndefined, loader=_BeamFileIOLoader())
.from_string(jinja_template)
.render(**jinja_variables))
.render(datetime=datetime, **jinja_variables))


class YamlTransform(beam.PTransform):
Expand Down

0 comments on commit 0bd6639

Please sign in to comment.