Skip to content

Commit

Permalink
[YAML] Make datetime available for jinja templatization.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertwb committed Dec 4, 2024
1 parent 78bde63 commit f080be0
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 f080be0

Please sign in to comment.