Skip to content

Github

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

Installation

Extra dependency needs to be installed:

pip install dplib-py[github]

Usage

Converting a Github descriptor to the Data Package notation:

from dplib.plugins.github.models import GithubPackage

package = GithubPackage.from_path("data/plugins/github/package.json").to_dp()
print(package.to_text(format='json'))
{
  "name": "octocat/Hello-World",
  "title": "Hello-World",
  "description": "This your first repo!",
  "homepage": "https://github.com/octocat/Hello-World",
  "licenses": [
    {
      "name": "MIT",
      "title": "MIT License"
    }
  ],
  "keywords": ["octocat", "atom", "electron", "api"],
  "created": "2011-01-26T19:01:12Z"
}

Converting a Data Package to Github notation:

from dplib.models import Package
from dplib.plugins.github.models import GithubPackage

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

Reference

dplib.plugins.github.models.GithubPackage

Bases: Model

Github Package model

Source code in dplib/plugins/github/models/package.py
class GithubPackage(Model):
    """Github Package model"""

    resources: List[GithubResource] = []

    license: Optional[GithubLicense] = None
    owner: Optional[GithubOwner] = None

    private: bool = False
    name: Optional[str] = None
    full_name: Optional[str] = None
    html_url: Optional[str] = None
    description: Optional[str] = None
    pushed_at: Optional[str] = None
    created_at: Optional[str] = None
    updated_at: Optional[str] = None
    topics: List[str] = []

    # Converters

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

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

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

        # Name
        if self.full_name:
            package.name = self.full_name

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

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

        # Created
        if self.created_at:
            package.created = self.created_at

        # License
        if self.license:
            license = License()
            if self.license.spdx_id:
                license.name = self.license.spdx_id
            if self.license.name:
                license.title = self.license.name
            if self.license.html_url:
                license.path = self.license.html_url
            package.licenses.append(license)

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

        # Resources
        for item in self.resources:
            resource = item.to_dp()
            package.resources.append(resource)

        return package

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

        Parameters:
            package: Data Package

        Returns:
            Github Package
        """
        github = GithubPackage()

        # Title
        if package.title:
            github.name = package.title

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

        # Licenses
        if package.licenses:
            license = package.licenses[0]
            github.license = GithubLicense()
            if license.name:
                github.license.spdx_id = license.name
            if license.title:
                github.license.name = license.title
            if license.path:
                github.license.html_url = license.path

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

        # Resources
        for resource in package.resources:
            item = GithubResource.from_dp(resource)
            if item:
                github.resources.append(item)

        return github

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

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

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

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

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

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

owner: Optional[GithubOwner] = None class-attribute instance-attribute

private: bool = False class-attribute instance-attribute

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

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

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

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

from_dp(package) classmethod

Create a Github Package from Data Package

Parameters:

Name Type Description Default
package Package

Data Package

required

Returns:

Type Description
GithubPackage

Github Package

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

    Parameters:
        package: Data Package

    Returns:
        Github Package
    """
    github = GithubPackage()

    # Title
    if package.title:
        github.name = package.title

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

    # Licenses
    if package.licenses:
        license = package.licenses[0]
        github.license = GithubLicense()
        if license.name:
            github.license.spdx_id = license.name
        if license.title:
            github.license.name = license.title
        if license.path:
            github.license.html_url = license.path

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

    # Resources
    for resource in package.resources:
        item = GithubResource.from_dp(resource)
        if item:
            github.resources.append(item)

    return github

to_dp()

Convert to Data Package

Returns:

Type Description
Package

Data Package

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

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

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

    # Name
    if self.full_name:
        package.name = self.full_name

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

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

    # Created
    if self.created_at:
        package.created = self.created_at

    # License
    if self.license:
        license = License()
        if self.license.spdx_id:
            license.name = self.license.spdx_id
        if self.license.name:
            license.title = self.license.name
        if self.license.html_url:
            license.path = self.license.html_url
        package.licenses.append(license)

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

    # Resources
    for item in self.resources:
        resource = item.to_dp()
        package.resources.append(resource)

    return package

dplib.plugins.github.models.GithubResource

Bases: Model

Github Resource model

Source code in dplib/plugins/github/models/resource.py
class GithubResource(Model):
    """Github Resource model"""

    name: str
    path: str
    type: Literal["file"] = "file"
    content: Optional[str] = None
    encoding: Optional[str] = None
    size: Optional[int] = None
    sha: Optional[str] = None
    html_url: Optional[str] = None
    download_url: Optional[str] = None

    # Converters

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

        Returns:
           Data Resource
        """
        resource = Resource(path=self.path, name=slugify_name(self.path))

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

        # Hash
        if self.sha:
            resource.hash = f"sha1:{self.sha}"

        return resource

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

        Parameters:
            resource: Data Resource

        Returns:
            Github Resource
        """
        if not resource.path or not isinstance(resource.path, str):
            return

        # Path
        github = GithubResource(path=resource.path, name=resource.path)

        # Bytes
        if resource.bytes:
            github.size = resource.bytes

        # Hash
        hash = resource.get_hash()
        if hash:
            if hash.type == "sha1":
                github.sha = hash.value

        return github

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

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

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

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

name: str instance-attribute

path: str instance-attribute

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

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

type: Literal['file'] = 'file' class-attribute instance-attribute

from_dp(resource) classmethod

Create Github Resource from Data Resource

Parameters:

Name Type Description Default
resource Resource

Data Resource

required

Returns:

Type Description
Optional[GithubResource]

Github Resource

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

    Parameters:
        resource: Data Resource

    Returns:
        Github Resource
    """
    if not resource.path or not isinstance(resource.path, str):
        return

    # Path
    github = GithubResource(path=resource.path, name=resource.path)

    # Bytes
    if resource.bytes:
        github.size = resource.bytes

    # Hash
    hash = resource.get_hash()
    if hash:
        if hash.type == "sha1":
            github.sha = hash.value

    return github

to_dp()

Convert to Data Package resource

Returns:

Type Description
Resource

Data Resource

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

    Returns:
       Data Resource
    """
    resource = Resource(path=self.path, name=slugify_name(self.path))

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

    # Hash
    if self.sha:
        resource.hash = f"sha1:{self.sha}"

    return resource