Skip to content

Package

The Data Package model allows to manipulate a Pydantic model in Python according to the Data Package specification

Usage

from dplib.models import Package, Resource

package = Package()
package.name = 'name'
package.add_resource(Resource(name='table', path='table.csv'))
print(package.to_text(format="json"))
{
  "resources": [
    {
      "name": "table",
      "path": "table.csv"
    }
  ],
  "name": "name"
}

Reference

dplib.models.Package

Bases: Model

Data Package model

Source code in dplib/models/package/package.py
class Package(Model):
    """Data Package model"""

    profile: str = pydantic.Field(
        default=settings.PROFILE_CURRENT_PACKAGE,
        alias="$schema",
    )
    """A profile URL"""

    basepath: Optional[str] = pydantic.Field(default=None, exclude=True)
    """
    Basepath of the package.
    All the resources are relative to this path.
    """

    resources: List[Resource] = []
    """
    List of resources
    """

    id: Optional[str] = None
    """
    A property reserved for globally unique identifiers.
    Examples of identifiers that are unique include UUIDs and DOIs.
    """

    name: Optional[str] = None
    """
    A short url-usable (and preferably human-readable) name of the package.
    This MUST be lower-case and contain only alphanumeric characters
    along with ”.”, ”_” or ”-” characters.
    """

    title: Optional[str] = None
    """
    A string providing a title or one sentence description for this package
    """

    description: Optional[str] = None
    """
    A description of the package. The description MUST be markdown formatted —
    this also allows for simple plain text as plain text is itself valid markdown.
    """

    homepage: Optional[str] = None
    """
    A URL for the home on the web that is related to this data package.
    """

    version: Optional[str] = None
    """
    A version string identifying the version of the package.
    It should conform to the Semantic Versioning requirements
    """

    licenses: List[License] = []
    """
    The license(s) under which the package is provided.
    This property is not legally binding and does not guarantee
    the package is licensed under the terms defined in this property.
    """

    sources: List[Source] = []
    """
    The raw sources for this data package.
    """

    contributors: List[Contributor] = []
    """
    The people or organizations who contributed to this Data Package.
    """

    keywords: List[str] = []
    """
    An Array of string keywords to assist users searching for the package in catalogs.
    """

    image: Optional[str] = None
    """
    An image to use for this data package.
    For example, when showing the package in a listing.
    """

    created: Optional[str] = None
    """
    The datetime on which this was created.
    """

    def model_post_init(self, _):
        for resource in self.resources:
            resource.basepath = self.basepath

    # Getters

    def get_resource(
        self, *, name: Optional[str] = None, path: Optional[str] = None
    ) -> Optional[Resource]:
        """Get a resource by name or path

        Parameters:
            name: The name of the resource
            path: The path of the resource

        Returns:
            The resource if found
        """
        for resource in self.resources:
            if name and resource.name == name:
                return resource
            if path and resource.path == path:
                return resource

    # Setters

    def add_resource(self, resource: Resource) -> None:
        """Add a resource to the package

        Parameters:
            resource: The resource to add
        """
        resource.basepath = self.basepath
        self.resources.append(resource)

    # Methods

    def dereference(self):
        """Dereference the package
        It will dereference all the resource's dialects and schemas in the package.
        """
        for resource in self.resources:
            resource.dereference()

    # Converters

    def to_dict(self):
        data = {"$schema": settings.PROFILE_CURRENT_PACKAGE}
        data.update(super().to_dict())
        return data

basepath: Optional[str] = pydantic.Field(default=None, exclude=True) class-attribute instance-attribute

Basepath of the package. All the resources are relative to this path.

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

The people or organizations who contributed to this Data Package.

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

The datetime on which this was created.

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

A description of the package. The description MUST be markdown formatted — this also allows for simple plain text as plain text is itself valid markdown.

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

A URL for the home on the web that is related to this data package.

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

A property reserved for globally unique identifiers. Examples of identifiers that are unique include UUIDs and DOIs.

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

An image to use for this data package. For example, when showing the package in a listing.

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

An Array of string keywords to assist users searching for the package in catalogs.

licenses: List[License] = [] class-attribute instance-attribute

The license(s) under which the package is provided. This property is not legally binding and does not guarantee the package is licensed under the terms defined in this property.

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

A short url-usable (and preferably human-readable) name of the package. This MUST be lower-case and contain only alphanumeric characters along with ”.”, ”_” or ”-” characters.

profile: str = pydantic.Field(default=settings.PROFILE_CURRENT_PACKAGE, alias='$schema') class-attribute instance-attribute

A profile URL

resources: List[Resource] = [] class-attribute instance-attribute

List of resources

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

The raw sources for this data package.

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

A string providing a title or one sentence description for this package

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

A version string identifying the version of the package. It should conform to the Semantic Versioning requirements

add_resource(resource)

Add a resource to the package

Parameters:

Name Type Description Default
resource Resource

The resource to add

required
Source code in dplib/models/package/package.py
def add_resource(self, resource: Resource) -> None:
    """Add a resource to the package

    Parameters:
        resource: The resource to add
    """
    resource.basepath = self.basepath
    self.resources.append(resource)

dereference()

Dereference the package It will dereference all the resource's dialects and schemas in the package.

Source code in dplib/models/package/package.py
def dereference(self):
    """Dereference the package
    It will dereference all the resource's dialects and schemas in the package.
    """
    for resource in self.resources:
        resource.dereference()

get_resource(*, name=None, path=None)

Get a resource by name or path

Parameters:

Name Type Description Default
name Optional[str]

The name of the resource

None
path Optional[str]

The path of the resource

None

Returns:

Type Description
Optional[Resource]

The resource if found

Source code in dplib/models/package/package.py
def get_resource(
    self, *, name: Optional[str] = None, path: Optional[str] = None
) -> Optional[Resource]:
    """Get a resource by name or path

    Parameters:
        name: The name of the resource
        path: The path of the resource

    Returns:
        The resource if found
    """
    for resource in self.resources:
        if name and resource.name == name:
            return resource
        if path and resource.path == path:
            return resource

model_post_init(_)

Source code in dplib/models/package/package.py
def model_post_init(self, _):
    for resource in self.resources:
        resource.basepath = self.basepath

to_dict()

Source code in dplib/models/package/package.py
def to_dict(self):
    data = {"$schema": settings.PROFILE_CURRENT_PACKAGE}
    data.update(super().to_dict())
    return data

dplib.models.License

Bases: Model

Source code in dplib/models/license.py
6
7
8
9
class License(Model):
    name: Optional[str] = None
    path: Optional[str] = None
    title: Optional[str] = None

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

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

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

dplib.models.Source

Bases: Model

Source code in dplib/models/source.py
class Source(Model):
    title: Optional[str] = None
    path: Optional[str] = None
    email: Optional[str] = None
    version: Optional[str] = None

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

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

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

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

dplib.models.Contributor

Bases: Model

Source code in dplib/models/contributor.py
class Contributor(Model):
    title: Optional[str] = None
    givenName: Optional[str] = None
    familyName: Optional[str] = None
    path: Optional[str] = None
    email: Optional[str] = None
    roles: List[str] = []
    organization: Optional[str] = None

    # Compat

    @pydantic.model_validator(mode="before")
    @classmethod
    def compat(cls, data: types.IDict):
        if not isinstance(data, dict):  # type: ignore
            return data

        # contributor.role
        if not data.get("roles"):
            role = data.pop("role", None)
            if role:
                data["roles"] = [role]

        return data

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

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

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

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

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

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

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

compat(data) classmethod

Source code in dplib/models/contributor.py
@pydantic.model_validator(mode="before")
@classmethod
def compat(cls, data: types.IDict):
    if not isinstance(data, dict):  # type: ignore
        return data

    # contributor.role
    if not data.get("roles"):
        role = data.pop("role", None)
        if role:
            data["roles"] = [role]

    return data