From 8788edb20a51c03541bcbbe2c77f186d0c2d78ee Mon Sep 17 00:00:00 2001 From: tatianag Date: Thu, 5 Dec 2024 17:09:44 -0800 Subject: [PATCH] DM-47976: Support conversion field in file template format strings --- doc/changes/DM-47976.bugfix.md | 1 + python/lsst/daf/butler/datastore/file_templates.py | 6 +++++- tests/test_templates.py | 9 +++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 doc/changes/DM-47976.bugfix.md diff --git a/doc/changes/DM-47976.bugfix.md b/doc/changes/DM-47976.bugfix.md new file mode 100644 index 0000000000..1c427373a8 --- /dev/null +++ b/doc/changes/DM-47976.bugfix.md @@ -0,0 +1 @@ +Support type conversion field in file template format strings. diff --git a/python/lsst/daf/butler/datastore/file_templates.py b/python/lsst/daf/butler/datastore/file_templates.py index 85cbc8d93a..5496bdf952 100644 --- a/python/lsst/daf/butler/datastore/file_templates.py +++ b/python/lsst/daf/butler/datastore/file_templates.py @@ -571,7 +571,7 @@ def format(self, ref: DatasetRef) -> str: parts = fmt.parse(self.template) output = "" - for literal, field_name, format_spec, _ in parts: + for literal, field_name, format_spec, conversion in parts: if field_name and "|" in field_name: alternates = field_name.split("|") for alt in alternates: @@ -678,6 +678,10 @@ def format(self, ref: DatasetRef) -> str: if replace_slash: value = value.replace("/", "_") + # Apply conversion (e.g., integer to string) + if conversion: + value = fmt.convert_field(value, conversion) + # Now use standard formatting output = output + literal + format(value, format_spec) diff --git a/tests/test_templates.py b/tests/test_templates.py index 9f66ba5e49..0d288861ad 100644 --- a/tests/test_templates.py +++ b/tests/test_templates.py @@ -152,6 +152,15 @@ def testBasic(self): self.makeDatasetRef("calexp", run="run.2", dataId=dataId), ) + # Check that type conversion is applied + dataId = {"instrument": "dummy", "day_obs": 20250203} + tmplstr = "{run}/{datasetType}/{day_obs!s:.4s}" + self.assertTemplate( + tmplstr, + "run3/calexp/2025", + self.makeDatasetRef("calexp", run="run3", dataId=dataId), + ) + with self.assertRaises(FileTemplateValidationError): FileTemplate("no fields at all")