Changeset View
Changeset View
Standalone View
Standalone View
swh/model/model.py
Show First 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | |||||
@attr.s | @attr.s | ||||
class OriginVisit(BaseModel): | class OriginVisit(BaseModel): | ||||
"""Represents a visit of an origin at a given point in time, by a | """Represents a visit of an origin at a given point in time, by a | ||||
SWH loader.""" | SWH loader.""" | ||||
origin = attr.ib(type=Origin) | origin = attr.ib(type=Origin) | ||||
date = attr.ib(type=datetime.datetime) | date = attr.ib(type=datetime.datetime) | ||||
status = attr.ib( | |||||
type=str, | |||||
validator=attr.validators.in_(['ongoing', 'full', 'partial'])) | |||||
type = attr.ib(type=str) | |||||
snapshot = attr.ib(type=Sha1Git) | |||||
metadata = attr.ib(type=Optional[Dict[str, object]], | |||||
default=None) | |||||
visit = attr.ib(type=Optional[int], | visit = attr.ib(type=Optional[int], | ||||
validator=attr.validators.optional([])) | default=None) | ||||
"""Should not be set before calling 'origin_visit_add()'.""" | """Should not be set before calling 'origin_visit_add()'.""" | ||||
def to_dict(self): | def to_dict(self): | ||||
"""Serializes the date as a string and omits the visit id if it is | """Serializes the date as a string and omits the visit id if it is | ||||
`None`.""" | `None`.""" | ||||
ov = super().to_dict() | ov = super().to_dict() | ||||
if ov['visit'] is None: | if ov['visit'] is None: | ||||
del ov['visit'] | del ov['visit'] | ||||
return ov | return ov | ||||
@classmethod | @classmethod | ||||
def from_dict(cls, d): | def from_dict(cls, d): | ||||
"""Parses the date from a string, and accepts missing visit ids.""" | """Parses the date from a string, and accepts missing visit ids.""" | ||||
d = d.copy() | d = d.copy() | ||||
date = d.pop('date') | date = d.pop('date') | ||||
return cls( | return cls( | ||||
origin=Origin.from_dict(d.pop('origin')), | origin=Origin.from_dict(d.pop('origin')), | ||||
date=(date | date=(date | ||||
if isinstance(date, datetime.datetime) | if isinstance(date, datetime.datetime) | ||||
else dateutil.parser.parse(date)), | else dateutil.parser.parse(date)), | ||||
visit=d.get('visit')) | **d) | ||||
class TargetType(Enum): | class TargetType(Enum): | ||||
"""The type of content pointed to by a snapshot branch. Usually a | """The type of content pointed to by a snapshot branch. Usually a | ||||
revision or an alias.""" | revision or an alias.""" | ||||
CONTENT = 'content' | CONTENT = 'content' | ||||
DIRECTORY = 'directory' | DIRECTORY = 'directory' | ||||
REVISION = 'revision' | REVISION = 'revision' | ||||
▲ Show 20 Lines • Show All 236 Lines • Show Last 20 Lines |