Skip to content

Commit

Permalink
feat(client): add several classes about Diff
Browse files Browse the repository at this point in the history
  • Loading branch information
rexzheng324-c committed Aug 9, 2021
1 parent 6b7d2ca commit 1c82356
Showing 1 changed file with 224 additions and 0 deletions.
224 changes: 224 additions & 0 deletions tensorbay/client/diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
#!/usr/bin/env python3
#
# Copyright 2021 Graviti. Licensed under MIT License.
#

"""Class about the diff.
:class:`DiffBase` defines the basic structure of a diff.
:class:`DiffNotes` defines the basic structure of a brief diff of notes.
:class:`DiffCatalog` defines the basic structure of a brief diff of catalog.
:class:`DiffFile` defines the basic structure of a brief diff of data file.
:class:`DiffLabel` defines the basic structure of a brief diff of data label.
:class:`DiffSensor` defines the basic structure of a brief diff of sensor.
:class:`DiffData` defines the basic structure of a brief diff of data.
:class:`DiffSegment` defines the basic structure of a brief diff of a segment.
:class:`DiffDataset` defines the basic structure of a brief diff of a dataset.
"""

from typing import Any, Dict, List, Sequence, Tuple, Type, TypeVar, Union, overload

from ..utility import (
AttrsMixin,
NameMixin,
ReprMixin,
SortedNameList,
UserSequence,
attr,
camel,
common_loads,
)


class DiffBase(AttrsMixin, ReprMixin):
"""This class defines the basic structure of a diff.
Attributes:
action: The concrete action.
"""

_T = TypeVar("_T", bound="DiffBase")

_repr_attrs: Tuple[str, ...] = ("action",)

action: str = attr()

def __init__(self, action: str) -> None:
self.action = action

@classmethod
def loads(cls: Type[_T], contents: Dict[str, Any]) -> _T:
"""Loads a :class:`DiffBase` instance from the given contents.
Arguments:
contents: A dict containing all the information of the diff::
{
"action": <str>
}
Returns:
A :class:`DiffBase` instance containing all the information in the given contents.
"""
return common_loads(cls, contents)

def dumps(self) -> Dict[str, Any]:
"""Dumps all the information of the diff into a dict.
Returns:
A dict containing all the information of the diff::
{
"action": <str>
}
"""
return self._dumps()


class DiffNotes(DiffBase):
"""This class defines the basic structure of a brief diff of notes."""


class DiffCatalog(DiffBase):
"""This class defines the basic structure of a brief diff of catalog."""


class DiffFile(DiffBase):
"""This class defines the basic structure of a brief diff of data file."""


class DiffLabel(DiffBase):
"""This class defines the basic structure of a brief diff of data label."""


class DiffSensor(DiffBase):
"""This class defines the basic structure of a brief diff of sensor."""


class DiffData(DiffBase):
"""This class defines the basic structure of a diff statistic.
Attributes:
remote_path: The remote path.
action: The action of data.
file: The brief diff information of the file.
label: The brief diff information of the labels.
"""

_T = TypeVar("_T", bound="DiffData")

_repr_attrs = ("remote_path", "action")

remote_path: str = attr(key=camel)
file: DiffFile = attr()
label: DiffLabel = attr()

@classmethod
def loads(cls: Type[_T], contents: Dict[str, Any]) -> _T:
"""Loads a :class:`DiffData` instance from the given contents.
Arguments:
contents: A dict containing all the brief diff information of data::
{
"remotePath": <str>,
"action": <str>,
"file": {
"action": <str>
}
"label": {
"action": <str>
}
}
Returns:
A :class:`DiffData` instance containing all the information in the given contents.
"""
return common_loads(cls, contents)

def dumps(self) -> Dict[str, Any]:
"""Dumps all the brief diff information of data into a dict.
Returns:
A dict containing all the brief diff information of data::
{
"remotePath": <str>,
"action": <str>,
"file": {
"action": <str>
}
"label": {
"action": <str>
}
}
"""
return self._dumps()


class DiffSegment(UserSequence[DiffData], NameMixin):
"""This class defines the basic structure of a brief diff of a segment.
Arguments:
name: The segment name.
action: The action of a segment.
"""

def __init__(self, name: str, action: str) -> None:
super().__init__(name)

self.action = action
self._data: List[DiffData] = []


class DiffDataset(Sequence[DiffSegment], NameMixin): # pylint: disable=too-many-ancestors
"""This class defines the basic structure of a brief diff of a dataset.
Arguments:
name: The segment name.
action: The action of a segment.
"""

def __init__(self, name: str, action: str) -> None:
super().__init__(name)

self.action = action
self._segments: SortedNameList[DiffSegment] = SortedNameList()
self.notes: DiffNotes = DiffNotes("unchanged")
self.catalog: DiffCatalog = DiffCatalog("unchanged")

def __len__(self) -> int:
return self._segments.__len__()

@overload
def __getitem__(self, index: Union[int, str]) -> DiffSegment:
...

@overload
def __getitem__(self, index: slice) -> Sequence[DiffSegment]:
...

def __getitem__(
self, index: Union[int, str, slice]
) -> Union[Sequence[DiffSegment], DiffSegment]:
return self._segments.__getitem__(index)

def __contains__(self, key: Any) -> bool:
return self._segments.__contains__(key)

0 comments on commit 1c82356

Please sign in to comment.