diff options
Diffstat (limited to 'python/knot_resolver')
-rw-r--r-- | python/knot_resolver/client/command.py | 97 | ||||
-rw-r--r-- | python/knot_resolver/client/commands/cache.py | 2 | ||||
-rw-r--r-- | python/knot_resolver/client/commands/completion.py | 8 | ||||
-rw-r--r-- | python/knot_resolver/client/commands/config.py | 2 | ||||
-rw-r--r-- | python/knot_resolver/client/commands/convert.py | 2 | ||||
-rw-r--r-- | python/knot_resolver/client/commands/debug.py | 2 | ||||
-rw-r--r-- | python/knot_resolver/client/commands/help.py | 2 | ||||
-rw-r--r-- | python/knot_resolver/client/commands/metrics.py | 2 | ||||
-rw-r--r-- | python/knot_resolver/client/commands/schema.py | 2 | ||||
-rw-r--r-- | python/knot_resolver/client/commands/validate.py | 2 |
10 files changed, 53 insertions, 68 deletions
diff --git a/python/knot_resolver/client/command.py b/python/knot_resolver/client/command.py index 79dd9bec..3966f8ca 100644 --- a/python/knot_resolver/client/command.py +++ b/python/knot_resolver/client/command.py @@ -1,5 +1,5 @@ import argparse -from abc import ABC, abstractmethod # pylint: disable=[no-name-in-module] +from abc import ABC, abstractmethod from pathlib import Path from typing import Dict, List, Optional, Set, Tuple, Type, TypeVar from urllib.parse import quote @@ -21,43 +21,17 @@ COMP_NOSPACE = "#nospace#" _registered_commands: List[Type["Command"]] = [] -def get_mutually_exclusive_commands(parser: argparse.ArgumentParser) -> List[Set[str]]: - command_names: List[Set[str]] = [] +def get_mutually_exclusive_args(parser: argparse.ArgumentParser) -> List[Set[str]]: + groups: List[Set[str]] = [] + for group in parser._mutually_exclusive_groups: # noqa: SLF001 - command_names.append(set()) + group_args: Set[str] = set() for action in group._group_actions: # noqa: SLF001 if action.option_strings: - command_names[-1].update(action.option_strings) - return command_names - - -def is_unique_and_new(arg: str, args: Set[str], exclusive: List[Set[str]], last: str) -> bool: - if arg not in args: - for excl in exclusive: - if arg in excl: - for cmd in excl: - if cmd in args: - return False - return True - - return arg == last - - -def get_subparsers_words( - subparser_actions: List[argparse.Action], args: Set[str], exclusive: List[Set[str]], last: str -) -> CompWords: - words: CompWords = {} - for action in subparser_actions: - if isinstance(action, argparse._SubParsersAction) and action.choices: # noqa: SLF001 - for choice, parser in action.choices.items(): - if is_unique_and_new(choice, args, exclusive, last): - words[choice] = parser.description - else: - for opt in action.option_strings: - if is_unique_and_new(opt, args, exclusive, last): - words[opt] = action.help - - return words + group_args.update(action.option_strings) + if group_args: + groups.append(group_args) + return groups def get_parser_action(name: str, parser_actions: List[argparse.Action]) -> Optional[argparse.Action]: @@ -91,24 +65,34 @@ def comp_get_actions_words(parser_actions: List[argparse.Action]) -> CompWords: return words -def comp_get_words(args: List[str], parser_actions: List[argparse.Action]) -> CompWords: # noqa: PLR0912 - words: CompWords = comp_get_actions_words(parser_actions) +def comp_get_words(args: List[str], parser: argparse.ArgumentParser) -> CompWords: # noqa: PLR0912 + words: CompWords = comp_get_actions_words(parser._actions) # noqa: SLF001 nargs = len(args) skip_arg = False for i, arg in enumerate(args): - action: Optional[argparse.Action] = get_parser_action(arg, parser_actions) # noqa: SLF001 + action: Optional[argparse.Action] = get_parser_action(arg, parser._actions) # noqa: SLF001 if skip_arg: skip_arg = False continue if not action: - words = comp_get_actions_words(parser_actions) continue if i + 1 >= nargs: - return words + continue + + # remove exclusive arguments from words + for exclusive_args in get_mutually_exclusive_args(parser): + if arg in exclusive_args: + for earg in exclusive_args: + if earg in words.keys(): + del words[earg] + # remove alternative arguments from words + for opt in action.option_strings: + if opt in words.keys(): + del words[opt] # if not action or action is HelpAction or VersionAction if isinstance(action, (argparse._HelpAction, argparse._VersionAction)): # noqa: SLF001 @@ -121,27 +105,28 @@ def comp_get_words(args: List[str], parser_actions: List[argparse.Action]) -> Co # if action is StoreAction if isinstance(action, argparse._StoreAction): # noqa: SLF001 - choices = {} - if action.choices: - for choice in action.choices: - choices[choice] = action.help - else: - choices[COMP_DIRNAMES] = None - choices[COMP_FILENAMES] = None - words = choices + if i + 2 >= nargs: + choices = {} + if action.choices: + for choice in action.choices: + choices[choice] = action.help + else: + choices[COMP_DIRNAMES] = None + choices[COMP_FILENAMES] = None + words = choices skip_arg = True continue # if action is SubParserAction if isinstance(action, argparse._SubParsersAction): # noqa: SLF001 - subparser = action.choices[arg] - cmd = get_subparser_command(subparser) - return cmd.completion(args[i + 1 :], subparser) if cmd else {} - - # delete already used args - for arg in args: - if arg in words.keys(): - del words[arg] + subparser: Optional[argparse.ArgumentParser] = action.choices[arg] if arg in action.choices else None + + command = get_subparser_command(subparser) if subparser else None + if command and subparser: + return command.completion(args[i + 1 :], subparser) + if subparser: + return comp_get_words(args[i + 1 :], subparser) # noqa: SLF001 + return {} return words diff --git a/python/knot_resolver/client/commands/cache.py b/python/knot_resolver/client/commands/cache.py index 66ee77c9..a1bebeed 100644 --- a/python/knot_resolver/client/commands/cache.py +++ b/python/knot_resolver/client/commands/cache.py @@ -99,7 +99,7 @@ class CacheCommand(Command): @staticmethod def completion(args: List[str], parser: argparse.ArgumentParser) -> CompWords: - return comp_get_words(args, parser._actions) # noqa: SLF001 + return comp_get_words(args, parser) def run(self, args: CommandArgs) -> None: if not self.operation: diff --git a/python/knot_resolver/client/commands/completion.py b/python/knot_resolver/client/commands/completion.py index 75f8e1eb..5e3d3628 100644 --- a/python/knot_resolver/client/commands/completion.py +++ b/python/knot_resolver/client/commands/completion.py @@ -45,14 +45,14 @@ class CompletionCommand(Command): @staticmethod def completion(args: List[str], parser: argparse.ArgumentParser) -> CompWords: - return comp_get_words(args, parser._actions) # noqa: SLF001 + return comp_get_words(args, parser) def run(self, args: CommandArgs) -> None: # noqa: PLR0912 words: CompWords = {} - subparsers = args.parser._subparsers # noqa: SLF001 - if subparsers: - words = comp_get_words(self.args, subparsers._actions) # noqa: SLF001 + parser = args.parser + if parser: + words = comp_get_words(self.args, args.parser) # print completion words # based on required bash/fish shell format diff --git a/python/knot_resolver/client/commands/config.py b/python/knot_resolver/client/commands/config.py index fef3ad79..69f7edfa 100644 --- a/python/knot_resolver/client/commands/config.py +++ b/python/knot_resolver/client/commands/config.py @@ -158,7 +158,7 @@ class ConfigCommand(Command): return words - return comp_get_words(args, parser._actions) # noqa: SLF001 + return comp_get_words(args, parser) def run(self, args: CommandArgs) -> None: if not self.operation: diff --git a/python/knot_resolver/client/commands/convert.py b/python/knot_resolver/client/commands/convert.py index 28a5be95..aab07519 100644 --- a/python/knot_resolver/client/commands/convert.py +++ b/python/knot_resolver/client/commands/convert.py @@ -50,7 +50,7 @@ class ConvertCommand(Command): @staticmethod def completion(args: List[str], parser: argparse.ArgumentParser) -> CompWords: - return comp_get_words(args, parser._actions) # noqa: SLF001 + return comp_get_words(args, parser) def run(self, args: CommandArgs) -> None: with open(self.input_file, "r") as f: diff --git a/python/knot_resolver/client/commands/debug.py b/python/knot_resolver/client/commands/debug.py index 71ce673e..14341e9d 100644 --- a/python/knot_resolver/client/commands/debug.py +++ b/python/knot_resolver/client/commands/debug.py @@ -61,7 +61,7 @@ class DebugCommand(Command): @staticmethod def completion(args: List[str], parser: argparse.ArgumentParser) -> CompWords: - return comp_get_words(args, parser._actions) # noqa: SLF001 + return comp_get_words(args, parser) def run(self, args: CommandArgs) -> None: # noqa: PLR0912, PLR0915 if self.gdb is None: diff --git a/python/knot_resolver/client/commands/help.py b/python/knot_resolver/client/commands/help.py index 1db83fb6..94942091 100644 --- a/python/knot_resolver/client/commands/help.py +++ b/python/knot_resolver/client/commands/help.py @@ -14,7 +14,7 @@ class HelpCommand(Command): @staticmethod def completion(args: List[str], parser: argparse.ArgumentParser) -> CompWords: - return comp_get_words(args, parser._actions) # noqa: SLF001 + return comp_get_words(args, parser) @staticmethod def register_args_subparser( diff --git a/python/knot_resolver/client/commands/metrics.py b/python/knot_resolver/client/commands/metrics.py index eaf83090..57ff9171 100644 --- a/python/knot_resolver/client/commands/metrics.py +++ b/python/knot_resolver/client/commands/metrics.py @@ -44,7 +44,7 @@ class MetricsCommand(Command): @staticmethod def completion(args: List[str], parser: argparse.ArgumentParser) -> CompWords: - return comp_get_words(args, parser._actions) # noqa: SLF001 + return comp_get_words(args, parser) def run(self, args: CommandArgs) -> None: response = request(args.socket, "GET", "metrics/prometheus" if self.prometheus else "metrics/json") diff --git a/python/knot_resolver/client/commands/schema.py b/python/knot_resolver/client/commands/schema.py index fdb65bde..c5e4dfc4 100644 --- a/python/knot_resolver/client/commands/schema.py +++ b/python/knot_resolver/client/commands/schema.py @@ -35,7 +35,7 @@ class SchemaCommand(Command): @staticmethod def completion(args: List[str], parser: argparse.ArgumentParser) -> CompWords: - return comp_get_words(args, parser._actions) # noqa: SLF001 + return comp_get_words(args, parser) def run(self, args: CommandArgs) -> None: if self.live: diff --git a/python/knot_resolver/client/commands/validate.py b/python/knot_resolver/client/commands/validate.py index 2347519e..92848b58 100644 --- a/python/knot_resolver/client/commands/validate.py +++ b/python/knot_resolver/client/commands/validate.py @@ -41,7 +41,7 @@ class ValidateCommand(Command): @staticmethod def completion(args: List[str], parser: argparse.ArgumentParser) -> CompWords: - return comp_get_words(args, parser._actions) # noqa: SLF001 + return comp_get_words(args, parser) def run(self, args: CommandArgs) -> None: if self.input_file: |