Skip to content

Datacite

Datacite plugin provides Package model and converters between Datacite and Data Package notations

Installation

Not extra dependencies are required

Usage

Converting a Datacite descriptor to the Data Package notation:

from dplib.plugins.datacite.models import DatacitePackage

package = DatacitePackage.from_path("data/plugins/datacite/package.json").to_dp()
print(package.to_text(format='json'))
{
  "id": "https://doi.org/https://doi.org/10.1234/example-full",
  "title": "Full DataCite XML Example",
  "description": "XML example of all DataCite Metadata Schema v4.3 properties.",
  "homepage": "https://schema.datacite.org/meta/kernel-4.3/example/datacite-example-full-v4.3.xml",
  "version": "4.2",
  "licenses": [
    {
      "path": "http://creativecommons.org/publicdomain/zero/1.0",
      "title": "Creative Commons Zero v1.0 Universal"
    }
  ],
  "contributors": [
    {
      "title": "Miller, Elizabeth",
      "role": "creator",
      "organization": "DataCite"
    },
    {
      "title": "Starr, Joan",
      "role": "ProjectLeader",
      "organization": "California Digital Library"
    }
  ],
  "keywords": ["000 computer science"]
}

Converting a Data Package to Datacite notation:

from dplib.models import Package
from dplib.plugins.datacite.models import Datacite

package = Datacite.from_dp(Package.from_path("data/package.json"))
print(package.to_text(format="json"))

Reference

dplib.plugins.datacite.models.DatacitePackage

Bases: Model

Datacite Package model

Source code in dplib/plugins/datacite/models/package.py
class DatacitePackage(Model):
    """Datacite Package model"""

    version: Optional[str] = None
    language: Optional[str] = None
    publisher: Optional[str] = None
    publicationYear: Optional[str] = None
    schemaVersion: Optional[str] = None

    creators: List[DataciteContributor] = []
    contributors: List[DataciteContributor] = []
    descriptions: List[DataciteDescription] = []
    identifiers: List[DataciteIdentifier] = []
    rightsList: List[DataciteRights] = []
    subjects: List[DataciteSubject] = []
    titles: List[DataciteTitle] = []

    # Converters

    def to_dp(self) -> Package:
        """Convert to Data Package

        Returns:
           Data Package
        """
        package = Package()

        # Id
        for identifier in self.identifiers:
            if identifier.identifierType == "DOI":
                package.id = f"https://doi.org/{identifier.identifier}"
                break

        # Version
        if self.version:
            package.version = self.version

        # Title
        for title in self.titles:
            if not title.titleType:
                package.title = title.title
                break

        # Description
        for description in self.descriptions:
            if description.descriptionType == "Abstract":
                package.description = description.description
                break

        # Homepage
        for identifier in self.identifiers:
            if identifier.identifierType == "URL":
                package.homepage = identifier.identifier
                break

        # Keywords
        for subject in self.subjects:
            package.keywords.append(subject.subject)

        # Contributors
        for type, items in [("creator", self.creators), ("other", self.contributors)]:
            for item in items:
                contributor = Contributor(
                    title=item.name,
                    givenName=item.givenName,
                    familyName=item.familyName,
                )
                if type == "creator":
                    contributor.roles = [type]
                elif item.contributorType:
                    contributor.roles = [item.contributorType]
                for affiliation in item.affiliation:
                    contributor.organization = affiliation.name
                    break
                package.contributors.append(contributor)

        # Licenses
        for rights in self.rightsList:
            if rights.rightsIdentifier or rights.rightsUri:
                license = License(name=rights.rightsIdentifier, path=rights.rightsUri)
                if rights.rights:
                    license.title = rights.rights
                package.licenses.append(license)

        return package

    @classmethod
    def from_dp(cls, package: Package) -> DatacitePackage:
        """Create a Datacite Package from Data Package

        Parameters:
            package: Data Package

        Returns:
            Datacite Package
        """
        datacite = DatacitePackage()

        # Id
        if package.id:
            if package.id.startswith("https://doi.org/"):
                doi = package.id.replace("https://doi.org/", "")
                datacite.identifiers.append(
                    DataciteIdentifier(identifierType="DOI", identifier=doi)
                )

        # Version
        if package.version:
            datacite.version = package.version

        # Title
        if package.title:
            datacite.titles.append(DataciteTitle(title=package.title))

        # Description
        if package.description:
            datacite.descriptions.append(
                DataciteDescription(
                    descriptionType="Abstract",
                    description=package.description,
                )
            )

        # Homepage
        if package.homepage:
            datacite.identifiers.append(
                DataciteIdentifier(identifierType="URL", identifier=package.homepage)
            )

        # Keywords
        for keyword in package.keywords:
            datacite.subjects.append(DataciteSubject(subject=keyword))

        # Contributors
        for contributor in package.contributors:
            item = DataciteContributor(
                name=contributor.title,
                givenName=contributor.givenName,
                familyName=contributor.familyName,
            )
            if contributor.organization:
                org = DataciteContributorAffiliation(name=contributor.organization)
                item.affiliation.append(org)
            target = datacite.contributors
            if contributor.roles:
                item.contributorType = contributor.roles[0]
                if set(contributor.roles).intersection(["author", "creator"]):
                    target = datacite.creators
            target.append(item)

        return datacite

contributors: List[DataciteContributor] = [] class-attribute instance-attribute

creators: List[DataciteContributor] = [] class-attribute instance-attribute

descriptions: List[DataciteDescription] = [] class-attribute instance-attribute

identifiers: List[DataciteIdentifier] = [] class-attribute instance-attribute

language: Optional[str] = None class-attribute instance-attribute

publicationYear: Optional[str] = None class-attribute instance-attribute

publisher: Optional[str] = None class-attribute instance-attribute

rightsList: List[DataciteRights] = [] class-attribute instance-attribute

schemaVersion: Optional[str] = None class-attribute instance-attribute

subjects: List[DataciteSubject] = [] class-attribute instance-attribute

titles: List[DataciteTitle] = [] class-attribute instance-attribute

version: Optional[str] = None class-attribute instance-attribute

from_dp(package) classmethod

Create a Datacite Package from Data Package

Parameters:

Name Type Description Default
package Package

Data Package

required

Returns:

Type Description
DatacitePackage

Datacite Package

Source code in dplib/plugins/datacite/models/package.py
@classmethod
def from_dp(cls, package: Package) -> DatacitePackage:
    """Create a Datacite Package from Data Package

    Parameters:
        package: Data Package

    Returns:
        Datacite Package
    """
    datacite = DatacitePackage()

    # Id
    if package.id:
        if package.id.startswith("https://doi.org/"):
            doi = package.id.replace("https://doi.org/", "")
            datacite.identifiers.append(
                DataciteIdentifier(identifierType="DOI", identifier=doi)
            )

    # Version
    if package.version:
        datacite.version = package.version

    # Title
    if package.title:
        datacite.titles.append(DataciteTitle(title=package.title))

    # Description
    if package.description:
        datacite.descriptions.append(
            DataciteDescription(
                descriptionType="Abstract",
                description=package.description,
            )
        )

    # Homepage
    if package.homepage:
        datacite.identifiers.append(
            DataciteIdentifier(identifierType="URL", identifier=package.homepage)
        )

    # Keywords
    for keyword in package.keywords:
        datacite.subjects.append(DataciteSubject(subject=keyword))

    # Contributors
    for contributor in package.contributors:
        item = DataciteContributor(
            name=contributor.title,
            givenName=contributor.givenName,
            familyName=contributor.familyName,
        )
        if contributor.organization:
            org = DataciteContributorAffiliation(name=contributor.organization)
            item.affiliation.append(org)
        target = datacite.contributors
        if contributor.roles:
            item.contributorType = contributor.roles[0]
            if set(contributor.roles).intersection(["author", "creator"]):
                target = datacite.creators
        target.append(item)

    return datacite

to_dp()

Convert to Data Package

Returns:

Type Description
Package

Data Package

Source code in dplib/plugins/datacite/models/package.py
def to_dp(self) -> Package:
    """Convert to Data Package

    Returns:
       Data Package
    """
    package = Package()

    # Id
    for identifier in self.identifiers:
        if identifier.identifierType == "DOI":
            package.id = f"https://doi.org/{identifier.identifier}"
            break

    # Version
    if self.version:
        package.version = self.version

    # Title
    for title in self.titles:
        if not title.titleType:
            package.title = title.title
            break

    # Description
    for description in self.descriptions:
        if description.descriptionType == "Abstract":
            package.description = description.description
            break

    # Homepage
    for identifier in self.identifiers:
        if identifier.identifierType == "URL":
            package.homepage = identifier.identifier
            break

    # Keywords
    for subject in self.subjects:
        package.keywords.append(subject.subject)

    # Contributors
    for type, items in [("creator", self.creators), ("other", self.contributors)]:
        for item in items:
            contributor = Contributor(
                title=item.name,
                givenName=item.givenName,
                familyName=item.familyName,
            )
            if type == "creator":
                contributor.roles = [type]
            elif item.contributorType:
                contributor.roles = [item.contributorType]
            for affiliation in item.affiliation:
                contributor.organization = affiliation.name
                break
            package.contributors.append(contributor)

    # Licenses
    for rights in self.rightsList:
        if rights.rightsIdentifier or rights.rightsUri:
            license = License(name=rights.rightsIdentifier, path=rights.rightsUri)
            if rights.rights:
                license.title = rights.rights
            package.licenses.append(license)

    return package