Skip to content

Commit

Permalink
Add script to generate bash autocompletion setup
Browse files Browse the repository at this point in the history
This commit replaces the bash autocomplete script with a command to
generate a bash autocomplete script.

This is because the setuptools `data_files` option/section is
deprecated, and there's really no good way to install this file with
setuptools.
  • Loading branch information
fredrikhl committed Jun 2, 2023
1 parent 3120d3e commit 738ed74
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 32 deletions.
28 changes: 0 additions & 28 deletions autocomplete/passlib-mkpasswd.bash

This file was deleted.

5 changes: 1 addition & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ classifiers =
Topic :: Utilities

[options]
include_package_data = True
package_dir =
= src/
packages = find:
Expand All @@ -33,9 +32,6 @@ install_requires =

[options.packages.find]
where = src
include =
passlib_cli
passlib_cli.*

[options.extras_require]
dev =
Expand All @@ -50,6 +46,7 @@ scrypt =
[options.entry_points]
console_scripts =
passlib-mkpasswd = passlib_cli.__main__:main
passlib-autocomplete = passlib_cli.complete:main

[aliases]
test = pytest
Expand Down
100 changes: 100 additions & 0 deletions src/passlib_cli/complete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/env python
# encoding: utf-8
""" Get autocomplete script """
from __future__ import (
absolute_import,
division,
print_function,
unicode_literals,
)

import argparse
import logging
import shlex
# import textwrap

from . import methods
from . import cli_utils

logger = logging.getLogger(__name__)


OPTIONS = (
"--list-all",
"--list-methods",
"--list-params",
"--no-verify",
"--show-docstring",
"--show-params",
"--version",
"-h", "--help",
"-p", "--param",
"-s", "--show-plaintext",
"-v", "--verbose",
)


template = """
_mkcrypt_autocomplete()
{{
local curr
local -a methods opts
curr="${{COMP_WORDS[$COMP_CWORD]}}"
methods=( {methods} )
opts=( {options} )
COMPREPLY=()
if [[ "$curr" == -* ]];
then
COMPREPLY=( $(compgen -W "${{opts[*]}}" -- "$curr") )
elif [ "${{COMP_CWORD}}" -gt 1 ] \\
&& [ "${{COMP_WORDS[$COMP_CWORD-1]}}" == "-p" ];
then
# TODO: Implement fetching known/valid settings from mkcrypt
COMPREPLY=()
else
COMPREPLY=( $(compgen -W "${{methods[*]}}" -- "$curr") )
fi
}}
complete -F _mkcrypt_autocomplete passlib-mkpasswd
"""


def format_text_list(items):
return " ".join(shlex.quote(item) for item in items)


def format_autocomplete_script(methods):
return template.format(
options=format_text_list(OPTIONS),
methods=format_text_list(methods),
)


def main(inargs=None):
parser = argparse.ArgumentParser(
description="Make autocomplete bash script for passlib",
)

cli_utils.add_verbosity_mutex(parser)
cli_utils.add_version_arg(parser)
args = parser.parse_args(inargs)

cli_utils.setup_logging(args.verbosity)

supported_methods = [m for m in methods.values() if m.supported]
method_list = [m.name for m in supported_methods]

# m = methods[args.show_params]
# for param in sorted(m.settings):
# print(param)

script = format_autocomplete_script(method_list)
print(script)


if __name__ == '__main__':
main()

0 comments on commit 738ed74

Please sign in to comment.