Skip to content

Commit

Permalink
Move all completion logic into a completion module.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Dec 18, 2022
1 parent 40af6de commit 3d1129f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 52 deletions.
24 changes: 0 additions & 24 deletions keyring/_shtab.py

This file was deleted.

31 changes: 4 additions & 27 deletions keyring/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,27 @@

from . import core
from . import backend
from . import completion
from . import set_keyring, get_password, set_password, delete_password

try:
import shtab
except ImportError:
from . import _shtab as shtab

# it completes keyring backends for `keyring -b` by the output of `keyring --list-backends`
# % keyring -b <TAB>
# keyring priority
# keyring.backends.chainer.ChainerBackend 10
# keyring.backends.fail.Keyring 0
# ... ...
PREAMBLE = {
"zsh": r"""
keyring_complete() {
local line
while read -r line; do
choices+=(${${line/ \(priority: /\\\\:}/)/})
done <<< "$($words[1] --list-backends)"
_arguments "*:keyring priority:(($choices))"
}
"""
}
KEYRING_COMPLETE = {"zsh": "keyring_complete"}


class CommandLineTool:
def __init__(self):
self.parser = argparse.ArgumentParser()
shtab.add_argument_to(self.parser, preamble=PREAMBLE)
self.parser.add_argument(
"-p",
"--keyring-path",
dest="keyring_path",
default=None,
help="Path to the keyring backend",
).complete = shtab.DIR
)
self.parser.add_argument(
"-b",
"--keyring-backend",
dest="keyring_backend",
default=None,
help="Name of the keyring backend",
).complete = KEYRING_COMPLETE
)
self.parser.add_argument(
"--list-backends",
action="store_true",
Expand All @@ -73,6 +49,7 @@ def __init__(self):
'username',
nargs="?",
)
completion.install(self.parser)

def run(self, argv):
args = self.parser.parse_args(argv)
Expand Down
63 changes: 63 additions & 0 deletions keyring/completion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import argparse

try:
import shtab
except ImportError:
pass


# it completes keyring backends for `keyring -b` by the output
# of `keyring --list-backends`
# % keyring -b <TAB>
# keyring priority
# keyring.backends.chainer.ChainerBackend 10
# keyring.backends.fail.Keyring 0
# ... ...
PREAMBLE = {
"zsh": r"""
backend_complete() {
local line
while read -r line; do
choices+=(${${line/ \(priority: /\\\\:}/)/})
done <<< "$($words[1] --list-backends)"
_arguments "*:keyring priority:(($choices))"
}
"""
}
BACKEND_COMPLETE = {"zsh": "backend_complete"}


class _MissingCompletionAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string):
print("Install keyring[completion] for completion support.")
parser.exit(0)


def add_completion_notice(parser):
"""Add completion argument to parser."""
parser.add_argument(
"--print-completion",
choices=["bash", "zsh", "tcsh"],
action=_MissingCompletionAction,
help="print shell completion script",
)
return parser


def get_action(parser, option):
(match,) = (action for action in parser._actions if option in action.option_strings)
return match


def install(parser):
try:
install_completion(parser)
except NameError:
add_completion_notice(parser)


def install_completion(parser):
shtab.add_argument_to(parser, preamble=PREAMBLE)
get_action(parser, '--keyring-path').completion = shtab.DIR
get_action(parser, '--keyring-backend').completion = BACKEND_COMPLETE
return parser
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ docs =
# local

completion =
# upstream
shtab

[options.entry_points]
Expand Down

0 comments on commit 3d1129f

Please sign in to comment.