unidep.utils moduleΒΆ

unidep - Unified Conda and Pip requirements management.

This module provides utility functions used throughout the package.

class unidep.utils.LocalDependency(local: str, pypi: str | None = None, use: LocalDependencyUse = 'local')[source]ΒΆ

Bases: NamedTuple

A local dependency with optional PyPI alternative and use mode.

local: strΒΆ

Alias for field number 0

pypi: str | NoneΒΆ

Alias for field number 1

use: Literal['local', 'pypi', 'skip']ΒΆ

Alias for field number 2

class unidep.utils.ParsedPackageStr(name: str, pin: str | None = None, selector: str | None = None)[source]ΒΆ

Bases: NamedTuple

A package name and version pinning.

name: strΒΆ

Alias for field number 0

pin: str | NoneΒΆ

Alias for field number 1

selector: str | NoneΒΆ

Alias for field number 2

class unidep.utils.PathWithExtras(path: Path, extras: list[str])[source]ΒΆ

Bases: NamedTuple

A dependency file and extras.

canonicalized()[source]ΒΆ

Resolve path and normalize extras for deterministic graph keys.

Return type:

PathWithExtras

extras: list[str]ΒΆ

Alias for field number 1

path: PathΒΆ

Alias for field number 0

property path_with_extras: PathΒΆ

Path including extras, e.g., path/to/file[test,docs].

resolved()[source]ΒΆ

Resolve the path and extras.

Return type:

PathWithExtras

exception unidep.utils.UnsupportedPlatformError[source]ΒΆ

Bases: Exception

Raised when the current platform is not supported.

unidep.utils.add_comment_to_file(filename, extra_lines=None)[source]ΒΆ

Add a comment to the top of a file.

Return type:

None

unidep.utils.build_pep508_environment_marker(platforms)[source]ΒΆ

Generate a PEP 508 selector for a list of platforms.

Return type:

str

unidep.utils.collect_selector_platforms(requirements, optional_dependencies=None)[source]ΒΆ

Collect all platforms referenced by dependency selectors.

Return type:

list[Literal['linux-64', 'linux-aarch64', 'linux-ppc64le', 'osx-64', 'osx-arm64', 'win-64']]

unidep.utils.dedupe(items)[source]ΒΆ

Remove duplicates from a list while preserving order.

Return type:

list[str]

unidep.utils.defaultdict_to_dict(d)[source]ΒΆ

Convert (nested) defaultdict to (nested) dict.

Return type:

dict

unidep.utils.escape_unicode(string)[source]ΒΆ

Escape unicode characters.

Return type:

str

unidep.utils.extract_matching_platforms(comment)[source]ΒΆ

Get all platforms matching a comment.

Return type:

list[Literal['linux-64', 'linux-aarch64', 'linux-ppc64le', 'osx-64', 'osx-arm64', 'win-64']]

unidep.utils.get_package_version(package_name)[source]ΒΆ

Returns the version of the given package.

Parameters:

package_name (str) – The name of the package to find the version of.

Return type:

The version of the package, or None if the package is not found.

unidep.utils.identify_current_platform()[source]ΒΆ

Detect the current platform.

Return type:

Literal['linux-64', 'linux-aarch64', 'linux-ppc64le', 'osx-64', 'osx-arm64', 'win-64']

unidep.utils.is_pip_installable(folder)[source]ΒΆ

Determine if the project is pip installable.

Checks for existence of setup.py or [build-system] in pyproject.toml. If the toml library is available, it is used to parse the pyproject.toml file. If the toml library is not available, the function checks for the existence of a line starting with β€œ[build-system]”. This does not handle the case where [build-system] is inside of a multi-line literal string.

Return type:

bool

unidep.utils.package_name_from_path(path)[source]ΒΆ

Get the package name from pyproject.toml, setup.cfg, or setup.py.

Return type:

str

unidep.utils.package_name_from_pyproject_toml(file_path)[source]ΒΆ

Read project name from pyproject.toml (PEP 621 or Poetry).

Return type:

str

unidep.utils.package_name_from_setup_cfg(file_path)[source]ΒΆ

Read the package name from setup.cfg metadata.

Return type:

str

unidep.utils.package_name_from_setup_py(file_path)[source]ΒΆ

Read the package name from a simple setup.py AST.

Return type:

str

unidep.utils.parse_folder_or_filename(folder_or_file)[source]ΒΆ

Get the path to requirements.yaml or pyproject.toml file.

Return type:

PathWithExtras

unidep.utils.parse_package_str(package_str)[source]ΒΆ

Splits a string into package name, version pinning, and platform selector.

Return type:

ParsedPackageStr

unidep.utils.remove_top_comments(filename)[source]ΒΆ

Removes the top comments (lines starting with β€˜#’) from a file.

Return type:

None

unidep.utils.resolve_platforms(*, requested_platforms, declared_platforms=None, selector_platforms=None, default_current=True)[source]ΒΆ

Resolve effective platforms with a shared precedence policy.

Precedence is: 1) explicitly requested platforms 2) declared platforms from requirements files 3) selector-derived platforms from dependency specs 4) current platform fallback (optional)

Return type:

list[Literal['linux-64', 'linux-aarch64', 'linux-ppc64le', 'osx-64', 'osx-arm64', 'win-64']]

unidep.utils.selector_from_comment(comment)[source]ΒΆ

Extract a valid selector from a comment.

Return type:

str | None

unidep.utils.split_path_and_extras(input_str)[source]ΒΆ

Parse a string of the form path/to/file[extra1,extra2] into parts.

Returns a tuple of the pathlib.Path and a list of extras

Return type:

tuple[Path, list[str]]

unidep.utils.unidep_configured_in_toml(path)[source]ΒΆ

Check if dependencies are specified in pyproject.toml.

Return type:

bool

unidep.utils.warn(message, category=<class 'UserWarning'>, stacklevel=1)[source]ΒΆ

Emit a warning with a custom format specific to this package.

Return type:

None