Skip to content

Commit

Permalink
needs_plugins with PluginRequirements PoC
Browse files Browse the repository at this point in the history
[noissue]
  • Loading branch information
David Davis committed Apr 28, 2021
1 parent e2060bc commit f56ba15
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 34 deletions.
9 changes: 7 additions & 2 deletions pulpcore/cli/ansible/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"),
]
)
)


Expand Down
5 changes: 3 additions & 2 deletions pulpcore/cli/ansible/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
PulpAnsibleRoleRemoteContext,
)
from pulpcore.cli.common.context import (
PluginRequirement,
PulpContext,
PulpRepositoryContext,
pass_pulp_context,
Expand Down Expand Up @@ -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))
Expand Down
10 changes: 4 additions & 6 deletions pulpcore/cli/common/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

This comment has been minimized.

Copy link
@daviddavis

daviddavis Apr 28, 2021

Owner

I had to set this to None otherwise I have no idea if the value was explicitly set or not when evaluating PluginRequirement.

This comment has been minimized.

Copy link
@mdellweg

mdellweg Apr 28, 2021

I know that the "old" version of named tuple allowed to specify default values.
So what exactly happened, when you provide a string here?

This comment has been minimized.

Copy link
@daviddavis

daviddavis Apr 28, 2021

Owner

If a plugin writer supplies a PluginRequirement without feature set, then it defaults to "this command". When I evaluate this in PulpOption's process_value(), then I have no idea if feature was explicitly set to "this command" or not. Seems better to not have a default value and check if feature is None when outputting feature.



new_component_names_to_pre_3_11_names: Dict[str, str] = dict(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
36 changes: 18 additions & 18 deletions pulpcore/cli/common/generic.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions pulpcore/cli/file/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import click

from pulpcore.cli.common.context import (
PluginRequirement,
PulpContext,
PulpEntityContext,
PulpRepositoryContext,
Expand Down Expand Up @@ -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),
Expand Down
5 changes: 3 additions & 2 deletions pulpcore/cli/python/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import click

from pulpcore.cli.common.context import (
PluginRequirement,
PulpContext,
PulpEntityContext,
PulpRepositoryContext,
Expand Down Expand Up @@ -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]))
Expand Down
7 changes: 5 additions & 2 deletions pulpcore/cli/rpm/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
PulpContext,
PulpEntityContext,
PulpRepositoryContext,
PluginRequirement,
pass_pulp_context,
pass_repository_context,
)
Expand Down Expand Up @@ -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)]

Expand Down

0 comments on commit f56ba15

Please sign in to comment.