-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add BinaryContent.from_path convenience method
#3482
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
Merged
+56
−0
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e810734
implement `FIlePart.from_path`
dsfaccini fa7aabf
add unknown extension
dsfaccini 47a3db3
Merge branch 'main' into filepart-from-path
dsfaccini aa11f5d
format
dsfaccini 9642d9e
remove Self
dsfaccini ae701f3
remove claudemd
dsfaccini aad356e
move classmethod to BinaryContent
dsfaccini 7038dcf
handle narrow type and test string path
dsfaccini 3ec7744
Merge branch 'main' into filepart-from-path
DouweM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ | |
| from dataclasses import KW_ONLY, dataclass, field, replace | ||
| from datetime import datetime | ||
| from mimetypes import guess_type | ||
| from os import PathLike | ||
| from pathlib import Path | ||
| from typing import TYPE_CHECKING, Annotated, Any, Literal, TypeAlias, cast, overload | ||
|
|
||
| import pydantic | ||
|
|
@@ -530,6 +532,25 @@ def from_data_uri(cls, data_uri: str) -> BinaryContent: | |
| media_type, data = data_uri[len(prefix) :].split(';base64,', 1) | ||
| return cls.narrow_type(cls(data=base64.b64decode(data), media_type=media_type)) | ||
|
|
||
| @classmethod | ||
| def from_path(cls, path: PathLike[str]) -> BinaryContent: | ||
| """Create a `BinaryContent` from a path. | ||
|
|
||
| Defaults to 'application/octet-stream' if the media type cannot be inferred. | ||
|
|
||
| Raises: | ||
| FileNotFoundError: if the file does not exist. | ||
| PermissionError: if the file cannot be read. | ||
| """ | ||
| path = Path(path) | ||
| if not path.exists(): | ||
| raise FileNotFoundError(f'File not found: {path}') | ||
| media_type, _ = guess_type(path.as_posix()) | ||
| if media_type is None: | ||
| media_type = 'application/octet-stream' | ||
|
|
||
| return cls(data=path.read_bytes(), media_type=media_type) | ||
|
||
|
|
||
| @pydantic.computed_field | ||
| @property | ||
| def identifier(self) -> str: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,5 +1,6 @@ | ||||||
| import sys | ||||||
| from datetime import datetime, timezone | ||||||
| from pathlib import Path | ||||||
|
|
||||||
| import pytest | ||||||
| from inline_snapshot import snapshot | ||||||
|
|
@@ -612,3 +613,22 @@ def test_binary_content_validation_with_optional_identifier(): | |||||
| 'identifier': 'foo', | ||||||
| } | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| def test_file_part_from_path(tmp_path: Path): | ||||||
|
||||||
| def test_file_part_from_path(tmp_path: Path): | |
| def test_binary_content_from_path(tmp_path: Path): |
DouweM marked this conversation as resolved.
Show resolved
Hide resolved
DouweM marked this conversation as resolved.
Show resolved
Hide resolved
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do we need the
as_posixfor?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought I had a linter error from the pathlib.Path type, probably was something else