Changeset View
Changeset View
Standalone View
Standalone View
swh/model/model.py
Show All 33 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 531 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 487 Lines • ▼ Show 20 Lines | def from_dict(cls, d): | ||||
} | } | ||||
swhid_keys = ("snapshot", "release", "revision", "directory") | swhid_keys = ("snapshot", "release", "revision", "directory") | ||||
for swhid_key in swhid_keys: | for swhid_key in swhid_keys: | ||||
if d.get(swhid_key): | if d.get(swhid_key): | ||||
d[swhid_key] = CoreSWHID.from_string(d[swhid_key]) | d[swhid_key] = CoreSWHID.from_string(d[swhid_key]) | ||||
return super().from_dict(d) | return super().from_dict(d) | ||||
@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=ExtendedSWHID, validator=type_validator()) | |||||
@classmethod | |||||
def from_dict(cls, d): | |||||
return super().from_dict( | |||||
dict( | |||||
extid=d["extid"], | |||||
extid_type=d["extid_type"], | |||||
target=ExtendedSWHID.from_string(d["target"]), | |||||
) | |||||
) | |||||
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 | |||||
def unique_key(self) -> KeyType: | |||||
return attr.astuple(self) |
Not an enum?