Changeset View
Changeset View
Standalone View
Standalone View
swh/model/model.py
Show First 20 Lines • Show All 335 Lines • ▼ Show 20 Lines | data = attr.ib(type=Optional[bytes], | ||||
validator=attr.validators.optional([])) | validator=attr.validators.optional([])) | ||||
ctime = attr.ib(type=Optional[datetime.datetime], | ctime = attr.ib(type=Optional[datetime.datetime], | ||||
default=None) | default=None) | ||||
@length.validator | @length.validator | ||||
def check_length(self, attribute, value): | def check_length(self, attribute, value): | ||||
"""Checks the length is positive.""" | """Checks the length is positive.""" | ||||
if value < 0: | if self.status == 'absent' and value < -1: | ||||
raise ValueError('Length must be positive.') | raise ValueError('Length must be positive or -1.') | ||||
elif self.status != 'absent' and value < 0: | |||||
raise ValueError('Length must be positive, unless status=absent.') | |||||
@reason.validator | @reason.validator | ||||
def check_reason(self, attribute, value): | def check_reason(self, attribute, value): | ||||
"""Checks the reason is full iff status != absent.""" | """Checks the reason is full iff status != absent.""" | ||||
assert self.reason == value | assert self.reason == value | ||||
if self.status == 'absent' and value is None: | if self.status == 'absent' and value is None: | ||||
raise ValueError('Must provide a reason if content is absent.') | raise ValueError('Must provide a reason if content is absent.') | ||||
elif self.status != 'absent' and value is not None: | elif self.status != 'absent' and value is not None: | ||||
Show All 9 Lines |