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)[source]

Bases: NamedTuple

A local dependency with optional PyPI alternative.

local: str

Alias for field number 0

pypi: str | None

Alias for field number 1

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.

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.change_directory(new_path)[source]

A context manager to change the current working directory.

Return type:

Generator[None, None, None]

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.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.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