Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/parsers.py
# Copyright (C) 2017-2018 The Software Heritage developers | # Copyright (C) 2017-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 | ||||
"""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 xmltodict | import xmltodict | ||||
from django.conf import settings | from django.conf import settings | ||||
from rest_framework.parsers import BaseParser | from rest_framework.parsers import BaseParser | ||||
from rest_framework.parsers import FileUploadParser | from rest_framework.parsers import FileUploadParser | ||||
from rest_framework.parsers import MultiPartParser | from rest_framework.parsers import MultiPartParser | ||||
from xml.parsers.expat import ExpatError | |||||
from swh.deposit.errors import ParserError | |||||
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 46 Lines • ▼ Show 20 Lines | |||||
def parse_xml(raw_content): | def parse_xml(raw_content): | ||||
"""Parse xml body. | """Parse xml body. | ||||
Args: | Args: | ||||
raw_content (bytes): The content to parse | raw_content (bytes): The content to parse | ||||
Raises: | |||||
ParserError in case of a malformed xml | |||||
Returns: | Returns: | ||||
content parsed as dict. | content parsed as dict. | ||||
""" | """ | ||||
try: | |||||
return SWHXMLParser().parse(raw_content) | return SWHXMLParser().parse(raw_content) | ||||
except ExpatError as e: | |||||
raise ParserError(str(e)) |