diff --git a/CHANGES.md b/CHANGES.md index fc32398a7a5a..5258183140a2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/sdks/python/apache_beam/yaml/main_test.py b/sdks/python/apache_beam/yaml/main_test.py index 1a3da6443b72..d5fbfedc0349 100644 --- a/sdks/python/apache_beam/yaml/main_test.py +++ b/sdks/python/apache_beam/yaml/main_test.py @@ -15,6 +15,7 @@ # limitations under the License. # +import datetime import glob import logging import os @@ -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) diff --git a/sdks/python/apache_beam/yaml/yaml_transform.py b/sdks/python/apache_beam/yaml/yaml_transform.py index 0190fe20413f..82cd404b4ddc 100644 --- a/sdks/python/apache_beam/yaml/yaml_transform.py +++ b/sdks/python/apache_beam/yaml/yaml_transform.py @@ -16,6 +16,7 @@ # import collections +import datetime import functools import json import logging @@ -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):