Skip to content

dependencies

dev_tool.services.python.package.dependencies

log = logging.getLogger(__name__) module-attribute

SourceType

Bases: StrEnum

An enumeration of dependency source types.

This enum defines the types of sources from which dependencies can be installed.

PYPROJECT = 'pyproject' class-attribute instance-attribute

REQUIREMENTS = 'requirements' class-attribute instance-attribute

DependencySource dataclass

A class for representing a source of dependencies.

This class holds information about where dependencies can be installed from, either requirements files or pyproject.toml extras.

name instance-attribute

source instance-attribute

path = None class-attribute instance-attribute

extra = None class-attribute instance-attribute

priority = 0 class-attribute instance-attribute

available property

A method that checks if this dependency source is available.

This method determines if the source exists and contains content.

Returns:

  • bool

    True if the source is available, False otherwise.

formatted

A method that returns a formatted string representation of the source.

This method creates a human-readable description of the dependency source.

Returns:

  • str

    A formatted string describing the dependency source.

Source code in dev_tool/services/python/package/dependencies.py
def formatted(self) -> str:
    """
    A method that returns a formatted string representation of the source.

    This method creates a human-readable description of the dependency source.

    :return: A formatted string describing the dependency source.
    """

    if self.source == SourceType.REQUIREMENTS:
        if self.path is None:
            return self.name

        return f'{self.name} ({self.path.name})'

    extra = f' [extra: {self.extra}]' if self.extra else ' [base]'
    return f'{self.name}{extra}'

DependencyResolver

A class for discovering and resolving dependency sources.

This class provides methods for finding all available dependency sources and determining the best installation strategy.

The constructor for the DependencyResolver class.

This method initializes the resolver with a config manager and sets up the patterns for discovering dependency sources.

Source code in dev_tool/services/python/package/dependencies.py
def __init__(self) -> None:
    """
    The constructor for the DependencyResolver class.

    This method initializes the resolver with a config manager and
    sets up the patterns for discovering dependency sources.
    """

    self._requirements = [
        'requirements.txt',
        'development_requirements.txt',
        'production_requirements.txt',
        'mkdocs_requirements.txt',
        'test_requirements.txt'
    ]

clean_content

A method that returns requirements file content with -r references removed.

This method reads a requirements file and filters out any -r directives to prevent redundant installations while keeping the file's own dependencies.

Parameters:

  • requirements (Path) –

    The path to the requirements file.

Returns:

  • str

    The cleaned requirements content as a string.

Source code in dev_tool/services/python/package/dependencies.py
def clean_content(self, requirements: Path) -> str:
    """
    A method that returns requirements file content with `-r` references removed.

    This method reads a requirements file and filters out any `-r` directives
    to prevent redundant installations while keeping the file's own dependencies.

    :param requirements: The path to the requirements file.
    :return: The cleaned requirements content as a string.
    """

    if not requirements.exists():
        return ''

    try:
        with open(requirements, 'r', encoding='utf-8') as handle:
            lines = handle.readlines()

        cleaned = [
            line for line in lines
            if not line.strip().startswith('-r ')
        ]

        return ''.join(cleaned)
    except Exception:
        return ''

discover_sources

A method that discovers all available dependency sources.

This method finds both requirements files and pyproject.toml sources and returns them sorted by priority.

Returns:

Source code in dev_tool/services/python/package/dependencies.py
def discover_sources(self) -> list[DependencySource]:
    """
    A method that discovers all available dependency sources.

    This method finds both requirements files and pyproject.toml sources
    and returns them sorted by priority.

    :return: A list of all discovered dependency sources.
    """

    sources = []

    requirements = self._discover_requirements_sources()
    sources.extend(requirements)

    pyproject = self._discover_pyproject_sources()
    sources.extend(pyproject)

    return sorted(sources, key=lambda x: x.priority)

get_available_sources

A method that gets formatted descriptions of available dependency sources.

This method discovers sources and returns formatted descriptions organized by source type.

Returns:

  • dict[str, list[str]]

    A dictionary mapping source types to formatted source descriptions.

Source code in dev_tool/services/python/package/dependencies.py
def get_available_sources(self) -> dict[str, list[str]]:
    """
    A method that gets formatted descriptions of available dependency sources.

    This method discovers sources and returns formatted descriptions
    organized by source type.

    :return: A dictionary mapping source types to formatted source descriptions.
    """

    sources = self.discover_sources()

    result = {
        'requirements': [],
        'pyproject': []
    }

    for source in sources:
        if source.available:
            formatted = source.formatted()

            if source.source == SourceType.REQUIREMENTS:
                result['requirements'].append(formatted)
            else:
                result['pyproject'].append(formatted)

    return result

get_fallback_strategy

A method that gets the fallback installation strategy.

This method returns the opposite type of sources for fallback installation.

Parameters:

Returns:

Source code in dev_tool/services/python/package/dependencies.py
def get_fallback_strategy(self, sources: list[DependencySource]) -> list[DependencySource]:
    """
    A method that gets the fallback installation strategy.

    This method returns the opposite type of sources for fallback installation.

    :param sources: The primary sources that failed.
    :return: The fallback sources to try.
    """

    primary_types = {source.source for source in sources}

    if SourceType.PYPROJECT in primary_types:
        return [
            source
            for source in self.discover_sources()
            if source.source == SourceType.REQUIREMENTS
        ]

    return [
        source
        for source in self.discover_sources()
        if source.source == SourceType.PYPROJECT
    ]

get_strategy

A method that gets the installation strategy for dependencies.

This method returns either all sources for automatic detection or filters sources based on requested dependency types.

Parameters:

  • requested (list[str] | None, default: None ) –

    Optional list of specific dependency types to install.

Returns:

Source code in dev_tool/services/python/package/dependencies.py
def get_strategy(self, requested: list[str] | None = None) -> list[DependencySource]:
    """
    A method that gets the installation strategy for dependencies.

    This method returns either all sources for automatic detection
    or filters sources based on requested dependency types.

    :param requested: Optional list of specific dependency types to install.
    :return: The dependency sources to install based on the strategy.
    """

    sources = self.discover_sources()

    if not sources:
        return []

    if requested is None:
        return self._get_automatic_strategy(sources)

    return [
        source
        for source in sources
        if source.name in requested
    ]