-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from ArcanaFramework/touch-ups
Handle Self in extras signature matching
- Loading branch information
Showing
9 changed files
with
108 additions
and
9 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
if __name__.startswith("fileformats."): | ||
from . import converters # noqa: F401 |
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,34 @@ | ||
import tempfile | ||
from pathlib import Path | ||
import typing as ty | ||
import pydra.mark | ||
from fileformats.core import converter, FileSet | ||
from fileformats.generic import DirectoryOf, SetOf, TypedDirectory, TypedSet | ||
|
||
T = FileSet.type_var("T") | ||
|
||
|
||
@converter(target_format=SetOf[T], source_format=DirectoryOf[T]) # type: ignore[misc] | ||
@pydra.mark.task # type: ignore[misc] | ||
@pydra.mark.annotate({"return": {"out_file": TypedSet}}) # type: ignore[misc] | ||
def list_dir_contents(in_file: TypedDirectory) -> TypedSet: | ||
classified_set: ty.Type[TypedSet] = SetOf.__class_getitem__(*in_file.content_types) # type: ignore[assignment, arg-type] | ||
return classified_set(in_file.contents) | ||
|
||
|
||
@converter(target_format=DirectoryOf[T], source_format=SetOf[T]) # type: ignore[misc] | ||
@pydra.mark.annotate({"return": {"out_file": TypedDirectory}}) # type: ignore[misc] | ||
@pydra.mark.task # type: ignore[misc] | ||
def put_contents_in_dir( | ||
in_file: TypedSet, | ||
out_dir: ty.Optional[Path] = None, | ||
copy_mode: FileSet.CopyMode = FileSet.CopyMode.copy, | ||
) -> TypedDirectory: | ||
if out_dir is None: | ||
out_dir = Path(tempfile.mkdtemp()) | ||
for fset in in_file.contents: | ||
fset.copy(out_dir, mode=copy_mode) | ||
classified_dir: ty.Type[TypedDirectory] = DirectoryOf.__class_getitem__( # type: ignore[assignment] | ||
*in_file.content_types # type: ignore[arg-type] | ||
) | ||
return classified_dir(out_dir) |
36 changes: 36 additions & 0 deletions
36
extras/fileformats/extras/generic/tests/test_converters.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,36 @@ | ||
import pytest | ||
from pathlib import Path | ||
import typing as ty | ||
from fileformats.generic import DirectoryOf, SetOf | ||
from fileformats.text import TextFile | ||
|
||
|
||
@pytest.fixture | ||
def file_names() -> ty.List[str]: | ||
return ["file1.txt", "file2.txt", "file3.txt"] | ||
|
||
|
||
@pytest.fixture | ||
def files_dir(file_names: ty.List[str], tmp_path: Path) -> Path: | ||
out_dir = tmp_path / "out" | ||
out_dir.mkdir() | ||
for fname in file_names: | ||
(out_dir / fname).write_text(fname) | ||
return out_dir | ||
|
||
|
||
@pytest.fixture | ||
def test_files(files_dir: Path, file_names: ty.List[str]) -> ty.List[Path]: | ||
return [files_dir / n for n in file_names] | ||
|
||
|
||
def test_list_dir_contents(files_dir: Path, test_files: ty.List[Path]) -> None: | ||
text_set = SetOf[TextFile].convert(DirectoryOf[TextFile](files_dir)) # type: ignore[misc] | ||
assert sorted(t.fspath for t in text_set.contents) == test_files | ||
|
||
|
||
def test_put_contents_in_dir( | ||
file_names: ty.List[str], test_files: ty.List[Path] | ||
) -> None: | ||
text_dir = DirectoryOf[TextFile].convert(SetOf[TextFile](test_files)) # type: ignore[misc] | ||
assert sorted(t.name for t in text_dir.contents) == file_names |
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
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
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