Skip to content

Commit

Permalink
DM-47976: Support conversion field in file template format strings
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoldina committed Dec 6, 2024
1 parent 0155486 commit 8788edb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/changes/DM-47976.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support type conversion field in file template format strings.
6 changes: 5 additions & 1 deletion python/lsst/daf/butler/datastore/file_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down
9 changes: 9 additions & 0 deletions tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 8788edb

Please sign in to comment.