Skip to content

Commit

Permalink
Merge pull request #317 from jacebrowning/match-defaults
Browse files Browse the repository at this point in the history
Fix matching files for patterns with defaults
  • Loading branch information
jacebrowning authored Jan 6, 2024
2 parents 121b3b9 + 2246004 commit e1d9ddf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## 2.2.2 (2024-01-06)

- Fixed `Manager.all()` behavior for patterns with default vales.

## 2.2.1 (2024-01-04)

- Updated `Manager.get()` to handle default values in pattern arguments.
Expand Down
7 changes: 5 additions & 2 deletions datafiles/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ def all(self, *, _exclude: str = "") -> Iterator[Model]:
path = root / self.model.Meta.datafile_pattern
log.debug(f"Detected dynamic pattern: {path}")

pattern = str(path.resolve())
pattern = alt_pattern = str(path.resolve())
for field in dataclasses.fields(self.model):
if not isinstance(field.default, Missing):
alt_pattern = alt_pattern.replace("{self." + field.name + "}", "")
splatted = pattern.format(self=Splats()).replace(
f"{os.sep}*{os.sep}", f"{os.sep}**{os.sep}"
)
Expand All @@ -131,7 +134,7 @@ def all(self, *, _exclude: str = "") -> Iterator[Model]:
continue

log.debug(f"Found matching path {index + 1}: {filename}")
result = parse(pattern, filename)
result = parse(pattern, filename) or parse(alt_pattern, filename)
if result:
values = list(result.named.values())

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]

name = "datafiles"
version = "2.2.1"
version = "2.2.2"
description = "File-based ORM for dataclasses."

license = "MIT"
Expand Down

0 comments on commit e1d9ddf

Please sign in to comment.