Skip to main content

Plugin for poetry that creates/updates a package_info.py file with git and project file details.

Project description

Poetry Plugin: Package Info

PyPi Stable Version Pre-release Version Python Versions Code coverage Status PyTest Download Stats License: MIT pre-commit Code Style

This package is a plugin that generates a package_info.py file with variables containing values from pyproject.toml and git.

Installation

The easiest way to install the package-info plugin is via the self add command of Poetry.

poetry self add poetry-plugin-package-info

If you used pipx to install Poetry you can add the plugin via the pipx inject command.

pipx inject poetry poetry-plugin-package-info

Otherwise, if you used pip to install Poetry you can add the plugin packages via the pip install command.

pip install poetry-plugin-plugin-package-info

Usage

By default, the package-info.py file is generated only when using the package-info generate-file command in poetry,

poetry package-info generate-file

The plugin can be enabled to automatically patch wheel files after poetry build is run by adding the patch-wheels to the project pyproject.toml file.

[tool.poetry-plugin-package-info]
patch-build-formats = ['wheel', 'sdist']  # or can just do ['all']

The plugin can be re-configured in the pyproject.toml file, below are the options and their defaults.

[tool.poetry-plugin-package-info]

# Patch any .whl files produced by `poetry build`
patch-build-formats = []

# The path relative to the pyproject.toml file
package-info-file-path = "package_name_in_snake_case/package_info.py"

# Search parent directories (relative to pyproject.toml) for .git
git-search-parent-directories = false

# The formatter to format the generated package_info.py file.
formatters = [ "poetry-plugin-package-info.formatters.black:BlackContentFormatter" ]

# The generators to use to extract property values.
generators = { project = "poetry-plugin-package-info.generators.project:ProjectPropertyGenerator", git = "poetry-plugin-package-info.generators.git:GitPropertyGenerator" }

template = """\
\"\"\"Auto-generated by poetry-plugin-package-info at {{ now().replace(microsecond=0).isoformat()  }}.\"\"\"\
{% for import in imports %}
import {{import}}
{% endfor %}
class PackageInfo:
{% for property in properties %}\
{{ "    " }}{{property.property_config.variable_name}}: {{as_python(property.property_type)}} = {{as_python(property.property_value)}}
{% endfor %}
"""

# ordered list of variables to include in the file.
properties = [
    "project-name",
    "project-description",
    "project-version",
    "project-authors",
    "project-license",
    "project-classifiers",
    "project-documentation",
    "project-repository",
    "project-homepage",
    "project-maintainers",
    "project-keywords",
    "git-commit-id",
    "git-commit-author-name",
    "git-commit-author-email",
    "git-commit-timestamp",
    "git-branch-name",
    "git-branch-path",
    "git-has-staged-changes",
    "git-has-unstaged-changes",
    "git-has-changes"
]

Give the defaults, below is an example package_info.py file.

"""Auto-generated by poetry-plugin-package-info at 2023-06-11T00:38:17."""
import datetime


class PackageInfo:
    project_name: str | None = "poetry-plugin-package-info"
    project_description: str | None = "Plugin for poetry that creates/updates a package_info.py file with various details about the project/package."
    project_version: str | None = "0.2.0"
    project_authors: list[str] | None = ["Ben Ellis <ben.ellis@softweyr.co.uk>"]
    project_license: str | None = "MIT"
    project_classifiers: list[str] | None = [
        "Intended Audience :: Developers",
        "Intended Audience :: Information Technology",
        "Intended Audience :: System Administrators",
        "Operating System :: OS Independent",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python",
        "Topic :: Software Development :: Libraries :: Application Frameworks",
        "Topic :: Software Development :: Libraries :: Python Modules",
        "Topic :: Software Development :: Libraries",
        "Topic :: Software Development :: Version Control :: Git",
        "Topic :: Software Development",
        "Topic :: System :: Archiving :: Packaging",
        "Topic :: System :: Installation/Setup",
        "Topic :: System :: Software Distribution",
        "Development Status :: 3 - Alpha",
        "Programming Language :: Python :: 3 :: Only",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
        "Programming Language :: Python :: 3.10",
        "License :: OSI Approved :: MIT License",
    ]
    project_documentation: str | None = (
        "https://github.com/bellis/poetry-plugin-package-info"
    )
    project_repository: str | None = None
    project_homepage: str | None = (
        "https://github.com/bellis/poetry-plugin-package-info"
    )
    project_maintainers: list[str] | None = None
    project_keywords: list[str] | None = None
    git_commit_id: str | None = "b69755ca54300baaabe5f92bc99b7e101712739b"
    git_commit_author_name: str | None = "Ben Ellis"
    git_commit_author_email: str | None = "ben.ellis@softweyr.co.uk"
    git_commit_timestamp: datetime.datetime | None = datetime.datetime.fromisoformat(
        "2023-06-11T00:31:29+01:00"
    )
    git_branch_name: str | None = "main"
    git_branch_path: str | None = "refs/heads/main"
    git_is_dirty: bool | None = True
    git_is_dirty_excluding_untracked: bool | None = True
    git_has_staged_changes: bool | None = False
    git_has_unstaged_changes: bool | None = True
    git_has_untracked_changes: bool | None = False

How-to

Change variable names

It is possible to override the name of the generated variable by expanding the properties section to

[tool.poetry-plugin-package-info]

properties = [
    "project-name",
    "project-description",
    "git-commit-id",
    { "property-generator" = "git", "property-name" = "is-dirty", "variable_name" = "clean_me" },
    { "property-name" = "git-is-dirty", "variable_name" = "clean_me_too" }
]

Create a custom formatter

To create a custom formatter for the generated package-info.py file, you can implement the ContentFormatter abstract class.

from poetry_plugin_package_info.plugin import (
    ContentFormatter,
    PackageInfoApplicationPlugin,
)

class MyContentFormatter(ContentFormatter):
    def init(
        self,
        plugin: PackageInfoApplicationPlugin,
    ) -> None:
        """Initialise the ContentFormatter for the provided plugin."""
        ...

    def format_content(self, content: str) -> str:
        """Format the given python file content."""
        ...

Once your class is available, you can add it to the formatter configuration in the pyproject.toml file.

formatters = [
    "poetry-plugin-package-info.formatters.black:BlackContentFormatter",
    "my_package.formatters.my_formatter:MyContentFormatter",
]

Create a custom generator

To create a custom generator for the generated package-info.py file, you can implement the PropertyGenerator abstract class.

from poetry_plugin_package_info.plugin import (
    PackageInfoApplicationPlugin,
    Property,
    PropertyConfig,
    PropertyGenerator,
)

class MyPropertyGenerator(PropertyGenerator):

    def short_name(self) -> str:
        """Shortname/Prefix for properties belonging to this generator."""

    def init(
        self,
        plugin: PackageInfoApplicationPlugin,
    ) -> None:
        """Initialise the PropertyGenerator for the provided plugin."""

    def generate_property(
        self,
        property_config: PropertyConfig,
    ) -> Property:
        """Generate the property for the given include configuration."""

Once your class is available, you can add it to the generators configuration in the pyproject.toml file.

generators = { mycustom = "my_package.generators.my_generator:MyPropertyGenerator", project = "poetry-plugin-package-info.generators.project:ProjectPropertyGenerator", git = "poetry-plugin-package-info.generators.git:GitPropertyGenerator" }

Related Projects

  • website: The official Poetry website and blog
  • poetry-plugin-export: Export Poetry projects/lock files to foreign formats like requirements.txt (Used some test code from this project)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

poetry_plugin_package_info-1.0.0.tar.gz (40.9 kB view details)

Uploaded Source

Built Distribution

poetry_plugin_package_info-1.0.0-py3-none-any.whl (45.2 kB view details)

Uploaded Python 3

File details

Details for the file poetry_plugin_package_info-1.0.0.tar.gz.

File metadata

File hashes

Hashes for poetry_plugin_package_info-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9cfa0f7b780b6f64a8973aef873bd4435efbfc136b6467b87f74f7af91e4e950
MD5 a2b75bdf4cdd1f456e789ee535a81ef8
BLAKE2b-256 c0e710d06d346185e4b87c35db08b141fa656bd640bb9853548d5f651c4c5549

See more details on using hashes here.

File details

Details for the file poetry_plugin_package_info-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for poetry_plugin_package_info-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9b37b9dbe70735ac9ff411def76e56337ce2962267ff28a9bee01b31bc5cb2a5
MD5 f5f54843490a05890e7b3e7fe78472a2
BLAKE2b-256 fb31bc8155f00408e24f6d38c3982230ba18878aa03217daf1cd980a55fcbbf9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page