Skip to content

Commit

Permalink
Merge pull request #558 from bioimage-io/fix-win-line-endings
Browse files Browse the repository at this point in the history
convert line endings to unix
  • Loading branch information
FynnBe authored Mar 14, 2024
2 parents bf71654 + 638dffe commit e086210
Show file tree
Hide file tree
Showing 18 changed files with 1,195 additions and 1,195 deletions.
26 changes: 13 additions & 13 deletions bioimageio/spec/_internal/io_basics.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from pathlib import Path

from annotated_types import Predicate
from pydantic import DirectoryPath, FilePath
from typing_extensions import Annotated

FileName = str
AbsoluteDirectory = Annotated[DirectoryPath, Predicate(Path.is_absolute)]
AbsoluteFilePath = Annotated[FilePath, Predicate(Path.is_absolute)]

BIOIMAGEIO_YAML = "rdf.yaml"
ALTERNATIVE_BIOIMAGEIO_YAML_NAMES = ("bioimageio.yaml", "model.yaml")
ALL_BIOIMAGEIO_YAML_NAMES = (BIOIMAGEIO_YAML,) + ALTERNATIVE_BIOIMAGEIO_YAML_NAMES
from pathlib import Path

from annotated_types import Predicate
from pydantic import DirectoryPath, FilePath
from typing_extensions import Annotated

FileName = str
AbsoluteDirectory = Annotated[DirectoryPath, Predicate(Path.is_absolute)]
AbsoluteFilePath = Annotated[FilePath, Predicate(Path.is_absolute)]

BIOIMAGEIO_YAML = "rdf.yaml"
ALTERNATIVE_BIOIMAGEIO_YAML_NAMES = ("bioimageio.yaml", "model.yaml")
ALL_BIOIMAGEIO_YAML_NAMES = (BIOIMAGEIO_YAML,) + ALTERNATIVE_BIOIMAGEIO_YAML_NAMES
104 changes: 52 additions & 52 deletions bioimageio/spec/_internal/packaging_context.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
from __future__ import annotations

from contextvars import ContextVar, Token
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Union

from .io_basics import AbsoluteFilePath, FileName
from .url import HttpUrl


@dataclass(frozen=True)
class PackagingContext:
_context_tokens: "List[Token[Optional[PackagingContext]]]" = field(
init=False, default_factory=list
)

bioimageio_yaml_file_name: FileName

file_sources: Dict[FileName, Union[AbsoluteFilePath, HttpUrl]] = field(
default_factory=dict
)
"""File sources to include in the packaged resource"""

def replace(
self,
*,
bioimageio_yaml_file_name: Optional[FileName] = None,
file_sources: Optional[Dict[FileName, Union[AbsoluteFilePath, HttpUrl]]] = None,
) -> "PackagingContext":
"""return a modiefied copy"""
return PackagingContext(
bioimageio_yaml_file_name=(
self.bioimageio_yaml_file_name
if bioimageio_yaml_file_name is None
else bioimageio_yaml_file_name
),
file_sources=(
dict(self.file_sources) if file_sources is None else file_sources
),
)

def __enter__(self):
self._context_tokens.append(packaging_context_var.set(self))
return self

def __exit__(self, type, value, traceback): # type: ignore
packaging_context_var.reset(self._context_tokens.pop(-1))


packaging_context_var: ContextVar[Optional[PackagingContext]] = ContextVar(
"packaging_context_var", default=None
)
from __future__ import annotations

from contextvars import ContextVar, Token
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Union

from .io_basics import AbsoluteFilePath, FileName
from .url import HttpUrl


@dataclass(frozen=True)
class PackagingContext:
_context_tokens: "List[Token[Optional[PackagingContext]]]" = field(
init=False, default_factory=list
)

bioimageio_yaml_file_name: FileName

file_sources: Dict[FileName, Union[AbsoluteFilePath, HttpUrl]] = field(
default_factory=dict
)
"""File sources to include in the packaged resource"""

def replace(
self,
*,
bioimageio_yaml_file_name: Optional[FileName] = None,
file_sources: Optional[Dict[FileName, Union[AbsoluteFilePath, HttpUrl]]] = None,
) -> "PackagingContext":
"""return a modiefied copy"""
return PackagingContext(
bioimageio_yaml_file_name=(
self.bioimageio_yaml_file_name
if bioimageio_yaml_file_name is None
else bioimageio_yaml_file_name
),
file_sources=(
dict(self.file_sources) if file_sources is None else file_sources
),
)

def __enter__(self):
self._context_tokens.append(packaging_context_var.set(self))
return self

def __exit__(self, type, value, traceback): # type: ignore
packaging_context_var.reset(self._context_tokens.pop(-1))


packaging_context_var: ContextVar[Optional[PackagingContext]] = ContextVar(
"packaging_context_var", default=None
)
196 changes: 98 additions & 98 deletions bioimageio/spec/_internal/validation_context.py
Original file line number Diff line number Diff line change
@@ -1,98 +1,98 @@
from __future__ import annotations

from contextvars import ContextVar, Token
from dataclasses import dataclass, field
from pathlib import Path
from typing import List, Optional, Union
from urllib.parse import urlsplit, urlunsplit

from pydantic import DirectoryPath

from ._settings import settings
from .io_basics import AbsoluteDirectory, FileName
from .root_url import RootHttpUrl
from .warning_levels import WarningLevel


@dataclass(frozen=True)
class ValidationContext:
_context_tokens: "List[Token[ValidationContext]]" = field(
init=False, default_factory=list
)

root: Union[RootHttpUrl, AbsoluteDirectory] = Path()
"""url/directory serving as base to resolve any relative file paths"""

warning_level: WarningLevel = 50
"""raise warnings of severity `s` as validation errors if `s >= warning_level`"""

log_warnings: bool = settings.log_warnings
"""if `True` log warnings that are not raised to the console"""

file_name: Optional[FileName] = None
"""file name of the bioimageio Yaml file"""

perform_io_checks: bool = settings.perform_io_checks
"""wether or not to perform validation that requires file io,
e.g. downloading a remote files.
Existence of local absolute file paths is still being checked."""

def replace(
self,
root: Optional[Union[RootHttpUrl, DirectoryPath]] = None,
warning_level: Optional[WarningLevel] = None,
log_warnings: Optional[bool] = None,
file_name: Optional[str] = None,
perform_io_checks: Optional[bool] = None,
) -> "ValidationContext":
return ValidationContext(
root=self.root if root is None else root,
warning_level=(
self.warning_level if warning_level is None else warning_level
),
log_warnings=self.log_warnings if log_warnings is None else log_warnings,
file_name=self.file_name if file_name is None else file_name,
perform_io_checks=(
self.perform_io_checks
if perform_io_checks is None
else perform_io_checks
),
)

def __enter__(self):
self._context_tokens.append(validation_context_var.set(self))
return self

def __exit__(self, type, value, traceback): # type: ignore
validation_context_var.reset(self._context_tokens.pop(-1))

@property
def source_name(self) -> str:
if self.file_name is None:
return "in-memory"
else:
try:
if isinstance(self.root, Path):
source = (self.root / self.file_name).absolute()
else:
parsed = urlsplit(str(self.root))
path = list(parsed.path.strip("/").split("/")) + [self.file_name]
source = urlunsplit(
(
parsed.scheme,
parsed.netloc,
"/".join(path),
parsed.query,
parsed.fragment,
)
)
except ValueError:
return self.file_name
else:
return str(source)


validation_context_var: ContextVar[ValidationContext] = ContextVar(
"validation_context_var", default=ValidationContext()
)
from __future__ import annotations

from contextvars import ContextVar, Token
from dataclasses import dataclass, field
from pathlib import Path
from typing import List, Optional, Union
from urllib.parse import urlsplit, urlunsplit

from pydantic import DirectoryPath

from ._settings import settings
from .io_basics import AbsoluteDirectory, FileName
from .root_url import RootHttpUrl
from .warning_levels import WarningLevel


@dataclass(frozen=True)
class ValidationContext:
_context_tokens: "List[Token[ValidationContext]]" = field(
init=False, default_factory=list
)

root: Union[RootHttpUrl, AbsoluteDirectory] = Path()
"""url/directory serving as base to resolve any relative file paths"""

warning_level: WarningLevel = 50
"""raise warnings of severity `s` as validation errors if `s >= warning_level`"""

log_warnings: bool = settings.log_warnings
"""if `True` log warnings that are not raised to the console"""

file_name: Optional[FileName] = None
"""file name of the bioimageio Yaml file"""

perform_io_checks: bool = settings.perform_io_checks
"""wether or not to perform validation that requires file io,
e.g. downloading a remote files.
Existence of local absolute file paths is still being checked."""

def replace(
self,
root: Optional[Union[RootHttpUrl, DirectoryPath]] = None,
warning_level: Optional[WarningLevel] = None,
log_warnings: Optional[bool] = None,
file_name: Optional[str] = None,
perform_io_checks: Optional[bool] = None,
) -> "ValidationContext":
return ValidationContext(
root=self.root if root is None else root,
warning_level=(
self.warning_level if warning_level is None else warning_level
),
log_warnings=self.log_warnings if log_warnings is None else log_warnings,
file_name=self.file_name if file_name is None else file_name,
perform_io_checks=(
self.perform_io_checks
if perform_io_checks is None
else perform_io_checks
),
)

def __enter__(self):
self._context_tokens.append(validation_context_var.set(self))
return self

def __exit__(self, type, value, traceback): # type: ignore
validation_context_var.reset(self._context_tokens.pop(-1))

@property
def source_name(self) -> str:
if self.file_name is None:
return "in-memory"
else:
try:
if isinstance(self.root, Path):
source = (self.root / self.file_name).absolute()
else:
parsed = urlsplit(str(self.root))
path = list(parsed.path.strip("/").split("/")) + [self.file_name]
source = urlunsplit(
(
parsed.scheme,
parsed.netloc,
"/".join(path),
parsed.query,
parsed.fragment,
)
)
except ValueError:
return self.file_name
else:
return str(source)


validation_context_var: ContextVar[ValidationContext] = ContextVar(
"validation_context_var", default=ValidationContext()
)
Loading

0 comments on commit e086210

Please sign in to comment.