Skip to content

Commit

Permalink
remove ...WithDefaults classes
Browse files Browse the repository at this point in the history
  • Loading branch information
FynnBe committed Apr 26, 2024
1 parent a7173d9 commit c91c85d
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 218 deletions.
4 changes: 2 additions & 2 deletions bioimageio_collection_backoffice/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from typing_extensions import Literal, assert_never

from ._settings import settings
from .db_structure.versions import PublishedVersionInfo, VersionsWithDefaults
from .db_structure.versions import PublishedVersionInfo, Versions
from .remote_collection import RemoteCollection
from .remote_resource import PublishedVersion
from .requests_utils import put_file_from_url, raise_for_status_discretely
Expand Down Expand Up @@ -144,7 +144,7 @@ def backup_published_version(
or "testing" in v.client.prefix
):
v.concept.extend_versions(
VersionsWithDefaults(
Versions(
doi=concept_doi,
published={
v.number: PublishedVersionInfo(
Expand Down
14 changes: 1 addition & 13 deletions bioimageio_collection_backoffice/db_structure/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
class Message(Node, frozen=True):
author: str
text: str
timestamp: datetime


class MessageWithDefaults(Message, frozen=True):
timestamp: datetime = datetime.now()


Expand All @@ -25,17 +21,9 @@ class Chat(Node, frozen=True):

file_name: ClassVar[str] = "chat.json"

messages: Sequence[Message]
messages: Sequence[Message] = Field(default_factory=list)
"""messages"""

def get_updated(self, update: Chat) -> Chat:
assert set(self.model_fields) == {"messages"}, set(self.model_fields)
return Chat(messages=list(self.messages) + list(update.messages))

@staticmethod
def get_class_with_defaults():
return ChatWithDefaults


class ChatWithDefaults(Chat, frozen=True):
messages: Sequence[Message] = Field(default_factory=list)
33 changes: 4 additions & 29 deletions bioimageio_collection_backoffice/db_structure/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,28 @@


class _LogEntryBase(Node, frozen=True):
timestamp: datetime
timestamp: datetime = datetime.now()
"""creation of log entry"""
log: Any
"""log content"""


class _LogEntryBaseWithDefaults(_LogEntryBase, frozen=True):
timestamp: datetime = datetime.now()
"""creation of log entry"""


class CollectionLog(_LogEntryBase, frozen=True):
log: str


class CollectionLogWithDefaults(_LogEntryBaseWithDefaults, CollectionLog, frozen=True):
pass


class BioimageioLog(_LogEntryBase, frozen=True):
log: ValidationSummary


class BioimageioLogWithDefaults(_LogEntryBaseWithDefaults, BioimageioLog, frozen=True):
pass


class Log(Node, frozen=True, extra="allow"):
"""`<id>/<version>/log.json` contains a version specific log"""

file_name: ClassVar[str] = "log.json"

bioimageio_spec: Sequence[BioimageioLog]
bioimageio_core: Sequence[BioimageioLog]
collection: Sequence[CollectionLog]
bioimageio_spec: Sequence[BioimageioLog] = Field(default_factory=list)
bioimageio_core: Sequence[BioimageioLog] = Field(default_factory=list)
collection: Sequence[CollectionLog] = Field(default_factory=list)

def get_updated(self, update: Log):
v: Union[Any, Sequence[Any]]
Expand All @@ -57,15 +44,3 @@ def get_updated(self, update: Log):
data[k] = list(old) + list(v)

return Log.model_validate(data)

@staticmethod
def get_class_with_defaults():
return LogWithDefaults


class LogWithDefaults(Log, frozen=True, extra="allow"):
"""`<id>/<version>/log.json` contains a version specific log"""

bioimageio_spec: Sequence[BioimageioLogWithDefaults] = Field(default_factory=list)
bioimageio_core: Sequence[BioimageioLogWithDefaults] = Field(default_factory=list)
collection: Sequence[CollectionLogWithDefaults] = Field(default_factory=list)
131 changes: 12 additions & 119 deletions bioimageio_collection_backoffice/db_structure/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,103 +17,56 @@


class _StatusBase(Node, frozen=True):
timestamp: datetime
run_url: Optional[str]


class _StatusBaseWithDefaults(Node, frozen=True):
timestamp: datetime = datetime.now()
run_url: Optional[str] = settings.run_url


class _StagedStatusBase(_StatusBase, frozen=True):
description: str
num_steps: int
num_steps: int = 6

@pydantic.model_validator(mode="after")
def _validate_num_steps(self):
assert self.num_steps >= getattr(self, "step", 0)
return self


class _StagedStatusBaseWithDefaults(
_StatusBaseWithDefaults, _StagedStatusBase, frozen=True
):
num_steps: int = 6


class UnpackingStatus(_StagedStatusBase, frozen=True):
name: Literal["unpacking"] = "unpacking"
step: Literal[1] = 1


class UnpackingStatusWithDefaults(
_StagedStatusBaseWithDefaults, UnpackingStatus, frozen=True
):
pass


class UnpackedStatus(_StagedStatusBase, frozen=True):
name: Literal["unpacked"] = "unpacked"
description: str
step: Literal[2] = 2


class UnpackedStatusWithDefaults(
_StagedStatusBaseWithDefaults, UnpackedStatus, frozen=True
):
description: str = "staging was successful; awaiting automated tests to start ⏳"
step: Literal[2] = 2


class TestingStatus(_StagedStatusBase, frozen=True):
name: Literal["testing"] = "testing"
step: Literal[3] = 3


class TestingStatusWithDefaults(
_StagedStatusBaseWithDefaults, TestingStatus, frozen=True
):
pass


class AwaitingReviewStatus(_StagedStatusBase, frozen=True):
name: Literal["awaiting review"] = "awaiting review"
description: str
step: Literal[4] = 4


class AwaitingReviewStatusWithDefaults(
_StagedStatusBaseWithDefaults, AwaitingReviewStatus, frozen=True
):
description: str = (
"Thank you for your contribution! 🎉"
"Our bioimage.io maintainers will take a look soon. 🦒"
)
step: Literal[4] = 4


class ChangesRequestedStatus(_StagedStatusBase, frozen=True):
name: Literal["changes requested"] = "changes requested"
step: Literal[5] = 5


class ChangesRequestedStatusWithDefaults(
_StagedStatusBaseWithDefaults, ChangesRequestedStatus, frozen=True
):
pass


class AcceptedStatus(_StagedStatusBase, frozen=True):
name: Literal["accepted"] = "accepted"
description: str
step: Literal[5] = 5


class AcceptedStatusWithDefaults(
_StagedStatusBaseWithDefaults, AcceptedStatus, frozen=True
):
description: str = (
"This staged version has been accepted by a bioimage.io maintainer and is about to be published."
)
step: Literal[5] = 5


class SupersededStatus(_StagedStatusBase, frozen=True):
Expand All @@ -124,27 +77,15 @@ class SupersededStatus(_StagedStatusBase, frozen=True):
by: StageNumber


class SupersededStatusWithDefaults(
_StagedStatusBaseWithDefaults, SupersededStatus, frozen=True
):
pass


class PublishedStagedStatus(_StagedStatusBase, frozen=True):
"""following `AcceptedStatus`"""

name: Literal["published"] = "published"
description: str
description: str = "published! 🎉"
step: Literal[6] = 6
publish_number: PublishNumber


class PublishedStagedStatusWithDefaults(
_StagedStatusBaseWithDefaults, PublishedStagedStatus, frozen=True
):
description: str = "published! 🎉"


StagedVersionStatus = Annotated[
Union[
UnpackingStatus,
Expand All @@ -158,34 +99,14 @@ class PublishedStagedStatusWithDefaults(
],
pydantic.Discriminator("name"),
]
StagedVersionStatusWithDefaults = Annotated[
Union[
UnpackingStatusWithDefaults,
UnpackedStatusWithDefaults,
TestingStatusWithDefaults,
AwaitingReviewStatusWithDefaults,
ChangesRequestedStatusWithDefaults,
AcceptedStatusWithDefaults,
SupersededStatusWithDefaults,
PublishedStagedStatusWithDefaults,
],
pydantic.Discriminator("name"),
]


class PublishedStatus(_StatusBase, frozen=True):
name: Literal["published"] = "published"
stage_number: StageNumber


class PublishedStatusWithDefaults(
_StatusBaseWithDefaults, PublishedStatus, frozen=True
):
pass


PulishedVersionStatus = PublishedStatus
PulishedVersionStatusWithDefaults = PublishedStatusWithDefaults


class ErrorStatus(_StatusBase, frozen=True):
Expand All @@ -197,11 +118,6 @@ class ErrorStatus(_StatusBase, frozen=True):


class VersionInfo(Node, frozen=True):
sem_ver: Optional[str]
timestamp: datetime


class VersionInfoWithDefaults(VersionInfo, frozen=True):
sem_ver: Optional[str] = None
timestamp: datetime = datetime.now()

Expand All @@ -210,13 +126,6 @@ class PublishedVersionInfo(VersionInfo, frozen=True):
status: Annotated[
Union[PulishedVersionStatus, ErrorStatus], pydantic.Discriminator("name")
]
doi: Optional[str]
"""version specific zenodo DOI"""


class PublishedVersionInfoWithDefaults(
VersionInfoWithDefaults, PublishedVersionInfo, frozen=True
):
doi: Optional[str] = None
"""version specific zenodo DOI"""

Expand All @@ -227,20 +136,18 @@ class StagedVersionInfo(VersionInfo, frozen=True):
]


class StagedVersionInfoWithDefaults(
VersionInfoWithDefaults, StagedVersionInfo, frozen=True
):
pass


class Versions(Node, frozen=True):
"""`<id>/versions.json` containing an overview of all published and staged resource versions"""

file_name: ClassVar[str] = "versions.json"

published: Mapping[PublishNumber, PublishedVersionInfo]
staged: Mapping[StageNumber, StagedVersionInfo]
doi: Optional[str]
published: Mapping[PublishNumber, PublishedVersionInfo] = pydantic.Field(
default_factory=dict
)
staged: Mapping[StageNumber, StagedVersionInfo] = pydantic.Field(
default_factory=dict
)
doi: Optional[str] = None

def get_updated(self, update: Versions) -> Versions:
if update.doi is None:
Expand All @@ -257,17 +164,3 @@ def get_updated(self, update: Versions) -> Versions:
staged={**self.staged, **update.staged},
doi=concept_doi,
)

@staticmethod
def get_class_with_defaults():
return VersionsWithDefaults


class VersionsWithDefaults(Versions, frozen=True):
published: Mapping[
PublishNumber, Union[PublishedVersionInfo, PublishedVersionInfoWithDefaults]
] = pydantic.Field(default_factory=dict)
staged: Mapping[
StageNumber, Union[StagedVersionInfo, StagedVersionInfoWithDefaults]
] = pydantic.Field(default_factory=dict)
doi: Optional[str] = None
Loading

0 comments on commit c91c85d

Please sign in to comment.