Skip to content

Commit

Permalink
bump version, merge pull request #53 from iterative/devel
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl authored Oct 11, 2021
2 parents af6aefb + 2a4b498 commit 5af77eb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 37 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ jobs:
- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python }}
- run: pip install -U -r requirements-dev.txt
- name: Install
run: |
if [[ ${{ matrix.python }} == '2.7' ]]; then
pip install -U pytest-cov 'pytest-timeout<2'
else
pip install -U -r requirements-dev.txt
fi
- run: pytest
- uses: codecov/codecov-action@v1
deploy:
Expand Down
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ repos:
- id: flake8
args: [-j8]
additional_dependencies:
- flake8-broken-line
- flake8-bugbear
- flake8-comprehensions
- flake8-debugger
- flake8-isort
- flake8-string-format
- repo: https://github.com/psf/black
rev: 21.8b0
rev: 21.9b0
hooks:
- id: black
- repo: https://github.com/PyCQA/isort
Expand Down
4 changes: 2 additions & 2 deletions examples/customcomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ def get_main_parser():
subparsers.required = True
subparsers.dest = "subcommand"

parser = subparsers.add_parser("completion")
parser = subparsers.add_parser("completion", help="print tab completion")
shtab.add_argument_to(
parser, "shell", parent=main_parser, preamble=PREAMBLE
) # magic!

parser = subparsers.add_parser("process")
parser = subparsers.add_parser("process", help="parse files")
# `*.txt` file tab completion
parser.add_argument("input_txt", nargs="?").complete = TXT_FILE
# file tab completion builtin shortcut
Expand Down
62 changes: 29 additions & 33 deletions shtab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
_VersionAction,
)
from functools import total_ordering
from string import Template

# version detector. Precedence: installed dist, git, 'UNKNOWN'
try:
Expand Down Expand Up @@ -129,13 +130,6 @@ def complete2pattern(opt_complete, shell, choice_type2fn):
)


def replace_format(string, **fmt):
"""Similar to `string.format(**fmt)` but ignores unknown `{key}`s."""
for k, v in fmt.items():
string = string.replace("{" + k + "}", v)
return string


def wordify(string):
"""Replace non-word chars [-. ] with underscores [_]"""
return string.replace("-", "_").replace(".", " ").replace(" ", "_")
Expand Down Expand Up @@ -362,22 +356,22 @@ def complete_bash(parser, root_prefix=None, preamble="", choice_functions=None):
# Programmable-Completion.html
# - https://opensource.com/article/18/3/creating-bash-completion-script
# - https://stackoverflow.com/questions/12933362
return replace_format(
return Template(
"""\
#!/usr/bin/env bash
# AUTOMATCALLY GENERATED by `shtab`
{subparsers}
${subparsers}
{option_strings}
${option_strings}
{compgens}
${compgens}
{choices}
${choices}
{nargs}
${nargs}
{preamble}
${preamble}
# $1=COMP_WORDS[1]
_shtab_compgen_files() {
compgen -f -- $1 # files
Expand All @@ -396,7 +390,7 @@ def complete_bash(parser, root_prefix=None, preamble="", choice_functions=None):
# set default values (called for the initial parser & any subparsers)
_set_parser_defaults() {
local subparsers_var="${prefix}_subparsers[@]"
subparsers=${!subparsers_var}
sub_parsers=${!subparsers_var}
local current_option_strings_var="${prefix}_option_strings[@]"
current_option_strings=${!current_option_strings_var}
Expand Down Expand Up @@ -437,11 +431,11 @@ def complete_bash(parser, root_prefix=None, preamble="", choice_functions=None):
# hello="world"
# x="hello"
# ${!x} -> ${hello} -> "world"
{root_prefix}() {
${root_prefix}() {
local completing_word="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=()
prefix={root_prefix}
prefix=${root_prefix}
word_index=0
_set_parser_defaults
word_index=1
Expand All @@ -451,7 +445,7 @@ def complete_bash(parser, root_prefix=None, preamble="", choice_functions=None):
while [ $word_index -ne $COMP_CWORD ]; do
local this_word="${COMP_WORDS[$word_index]}"
if [[ -n $subparsers && " ${subparsers[@]} " =~ " ${this_word} " ]]; then
if [[ -n $sub_parsers && " ${sub_parsers[@]} " =~ " ${this_word} " ]]; then
# valid subcommand: add it to the prefix & reset the current action
prefix="${prefix}_$(_shtab_replace_nonword $this_word)"
_set_parser_defaults
Expand Down Expand Up @@ -491,7 +485,8 @@ def complete_bash(parser, root_prefix=None, preamble="", choice_functions=None):
return 0
}
complete -o filenames -F {root_prefix} {prog}""",
complete -o filenames -F ${root_prefix} ${prog}"""
).safe_substitute(
subparsers="\n".join(subparsers),
option_strings="\n".join(option_strings),
compgens="\n".join(compgens),
Expand Down Expand Up @@ -632,37 +627,38 @@ def format_positional(opt):
# - https://mads-hartmann.com/2017/08/06/
# writing-zsh-completion-scripts.html
# - http://www.linux-mag.com/id/1106/
return replace_format(
return Template(
"""\
#compdef {prog}
#compdef ${prog}
# AUTOMATCALLY GENERATED by `shtab`
{root_prefix}_options_=(
{root_options}
${root_prefix}_options_=(
${root_options}
)
{root_prefix}_commands_() {
${root_prefix}_commands_() {
local _commands=(
{commands}
${commands}
)
_describe '{prog} commands' _commands
_describe '${prog} commands' _commands
}
{subcommands}
{preamble}
${subcommands}
${preamble}
typeset -A opt_args
local context state line curcontext="$curcontext"
_arguments \\
${root_prefix}_options_ \\
{root_arguments} \\
': :{root_prefix}_commands_' \\
$$${root_prefix}_options_ \\
${root_arguments} \\
': :${root_prefix}_commands_' \\
'*::args:->args'
case $words[1] in
{commands_case}
esac""",
${commands_case}
esac"""
).safe_substitute(
root_prefix=root_prefix,
prog=parser.prog,
commands="\n ".join(
Expand Down

0 comments on commit 5af77eb

Please sign in to comment.