Changeset View
Changeset View
Standalone View
Standalone View
swh/model/identifiers.py
# Copyright (C) 2015-2019 The Software Heritage developers | # Copyright (C) 2015-2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import binascii | import binascii | ||||
import datetime | import datetime | ||||
import hashlib | import hashlib | ||||
from collections import namedtuple | |||||
from functools import lru_cache | from functools import lru_cache | ||||
from typing import Any, Dict, NamedTuple | |||||
from .exceptions import ValidationError | from .exceptions import ValidationError | ||||
from .fields.hashes import validate_sha1 | from .fields.hashes import validate_sha1 | ||||
from .hashutil import hash_git_data, hash_to_hex, MultiHash | from .hashutil import hash_git_data, hash_to_hex, MultiHash | ||||
ORIGIN = 'origin' | ORIGIN = 'origin' | ||||
SNAPSHOT = 'snapshot' | SNAPSHOT = 'snapshot' | ||||
REVISION = 'revision' | REVISION = 'revision' | ||||
RELEASE = 'release' | RELEASE = 'release' | ||||
DIRECTORY = 'directory' | DIRECTORY = 'directory' | ||||
CONTENT = 'content' | CONTENT = 'content' | ||||
PID_NAMESPACE = 'swh' | PID_NAMESPACE = 'swh' | ||||
PID_VERSION = 1 | PID_VERSION = 1 | ||||
PID_TYPES = ['ori', 'snp', 'rel', 'rev', 'dir', 'cnt'] | PID_TYPES = ['ori', 'snp', 'rel', 'rev', 'dir', 'cnt'] | ||||
PID_KEYS = ['namespace', 'scheme_version', 'object_type', 'object_id', | |||||
'metadata'] | |||||
PID_SEP = ':' | PID_SEP = ':' | ||||
PID_CTXT_SEP = ';' | PID_CTXT_SEP = ';' | ||||
@lru_cache() | @lru_cache() | ||||
def identifier_to_bytes(identifier): | def identifier_to_bytes(identifier): | ||||
"""Convert a text identifier to bytes. | """Convert a text identifier to bytes. | ||||
▲ Show 20 Lines • Show All 597 Lines • ▼ Show 20 Lines | _object_type_map = { | ||||
}, | }, | ||||
CONTENT: { | CONTENT: { | ||||
'short_name': 'cnt', | 'short_name': 'cnt', | ||||
'key_id': 'sha1_git' | 'key_id': 'sha1_git' | ||||
} | } | ||||
} | } | ||||
class PersistentId(namedtuple('PersistentId', PID_KEYS)): | class PersistentId(NamedTuple( | ||||
'PersistentId', [ | |||||
('namespace', str), | |||||
('scheme_version', int), | |||||
('object_type', str), | |||||
('object_id', str), | |||||
('metadata', Dict[str, Any]), | |||||
])): | |||||
""" | """ | ||||
vlorentz: Declare the NamedTuple in another class, and name it eg. `_PersistentId`.
Or just declare… | |||||
Named tuple holding the relevant info associated to a Software Heritage | Named tuple holding the relevant info associated to a Software Heritage | ||||
persistent identifier. | persistent identifier. | ||||
Args: | Args: | ||||
namespace (str): the namespace of the identifier, defaults to 'swh' | namespace (str): the namespace of the identifier, defaults to 'swh' | ||||
scheme_version (int): the scheme version of the identifier, | scheme_version (int): the scheme version of the identifier, | ||||
defaults to 1 | defaults to 1 | ||||
object_type (str): the type of object the identifier points to, | object_type (str): the type of object the identifier points to, | ||||
▲ Show 20 Lines • Show All 156 Lines • Show Last 20 Lines |
Declare the NamedTuple in another class, and name it eg. _PersistentId.
Or just declare attributes using the attr lib.