Changeset View
Changeset View
Standalone View
Standalone View
swh/model/model.py
Show All 13 Lines | |||||
import dateutil.parser | import dateutil.parser | ||||
import iso8601 | import iso8601 | ||||
from typing_extensions import Final | from typing_extensions import Final | ||||
from .collections import ImmutableDict | from .collections import ImmutableDict | ||||
from .hashutil import DEFAULT_ALGORITHMS, MultiHash, hash_to_bytes | from .hashutil import DEFAULT_ALGORITHMS, MultiHash, hash_to_bytes | ||||
from .identifiers import ( | from .identifiers import ( | ||||
directory_identifier, | directory_identifier, | ||||
extid_identifier, | |||||
normalize_timestamp, | normalize_timestamp, | ||||
origin_identifier, | origin_identifier, | ||||
raw_extrinsic_metadata_identifier, | raw_extrinsic_metadata_identifier, | ||||
release_identifier, | release_identifier, | ||||
revision_identifier, | revision_identifier, | ||||
snapshot_identifier, | snapshot_identifier, | ||||
) | ) | ||||
from .identifiers import CoreSWHID | from .identifiers import CoreSWHID | ||||
▲ Show 20 Lines • Show All 552 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 490 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(HashableObject, BaseModel): | |||||
object_type: Final = "extid" | |||||
extid_type = attr.ib(type=str, validator=type_validator()) | |||||
extid = attr.ib(type=bytes, validator=type_validator()) | |||||
target = attr.ib(type=CoreSWHID, validator=type_validator()) | |||||
id = attr.ib(type=Sha1Git, validator=type_validator(), default=b"") | |||||
@classmethod | |||||
def from_dict(cls, d): | |||||
return super().from_dict( | |||||
dict( | |||||
extid=d["extid"], | |||||
extid_type=d["extid_type"], | |||||
target=CoreSWHID.from_string(d["target"]), | |||||
) | |||||
) | |||||
olasdUnsubmitted 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? | |||||
douarddaAuthorUnsubmitted Done Inline Actionspossibly "just" the result of several refactorings... lemme check douardda: possibly "just" the result of several refactorings... lemme check | |||||
def compute_hash(self) -> bytes: | |||||
return hash_to_bytes(extid_identifier(self.to_dict())) |
Not an enum?