Lock Files¶
Parse and validate pylock.toml files.
Usage¶
import tomllib
from pathlib import Path
from packaging.pylock import Package, PackageWheel, Pylock
from packaging.utils import NormalizedName
from packaging.version import Version
# validate a pylock file name
assert is_valid_pylock_path(Path("pylock.example.toml"))
# parse and validate pylock file
toml_dict = tomllib.loads(Path("pylock.toml").read_text(encoding="utf-8"))
pylock = Pylock.from_dict(toml_dict)
# the resulting pylock object is validated against the specification,
# else a PylockValidationError is raised
# generate a pylock file
pylock = Pylock(
lock_version=Version("1.0"),
created_by="some_tool",
packages=[
Package(
name=NormalizedName("example-package"),
version=Version("1.0.0"),
wheels=[
PackageWheel(
url="https://example.com/example_package-1.0.0-py3-none-any.whl",
hashes={"sha256": "0fd.."},
)
],
)
],
)
toml_dict = pylock.to_dict()
# use a third-party library to serialize to TOML
# you can validate a manually constructed Pylock class
pylock.validate()
Reference¶
- packaging.pylock.is_valid_pylock_path(path)¶
Check if the given path is a valid pylock file path.
- Parameters:
path (Path)
- Return type:
The following frozen keyword-only dataclasses are used to represent the structure of a pylock file. The attributes correspond to the fields in the pylock file specification.
- class packaging.pylock.Pylock¶
A class representing a pylock file.
- classmethod from_dict(d, /)¶
Create and validate a Pylock instance from a TOML dictionary.
Raises
PylockValidationErrorif the input data is not spec-compliant.- Parameters:
d (Mapping[str, Any])
- Return type:
Self
- validate()¶
Validate the Pylock instance against the specification.
Raises
PylockValidationErrorotherwise.- Return type:
None
- class packaging.pylock.Package¶
- class packaging.pylock.PackageWheel¶
- class packaging.pylock.PackageSdist¶
- class packaging.pylock.PackageArchive¶
- class packaging.pylock.PackageVcs¶
- class packaging.pylock.PackageDirectory¶
The following exception may be raised by this module:
- exception packaging.pylock.PylockValidationError¶
Raised when when input data is not spec-compliant.
- exception packaging.pylock.PylockUnsupportedVersionError¶
Raised when encountering an unsupported lock_version.