src/pds_doi_service/core/outputs/datacite/datacite_web_parser.py
def parse_dois_from_label(label_text, content_type=CONTENT_TYPE_JSON):
        """
        Parses one or more Doi objects from the provided DataCite label.

        Parameters
        ----------
        label_text : str
            Text body of the label to parse.
        content_type : str
            The format of the label's content.

        Returns
        -------
        dois : list of Doi
            Doi objects parsed from the provided label.
        errors: list
            List of strings containing any errors encountered while parsing.

        """
        if content_type != CONTENT_TYPE_JSON:
            raise InputFormatException(
                f"Unexpected content type provided. Value must be one of the following: [{CONTENT_TYPE_JSON}]"
            )

        dois = []
        errors = []  # DataCite does not return error information in response

        datacite_records = json.loads(label_text)["data"]

        # DataCite can return multiple records in a list under the data key, or
        # a just a dictionary for a single record, make the loop work either way
        if not isinstance(datacite_records, list):
            datacite_records = [datacite_records]

        for index, datacite_record in enumerate(datacite_records):
            try:
                logger.info("Parsing record index %d", index)
                doi_fields = {}

                # Everything we care about in a DataCite response is under attributes
                datacite_record = datacite_record["attributes"]

                for mandatory_field in DOIDataCiteWebParser._mandatory_fields:
                    doi_fields[mandatory_field] = getattr(DOIDataCiteWebParser, f"_parse_{mandatory_field}")(
                        datacite_record
                    )
                    logger.debug("Parsed value %s for mandatory field %s", doi_fields[mandatory_field], mandatory_field)

                for optional_field in DOIDataCiteWebParser._optional_fields:
                    try:
                        parser = getattr(DOIDataCiteWebParser, f"_parse_{optional_field}")
                        parsed_value = parser(datacite_record)

                        if parsed_value is not None:
                            doi_fields[optional_field] = parsed_value
                            logger.debug("Parsed value %s for optional field %s", parsed_value, optional_field)
                    except UserWarning as warning:
                        logger.warning("Record %d: %s", index, str(warning))

                doi = Doi(**doi_fields)

                dois.append(doi)
            except InputFormatException as err:
                logger.warning(
                    "Failed to parse a DOI object from record index %d of the provided label, reason: %s",
                    index,
                    str(err),
                )
                continue

        logger.info("Parsed %d DOI objects from %d records", len(dois), len(datacite_records))

        return dois, errors

        Parses one or more Doi objects from the provided DataCite label.

        Parameters
        ----------
        label_text : str
            Text body of the label to parse.
        content_type : str
            The format of the label's content.

        Returns
        -------
        dois : list of Doi
            Doi objects parsed from the provided label.
        errors: list
            List of strings containing any errors encountered while parsing.

        
Parses one or more Doi objects from the provided DataCite label.
Parameters

label_text : str
Text body of the label to parse.
content_type : str
The format of the label's content.

Returns

dois : list of Doi
Doi objects parsed from the provided label.
errors: list
List of strings containing any errors encountered while parsing.
Parses one or more Doi objects from the provided DataCite label . Parameters label_text : str Text body of the label to parse . content_type : str The format of the label ' s content . Returns dois : list of Doi Doi objects parsed from the provided label . errors : list List of strings containing any errors encountered while parsing .
def parse_dois_from_label(label_text, content_type=CONTENT_TYPE_JSON):
        if content_type != CONTENT_TYPE_JSON:
            raise InputFormatException(
                f"Unexpected content type provided. Value must be one of the following: [{CONTENT_TYPE_JSON}]"
            )
        dois = []
        errors = []  
        datacite_records = json.loads(label_text)["data"]
        if not isinstance(datacite_records, list):
            datacite_records = [datacite_records]
        for index, datacite_record in enumerate(datacite_records):
            try:
                logger.info("Parsing record index %d", index)
                doi_fields = {}
                datacite_record = datacite_record["attributes"]
                for mandatory_field in DOIDataCiteWebParser._mandatory_fields:
                    doi_fields[mandatory_field] = getattr(DOIDataCiteWebParser, f"_parse_{mandatory_field}")(
                        datacite_record
                    )
                    logger.debug("Parsed value %s for mandatory field %s", doi_fields[mandatory_field], mandatory_field)
                for optional_field in DOIDataCiteWebParser._optional_fields:
                    try:
                        parser = getattr(DOIDataCiteWebParser, f"_parse_{optional_field}")
                        parsed_value = parser(datacite_record)
                        if parsed_value is not None:
                            doi_fields[optional_field] = parsed_value
                            logger.debug("Parsed value %s for optional field %s", parsed_value, optional_field)
                    except UserWarning as warning:
                        logger.warning("Record %d: %s", index, str(warning))
                doi = Doi(**doi_fields)
                dois.append(doi)
            except InputFormatException as err:
                logger.warning(
                    "Failed to parse a DOI object from record index %d of the provided label, reason: %s",
                    index,
                    str(err),
                )
                continue
        logger.info("Parsed %d DOI objects from %d records", len(dois), len(datacite_records))
        return dois, errors
def parse_dois_from_label ( label_text , content_type = CONTENT_TYPE_JSON ) : if content_type != CONTENT_TYPE_JSON : raise InputFormatException ( f"Unexpected content type provided. Value must be one of the following: [{CONTENT_TYPE_JSON}]" ) dois = [ ] errors = [ ] datacite_records = json . loads ( label_text ) [ "data" ] if not isinstance ( datacite_records , list ) : datacite_records = [ datacite_records ] for index , datacite_record in enumerate ( datacite_records ) : try : logger . info ( "Parsing record index %d" , index ) doi_fields = { } datacite_record = datacite_record [ "attributes" ] for mandatory_field in DOIDataCiteWebParser . _mandatory_fields : doi_fields [ mandatory_field ] = getattr ( DOIDataCiteWebParser , f"_parse_{mandatory_field}" ) ( datacite_record ) logger . debug ( "Parsed value %s for mandatory field %s" , doi_fields [ mandatory_field ] , mandatory_field ) for optional_field in DOIDataCiteWebParser . _optional_fields : try : parser = getattr ( DOIDataCiteWebParser , f"_parse_{optional_field}" ) parsed_value = parser ( datacite_record ) if parsed_value is not None : doi_fields [ optional_field ] = parsed_value logger . debug ( "Parsed value %s for optional field %s" , parsed_value , optional_field ) except UserWarning as warning : logger . warning ( "Record %d: %s" , index , str ( warning ) ) doi = Doi ( ** doi_fields ) dois . append ( doi ) except InputFormatException as err : logger . warning ( "Failed to parse a DOI object from record index %d of the provided label, reason: %s" , index , str ( err ) , ) continue logger . info ( "Parsed %d DOI objects from %d records" , len ( dois ) , len ( datacite_records ) ) return dois , errors
Parses one or more Doi objects from the provided DataCite label.
Parses one or more Doi objects from the provided DataCite label .
"""
        Parses one or more Doi objects from the provided DataCite label.

        Parameters
        ----------
        label_text : str
            Text body of the label to parse.
        content_type : str
            The format of the label's content.

        Returns
        -------
        dois : list of Doi
            Doi objects parsed from the provided label.
        errors: list
            List of strings containing any errors encountered while parsing.

        """ # DataCite does not return error information in response # DataCite can return multiple records in a list under the data key, or # a just a dictionary for a single record, make the loop work either way # Everything we care about in a DataCite response is under attributes
label_text
content_type