Direct URLs

Parse and validate direct_url.json files.

Usage

import json
from pathlib import Path

from packaging.direct_url import ArchiveInfo, DirectUrl, DirInfo, VcsInfo

# A VCS direct URL
vcs_direct_url = DirectUrl(
    url="https://git.example.com/repo.git",
    vcs_info=VcsInfo(
        vcs="git",
        commit_id="2df7bdd8dfef7b879390b9fc4016f02af2f118d4",
        requested_revision="1.1.0",
    ),
)

# An archive direct URL
archive_direct_url = DirectUrl(
    url="https://example.com/archive.tar.gz",
    archive_info=ArchiveInfo(
        hashes={
            "sha256": "dc321a1c18a37b5438424ef3714524229dab5f4f78b297671359426fef51be6c"
        }
    ),
)

# A local editable direct URL
archive_direct_url = DirectUrl(
    url="file:///home/project/example",
    dir_info=DirInfo(
        editable=True,
    ),
)

# Serialization to JSON
Path("/tmp/direct_url.json").write_text(
    json.dumps(vcs_direct_url.to_dict()), encoding="utf-8"
)

# Load from JSON. The resulting DirectUrl object is validated against the
# specification, else a DirectUrlalidationError is raised
direct_url = DirectUrl.from_dict(
    json.loads(Path("/tmp/direct_url.json").read_text(encoding="utf-8"))
)

# You can validate a manually constructed DirectUrl class
vcs_direct_url.validate()

Reference

class packaging.direct_url.DirectUrl

A class representing a direct URL.

classmethod from_dict(d, /)

Create and validate a DirectUrl instance from a JSON dictionary.

Parameters:

d (Mapping[str, Any])

Return type:

Self

to_dict(*, generate_legacy_hash=False, strip_user_password=True, safe_user_passwords=('git',))

Convert the DirectUrl instance to a JSON dictionary.

Parameters:
  • generate_legacy_hash (bool) – If True, include a legacy hash field in archive_info for backward compatibility with tools that don’t support the hashes field.

  • strip_user_password (bool) – If True, strip user:password from the URL unless it is formed with environment variables as specified in PEP 610, or it is a safe user:password such as git.

  • safe_user_passwords (Collection[str]) – A collection of user:password strings that should not be stripped from the URL even if strip_user_password is True.

Return type:

Mapping[str, Any]

validate()

Validate the DirectUrl instance against the specification.

Raises DirectUrlValidationError if invalid.

Return type:

None

class packaging.direct_url.ArchiveInfo
class packaging.direct_url.DirInfo
class packaging.direct_url.VcsInfo

The following exception may be raised by this module:

exception packaging.direct_url.DirectUrlValidationError

Raised when when input data is not spec-compliant.