diff --git a/keyring/backend_complete.zsh b/keyring/backend_complete.zsh new file mode 100644 index 00000000..eba76c6b --- /dev/null +++ b/keyring/backend_complete.zsh @@ -0,0 +1,14 @@ +# Complete keyring backends for `keyring -b` from `keyring --list-backends` +# % keyring -b +# 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))" +} diff --git a/keyring/completion.py b/keyring/completion.py index c33ea29d..37c9c053 100644 --- a/keyring/completion.py +++ b/keyring/completion.py @@ -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 -# 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): @@ -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 diff --git a/setup.cfg b/setup.cfg index 963feda0..e5df7efc 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 =