-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Add content-type filter method to folder_paths #4054
Merged
comfyanonymous
merged 5 commits into
comfyanonymous:master
from
christian-byrne:filter-audio-formats
Sep 11, 2024
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b696e28
Add content-type filter method to folder_paths
christian-byrne dff9796
Add unit tests
christian-byrne c83fb8a
Hardcode webp content-type
christian-byrne 20815cb
Annotate content_types as Literal["image", "video", "audio"]
christian-byrne eac1f73
Merge branch 'master' into filter-audio-formats
comfyanonymous 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 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
This file contains 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
Empty file.
52 changes: 52 additions & 0 deletions
52
tests-unit/folder_paths_test/filter_by_content_types_test.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import pytest | ||
import os | ||
import tempfile | ||
from folder_paths import filter_files_content_types | ||
|
||
@pytest.fixture(scope="module") | ||
def file_extensions(): | ||
return { | ||
'image': ['bmp', 'cdr', 'gif', 'heif', 'ico', 'jpeg', 'jpg', 'pcx', 'png', 'pnm', 'ppm', 'psd', 'sgi', 'svg', 'tiff', 'webp', 'xbm', 'xcf', 'xpm'], | ||
'audio': ['aif', 'aifc', 'aiff', 'au', 'awb', 'flac', 'm4a', 'mp2', 'mp3', 'ogg', 'sd2', 'smp', 'snd', 'wav'], | ||
'video': ['avi', 'flv', 'm2v', 'm4v', 'mj2', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'ogv', 'qt', 'webm', 'wmv'] | ||
} | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def mock_dir(file_extensions): | ||
with tempfile.TemporaryDirectory() as directory: | ||
for content_type, extensions in file_extensions.items(): | ||
for extension in extensions: | ||
with open(f"{directory}/sample_{content_type}.{extension}", "w") as f: | ||
f.write(f"Sample {content_type} file in {extension} format") | ||
yield directory | ||
|
||
|
||
def test_categorizes_all_correctly(mock_dir, file_extensions): | ||
files = os.listdir(mock_dir) | ||
for content_type, extensions in file_extensions.items(): | ||
filtered_files = filter_files_content_types(files, [content_type]) | ||
for extension in extensions: | ||
assert f"sample_{content_type}.{extension}" in filtered_files | ||
|
||
|
||
def test_categorizes_all_uniquely(mock_dir, file_extensions): | ||
files = os.listdir(mock_dir) | ||
for content_type, extensions in file_extensions.items(): | ||
filtered_files = filter_files_content_types(files, [content_type]) | ||
assert len(filtered_files) == len(extensions) | ||
|
||
|
||
def test_handles_bad_extensions(): | ||
files = ["file1.txt", "file2.py", "file3.example", "file4.pdf", "file5.ini", "file6.doc", "file7.md"] | ||
assert filter_files_content_types(files, ["image", "audio", "video"]) == [] | ||
|
||
|
||
def test_handles_no_extension(): | ||
files = ["file1", "file2", "file3", "file4", "file5", "file6", "file7"] | ||
assert filter_files_content_types(files, ["image", "audio", "video"]) == [] | ||
|
||
|
||
def test_handles_no_files(): | ||
files = [] | ||
assert filter_files_content_types(files, ["image", "audio", "video"]) == [] |
Oops, something went wrong.
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.
Thanks for adding tests!