-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from conda-incubator/code-review
Suggested changes for token support (#16)
- Loading branch information
Showing
10 changed files
with
136 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,27 @@ | ||
repos: | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v2.3.0 | ||
rev: v4.5.0 | ||
hooks: | ||
- id: check-yaml | ||
exclude: "(mkdocs.yml|recipe/meta.yaml)" | ||
- id: end-of-file-fixer | ||
- id: trailing-whitespace | ||
- repo: https://github.com/pre-commit/mirrors-mypy | ||
rev: '' # Use the sha / tag you want to point at | ||
rev: v1.6.1 | ||
hooks: | ||
- id: mypy | ||
additional_dependencies: ['types-requests'] | ||
- repo: https://github.com/asottile/pyupgrade | ||
rev: v3.3.1 | ||
rev: v3.15.0 | ||
hooks: | ||
- id: pyupgrade | ||
args: ["--py310-plus"] | ||
- repo: https://github.com/akaihola/darker | ||
rev: 1.5.1 | ||
rev: 1.7.2 | ||
hooks: | ||
- id: darker | ||
additional_dependencies: [black==22.10.0] | ||
- repo: https://github.com/PyCQA/flake8 | ||
rev: 6.0.0 | ||
rev: 6.1.0 | ||
hooks: | ||
- id: flake8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,62 @@ | ||
""" | ||
Module for custom click.Option classes | ||
""" | ||
from __future__ import annotations | ||
import click | ||
from typing import Any | ||
from collections.abc import Mapping | ||
|
||
|
||
class CustomOption(click.Option): | ||
class ConditionalOption(click.Option): | ||
""" | ||
Custom option that does the following things: | ||
- Allows you to define a "mutually_exclusive" tuple so certain options cannot be passed | ||
together | ||
- If ``prompt=True`` is set, can optionally control it to be prompted only in the presence of | ||
other options via ``prompt_when`` | ||
- Adds options which have been passed to ``ctx.obj.used_options`` | ||
- Define ``mutually_exclusive`` options that cannot be passed together | ||
- Control prompting in the presence of other options via ``prompt_when`` | ||
""" | ||
def __init__(self, *args, **kwargs): | ||
self.mutually_exclusive = set(kwargs.pop("mutually_exclusive", [])) | ||
self.prompt_when = kwargs.pop("prompt_when", None) | ||
help_message = kwargs.get("help", "") | ||
self.not_required_if = set(kwargs.pop("not_required_if", [])) | ||
|
||
self.prompt_when = set(kwargs.pop("prompt_when", [])) | ||
if self.prompt_when: | ||
# ensure prompt text is configured, | ||
# conditionally control whether we prompt in handle_parse_result | ||
kwargs["prompt"] = True | ||
|
||
self.mutually_exclusive = set(kwargs.pop("mutually_exclusive", [])) | ||
if self.mutually_exclusive: | ||
ex_str = ", ".join(f'"{option}"' for option in self.mutually_exclusive) | ||
kwargs[ | ||
"help" | ||
] = f"{help_message}; cannot be used with these options: {ex_str}" | ||
# augment help blurb to include details about mutually exclusive options | ||
help_ = kwargs.get("help", "") | ||
mutex = ", ".join(map(repr, self.mutually_exclusive)) | ||
kwargs["help"] = f"{help_}; cannot be used with these options: {mutex}" | ||
|
||
super().__init__(*args, **kwargs) | ||
|
||
def handle_parse_result(self, ctx, opts, args): | ||
if self.name in opts: | ||
ctx.obj.used_options.add(self.name) | ||
|
||
if self.prompt_when is not None and self.prompt_when not in opts: | ||
return None, args | ||
|
||
def handle_parse_result( | ||
self, | ||
ctx: click.Context, | ||
opts: Mapping[str, Any], | ||
args: list[str], | ||
) -> tuple[Any, list[str]]: | ||
# determine whether mutex has been violated | ||
if self.mutually_exclusive.intersection(opts) and self.name in opts: | ||
mutually_exclusive = ", ".join( | ||
f'"{option}"' for option in self.mutually_exclusive | ||
) | ||
raise click.UsageError( | ||
f'Option "{self.name}" cannot be used with {mutually_exclusive}' | ||
mutex = ", ".join(map(repr, self.mutually_exclusive)) | ||
raise click.UsageError(f"Option {self.name!r} cannot be used with {mutex}") | ||
|
||
# determine whether we want to prompt for this argument | ||
if self.prompt_when and not self.prompt_when.intersection(opts): | ||
self.prompt = None | ||
|
||
if ( | ||
self.not_required_if | ||
and self.name not in opts | ||
and not self.not_required_if.intersection(opts) | ||
): | ||
required = {self.name, *self.not_required_if} | ||
raise click.MissingParameter( | ||
ctx=ctx, | ||
param_type="option", | ||
param_hint=" / ".join(sorted(map(repr, required))), | ||
) | ||
|
||
return super().handle_parse_result(ctx, opts, args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.