Skip to content

DCAT

DCAT plugin provides Package and Resource models and converters between DCAT and Data Package notations

Installation

Extra dependency needs to be installed:

pip install dplib-py[dcat]

Usage

Converting a DCAT descriptor to the Data Package notation:

from dplib.plugins.dcat.models import DcatPackage

package = DcatPackage.from_path("data/plugins/dcat/package.xml").to_dp()
print(package.to_text(format='json'))
{
  "resources": [
    {
      "name": "fishway_obstruction_data_v1",
      "path": "https://zenodo.org/records/5770714/files/Fishway_Obstruction_Data_v1.csv",
      "mediatype": "text/csv",
      "bytes": 1377
    },
    {
      "name": "readme",
      "path": "https://zenodo.org/records/5770714/files/readme.md",
      "bytes": 1577
    }
  ],
  "id": "https://doi.org/10.5281/zenodo.5770714",
  "title": "Fishway_Obstruction_Data_v1.csv",
  "description": "This dataset contains pool-weir type fishway (sumerged notch and orifice) hydraulic scenarios with and without obstruction events in accordance with\u00a0the publication:\u00a0 Fuentes-P\u00e9rez, J.F., Garc\u00eda-Vega, A., Bravo-C\u00f3rdoba, F.J., Sanz-Ronda, F.J. 2021. A Step to Smart Fishways: An Autonomous Obstruction Detection System Using Hydraulic Modeling and Sensor Networks. Sensors 2021, 21(20), 6909.",
  "version": "v1",
  "keywords": [
    "fishways",
    "hydraulics",
    "smart fishways",
    "pool-weir",
    "hydrological variability",
    "nonuniformity",
    "clogging",
    "water-level sensors"
  ]
}

Converting a Data Package to DCAT notation:

from dplib.models import Package
from dplib.plugins.dcat.models import DcatPackage

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

Reference

dplib.plugins.dcat.models.DcatPackage

Bases: Model

DCAT Package model

Source code in dplib/plugins/dcat/models/package.py
class DcatPackage(Model):
    """DCAT Package model"""

    identifier: Optional[str] = None
    distributions: List[DcatResource] = []

    accural_periodicity: Optional[str] = None
    alternate_identifiers: List[str] = []
    comforms_to: List[str] = []
    description: Optional[str] = None
    has_versions: List[str] = []
    homepage: Optional[str] = None
    issued: Optional[str] = None
    is_version_of: List[str] = []
    keywords: List[str] = []
    landing_page: Optional[str] = None
    languages: List[str] = []
    modified: Optional[str] = None
    pages: List[str] = []
    provenance: Optional[str] = None
    related_resources: List[str] = []
    samples: List[str] = []
    sources: List[str] = []
    themes: List[str] = []
    title: Optional[str] = None
    version: Optional[str] = None

    # Converters

    def to_text(self, *, format: str):
        g = self.to_graph()
        return g.serialize(format=format)

    @classmethod
    def from_text(cls, text: str, *, format: str, **kwargs: Any):
        g = Graph()
        g.parse(data=text, format=format)
        return cls.from_graph(g)

    def to_graph(self):
        g = Graph()
        for prefix, namespace in ns.BINDINGS.items():
            g.bind(prefix, namespace)

        # Identifier
        if not self.identifier:
            raise Error(f"Cannot dump DCAT package without identifier: {self}")
        id = dumpers.id(g, self.identifier, predicate=ns.TYPE, object=ns.DATASET)

        # Accural periodicity
        if self.accural_periodicity:
            dumpers.node(
                g, self.accural_periodicity, subject=id, predicate=ns.ACCURAL_PERIODICITY
            )

        # Alternate identifiers
        for identifier in self.alternate_identifiers:
            dumpers.node(g, identifier, subject=id, predicate=ns.ALTERNATE_IDENTIFIER)

        # Conforms to
        for conforms_to in self.comforms_to:
            dumpers.node(g, conforms_to, subject=id, predicate=ns.COMFORMS_TO)

        # Description
        if self.description:
            dumpers.node(g, self.description, subject=id, predicate=ns.DESCRIPTION)

        # Has versions
        for has_version in self.has_versions:
            dumpers.node(g, has_version, subject=id, predicate=ns.HAS_VERSION)

        # Homepage
        if self.homepage:
            dumpers.node(g, self.homepage, subject=id, predicate=ns.HOMEPAGE)

        # Issued
        if self.issued:
            dumpers.node(g, self.issued, subject=id, predicate=ns.ISSUED)

        # Is version of
        for is_version_of in self.is_version_of:
            dumpers.node(g, is_version_of, subject=id, predicate=ns.IS_VERSION_OF)

        # Keywords
        for keyword in self.keywords:
            dumpers.node(g, keyword, subject=id, predicate=ns.KEYWORD)

        # Landing page
        if self.landing_page:
            dumpers.node(g, self.landing_page, subject=id, predicate=ns.LANDING_PAGE)

        # Languages
        for language in self.languages:
            dumpers.node(g, language, subject=id, predicate=ns.LANGUAGE)

        # Modified
        if self.modified:
            dumpers.node(g, self.modified, subject=id, predicate=ns.MODIFIED)

        # Pages
        for page in self.pages:
            dumpers.node(g, page, subject=id, predicate=ns.PAGE)

        # Provenance
        if self.provenance:
            dumpers.node(g, self.provenance, subject=id, predicate=ns.PROVENANCE)

        # Related resources
        for related_resource in self.related_resources:
            dumpers.node(g, related_resource, subject=id, predicate=ns.RELATED_RESOURCE)

        # Samples
        for sample in self.samples:
            dumpers.node(g, sample, subject=id, predicate=ns.SAMPLE)

        # Sources
        for source in self.sources:
            dumpers.node(g, source, subject=id, predicate=ns.SOURCE)

        # Themes
        for theme in self.themes:
            dumpers.node(g, theme, subject=id, predicate=ns.THEME)

        # Title
        if self.title:
            dumpers.node(g, self.title, subject=id, predicate=ns.TITLE)

        # Version
        if self.version:
            dumpers.node(g, self.version, subject=id, predicate=ns.VERSION)

        # Distributions
        for distribution in self.distributions:
            distribution_id = BNode()
            g.add((id, ns.DISTRIBUTION, distribution_id))
            g.add((distribution_id, ns.TYPE, ns.DISTRIBUTION))
            distribution.to_graph(g, id=distribution_id)

        return g

    @classmethod
    def from_graph(cls, g: Graph):
        package = DcatPackage()

        # Identifier
        id = loaders.id(g, predicate=ns.TYPE, object=ns.DATASET)
        if not id:
            raise Error(f"Cannot load DCAT package without identifier: {g}")
        package.identifier = str(id)

        # Accural periodicity
        periodicity = loaders.string(g, subject=id, predicate=ns.ACCURAL_PERIODICITY)
        if periodicity:
            package.accural_periodicity = periodicity

        # Alternate identifiers
        identifiers = loaders.strings(g, subject=id, predicate=ns.ALTERNATE_IDENTIFIER)
        if identifiers:
            package.alternate_identifiers = identifiers

        # Conforms to
        conforms_to = loaders.strings(g, subject=id, predicate=ns.COMFORMS_TO)
        if conforms_to:
            package.comforms_to = conforms_to

        # Description
        description = loaders.string(g, subject=id, predicate=ns.DESCRIPTION)
        if description:
            package.description = description

        # Has versions
        has_versions = loaders.strings(g, subject=id, predicate=ns.HAS_VERSION)
        if has_versions:
            package.has_versions = has_versions

        # Homepage
        homepage = loaders.string(g, subject=id, predicate=ns.HOMEPAGE)
        if homepage:
            package.homepage = homepage

        # Issued
        issued = loaders.string(g, subject=id, predicate=ns.ISSUED)
        if issued:
            package.issued = issued

        # Is version of
        is_version_of = loaders.strings(g, subject=id, predicate=ns.IS_VERSION_OF)
        if is_version_of:
            package.is_version_of = is_version_of

        # Keywords
        keywords = loaders.strings(g, subject=id, predicate=ns.KEYWORD)
        if keywords:
            package.keywords = keywords

        # Landing page
        landing_page = loaders.string(g, subject=id, predicate=ns.LANDING_PAGE)
        if landing_page:
            package.landing_page = landing_page

        # Languages
        languages = loaders.strings(g, subject=id, predicate=ns.LANGUAGE)
        if languages:
            package.languages = languages

        # Modified
        modified = loaders.string(g, subject=id, predicate=ns.MODIFIED)
        if modified:
            package.modified = modified

        # Pages
        pages = loaders.strings(g, subject=id, predicate=ns.PAGE)
        if pages:
            package.pages = pages

        # Provenance
        provenance = loaders.string(g, subject=id, predicate=ns.PROVENANCE)
        if provenance:
            package.provenance = provenance

        # Related resources
        related_resources = loaders.strings(g, subject=id, predicate=ns.RELATED_RESOURCE)
        if related_resources:
            package.related_resources = related_resources

        # Samples
        samples = loaders.strings(g, subject=id, predicate=ns.SAMPLE)
        if samples:
            package.samples = samples

        # Sources
        sources = loaders.strings(g, subject=id, predicate=ns.SOURCE)
        if sources:
            package.sources = sources

        # Themes
        themes = loaders.strings(g, subject=id, predicate=ns.THEME)
        if themes:
            package.themes = themes

        # Title
        title = loaders.string(g, subject=id, predicate=ns.TITLE)
        if title:
            package.title = title

        # Version
        version = loaders.string(g, subject=id, predicate=ns.VERSION)
        if version:
            package.version = version

        # Distributions
        distributions = g.objects(subject=id, predicate=ns.DISTRIBUTION)
        for distribution in distributions:
            if isinstance(distribution, (URIRef, BNode)):
                resource = DcatResource.from_graph(g, id=distribution)
                package.distributions.append(resource)

        return package

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

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

        # Id
        if self.identifier:
            package.id = self.identifier

        # Title
        if self.title:
            package.title = self.title

        # Description
        if self.description:
            package.description = self.description

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

        # Homepage
        if self.homepage:
            package.homepage = self.homepage

        # Created
        if self.issued:
            if "T" in self.issued:
                package.created = self.issued

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

        # Sources
        for source in self.sources:
            source = Source(title=source)
            package.sources.append(source)

        # Resources
        for distribution in self.distributions:
            resource = distribution.to_dp()
            if resource:
                package.resources.append(resource)

        return package

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

        Parameters:
            package: Data Package

        Returns:
            DCAT Package
        """
        dcat = DcatPackage()

        # Identifier
        if package.id:
            dcat.identifier = package.id

        # Title
        if package.title:
            dcat.title = package.title

        # Description
        if package.description:
            dcat.description = package.description

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

        # Homepage
        if package.homepage:
            dcat.homepage = package.homepage

        # Issued
        if package.created:
            dcat.issued = package.created

        # Keywords
        for keyword in package.keywords:
            dcat.keywords.append(keyword)

        # Sources
        for source in package.sources:
            source = source.path or source.title
            if source:
                dcat.sources.append(source)

        # Resources
        for resource in package.resources:
            distribution = DcatResource.from_dp(resource)
            if distribution:
                dcat.distributions.append(distribution)

        return dcat

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

alternate_identifiers: List[str] = [] class-attribute instance-attribute

comforms_to: List[str] = [] class-attribute instance-attribute

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

distributions: List[DcatResource] = [] class-attribute instance-attribute

has_versions: List[str] = [] class-attribute instance-attribute

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

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

is_version_of: List[str] = [] class-attribute instance-attribute

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

keywords: List[str] = [] class-attribute instance-attribute

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

languages: List[str] = [] class-attribute instance-attribute

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

pages: List[str] = [] class-attribute instance-attribute

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

related_resources: List[str] = [] class-attribute instance-attribute

samples: List[str] = [] class-attribute instance-attribute

sources: List[str] = [] class-attribute instance-attribute

themes: List[str] = [] class-attribute instance-attribute

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

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

from_dp(package) classmethod

Create a DCAT Package from Data Package

Parameters:

Name Type Description Default
package Package

Data Package

required

Returns:

Type Description
DcatPackage

DCAT Package

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

    Parameters:
        package: Data Package

    Returns:
        DCAT Package
    """
    dcat = DcatPackage()

    # Identifier
    if package.id:
        dcat.identifier = package.id

    # Title
    if package.title:
        dcat.title = package.title

    # Description
    if package.description:
        dcat.description = package.description

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

    # Homepage
    if package.homepage:
        dcat.homepage = package.homepage

    # Issued
    if package.created:
        dcat.issued = package.created

    # Keywords
    for keyword in package.keywords:
        dcat.keywords.append(keyword)

    # Sources
    for source in package.sources:
        source = source.path or source.title
        if source:
            dcat.sources.append(source)

    # Resources
    for resource in package.resources:
        distribution = DcatResource.from_dp(resource)
        if distribution:
            dcat.distributions.append(distribution)

    return dcat

from_graph(g) classmethod

Source code in dplib/plugins/dcat/models/package.py
@classmethod
def from_graph(cls, g: Graph):
    package = DcatPackage()

    # Identifier
    id = loaders.id(g, predicate=ns.TYPE, object=ns.DATASET)
    if not id:
        raise Error(f"Cannot load DCAT package without identifier: {g}")
    package.identifier = str(id)

    # Accural periodicity
    periodicity = loaders.string(g, subject=id, predicate=ns.ACCURAL_PERIODICITY)
    if periodicity:
        package.accural_periodicity = periodicity

    # Alternate identifiers
    identifiers = loaders.strings(g, subject=id, predicate=ns.ALTERNATE_IDENTIFIER)
    if identifiers:
        package.alternate_identifiers = identifiers

    # Conforms to
    conforms_to = loaders.strings(g, subject=id, predicate=ns.COMFORMS_TO)
    if conforms_to:
        package.comforms_to = conforms_to

    # Description
    description = loaders.string(g, subject=id, predicate=ns.DESCRIPTION)
    if description:
        package.description = description

    # Has versions
    has_versions = loaders.strings(g, subject=id, predicate=ns.HAS_VERSION)
    if has_versions:
        package.has_versions = has_versions

    # Homepage
    homepage = loaders.string(g, subject=id, predicate=ns.HOMEPAGE)
    if homepage:
        package.homepage = homepage

    # Issued
    issued = loaders.string(g, subject=id, predicate=ns.ISSUED)
    if issued:
        package.issued = issued

    # Is version of
    is_version_of = loaders.strings(g, subject=id, predicate=ns.IS_VERSION_OF)
    if is_version_of:
        package.is_version_of = is_version_of

    # Keywords
    keywords = loaders.strings(g, subject=id, predicate=ns.KEYWORD)
    if keywords:
        package.keywords = keywords

    # Landing page
    landing_page = loaders.string(g, subject=id, predicate=ns.LANDING_PAGE)
    if landing_page:
        package.landing_page = landing_page

    # Languages
    languages = loaders.strings(g, subject=id, predicate=ns.LANGUAGE)
    if languages:
        package.languages = languages

    # Modified
    modified = loaders.string(g, subject=id, predicate=ns.MODIFIED)
    if modified:
        package.modified = modified

    # Pages
    pages = loaders.strings(g, subject=id, predicate=ns.PAGE)
    if pages:
        package.pages = pages

    # Provenance
    provenance = loaders.string(g, subject=id, predicate=ns.PROVENANCE)
    if provenance:
        package.provenance = provenance

    # Related resources
    related_resources = loaders.strings(g, subject=id, predicate=ns.RELATED_RESOURCE)
    if related_resources:
        package.related_resources = related_resources

    # Samples
    samples = loaders.strings(g, subject=id, predicate=ns.SAMPLE)
    if samples:
        package.samples = samples

    # Sources
    sources = loaders.strings(g, subject=id, predicate=ns.SOURCE)
    if sources:
        package.sources = sources

    # Themes
    themes = loaders.strings(g, subject=id, predicate=ns.THEME)
    if themes:
        package.themes = themes

    # Title
    title = loaders.string(g, subject=id, predicate=ns.TITLE)
    if title:
        package.title = title

    # Version
    version = loaders.string(g, subject=id, predicate=ns.VERSION)
    if version:
        package.version = version

    # Distributions
    distributions = g.objects(subject=id, predicate=ns.DISTRIBUTION)
    for distribution in distributions:
        if isinstance(distribution, (URIRef, BNode)):
            resource = DcatResource.from_graph(g, id=distribution)
            package.distributions.append(resource)

    return package

from_text(text, *, format, **kwargs) classmethod

Source code in dplib/plugins/dcat/models/package.py
@classmethod
def from_text(cls, text: str, *, format: str, **kwargs: Any):
    g = Graph()
    g.parse(data=text, format=format)
    return cls.from_graph(g)

to_dp()

Convert to Data Package

Returns:

Type Description
Package

Data Package

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

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

    # Id
    if self.identifier:
        package.id = self.identifier

    # Title
    if self.title:
        package.title = self.title

    # Description
    if self.description:
        package.description = self.description

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

    # Homepage
    if self.homepage:
        package.homepage = self.homepage

    # Created
    if self.issued:
        if "T" in self.issued:
            package.created = self.issued

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

    # Sources
    for source in self.sources:
        source = Source(title=source)
        package.sources.append(source)

    # Resources
    for distribution in self.distributions:
        resource = distribution.to_dp()
        if resource:
            package.resources.append(resource)

    return package

to_graph()

Source code in dplib/plugins/dcat/models/package.py
def to_graph(self):
    g = Graph()
    for prefix, namespace in ns.BINDINGS.items():
        g.bind(prefix, namespace)

    # Identifier
    if not self.identifier:
        raise Error(f"Cannot dump DCAT package without identifier: {self}")
    id = dumpers.id(g, self.identifier, predicate=ns.TYPE, object=ns.DATASET)

    # Accural periodicity
    if self.accural_periodicity:
        dumpers.node(
            g, self.accural_periodicity, subject=id, predicate=ns.ACCURAL_PERIODICITY
        )

    # Alternate identifiers
    for identifier in self.alternate_identifiers:
        dumpers.node(g, identifier, subject=id, predicate=ns.ALTERNATE_IDENTIFIER)

    # Conforms to
    for conforms_to in self.comforms_to:
        dumpers.node(g, conforms_to, subject=id, predicate=ns.COMFORMS_TO)

    # Description
    if self.description:
        dumpers.node(g, self.description, subject=id, predicate=ns.DESCRIPTION)

    # Has versions
    for has_version in self.has_versions:
        dumpers.node(g, has_version, subject=id, predicate=ns.HAS_VERSION)

    # Homepage
    if self.homepage:
        dumpers.node(g, self.homepage, subject=id, predicate=ns.HOMEPAGE)

    # Issued
    if self.issued:
        dumpers.node(g, self.issued, subject=id, predicate=ns.ISSUED)

    # Is version of
    for is_version_of in self.is_version_of:
        dumpers.node(g, is_version_of, subject=id, predicate=ns.IS_VERSION_OF)

    # Keywords
    for keyword in self.keywords:
        dumpers.node(g, keyword, subject=id, predicate=ns.KEYWORD)

    # Landing page
    if self.landing_page:
        dumpers.node(g, self.landing_page, subject=id, predicate=ns.LANDING_PAGE)

    # Languages
    for language in self.languages:
        dumpers.node(g, language, subject=id, predicate=ns.LANGUAGE)

    # Modified
    if self.modified:
        dumpers.node(g, self.modified, subject=id, predicate=ns.MODIFIED)

    # Pages
    for page in self.pages:
        dumpers.node(g, page, subject=id, predicate=ns.PAGE)

    # Provenance
    if self.provenance:
        dumpers.node(g, self.provenance, subject=id, predicate=ns.PROVENANCE)

    # Related resources
    for related_resource in self.related_resources:
        dumpers.node(g, related_resource, subject=id, predicate=ns.RELATED_RESOURCE)

    # Samples
    for sample in self.samples:
        dumpers.node(g, sample, subject=id, predicate=ns.SAMPLE)

    # Sources
    for source in self.sources:
        dumpers.node(g, source, subject=id, predicate=ns.SOURCE)

    # Themes
    for theme in self.themes:
        dumpers.node(g, theme, subject=id, predicate=ns.THEME)

    # Title
    if self.title:
        dumpers.node(g, self.title, subject=id, predicate=ns.TITLE)

    # Version
    if self.version:
        dumpers.node(g, self.version, subject=id, predicate=ns.VERSION)

    # Distributions
    for distribution in self.distributions:
        distribution_id = BNode()
        g.add((id, ns.DISTRIBUTION, distribution_id))
        g.add((distribution_id, ns.TYPE, ns.DISTRIBUTION))
        distribution.to_graph(g, id=distribution_id)

    return g

to_text(*, format)

Source code in dplib/plugins/dcat/models/package.py
def to_text(self, *, format: str):
    g = self.to_graph()
    return g.serialize(format=format)

dplib.plugins.dcat.models.DcatResource

Bases: Model

DCAT Resource model

Source code in dplib/plugins/dcat/models/resource.py
class DcatResource(Model):
    """DCAT Resource model"""

    access_url: Optional[str] = None
    byte_size: Optional[int] = None
    conforms_to: List[str] = []
    description: Optional[str] = None
    download_url: Optional[str] = None
    issued: Optional[str] = None
    languages: List[str] = []
    license: Optional[str] = None
    media_type: Optional[str] = None
    modified: Optional[str] = None
    pages: List[str] = []
    title: Optional[str] = None

    # Converters

    def to_graph(self, g: Graph, *, id: BNode):
        # Access URL
        if self.access_url:
            dumpers.node(g, self.access_url, subject=id, predicate=ns.ACCESS_URL)

        # Byte size
        if self.byte_size:
            dumpers.node(g, self.byte_size, subject=id, predicate=ns.BYTE_SIZE)

        # Conforms to
        for conforms_to in self.conforms_to:
            dumpers.node(g, conforms_to, subject=id, predicate=ns.COMFORMS_TO)

        # Description
        if self.description:
            dumpers.node(g, self.description, subject=id, predicate=ns.DESCRIPTION)

        # Download URL
        if self.download_url:
            dumpers.node(g, self.download_url, subject=id, predicate=ns.DOWNLOAD_URL)

        # Issued
        if self.issued:
            dumpers.node(g, self.issued, subject=id, predicate=ns.ISSUED)

        # Languages
        for language in self.languages:
            dumpers.node(g, language, subject=id, predicate=ns.LANGUAGE)

        # License
        if self.license:
            dumpers.node(g, self.license, subject=id, predicate=ns.LICENSE)

        # Media type
        if self.media_type:
            dumpers.node(g, self.media_type, subject=id, predicate=ns.MEDIA_TYPE)

        # Modified
        if self.modified:
            dumpers.node(g, self.modified, subject=id, predicate=ns.MODIFIED)

        # Pages
        for page in self.pages:
            dumpers.node(g, page, subject=id, predicate=ns.PAGE)

        # Title
        if self.title:
            dumpers.node(g, self.title, subject=id, predicate=ns.TITLE)

        return g

    @classmethod
    def from_graph(cls, g: Graph, *, id: ISubject) -> DcatResource:
        resource = DcatResource()

        # Access URL
        access_url = loaders.string(g, subject=id, predicate=ns.ACCESS_URL)
        if access_url:
            resource.access_url = access_url

        # Byte size
        byte_size = loaders.integer(g, subject=id, predicate=ns.BYTE_SIZE)
        if byte_size:
            resource.byte_size = byte_size

        # Conforms to
        conforms_to = loaders.strings(g, subject=id, predicate=ns.COMFORMS_TO)
        if conforms_to:
            resource.conforms_to = conforms_to

        # Description
        description = loaders.string(g, subject=id, predicate=ns.DESCRIPTION)
        if description:
            resource.description = description

        # Download URL
        download_url = loaders.string(g, subject=id, predicate=ns.DOWNLOAD_URL)
        if download_url:
            resource.download_url = download_url

        # Issued
        issued = loaders.string(g, subject=id, predicate=ns.ISSUED)
        if issued:
            resource.issued = issued

        # Languages
        languages = loaders.strings(g, subject=id, predicate=ns.LANGUAGE)
        if languages:
            resource.languages = languages

        # License
        license = loaders.string(g, subject=id, predicate=ns.LICENSE)
        if license:
            resource.license = license

        # Media type
        media_type = loaders.string(g, subject=id, predicate=ns.MEDIA_TYPE)
        if media_type:
            resource.media_type = media_type

        # Modified
        modified = loaders.string(g, subject=id, predicate=ns.MODIFIED)
        if modified:
            resource.modified = modified

        # Pages
        pages = loaders.strings(g, subject=id, predicate=ns.PAGE)
        if pages:
            resource.pages = pages

        # Title
        title = loaders.string(g, subject=id, predicate=ns.TITLE)
        if title:
            resource.title = title

        return resource

    def to_dp(self) -> Optional[Resource]:
        """Convert to Data Package resource

        Returns:
           Data Resource
        """
        if not self.download_url:
            return
        resource = Resource(path=self.download_url, name=slugify_name(self.download_url))

        # Title
        if self.title:
            resource.title = self.title

        # Description
        if self.description:
            resource.description = self.description

        # Media type
        if self.media_type:
            resource.mediatype = self.media_type

        # Bytes
        if self.byte_size:
            resource.bytes = self.byte_size

        # Licenses
        if self.license:
            license = License(path=self.license)
            resource.licenses.append(license)

        return resource

    @classmethod
    def from_dp(cls, resource: Resource) -> Optional[DcatResource]:
        """Create DCAT Resource from Data Resource

        Parameters:
            resource: Data Resource

        Returns:
            DCAT Resource
        """
        dcat = DcatResource()
        if not resource.path or not isinstance(resource.path, str):
            return

        # Download URL
        # TODO: improve logic -- use basepath and allow only urls
        if resource.path:
            dcat.download_url = resource.path

        # Title
        if resource.title:
            dcat.title = resource.title

        # Description
        if resource.description:
            dcat.description = resource.description

        # Media type
        if resource.mediatype:
            dcat.media_type = resource.mediatype

        # Bytes
        if resource.bytes:
            dcat.byte_size = resource.bytes

        # Licenses
        if resource.licenses:
            license = resource.licenses[0]
            if license.path:
                dcat.license = license.path

        return dcat

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

byte_size: Optional[int] = None class-attribute instance-attribute

conforms_to: List[str] = [] class-attribute instance-attribute

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

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

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

languages: List[str] = [] class-attribute instance-attribute

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

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

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

pages: List[str] = [] class-attribute instance-attribute

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

from_dp(resource) classmethod

Create DCAT Resource from Data Resource

Parameters:

Name Type Description Default
resource Resource

Data Resource

required

Returns:

Type Description
Optional[DcatResource]

DCAT Resource

Source code in dplib/plugins/dcat/models/resource.py
@classmethod
def from_dp(cls, resource: Resource) -> Optional[DcatResource]:
    """Create DCAT Resource from Data Resource

    Parameters:
        resource: Data Resource

    Returns:
        DCAT Resource
    """
    dcat = DcatResource()
    if not resource.path or not isinstance(resource.path, str):
        return

    # Download URL
    # TODO: improve logic -- use basepath and allow only urls
    if resource.path:
        dcat.download_url = resource.path

    # Title
    if resource.title:
        dcat.title = resource.title

    # Description
    if resource.description:
        dcat.description = resource.description

    # Media type
    if resource.mediatype:
        dcat.media_type = resource.mediatype

    # Bytes
    if resource.bytes:
        dcat.byte_size = resource.bytes

    # Licenses
    if resource.licenses:
        license = resource.licenses[0]
        if license.path:
            dcat.license = license.path

    return dcat

from_graph(g, *, id) classmethod

Source code in dplib/plugins/dcat/models/resource.py
@classmethod
def from_graph(cls, g: Graph, *, id: ISubject) -> DcatResource:
    resource = DcatResource()

    # Access URL
    access_url = loaders.string(g, subject=id, predicate=ns.ACCESS_URL)
    if access_url:
        resource.access_url = access_url

    # Byte size
    byte_size = loaders.integer(g, subject=id, predicate=ns.BYTE_SIZE)
    if byte_size:
        resource.byte_size = byte_size

    # Conforms to
    conforms_to = loaders.strings(g, subject=id, predicate=ns.COMFORMS_TO)
    if conforms_to:
        resource.conforms_to = conforms_to

    # Description
    description = loaders.string(g, subject=id, predicate=ns.DESCRIPTION)
    if description:
        resource.description = description

    # Download URL
    download_url = loaders.string(g, subject=id, predicate=ns.DOWNLOAD_URL)
    if download_url:
        resource.download_url = download_url

    # Issued
    issued = loaders.string(g, subject=id, predicate=ns.ISSUED)
    if issued:
        resource.issued = issued

    # Languages
    languages = loaders.strings(g, subject=id, predicate=ns.LANGUAGE)
    if languages:
        resource.languages = languages

    # License
    license = loaders.string(g, subject=id, predicate=ns.LICENSE)
    if license:
        resource.license = license

    # Media type
    media_type = loaders.string(g, subject=id, predicate=ns.MEDIA_TYPE)
    if media_type:
        resource.media_type = media_type

    # Modified
    modified = loaders.string(g, subject=id, predicate=ns.MODIFIED)
    if modified:
        resource.modified = modified

    # Pages
    pages = loaders.strings(g, subject=id, predicate=ns.PAGE)
    if pages:
        resource.pages = pages

    # Title
    title = loaders.string(g, subject=id, predicate=ns.TITLE)
    if title:
        resource.title = title

    return resource

to_dp()

Convert to Data Package resource

Returns:

Type Description
Optional[Resource]

Data Resource

Source code in dplib/plugins/dcat/models/resource.py
def to_dp(self) -> Optional[Resource]:
    """Convert to Data Package resource

    Returns:
       Data Resource
    """
    if not self.download_url:
        return
    resource = Resource(path=self.download_url, name=slugify_name(self.download_url))

    # Title
    if self.title:
        resource.title = self.title

    # Description
    if self.description:
        resource.description = self.description

    # Media type
    if self.media_type:
        resource.mediatype = self.media_type

    # Bytes
    if self.byte_size:
        resource.bytes = self.byte_size

    # Licenses
    if self.license:
        license = License(path=self.license)
        resource.licenses.append(license)

    return resource

to_graph(g, *, id)

Source code in dplib/plugins/dcat/models/resource.py
def to_graph(self, g: Graph, *, id: BNode):
    # Access URL
    if self.access_url:
        dumpers.node(g, self.access_url, subject=id, predicate=ns.ACCESS_URL)

    # Byte size
    if self.byte_size:
        dumpers.node(g, self.byte_size, subject=id, predicate=ns.BYTE_SIZE)

    # Conforms to
    for conforms_to in self.conforms_to:
        dumpers.node(g, conforms_to, subject=id, predicate=ns.COMFORMS_TO)

    # Description
    if self.description:
        dumpers.node(g, self.description, subject=id, predicate=ns.DESCRIPTION)

    # Download URL
    if self.download_url:
        dumpers.node(g, self.download_url, subject=id, predicate=ns.DOWNLOAD_URL)

    # Issued
    if self.issued:
        dumpers.node(g, self.issued, subject=id, predicate=ns.ISSUED)

    # Languages
    for language in self.languages:
        dumpers.node(g, language, subject=id, predicate=ns.LANGUAGE)

    # License
    if self.license:
        dumpers.node(g, self.license, subject=id, predicate=ns.LICENSE)

    # Media type
    if self.media_type:
        dumpers.node(g, self.media_type, subject=id, predicate=ns.MEDIA_TYPE)

    # Modified
    if self.modified:
        dumpers.node(g, self.modified, subject=id, predicate=ns.MODIFIED)

    # Pages
    for page in self.pages:
        dumpers.node(g, page, subject=id, predicate=ns.PAGE)

    # Title
    if self.title:
        dumpers.node(g, self.title, subject=id, predicate=ns.TITLE)

    return g