Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/parsers.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-2020 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 | ||||
"""Module in charge of defining parsers with SWORD 2.0 supported mediatypes. | """Module in charge of defining parsers with SWORD 2.0 supported mediatypes. | ||||
""" | """ | ||||
import logging | |||||
from typing import Dict, Optional, Union | from typing import Dict, Optional, Union | ||||
from xml.parsers.expat import ExpatError | from xml.parsers.expat import ExpatError | ||||
from django.conf import settings | from django.conf import settings | ||||
from rest_framework.parsers import BaseParser, FileUploadParser, MultiPartParser | from rest_framework.parsers import BaseParser, FileUploadParser, MultiPartParser | ||||
import xmltodict | import xmltodict | ||||
from swh.deposit.errors import ParserError | from swh.deposit.errors import ParserError | ||||
from swh.model.exceptions import ValidationError | from swh.model.exceptions import ValidationError | ||||
from swh.model.identifiers import ( | from swh.model.identifiers import ( | ||||
DIRECTORY, | DIRECTORY, | ||||
RELEASE, | RELEASE, | ||||
REVISION, | REVISION, | ||||
SNAPSHOT, | SNAPSHOT, | ||||
SWHID, | SWHID, | ||||
parse_swhid, | parse_swhid, | ||||
) | ) | ||||
logger = logging.getLogger(__name__) | |||||
class SWHFileUploadZipParser(FileUploadParser): | class SWHFileUploadZipParser(FileUploadParser): | ||||
"""File upload parser limited to zip archive. | """File upload parser limited to zip archive. | ||||
""" | """ | ||||
media_type = "application/zip" | media_type = "application/zip" | ||||
▲ Show 20 Lines • Show All 100 Lines • ▼ Show 20 Lines | def parse_swh_reference(metadata: Dict) -> Optional[Union[str, SWHID]]: | ||||
Raises: | Raises: | ||||
ValidationError in case the swhid referenced (if any) is invalid | ValidationError in case the swhid referenced (if any) is invalid | ||||
Returns: | Returns: | ||||
Either swhid or origin reference if any. None otherwise. | Either swhid or origin reference if any. None otherwise. | ||||
""" # noqa | """ # noqa | ||||
visit_swhid = None | |||||
anchor_swhid = None | |||||
swh_deposit = metadata.get("swh:deposit") | swh_deposit = metadata.get("swh:deposit") | ||||
if not swh_deposit: | if not swh_deposit: | ||||
return None | return None | ||||
swh_reference = swh_deposit.get("swh:reference") | swh_reference = swh_deposit.get("swh:reference") | ||||
if not swh_reference: | if not swh_reference: | ||||
return None | return None | ||||
Show All 26 Lines | if swhid_reference.metadata: | ||||
visit = swhid_reference.metadata.get("visit") | visit = swhid_reference.metadata.get("visit") | ||||
if visit: | if visit: | ||||
visit_swhid = parse_swhid(visit) | visit_swhid = parse_swhid(visit) | ||||
if visit_swhid.object_type != SNAPSHOT: | if visit_swhid.object_type != SNAPSHOT: | ||||
raise ValidationError( | raise ValidationError( | ||||
f"visit qualifier should be a core SWHID with type {SNAPSHOT}" | f"visit qualifier should be a core SWHID with type {SNAPSHOT}" | ||||
) | ) | ||||
if ( | |||||
visit_swhid | |||||
and anchor_swhid | |||||
and visit_swhid.object_type == SNAPSHOT | |||||
and anchor_swhid.object_type == SNAPSHOT | |||||
): | |||||
logger.warn( | |||||
"SWHID use of both anchor and visit targeting " | |||||
f"a snapshot: {swhid_reference}" | |||||
) | |||||
raise ValidationError( | |||||
"'anchor=swh:1:snp:' is not supported when 'visit' is also provided." | |||||
) | |||||
vlorentz: `'anchor=swh:1:snp:' is not supported when 'visit' is also provided.` | |||||
Done Inline Actionsthanks, clearer. ardumont: thanks, clearer. | |||||
return swhid_reference | return swhid_reference |
'anchor=swh:1:snp:' is not supported when 'visit' is also provided.