Skip to content

Commit

Permalink
Move zsh to its own file and load it using importlib resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Dec 18, 2022
1 parent 3d1129f commit ded09d9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
14 changes: 14 additions & 0 deletions keyring/backend_complete.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Complete keyring backends for `keyring -b` from `keyring --list-backends`
# % keyring -b <TAB>
# keyring priority
# keyring.backends.chainer.ChainerBackend 10
# keyring.backends.fail.Keyring 0
# ... ...

backend_complete() {
local line
while read -r line; do
choices+=(${${line/ \(priority: /\\\\:}/)/})
done <<< "$($words[1] --list-backends)"
_arguments "*:keyring priority:(($choices))"
}
42 changes: 15 additions & 27 deletions keyring/completion.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
import argparse
import sys

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"}
if sys.version_info < (3, 9):
from importlib_resources import files
else:
from importlib.resources import files


class _MissingCompletionAction(argparse.Action):
Expand All @@ -49,15 +34,18 @@ def get_action(parser, option):
return match


def install_completion(parser):
preamble = dict(
zsh=files(__package__).joinpath('backend_complete.zsh').read_text(),
)
shtab.add_argument_to(parser, preamble=preamble)
get_action(parser, '--keyring-path').completion = shtab.DIR
get_action(parser, '--keyring-backend').completion = dict(zsh='backend_complete')
return parser


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: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ install_requires =
jeepney>=0.4.2; sys_platform=="linux"
importlib_metadata >= 4.11.4; python_version < "3.12"
jaraco.classes
importlib_resources; python_version < "3.9"

[options.packages.find]
exclude =
Expand Down

0 comments on commit ded09d9

Please sign in to comment.