Skip to content

Commit 892abcf

Browse files
committed
add io functionality
1 parent 71dc65e commit 892abcf

File tree

7 files changed

+452
-28
lines changed

7 files changed

+452
-28
lines changed

bioimageio/spec/_internal/types/__init__.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99

1010
from bioimageio.spec._internal.constants import DOI_REGEX, SI_UNIT_REGEX
1111
from bioimageio.spec._internal.types._file_source import AbsoluteFilePath as AbsoluteFilePath
12+
from bioimageio.spec._internal.types._file_source import FileName as FileName
1213
from bioimageio.spec._internal.types._file_source import FileSource as FileSource
14+
from bioimageio.spec._internal.types._file_source import PermissiveFileSource as PermissiveFileSource
1315
from bioimageio.spec._internal.types._file_source import RelativeFilePath as RelativeFilePath
16+
from bioimageio.spec._internal.types._file_source import StrictFileSource as StrictFileSource
1417
from bioimageio.spec._internal.types._generated_spdx_license_type import DeprecatedLicenseId as DeprecatedLicenseId
1518
from bioimageio.spec._internal.types._generated_spdx_license_type import LicenseId as LicenseId
1619
from bioimageio.spec._internal.types._version import Version as Version
@@ -23,15 +26,15 @@
2326
validate_orcid_id,
2427
)
2528

26-
T = TypeVar("T")
2729
S = TypeVar("S", bound=Sequence[Any])
28-
2930
NotEmpty = Annotated[S, annotated_types.MinLen(1)]
3031

3132
Datetime = Annotated[datetime, BeforeValidator(validate_datetime)]
3233
"""Timestamp in [ISO 8601](#https://en.wikipedia.org/wiki/ISO_8601) format
3334
with a few restrictions listed [here](https://docs.python.org/3/library/datetime.html#datetime.datetime.fromisoformat)."""
3435

36+
Doi = NewType("Doi", Annotated[str, StringConstraints(pattern=DOI_REGEX)])
37+
FormatVersionPlaceholder = Literal["latest", "discover"]
3538
IdentifierStr = Annotated[ # allows to init child NewTypes with str
3639
NotEmpty[str],
3740
AfterValidator(validate_identifier),
@@ -40,6 +43,7 @@
4043
Identifier = NewType("Identifier", IdentifierStr)
4144
LowerCaseIdentifierStr = Annotated[IdentifierStr, annotated_types.LowerCase] # allows to init child NewTypes with str
4245
LowerCaseIdentifier = NewType("LowerCaseIdentifier", LowerCaseIdentifierStr)
46+
OrcidId = NewType("OrcidId", Annotated[str, AfterValidator(validate_orcid_id)])
4347
ResourceId = NewType(
4448
"ResourceId",
4549
Annotated[
@@ -48,11 +52,7 @@
4852
annotated_types.Predicate(lambda s: "\\" not in s and s[0] != "/" and s[-1] != "/"),
4953
],
5054
)
51-
DatasetId = NewType("DatasetId", ResourceId)
52-
FileName = str
53-
OrcidId = NewType("OrcidId", Annotated[str, AfterValidator(validate_orcid_id)])
5455
Sha256 = NewType("Sha256", Annotated[str, annotated_types.Len(64, 64)])
55-
5656
SiUnit = NewType(
5757
"SiUnit",
5858
Annotated[
@@ -62,8 +62,6 @@
6262
],
6363
)
6464
Unit = Union[Literal["arbitrary unit", "px"], SiUnit]
65-
FormatVersionPlaceholder = Literal["latest", "discover"]
66-
6765

6866
# types as loaded from YAML 1.2 (with ruamel.yaml)
6967
YamlLeafValue = Union[bool, date, datetime, float, int, str, None]
@@ -76,5 +74,6 @@
7674
# and don't open another issue a la https://github.com/pydantic/pydantic/issues/8021
7775
YamlValue = Union[YamlLeafValue, YamlArray, YamlMapping]
7876

77+
# derived types
78+
DatasetId = NewType("DatasetId", ResourceId)
7979
RdfContent = Dict[str, YamlValue]
80-
Doi = NewType("Doi", Annotated[str, StringConstraints(pattern=DOI_REGEX)])

bioimageio/spec/_internal/types/_file_source.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ def _check_exists(self, root: Union[DirectoryPath, AnyUrl]) -> None:
102102
raise ValueError(f"{p} does not point to an existing directory")
103103

104104

105+
FileName = str
105106
AbsoluteFilePath = Annotated[FilePath, Predicate(Path.is_absolute)]
106-
107107
FileSource = Union[HttpUrl, AbsoluteFilePath, RelativeFilePath]
108+
PermissiveFileSource = Union[FileSource, str]
109+
StrictFileSource = Union[HttpUrl, AbsoluteFilePath]

bioimageio/spec/_internal/utils.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from __future__ import annotations
22

33
import sys
4-
from pathlib import Path
4+
from pathlib import Path, PurePath
55
from typing import Any, Dict, Iterator, Tuple, Type, TypeVar, Union, get_args, get_origin
6+
from urllib.parse import urlparse
67

7-
from pydantic import BaseModel
8+
from pydantic import HttpUrl
89
from typing_extensions import Annotated
910

11+
from bioimageio.spec.typing import RelativeFilePath
12+
1013
K = TypeVar("K")
1114
V = TypeVar("V")
1215
NestedDict = Dict[K, "NestedDict[K, V] | V"]
@@ -94,4 +97,10 @@ def unindent(text: str, ignore_first_line: bool = False):
9497
return "\n".join(lines[:first] + [line[indent:] for line in lines[first:]])
9598

9699

97-
Model = TypeVar("Model", bound=BaseModel)
100+
def extract_file_name(src: Union[HttpUrl, PurePath, RelativeFilePath]) -> str:
101+
if isinstance(src, RelativeFilePath):
102+
return src.path.name
103+
elif isinstance(src, PurePath):
104+
return src.name
105+
else:
106+
return urlparse(str(src)).path.split("/")[-1]

bioimageio/spec/collection/v0_3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import ClassVar, List, Literal, Tuple, Union
1+
from typing import ClassVar, List, Literal, Union
22

33
from pydantic import Field, TypeAdapter, field_validator
44
from pydantic import HttpUrl as HttpUrl

0 commit comments

Comments
 (0)