Changeset View
Changeset View
Standalone View
Standalone View
swh/model/model.py
Show All 29 Lines | |||||
class MissingData(Exception): | class MissingData(Exception): | ||||
"""Raised by `Content.with_data` when it has no way of fetching the | """Raised by `Content.with_data` when it has no way of fetching the | ||||
data (but not when fetching the data fails).""" | data (but not when fetching the data fails).""" | ||||
pass | pass | ||||
KeyType = Union[Dict[str, str], Dict[str, bytes], bytes] | KeyType = Union[Dict[str, str], Dict[str, bytes], Tuple[Union[str, bytes], ...], bytes] | ||||
"""The type returned by BaseModel.unique_key().""" | """The type returned by BaseModel.unique_key().""" | ||||
SHA1_SIZE = 20 | SHA1_SIZE = 20 | ||||
# TODO: Limit this to 20 bytes | # TODO: Limit this to 20 bytes | ||||
Sha1Git = bytes | Sha1Git = bytes | ||||
Sha1 = bytes | Sha1 = bytes | ||||
▲ Show 20 Lines • Show All 508 Lines • ▼ Show 20 Lines | def anonymize(self) -> "Revision": | ||||
self, author=self.author.anonymize(), committer=self.committer.anonymize() | self, author=self.author.anonymize(), committer=self.committer.anonymize() | ||||
) | ) | ||||
@attr.s(frozen=True, slots=True) | @attr.s(frozen=True, slots=True) | ||||
class DirectoryEntry(BaseModel): | class DirectoryEntry(BaseModel): | ||||
object_type: Final = "directory_entry" | object_type: Final = "directory_entry" | ||||
name = attr.ib(type=bytes, validator=type_validator()) | name = attr.ib(type=bytes, validator=type_validator()) | ||||
vlorentz: Not an enum? | |||||
Done Inline ActionsNot sure yet if we want an enum for this. So I started simple... douardda: Not sure yet if we want an enum for this. So I started simple... | |||||
type = attr.ib(type=str, validator=attr.validators.in_(["file", "dir", "rev"])) | type = attr.ib(type=str, validator=attr.validators.in_(["file", "dir", "rev"])) | ||||
target = attr.ib(type=Sha1Git, validator=type_validator()) | target = attr.ib(type=Sha1Git, validator=type_validator()) | ||||
Not Done Inline ActionsWhat about a SWHID instead? vlorentz: What about a SWHID instead? | |||||
Done Inline Actionswe do not (want) to support generic SWHID (with context), plus swhid is a textual representation that will need string manipulations / (en|de)coding, etc. so the idea was to stick, in the data model, to stricly what we need. douardda: we do not (want) to support generic SWHID (with context), plus swhid is a textual… | |||||
Not Done Inline Actions
neither does raw_extrinsic_metadata, there's a validator to check it has no context. vlorentz: > we do not (want) to support generic SWHID (with context)
neither does… | |||||
perms = attr.ib(type=int, validator=type_validator()) | perms = attr.ib(type=int, validator=type_validator()) | ||||
"""Usually one of the values of `swh.model.from_disk.DentryPerms`.""" | """Usually one of the values of `swh.model.from_disk.DentryPerms`.""" | ||||
@attr.s(frozen=True, slots=True) | @attr.s(frozen=True, slots=True) | ||||
class Directory(HashableObject, BaseModel): | class Directory(HashableObject, BaseModel): | ||||
object_type: Final = "directory" | object_type: Final = "directory" | ||||
▲ Show 20 Lines • Show All 493 Lines • ▼ Show 20 Lines | def unique_key(self) -> KeyType: | ||||
"type": self.type.value, | "type": self.type.value, | ||||
"target": str(self.target), | "target": str(self.target), | ||||
"authority_type": self.authority.type.value, | "authority_type": self.authority.type.value, | ||||
"authority_url": self.authority.url, | "authority_url": self.authority.url, | ||||
"discovery_date": str(self.discovery_date), | "discovery_date": str(self.discovery_date), | ||||
"fetcher_name": self.fetcher.name, | "fetcher_name": self.fetcher.name, | ||||
"fetcher_version": self.fetcher.version, | "fetcher_version": self.fetcher.version, | ||||
} | } | ||||
@attr.s(frozen=True, slots=True) | |||||
class ExtID(BaseModel): | |||||
object_type: Final = "extid" | |||||
extid = attr.ib(type=bytes, validator=type_validator()) | |||||
extid_type = attr.ib(type=str, validator=type_validator()) | |||||
target = attr.ib(type=Sha1Git, validator=type_validator()) | |||||
Not Done Inline ActionsYou're manually handling all the attributes, why not just call the class initializer directly? olasd: You're manually handling all the attributes, why not just call the class initializer directly? | |||||
Done Inline Actionspossibly "just" the result of several refactorings... lemme check douardda: possibly "just" the result of several refactorings... lemme check | |||||
target_type = attr.ib(type=ObjectType, validator=type_validator()) | |||||
@classmethod | |||||
def from_dict(cls, d): | |||||
return cls( | |||||
extid=d["extid"], | |||||
extid_type=d["extid_type"], | |||||
target=d["target"], | |||||
target_type=ObjectType(d["target_type"]), | |||||
) | |||||
def unique_key(self) -> KeyType: | |||||
return attr.astuple(self) |
Not an enum?