Changeset View
Changeset View
Standalone View
Standalone View
swh/model/from_disk.py
Show All 13 Lines | |||||||||||||
import attr | import attr | ||||||||||||
from attrs_strict import type_validator | from attrs_strict import type_validator | ||||||||||||
from typing_extensions import Final | from typing_extensions import Final | ||||||||||||
from . import model | from . import model | ||||||||||||
from .exceptions import InvalidDirectoryPath | from .exceptions import InvalidDirectoryPath | ||||||||||||
from .hashutil import MultiHash | from .hashutil import MultiHash | ||||||||||||
from .identifiers import directory_entry_sort_key, directory_identifier | from .identifiers import ( | ||||||||||||
CoreSWHID, | |||||||||||||
ObjectType, | |||||||||||||
directory_entry_sort_key, | |||||||||||||
directory_identifier, | |||||||||||||
) | |||||||||||||
from .identifiers import identifier_to_bytes as id_to_bytes | from .identifiers import identifier_to_bytes as id_to_bytes | ||||||||||||
from .identifiers import identifier_to_str as id_to_str | from .identifiers import identifier_to_str as id_to_str | ||||||||||||
from .merkle import MerkleLeaf, MerkleNode | from .merkle import MerkleLeaf, MerkleNode | ||||||||||||
@attr.s(frozen=True, slots=True) | @attr.s(frozen=True, slots=True) | ||||||||||||
class DiskBackedContent(model.BaseContent): | class DiskBackedContent(model.BaseContent): | ||||||||||||
"""Content-like class, which allows lazy-loading data from the disk.""" | """Content-like class, which allows lazy-loading data from the disk.""" | ||||||||||||
▲ Show 20 Lines • Show All 171 Lines • ▼ Show 20 Lines | def from_file(cls, *, path, max_content_length=None): | ||||||||||||
ret["path"] = path | ret["path"] = path | ||||||||||||
ret["perms"] = mode_to_perms(mode) | ret["perms"] = mode_to_perms(mode) | ||||||||||||
ret["length"] = length | ret["length"] = length | ||||||||||||
obj = cls(ret) | obj = cls(ret) | ||||||||||||
return obj | return obj | ||||||||||||
def swhid(self) -> CoreSWHID: | |||||||||||||
zack: you need a docstring here, like "return node identifier as a SWHID" | |||||||||||||
"""Return node identifier as a SWHID | |||||||||||||
Not Done Inline ActionsReturn (capitalize first word for consistency with other docstrings) anlambert: **R**eturn (capitalize first word for consistency with other docstrings) | |||||||||||||
""" | |||||||||||||
return CoreSWHID( | |||||||||||||
object_type=ObjectType.CONTENT, object_id=self.hash | |||||||||||||
) | |||||||||||||
Not Done Inline Actions
vlorentz: | |||||||||||||
def __repr__(self): | def __repr__(self): | ||||||||||||
return "Content(id=%s)" % id_to_str(self.hash) | return "Content(id=%s)" % id_to_str(self.hash) | ||||||||||||
def compute_hash(self): | def compute_hash(self): | ||||||||||||
return self.data["sha1_git"] | return self.data["sha1_git"] | ||||||||||||
def to_model(self) -> model.BaseContent: | def to_model(self) -> model.BaseContent: | ||||||||||||
"""Builds a `model.BaseContent` object based on this leaf.""" | """Builds a `model.BaseContent` object based on this leaf.""" | ||||||||||||
▲ Show 20 Lines • Show All 259 Lines • ▼ Show 20 Lines | def entries(self): | ||||||||||||
self.child_to_directory_entry(name, child) | self.child_to_directory_entry(name, child) | ||||||||||||
for name, child in self.items() | for name, child in self.items() | ||||||||||||
), | ), | ||||||||||||
key=directory_entry_sort_key, | key=directory_entry_sort_key, | ||||||||||||
) | ) | ||||||||||||
return self.__entries | return self.__entries | ||||||||||||
def swhid(self) -> CoreSWHID: | |||||||||||||
"""Return node identifier as a SWHID | |||||||||||||
Not Done Inline Actionssame here anlambert: same here | |||||||||||||
""" | |||||||||||||
Not Done Inline Actionsbetter docstring: "return node identifier as a SWHID" zack: better docstring: "return node identifier as a SWHID" | |||||||||||||
return CoreSWHID( | |||||||||||||
object_type=ObjectType.DIRECTORY, object_id=self.hash | |||||||||||||
) | |||||||||||||
Not Done Inline Actions
vlorentz: | |||||||||||||
def compute_hash(self): | def compute_hash(self): | ||||||||||||
return id_to_bytes(directory_identifier({"entries": self.entries})) | return id_to_bytes(directory_identifier({"entries": self.entries})) | ||||||||||||
def to_model(self) -> model.Directory: | def to_model(self) -> model.Directory: | ||||||||||||
"""Builds a `model.Directory` object based on this node; | """Builds a `model.Directory` object based on this node; | ||||||||||||
ignoring its children.""" | ignoring its children.""" | ||||||||||||
return model.Directory.from_dict(self.get_data()) | return model.Directory.from_dict(self.get_data()) | ||||||||||||
▲ Show 20 Lines • Show All 48 Lines • Show Last 20 Lines |
you need a docstring here, like "return node identifier as a SWHID"