Skip to content

Commit 60fc015

Browse files
committed
refactor: location transformers and Storage.prepare_location do not accept FileData anymore`
1 parent c317ad8 commit 60fc015

6 files changed

Lines changed: 20 additions & 22 deletions

File tree

docs/extending/location_transformers.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ unique identifiers.
1010

1111
A location transformer is a callable (usually a function) that takes the
1212
original [Location][file_keeper.Location] string, optional
13-
[Upload][file_keeper.Upload] or [FileData][file_keeper.FileData] object, and
14-
any extra data as input, and returns a modified
15-
[Location][file_keeper.Location] string. They provide a flexible way to
16-
customize how locations are handled by file-keeper.
13+
[Upload][file_keeper.Upload], and any extra data as input, and returns a
14+
modified [Location][file_keeper.Location] string. They provide a flexible way
15+
to customize how locations are handled by file-keeper.
1716

1817
Location transformers are set per-storage via
1918
[location_transformers][file_keeper.Settings.location_transformers] option. To
@@ -44,11 +43,12 @@ storage.upload(safe_location, ...)
4443

4544
### Define your transformer
4645

47-
Create a function that accepts the [Location][file_keeper.Location], optional [Upload][file_keeper.Upload] or [FileData][file_keeper.FileData], and
48-
`extras` as input and returns the transformed [Location][file_keeper.Location].
46+
Create a function that accepts the [Location][file_keeper.Location], optional
47+
[Upload][file_keeper.Upload], and `extras` as input and returns the transformed
48+
[Location][file_keeper.Location].
4949

5050
```python
51-
def my_location_transformer(location, data, extras):
51+
def my_location_transformer(location, upload_or_none, extras):
5252
# Perform custom transformation here
5353
return "prefix_" + location
5454
```

src/file_keeper/core/storage.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -605,13 +605,12 @@ def full_path(self, location: types.Location, /, **kwargs: Any) -> str:
605605
"""
606606
return os.path.join(self.settings.path, location)
607607

608-
def prepare_location(
609-
self, location: str, upload_or_data: data.BaseData | Upload | None = None, /, **kwargs: Any
610-
) -> types.Location:
608+
def prepare_location(self, location: str, sample: Upload | None = None, /, **kwargs: Any) -> types.Location:
611609
"""Transform and sanitize location using configured functions."""
612610
for name in self.settings.location_transformers:
613611
if transformer := location_transformers.get(name):
614-
location = transformer(location, upload_or_data, kwargs)
612+
location = transformer(location, sample, kwargs)
613+
615614
else:
616615
raise exceptions.LocationTransformerError(name)
617616

src/file_keeper/core/types.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
from typing import TYPE_CHECKING, Any, Literal, NewType, Protocol, TypeAlias
77

88
if TYPE_CHECKING:
9-
from .data import BaseData
109
from .upload import Upload
1110

1211
Location = NewType("Location", str)
1312

14-
LocationTransformer: TypeAlias = Callable[[str, "Upload | BaseData | None", "dict[str, Any]"], str]
13+
LocationTransformer: TypeAlias = Callable[[str, "Upload | None", "dict[str, Any]"], str]
1514

1615
SignedAction = Literal["upload", "download", "delete"]
1716

src/file_keeper/default/__init__.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def fix_extension_transformer(location: str, upload: Upload | BaseData | None, e
4646
When upload is not specified, transformer does nothing.
4747
"""
4848
if not upload:
49+
log.debug("Location %s remains unchanged because upload is not specified", location)
4950
return location
5051

5152
name = os.path.splitext(location)[0]
@@ -61,17 +62,17 @@ def fix_extension_transformer(location: str, upload: Upload | BaseData | None, e
6162
return location
6263

6364

64-
def safe_relative_path_transformer(location: str, upload: Upload | BaseData | None, extras: dict[str, Any]) -> str:
65+
def safe_relative_path_transformer(location: str, upload: Upload | None, extras: dict[str, Any]) -> str:
6566
"""Remove unsafe segments from path and strip leading slash."""
6667
return os.path.normpath(location).lstrip("./")
6768

6869

69-
def uuid_transformer(location: str, upload: Upload | BaseData | None, extras: dict[str, Any]) -> str:
70+
def uuid_transformer(location: str, upload: Upload | None, extras: dict[str, Any]) -> str:
7071
"""Transform location into random UUID."""
7172
return str(uuid.uuid4())
7273

7374

74-
def static_uuid_transformer(location: str, upload: Upload | BaseData | None, extras: dict[str, Any]) -> str:
75+
def static_uuid_transformer(location: str, upload: Upload | None, extras: dict[str, Any]) -> str:
7576
"""Transform location into static UUID.
7677
7778
The same location always transformed into the same UUID. This transformer
@@ -81,23 +82,23 @@ def static_uuid_transformer(location: str, upload: Upload | BaseData | None, ext
8182
return str(uuid.uuid5(FILE_KEEPER_DNS, location))
8283

8384

84-
def uuid_prefix_transformer(location: str, upload: Upload | BaseData | None, extras: dict[str, Any]) -> str:
85+
def uuid_prefix_transformer(location: str, upload: Upload | None, extras: dict[str, Any]) -> str:
8586
"""Prefix the location with random UUID."""
8687
return str(uuid.uuid4()) + location
8788

8889

89-
def uuid_with_extension_transformer(location: str, upload: Upload | BaseData | None, extras: dict[str, Any]) -> str:
90+
def uuid_with_extension_transformer(location: str, upload: Upload | None, extras: dict[str, Any]) -> str:
9091
"""Replace location with random UUID, but keep the original extension."""
9192
ext = os.path.splitext(location)[1]
9293
return str(uuid.uuid4()) + ext
9394

9495

95-
def datetime_prefix_transformer(location: str, upload: Upload | BaseData | None, extras: dict[str, Any]) -> str:
96+
def datetime_prefix_transformer(location: str, upload: Upload | None, extras: dict[str, Any]) -> str:
9697
"""Prefix location with current date-timestamp."""
9798
return datetime.now(timezone.utc).isoformat() + location
9899

99100

100-
def datetime_with_extension_transformer(location: str, upload: Upload | BaseData | None, extras: dict[str, Any]) -> str:
101+
def datetime_with_extension_transformer(location: str, upload: Upload | None, extras: dict[str, Any]) -> str:
101102
"""Replace location with current date-timestamp, but keep the extension."""
102103
ext = os.path.splitext(location)[1]
103104
return datetime.now(timezone.utc).isoformat() + ext

src/file_keeper/default/adapters/azure_blob.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ class Manager(fk.Manager):
141141
def copy(self, location: fk.Location, data: fk.FileData, extras: dict[str, Any]) -> fk.FileData:
142142
"""Copy a file to a new location."""
143143
src_filepath = self.storage.full_path(data.location)
144-
145144
blob = self.storage.settings.container.get_blob_client(src_filepath)
146145
if not blob.exists():
147146
raise fk.exc.MissingFileError(self.storage, data.location)

src/file_keeper/default/adapters/fs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def upload(self, location: fk.types.Location, upload: fk.Upload, extras: dict[st
8585
# --8<-- [end:uploader_impl_path]
8686

8787
# --8<-- [start:uploader_impl_check]
88-
if os.path.exists(dest) and not self.storage.settings.override_existing:
88+
if not self.storage.settings.override_existing and os.path.exists(dest):
8989
raise fk.exc.ExistingFileError(self.storage, location)
9090
# --8<-- [end:uploader_impl_check]
9191

0 commit comments

Comments
 (0)