diff --git a/swh/model/cli.py b/swh/model/cli.py --- a/swh/model/cli.py +++ b/swh/model/cli.py @@ -57,22 +57,16 @@ def swhid_of_file(path) -> CoreSWHID: from swh.model.from_disk import Content - from swh.model.hashutil import hash_to_bytes - object = Content.from_file(path=path).get_data() - return CoreSWHID( - object_type=ObjectType.CONTENT, object_id=hash_to_bytes(object["sha1_git"]) - ) + object = Content.from_file(path=path) + return object.swhid def swhid_of_file_content(data) -> CoreSWHID: from swh.model.from_disk import Content - from swh.model.hashutil import hash_to_bytes - object = Content.from_bytes(mode=644, data=data).get_data() - return CoreSWHID( - object_type=ObjectType.CONTENT, object_id=hash_to_bytes(object["sha1_git"]) - ) + object = Content.from_bytes(mode=644, data=data) + return object.swhid def model_of_dir(path: bytes, exclude_patterns: Iterable[bytes] = None) -> Directory: @@ -88,13 +82,8 @@ def swhid_of_dir(path: bytes, exclude_patterns: Iterable[bytes] = None) -> CoreSWHID: - from swh.model.hashutil import hash_to_bytes - obj = model_of_dir(path, exclude_patterns) - - return CoreSWHID( - object_type=ObjectType.DIRECTORY, object_id=hash_to_bytes(obj.get_data()["id"]) - ) + return obj.swhid def swhid_of_origin(url): diff --git a/swh/model/from_disk.py b/swh/model/from_disk.py --- a/swh/model/from_disk.py +++ b/swh/model/from_disk.py @@ -19,7 +19,12 @@ from . import model from .exceptions import InvalidDirectoryPath 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_str as id_to_str from .merkle import MerkleLeaf, MerkleNode @@ -207,6 +212,14 @@ obj = cls(ret) return obj + @property + def swhid(self) -> CoreSWHID: + """return node identifier as a SWHID + """ + return CoreSWHID( + object_type=ObjectType[self.object_type.upper()], object_id=self.hash + ) + def __repr__(self): return "Content(id=%s)" % id_to_str(self.hash) @@ -482,6 +495,14 @@ return self.__entries + @property + def swhid(self) -> CoreSWHID: + """return node identifier as a SWHID + """ + return CoreSWHID( + object_type=ObjectType[self.object_type.upper()], object_id=self.hash + ) + def compute_hash(self): return id_to_bytes(directory_identifier({"entries": self.entries}))