Manage Package Metadata
Warning: This project is very early alpha release, so all APIs could change with little or no notice.
Python packages have metadata describing information like the package name, version, etc. The details of this metadata are defined in the Packaging User Guide. When stored in a file (either on the filesystem, or in a package distribution) the metadata is saved in a format which is based on email headers.
This library transforms that metadata to and from a JSON-compatible form, as defined in PEP 566. The JSON form is easier to use in a programming context. Two functions are provided:
msg_to_json(msg)- convert the email header format to JSON. Themsgargument is the metadata in email format, as anemail.message.Messageobject. Returns a dictionary following the layout in the "json" form.json_to_msg(json)- convert the JSON form back to email headers. Thejsonargument is a dictionary following the "json" form. Returns a (Unicode) string with the message form.
Note the discrepancy between the two msg forms: a Message object
and a string. This is something that may change, as it's a bit of
an awkward discrepancy, but there are reasons for this approach:
- When reading metadata, the file is supposed to be in the UTF-8
encoding, but historically this has not always been the case.
By using a
Messageas the input, this can be constructed from either text or bytes (message_from_stringormessage_from_bytes) which allows the email package to handle encoding issues. If a project uses non-UTF8 metadata, it's likely that this approach will result in mojibake, but at least the data will be usable. - When writing metadata, using a
Messageobject results in unwanted header fields, because the object assumes this is a "real" email, and not just data re-using that format. So it is more reliable to simply return the output in string format. It can then be written to a file (in UTF-8) as required.
An example of using the library:
with open(metadata_file, "r", encoding="utf-8") as f:
msg = email.message_from_file(f)
metadata = pkg_metadata.msg_to_json(msg)
metadata["keywords"] = ["example", "artificial"]
with open(metadata_file, "w", encoding="utf-8") as f:
f.write(json_to_msg(metadata))
In addition to the metadata file format, project metadata can
also be specified in the pyproject.toml file, in TOML
format as specified in PEP 621.
This library provides a function to read the [project] section
of pyproject.toml and convert it into a ("JSON format") metadata
dictionary.
pyproject_to_json(pyproject)- convertpyproject.tomlmetadata into a metadata dictionary. Thepyprojectargument is a dictionary representing the data in the[project]section ofpyproject.toml.
Example:
with open("pyproject.toml", "rb") as f:
pyproject_data = tomli.load(f)
metadata = pkg_metadata.pyproject_to_json(pyproject_data["project"])
Metadata
Release files for pkg-metadata 0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pkg_metadata-0.3.tar.gz | 5.9 kB | Without provenance | Details for pkg_metadata-0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pkg_metadata-0.3-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for pkg_metadata-0.3-py2.py3-none-any.whl |
Total release size: 12.5 kB
Release files / pkg_metadata-0.3.tar.gz
| Download URL | pkg_metadata-0.3.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
15b4fe318292fc99e16c4ed5d27e82ca2884ec7c84643696593f170be4768f5f
|
|
BLAKE2b-256 checksum How to use checksums |
94cae33214f1a8f97ce328e7eb8a83fcf1b4a100d1b948b5d5a8dc9a8de35072
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/31.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.4.0 rfc3986/1.5.0 colorama/0.4.4 CPython/3.10.3
|
Release files / pkg_metadata-0.3-py2.py3-none-any.whl
| Download URL | pkg_metadata-0.3-py2.py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
3dcd672608c1bfec9274c55b73561b142919a3310bf6bb765348fccded21e88f
|
|
BLAKE2b-256 checksum How to use checksums |
e9ff8e8a48c8abef2396428c70a16183a19ea3b3e56a7fbce712c08385368de7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/31.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.4.0 rfc3986/1.5.0 colorama/0.4.4 CPython/3.10.3
|
Security for pkg-metadata 0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.