From f56ba15f9f88105349bdadfe1fafa5616bd65fb1 Mon Sep 17 00:00:00 2001 From: David Davis Date: Wed, 28 Apr 2021 10:57:28 -0400 Subject: [PATCH] needs_plugins with PluginRequirements PoC [noissue] --- pulpcore/cli/ansible/distribution.py | 9 +++++-- pulpcore/cli/ansible/repository.py | 5 ++-- pulpcore/cli/common/context.py | 10 ++++---- pulpcore/cli/common/generic.py | 36 ++++++++++++++-------------- pulpcore/cli/file/repository.py | 7 ++++-- pulpcore/cli/python/repository.py | 5 ++-- pulpcore/cli/rpm/repository.py | 7 ++++-- 7 files changed, 45 insertions(+), 34 deletions(-) diff --git a/pulpcore/cli/ansible/distribution.py b/pulpcore/cli/ansible/distribution.py index e512be790..1faac0459 100644 --- a/pulpcore/cli/ansible/distribution.py +++ b/pulpcore/cli/ansible/distribution.py @@ -8,7 +8,7 @@ PulpAnsibleRepositoryContext, ) from pulpcore.cli.common.context import EntityDefinition, EntityFieldDefinition -from pulpcore.cli.common.context import PluginRequiredVersion as PRV +from pulpcore.cli.common.context import PluginRequirement from pulpcore.cli.common.context import PulpContext, pass_entity_context, pass_pulp_context from pulpcore.cli.common.generic import ( base_path_contains_option, @@ -77,7 +77,12 @@ def distribution(ctx: click.Context, pulp_ctx: PulpContext, distribution_type: s distribution.add_command(destroy_command(decorators=lookup_options)) distribution.add_command(create_command(decorators=create_options)) distribution.add_command( - label_command(need_plugins=[PRV("core", "3.10.0"), PRV("ansible", "0.8.0.dev")]) + label_command( + need_plugins=[ + PluginRequirement("core", "3.10.0"), + PluginRequirement("ansible", "0.8.0.dev"), + ] + ) ) diff --git a/pulpcore/cli/ansible/repository.py b/pulpcore/cli/ansible/repository.py index 8b4481a7c..3e2fd4972 100644 --- a/pulpcore/cli/ansible/repository.py +++ b/pulpcore/cli/ansible/repository.py @@ -9,6 +9,7 @@ PulpAnsibleRoleRemoteContext, ) from pulpcore.cli.common.context import ( + PluginRequirement, PulpContext, PulpRepositoryContext, pass_pulp_context, @@ -66,12 +67,12 @@ def repository(ctx: click.Context, pulp_ctx: PulpContext, repo_type: str) -> Non click.option("--name", required=True), click.option("--description"), click.option("--remote", callback=_remote_callback, help=_("an optional remote")), - pulp_option("--retained-versions", needs_plugin="core", min_version="3.13.0.dev"), + pulp_option("--retained-versions", needs_plugins=[PluginRequirement("core", "3.13.0.dev")]), ] update_options = [ click.option("--description"), click.option("--remote", callback=_remote_callback, help=_("new optional remote")), - pulp_option("--retained-versions", needs_plugin="core", min_version="3.13.0.dev"), + pulp_option("--retained-versions", needs_plugins=[PluginRequirement("core", "3.13.0.dev")]), ] repository.add_command(show_command(decorators=lookup_options)) diff --git a/pulpcore/cli/common/context.py b/pulpcore/cli/common/context.py index d27edeac2..207891b39 100644 --- a/pulpcore/cli/common/context.py +++ b/pulpcore/cli/common/context.py @@ -34,11 +34,11 @@ RepositoryVersionDefinition = Tuple[str, str, int] # name, pulp_type, version -class PluginRequiredVersion(NamedTuple): +class PluginRequirement(NamedTuple): name: str min: Optional[str] = None max: Optional[str] = None - feature: Optional[str] = _("this command") + feature: Optional[str] = None new_component_names_to_pre_3_11_names: Dict[str, str] = dict( @@ -82,7 +82,7 @@ class PulpContext: def __init__(self, api_kwargs: Dict[str, Any], format: str, background_tasks: bool) -> None: self._api: Optional[OpenAPI] = None self._api_kwargs = api_kwargs - self._needed_plugins: List[PluginRequiredVersion] = [] + self._needed_plugins: List[PluginRequirement] = [] self.isatty: bool = sys.stdout.isatty() self.format: str = format @@ -225,9 +225,7 @@ def needs_plugin( ) else: # Schedule for later checking - self._needed_plugins.append( - PluginRequiredVersion(name, min_version, max_version, feature) - ) + self._needed_plugins.append(PluginRequirement(name, min_version, max_version, feature)) class PulpEntityContext: diff --git a/pulpcore/cli/common/generic.py b/pulpcore/cli/common/generic.py index 88095bcf8..0c28bb328 100644 --- a/pulpcore/cli/common/generic.py +++ b/pulpcore/cli/common/generic.py @@ -1,13 +1,13 @@ import gettext import json -from typing import Any, Callable, Optional, Tuple, TypeVar, Union +from typing import Any, Callable, List, Optional, Tuple, TypeVar, Union import click from pulpcore.cli.common.context import ( DEFAULT_LIMIT, EntityDefinition, - PluginRequiredVersion, + PluginRequirement, PulpContext, PulpEntityContext, PulpRepositoryContext, @@ -39,27 +39,27 @@ class PulpOption(click.Option): def __init__( self, *args: Any, - needs_plugin: Optional[str] = None, - min_version: Optional[str] = None, - max_version: Optional[str] = None, + needs_plugins: Optional[List[PluginRequirement]] = None, **kwargs: Any, ): - if min_version or max_version: - assert needs_plugin, "Must supply required_version if min or max_version supplied." - self.needs_plugin = needs_plugin - self.min_version = min_version - self.max_version = max_version + self.needs_plugins = needs_plugins super().__init__(*args, **kwargs) def process_value(self, ctx: click.Context, value: Any) -> Any: - if value is not None and self.needs_plugin: + if value is not None and self.needs_plugins: pulp_ctx = ctx.find_object(PulpContext) - pulp_ctx.needs_plugin( - self.needs_plugin, - self.min_version, - self.max_version, - _("the {name} option").format(name=self.name), - ) + for plugin_requirement in self.needs_plugins: + if plugin_requirement.feature: + feature = plugin_requirement.feature + else: + feature = _("the {name} option").format(name=self.name) + + pulp_ctx.needs_plugin( + plugin_requirement.name, + plugin_requirement.min, + plugin_requirement.max, + feature, + ) return super().process_value(ctx, value) def get_help_record(self, ctx: click.Context) -> Tuple[str, str]: @@ -459,7 +459,7 @@ def label_command(**kwargs: Any) -> click.Command: if "name" not in kwargs: kwargs["name"] = "label" decorators = kwargs.pop("decorators", [name_option, href_option]) - need_plugins = kwargs.pop("need_plugins", [PluginRequiredVersion("core", "3.10.0")]) + need_plugins = kwargs.pop("need_plugins", [PluginRequirement("core", "3.10.0")]) @click.group(**kwargs) @pass_pulp_context diff --git a/pulpcore/cli/file/repository.py b/pulpcore/cli/file/repository.py index 85c0d5efa..9fc35e5e6 100644 --- a/pulpcore/cli/file/repository.py +++ b/pulpcore/cli/file/repository.py @@ -4,6 +4,7 @@ import click from pulpcore.cli.common.context import ( + PluginRequirement, PulpContext, PulpEntityContext, PulpRepositoryContext, @@ -70,9 +71,11 @@ def repository(ctx: click.Context, pulp_ctx: PulpContext, repo_type: str) -> Non click.option("--remote", callback=_remote_callback), click.option("--manifest"), pulp_option( - "--autopublish/--no-autopublish", needs_plugin="file", min_version="1.7.0", default=None + "--autopublish/--no-autopublish", + needs_plugins=[PluginRequirement("file", "1.7.0")], + default=None, ), - pulp_option("--retained-versions", needs_plugin="core", min_version="3.13.0.dev"), + pulp_option("--retained-versions", needs_plugins=[PluginRequirement("core", "3.13.0.dev")]), ] create_options = update_options + [ click.option("--name", required=True), diff --git a/pulpcore/cli/python/repository.py b/pulpcore/cli/python/repository.py index f67bfafdd..3f06c7025 100644 --- a/pulpcore/cli/python/repository.py +++ b/pulpcore/cli/python/repository.py @@ -3,6 +3,7 @@ import click from pulpcore.cli.common.context import ( + PluginRequirement, PulpContext, PulpEntityContext, PulpRepositoryContext, @@ -61,12 +62,12 @@ def repository(ctx: click.Context, pulp_ctx: PulpContext, repo_type: str) -> Non click.option("--name", required=True), click.option("--description"), click.option("--remote", callback=_remote_callback), - pulp_option("--retained-versions", needs_plugin="core", min_version="3.13.0.dev"), + pulp_option("--retained-versions", needs_plugins=[PluginRequirement("core", "3.13.0.dev")]), ] update_options = [ click.option("--description"), click.option("--remote", callback=_remote_callback), - pulp_option("--retained-versions", needs_plugin="core", min_version="3.13.0.dev"), + pulp_option("--retained-versions", needs_plugins=[PluginRequirement("core", "3.13.0.dev")]), ] repository.add_command(list_command(decorators=[label_select_option])) diff --git a/pulpcore/cli/rpm/repository.py b/pulpcore/cli/rpm/repository.py index b2b7c46bf..fbde8f2ab 100644 --- a/pulpcore/cli/rpm/repository.py +++ b/pulpcore/cli/rpm/repository.py @@ -7,6 +7,7 @@ PulpContext, PulpEntityContext, PulpRepositoryContext, + PluginRequirement, pass_pulp_context, pass_repository_context, ) @@ -71,9 +72,11 @@ def repository(ctx: click.Context, pulp_ctx: PulpContext, repo_type: str) -> Non click.option("--repo-gpgcheck", type=click.Choice(("0", "1"))), click.option("--sqlite-metadata/--no-sqlite-metadata", default=None), pulp_option( - "--autopublish/--no-autopublish", needs_plugin="rpm", min_version="3.11.0.dev", default=None + "--autopublish/--no-autopublish", + needs_plugins=[PluginRequirement("rpm", "3.11.0.dev")], + default=None, ), - pulp_option("--retained-versions", needs_plugin="core", min_version="3.13.0.dev"), + pulp_option("--retained-versions", needs_plugins=[PluginRequirement("core", "3.13.0.dev")]), ] create_options = update_options + [click.option("--name", required=True)]