Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-47976: Support conversion field in file template format strings #1127

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading