Changeset View
Changeset View
Standalone View
Standalone View
swh/model/identifiers.py
Show First 20 Lines • Show All 689 Lines • ▼ Show 20 Lines | class PersistentId(_PersistentId): | ||||
__slots__ = () | __slots__ = () | ||||
def __new__(cls, namespace=PID_NAMESPACE, scheme_version=PID_VERSION, | def __new__(cls, namespace=PID_NAMESPACE, scheme_version=PID_VERSION, | ||||
object_type='', object_id='', metadata={}): | object_type='', object_id='', metadata={}): | ||||
o = _object_type_map.get(object_type) | o = _object_type_map.get(object_type) | ||||
if not o: | if not o: | ||||
raise ValidationError('Wrong input: Supported types are %s' % ( | raise ValidationError('Wrong input: Supported types are %s' % ( | ||||
list(_object_type_map.keys()))) | list(_object_type_map.keys()))) | ||||
if namespace != PID_NAMESPACE: | |||||
raise ValidationError( | |||||
"Wrong format: only supported namespace is '%s'" | |||||
% PID_NAMESPACE) | |||||
if scheme_version != PID_VERSION: | |||||
raise ValidationError( | |||||
'Wrong format: only supported version is %d' % PID_VERSION) | |||||
# internal swh representation resolution | # internal swh representation resolution | ||||
if isinstance(object_id, dict): | if isinstance(object_id, dict): | ||||
object_id = object_id[o['key_id']] | object_id = object_id[o['key_id']] | ||||
validate_sha1(object_id) # can raise if invalid hash | validate_sha1(object_id) # can raise if invalid hash | ||||
object_id = hash_to_hex(object_id) | object_id = hash_to_hex(object_id) | ||||
return super(cls, PersistentId).__new__( | return super(cls, PersistentId).__new__( | ||||
cls, namespace, scheme_version, object_type, object_id, metadata) | cls, namespace, scheme_version, object_type, object_id, metadata) | ||||
▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | def parse_persistent_identifier(persistent_id): | ||||
pid_data = persistent_id_parts.pop(0).split(':') | pid_data = persistent_id_parts.pop(0).split(':') | ||||
if len(pid_data) != 4: | if len(pid_data) != 4: | ||||
raise ValidationError( | raise ValidationError( | ||||
'Wrong format: There should be 4 mandatory values') | 'Wrong format: There should be 4 mandatory values') | ||||
# Checking for parsing errors | # Checking for parsing errors | ||||
_ns, _version, _type, _id = pid_data | _ns, _version, _type, _id = pid_data | ||||
if _ns != PID_NAMESPACE: | |||||
raise ValidationError( | |||||
"Wrong format: only supported namespace is '%s'" % PID_NAMESPACE) | |||||
if _version != str(PID_VERSION): | |||||
raise ValidationError( | |||||
'Wrong format: only supported version is %d' % PID_VERSION) | |||||
pid_data[1] = int(pid_data[1]) | pid_data[1] = int(pid_data[1]) | ||||
expected_types = PID_TYPES | |||||
if _type not in expected_types: | |||||
raise ValidationError( | |||||
'Wrong format: Supported types are %s' % ( | |||||
', '.join(expected_types))) | |||||
for otype, data in _object_type_map.items(): | for otype, data in _object_type_map.items(): | ||||
if _type == data['short_name']: | if _type == data['short_name']: | ||||
pid_data[2] = otype | pid_data[2] = otype | ||||
break | break | ||||
if not _id: | if not _id: | ||||
raise ValidationError( | raise ValidationError( | ||||
'Wrong format: Identifier should be present') | 'Wrong format: Identifier should be present') | ||||
try: | |||||
validate_sha1(_id) | |||||
except ValidationError: | |||||
raise ValidationError( | |||||
'Wrong format: Identifier should be a valid hash') | |||||
persistent_id_metadata = {} | persistent_id_metadata = {} | ||||
for part in persistent_id_parts: | for part in persistent_id_parts: | ||||
try: | try: | ||||
key, val = part.split('=') | key, val = part.split('=') | ||||
persistent_id_metadata[key] = val | persistent_id_metadata[key] = val | ||||
except Exception: | except Exception: | ||||
msg = 'Contextual data is badly formatted, form key=val expected' | msg = 'Contextual data is badly formatted, form key=val expected' | ||||
raise ValidationError(msg) | raise ValidationError(msg) | ||||
pid_data.append(persistent_id_metadata) | pid_data.append(persistent_id_metadata) | ||||
return PersistentId._make(pid_data) | return PersistentId(pid_data[0], pid_data[1], | ||||
pid_data[2], pid_data[3], pid_data[4]) | |||||
douardda: why this chunk? If using the (kinda private) _make method is bothering you, I would rather use… | |||||
Not Done Inline ActionsSorry for the mistake, i will update it. The problem is that when the persistent identifier is built using the _make method the ValidationError in the constructor is not raised DanSeraf: Sorry for the mistake, i will update it. The problem is that when the persistent identifier is… | |||||
Not Done Inline ActionsYeah, just use the * form here, which is more concise/elegant, and I think we're good with this diff. Thanks! zack: Yeah, just use the `*` form here, which is more concise/elegant, and I think we're good with… |
why this chunk? If using the (kinda private) _make method is bothering you, I would rather use return PersistentId(*pid_data)