Skip to content
Snippets Groups Projects
Verified Commit bdf26f53 authored by Antoine R. Dumont's avatar Antoine R. Dumont
Browse files

swh.model.identifiers: persistent_identifier takes object as input

Related T335
parent 122326dd
No related branches found
Tags v0.0.21
No related merge requests found
...@@ -594,33 +594,55 @@ def snapshot_identifier(snapshot, *, ignore_unresolved=False): ...@@ -594,33 +594,55 @@ def snapshot_identifier(snapshot, *, ignore_unresolved=False):
return identifier_to_str(hash_git_data(b''.join(lines), 'snapshot')) return identifier_to_str(hash_git_data(b''.join(lines), 'snapshot'))
def persistent_identifier(type, hash): def persistent_identifier(type, object, version=1):
"""Compute persistent identifier as per the documentation. """Compute persistent identifier (stable over time) as per
documentation.
Source: https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html # noqa Documentation:
https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html # noqa
Args: Args:
type (str): Object type type (str): Object's type
hash (str): Object hash object (str): Object's dict representation
version (int): persistent identifier version (default to 1)
Returns: Returns:
Persistent identifier as string. Persistent identifier as string.
""" """
_map = { _map = {
SNAPSHOT: 'snp', SNAPSHOT: {
RELEASE: 'rel', 'short_name': 'snp',
REVISION: 'rev', 'key_id': 'id'
DIRECTORY: 'dir', },
CONTENT: 'cnt', RELEASE: {
'short_name': 'rel',
'key_id': 'id'
},
REVISION: {
'short_name': 'rev',
'key_id': 'id'
},
DIRECTORY: {
'short_name': 'dir',
'key_id': 'id'
},
CONTENT: {
'short_name': 'cnt',
'key_id': 'sha1_git'
},
} }
_hash = hash_to_hex(hash) o = _map[type]
_hash = hash_to_hex(object[o['key_id']])
return 'swh:%s:%s:%s' % (version, o['short_name'], _hash)
return 'swh:1:%s:%s' % (_map[type], _hash) PERSISTENT_IDENTIFIER_KEYS = [
'namespace', 'scheme_version', 'object_type', 'object_id']
def parse_persistent_identifier(persistent_id): def parse_persistent_identifier(persistent_id):
"""Parse swh's persistent identifier. """Parse swh's persistent identifier scheme.
Args: Args:
persistent_id (str): A persistent identifier persistent_id (str): A persistent identifier
...@@ -630,9 +652,4 @@ def parse_persistent_identifier(persistent_id): ...@@ -630,9 +652,4 @@ def parse_persistent_identifier(persistent_id):
""" """
data = persistent_id.split(':') data = persistent_id.split(':')
return { return dict(zip(PERSISTENT_IDENTIFIER_KEYS, data))
'namespace': data[0], # should be 'swh'
'scheme_version': data[1],
'object_type': data[2],
'object_id': data[3],
}
...@@ -773,39 +773,49 @@ class SnapshotIdentifier(unittest.TestCase): ...@@ -773,39 +773,49 @@ class SnapshotIdentifier(unittest.TestCase):
) )
def test_persistent_identifier(self): def test_persistent_identifier(self):
for full_type, _hash, expected_persistent_id in [ _snapshot = {'id': hashutil.hash_to_bytes(
(SNAPSHOT, hashutil.hash_to_bytes( 'c7c108084bc0bf3d81436bf980b46e98bd338453')}
'c7c108084bc0bf3d81436bf980b46e98bd338453'), _release = {'id': '22ece559cc7cc2364edc5e5593d63ae8bd229f9f'}
'swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453'), _revision = {'id': '309cf2674ee7a0749978cf8265ab91a60aea0f7d'}
(RELEASE, '22ece559cc7cc2364edc5e5593d63ae8bd229f9f', _directory = {'id': 'd198bc9d7a6bcf6db04f476d29314f157507d505'}
'swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f'), _content = {'sha1_git': '94a9ed024d3859793618152ea559a168bbcbb5e2'}
(REVISION, '309cf2674ee7a0749978cf8265ab91a60aea0f7d', for full_type, _hash, expected_persistent_id, version in [
'swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d'), (SNAPSHOT, _snapshot,
(DIRECTORY, 'd198bc9d7a6bcf6db04f476d29314f157507d505', 'swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453', None),
'swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505'), (RELEASE, _release,
(CONTENT, '94a9ed024d3859793618152ea559a168bbcbb5e2', 'swh:2:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f', 2),
'swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2') (REVISION, _revision,
'swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d', None),
(DIRECTORY, _directory,
'swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505', None),
(CONTENT, _content,
'swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2', 1)
]: ]:
actual_value = identifiers.persistent_identifier( if version:
full_type, _hash) actual_value = identifiers.persistent_identifier(
full_type, _hash, version)
else:
actual_value = identifiers.persistent_identifier(
full_type, _hash)
self.assertEquals(actual_value, expected_persistent_id) self.assertEquals(actual_value, expected_persistent_id)
def test_parse_persistent_identifier(self): def test_parse_persistent_identifier(self):
for pid, _type, _hash in [ for pid, _type, _version, _hash in [
('swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2', 'cnt', ('swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2', 'cnt',
'94a9ed024d3859793618152ea559a168bbcbb5e2'), '1', '94a9ed024d3859793618152ea559a168bbcbb5e2'),
('swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505', 'dir', ('swh:2:dir:d198bc9d7a6bcf6db04f476d29314f157507d505', 'dir',
'd198bc9d7a6bcf6db04f476d29314f157507d505'), '2', 'd198bc9d7a6bcf6db04f476d29314f157507d505'),
('swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d', 'rev', ('swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d', 'rev',
'309cf2674ee7a0749978cf8265ab91a60aea0f7d'), '1', '309cf2674ee7a0749978cf8265ab91a60aea0f7d'),
('swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f', 'rel', ('swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f', 'rel',
'22ece559cc7cc2364edc5e5593d63ae8bd229f9f'), '1', '22ece559cc7cc2364edc5e5593d63ae8bd229f9f'),
('swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453', 'snp', ('swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453', 'snp',
'c7c108084bc0bf3d81436bf980b46e98bd338453'), '1', 'c7c108084bc0bf3d81436bf980b46e98bd338453'),
]: ]:
expected_result = { expected_result = {
'namespace': 'swh', 'namespace': 'swh',
'scheme_version': '1', 'scheme_version': _version,
'object_type': _type, 'object_type': _type,
'object_id': _hash, 'object_id': _hash,
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment