hookspec#

Pluggy 钩子规范(“hookspecs”)用于注册 conda 插件。

CondaSpecs 中定义的每个 hookspec 都包含如何使用它的示例。

#

CondaSpecs

conda 插件 hookspecs,供开发者使用。

属性#

spec_name

用于组织 conda 钩子规范的名称

_hookspec

conda 插件 hookspecs,供开发者使用

hookimpl

用于标记插件钩子实现的装饰器

spec_name = 'conda'#

用于组织 conda 钩子规范的名称

_hookspec#

conda 插件 hookspecs,供开发者使用

hookimpl#

用于标记插件钩子实现的装饰器

class CondaSpecs#

conda 插件 hookspecs,供开发者使用。

conda_solvers() collections.abc.Iterable[conda.plugins.types.CondaSolver]#

在 conda 中注册求解器。

示例

import logging

from conda import plugins
from conda.core import solve

log = logging.getLogger(__name__)


class VerboseSolver(solve.Solver):
    def solve_final_state(self, *args, **kwargs):
        log.info("My verbose solver!")
        return super().solve_final_state(*args, **kwargs)


@plugins.hookimpl
def conda_solvers():
    yield plugins.CondaSolver(
        name="verbose-classic",
        backend=VerboseSolver,
    )
返回:

求解器条目的可迭代对象。

conda_subcommands() collections.abc.Iterable[conda.plugins.types.CondaSubcommand]#

在 conda 中注册外部子命令。

示例

from conda import plugins


def example_command(args):
    print("This is an example command!")


@plugins.hookimpl
def conda_subcommands():
    yield plugins.CondaSubcommand(
        name="example",
        summary="example command",
        action=example_command,
    )
返回:

子命令条目的可迭代对象。

conda_virtual_packages() collections.abc.Iterable[conda.plugins.types.CondaVirtualPackage]#

在 Conda 中注册虚拟包。

示例

from conda import plugins


@plugins.hookimpl
def conda_virtual_packages():
    yield plugins.CondaVirtualPackage(
        name="my_custom_os",
        version="1.2.3",
        build="x86_64",
    )
返回:

虚拟包条目的可迭代对象。

conda_pre_commands() collections.abc.Iterable[conda.plugins.types.CondaPreCommand]#

在 conda 中注册前置命令函数。

示例

from conda import plugins


def example_pre_command(command):
    print("pre-command action")


@plugins.hookimpl
def conda_pre_commands():
    yield plugins.CondaPreCommand(
        name="example-pre-command",
        action=example_pre_command,
        run_for={"install", "create"},
    )
conda_post_commands() collections.abc.Iterable[conda.plugins.types.CondaPostCommand]#

在 conda 中注册后置命令函数。

示例

from conda import plugins


def example_post_command(command):
    print("post-command action")


@plugins.hookimpl
def conda_post_commands():
    yield plugins.CondaPostCommand(
        name="example-post-command",
        action=example_post_command,
        run_for={"install", "create"},
    )
conda_auth_handlers() collections.abc.Iterable[conda.plugins.types.CondaAuthHandler]#

注册从 requests API 派生的 conda 身份验证处理程序。

此插件钩子允许附加 requests 身份验证处理程序子类,例如,当针对 HTTP/HTTPS 服务上托管的各个频道进行身份验证请求时。

示例

import os
from conda import plugins
from requests.auth import AuthBase


class EnvironmentHeaderAuth(AuthBase):
    def __init__(self, *args, **kwargs):
        self.username = os.environ["EXAMPLE_CONDA_AUTH_USERNAME"]
        self.password = os.environ["EXAMPLE_CONDA_AUTH_PASSWORD"]

    def __call__(self, request):
        request.headers["X-Username"] = self.username
        request.headers["X-Password"] = self.password
        return request


@plugins.hookimpl
def conda_auth_handlers():
    yield plugins.CondaAuthHandler(
        name="environment-header-auth",
        handler=EnvironmentHeaderAuth,
    )
conda_health_checks() collections.abc.Iterable[conda.plugins.types.CondaHealthCheck]#

为 conda doctor 注册健康检查。

此插件钩子允许您向 conda doctor 添加更多“健康检查”,您可以编写这些检查来诊断 conda 环境中的问题。查看 conda 已经附带的健康检查以获取灵感。

示例

from conda import plugins


def example_health_check(prefix: str, verbose: bool):
    print("This is an example health check!")


@plugins.hookimpl
def conda_health_checks():
    yield plugins.CondaHealthCheck(
        name="example-health-check",
        action=example_health_check,
    )
conda_pre_solves() collections.abc.Iterable[conda.plugins.types.CondaPreSolve]#

在 conda 中注册预求解函数,这些函数在通用求解器 API 中使用,在求解器处理包规范以搜索解决方案之前。

示例

from conda import plugins
from conda.models.match_spec import MatchSpec


def example_pre_solve(
    specs_to_add: frozenset[MatchSpec],
    specs_to_remove: frozenset[MatchSpec],
):
    print(f"Adding {len(specs_to_add)} packages")
    print(f"Removing {len(specs_to_remove)} packages")


@plugins.hookimpl
def conda_pre_solves():
    yield plugins.CondaPreSolve(
        name="example-pre-solve",
        action=example_pre_solve,
    )
conda_post_solves() collections.abc.Iterable[conda.plugins.types.CondaPostSolve]#

在 conda 中注册后求解函数,这些函数在通用求解器 API 中使用,在求解器提供要添加到或从 conda 环境中删除的包记录之后。

示例

from conda import plugins
from conda.models.records import PackageRecord


def example_post_solve(
    repodata_fn: str,
    unlink_precs: tuple[PackageRecord, ...],
    link_precs: tuple[PackageRecord, ...],
):
    print(f"Uninstalling {len(unlink_precs)} packages")
    print(f"Installing {len(link_precs)} packages")


@plugins.hookimpl
def conda_post_solves():
    yield plugins.CondaPostSolve(
        name="example-post-solve",
        action=example_post_solve,
    )
conda_settings() collections.abc.Iterable[conda.plugins.types.CondaSetting]#

注册新设置

下面的示例定义了一个简单的字符串类型参数

示例

from conda import plugins
from conda.common.configuration import PrimitiveParameter, SequenceParameter


@plugins.hookimpl
def conda_settings():
    yield plugins.CondaSetting(
        name="example_option",
        description="This is an example option",
        parameter=PrimitiveParameter("default_value", element_type=str),
        aliases=("example_option_alias",),
    )
conda_reporter_backends() collections.abc.Iterable[conda.plugins.types.CondaReporterBackend]#

注册新的报告器后端

下面的示例定义了一个使用 Python 中的 pprint 模块的报告器后端。

示例

from pprint import pformat

from conda import plugins
from conda.plugins.types import (
    CondaReporterBackend,
    ReporterRendererBase,
    ProgressBarBase,
)


class PprintReporterRenderer(ReporterRendererBase):
    "Implementation of the ReporterRendererBase"

    def detail_view(self, data):
        return pformat(data)

    def envs_list(self, data):
        formatted_data = pformat(data)
        return f"Environments: {formatted_data}"

    def progress_bar(self, description, io_context_manager) -> ProgressBarBase:
        "Returns our custom progress bar implementation"
        return PprintProgressBar(description, io_context_manager)


class PprintProgressBar(ProgressBarBase):
    "Blank implementation of ProgressBarBase which does nothing"

    def update_to(self, fraction) -> None:
        pass

    def refresh(self) -> None:
        pass

    def close(self) -> None:
        pass


@plugins.hookimpl
def conda_reporter_backends():
    yield CondaReporterBackend(
        name="pprint",
        description="Reporter backend based on the pprint module",
        renderer=PprintReporterRenderer,
    )
conda_session_headers(host: str) collections.abc.Iterable[conda.plugins.types.CondaRequestHeader]#

注册新的 HTTP 请求标头

下面的示例定义了如何为所有主机名为 example.com 的请求添加 HTTP 标头。

示例

from conda import plugins

HOSTS = {"example.com", "sub.example.com"}


@plugins.hookimpl
def conda_session_headers(host: str):
    if host in HOSTS:
        yield plugins.CondaRequestHeader(
            name="Example-Header",
            value="example",
        )
conda_request_headers(host: str, path: str) collections.abc.Iterable[conda.plugins.types.CondaRequestHeader]#

注册新的 HTTP 请求标头

下面的示例定义了如何为所有主机名为 example.compath/to/endpoint.json 路径的请求添加 HTTP 标头。

示例

from conda import plugins

HOSTS = {"example.com", "sub.example.com"}
ENDPOINT = "/path/to/endpoint.json"


@plugins.hookimpl
def conda_request_headers(host: str, path: str):
    if host in HOSTS and path == ENDPOINT:
        yield plugins.CondaRequestHeader(
            name="Example-Header",
            value="example",
        )