Package: poetry-plugin-package-info
Poetry Plugin: Package InfoThis package is a plugin that generates apackage_info.pyfile with variables containing values from pyproject.toml and git.InstallationThe easiest way to install thepackage-infoplugin is via theself addcommand of Poetry.poetryselfaddpoetry-plugin-package-infoIf you usedpipxto install Poetry you can add the plugin via thepipx injectcommand.pipxinjectpoetrypoetry-plugin-package-infoOtherwise, if you usedpipto install Poetry you can add the plugin packages via thepip installcommand.pipinstallpoetry-plugin-plugin-package-infoUsageBy default, thepackage-info.pyfile is generated only when using thepackage-info generate-filecommand in poetry,poetry package-info generate-fileThe plugin can be enabled to automatically patch wheel files afterpoetry buildis run by adding thepatch-wheelsto 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 filepackage-info-file-path="package_name_in_snake_case/package_info.py"# Search parent directories (relative to pyproject.toml) for .gitgit-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 examplepackage_info.pyfile."""Auto-generated by poetry-plugin-package-info at 2023-06-11T00:38:17."""importdatetimeclassPackageInfo: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=Noneproject_homepage:str|None=("https://github.com/bellis/poetry-plugin-package-info")project_maintainers:list[str]|None=Noneproject_keywords:list[str]|None=Nonegit_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=Truegit_is_dirty_excluding_untracked:bool|None=Truegit_has_staged_changes:bool|None=Falsegit_has_unstaged_changes:bool|None=Truegit_has_untracked_changes:bool|None=FalseHow-toChange variable namesIt 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 formatterTo create a custom formatter for the generatedpackage-info.pyfile, you can implement theContentFormatterabstract class.frompoetry_plugin_package_info.pluginimport(ContentFormatter,PackageInfoApplicationPlugin,)classMyContentFormatter(ContentFormatter):definit(self,plugin:PackageInfoApplicationPlugin,)->None:"""Initialise the ContentFormatter for the provided plugin."""...defformat_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 thepyproject.tomlfile.formatters=["poetry-plugin-package-info.formatters.black:BlackContentFormatter","my_package.formatters.my_formatter:MyContentFormatter",]Create a custom generatorTo create a custom generator for the generatedpackage-info.pyfile, you can implement thePropertyGeneratorabstract class.frompoetry_plugin_package_info.pluginimport(PackageInfoApplicationPlugin,Property,PropertyConfig,PropertyGenerator,)classMyPropertyGenerator(PropertyGenerator):defshort_name(self)->str:"""Shortname/Prefix for properties belonging to this generator."""definit(self,plugin:PackageInfoApplicationPlugin,)->None:"""Initialise the PropertyGenerator for the provided plugin."""defgenerate_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 thepyproject.tomlfile.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 Projectswebsite: The official Poetry website and blogpoetry-plugin-export: Export Poetry projects/lock files to
foreign formats like requirements.txt (Used some test code from this project)