"""Helper utilities for integrating argcomplete with traitlets""" # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. from __future__ import annotations import argparse import os import typing as t try: import argcomplete from argcomplete import CompletionFinder # type:ignore[attr-defined] except ImportError: # This module and its utility methods are written to not crash even # if argcomplete is not installed. class StubModule: def __getattr__(self, attr: str) -> t.Any: if not attr.startswith("__"): raise ModuleNotFoundError("No module named 'argcomplete'") raise AttributeError(f"argcomplete stub module has no attribute '{attr}'") argcomplete = StubModule() # type:ignore[assignment] CompletionFinder = object # type:ignore[assignment, misc] def get_argcomplete_cwords() -> t.Optional[t.List[str]]: """Get current words prior to completion point This is normally done in the `argcomplete.CompletionFinder` constructor, but is exposed here to allow `traitlets` to follow dynamic code-paths such as determining whether to evaluate a subcommand. """ if "_ARGCOMPLETE" not in os.environ: return None comp_line = os.environ["COMP_LINE"] comp_point = int(os.environ["COMP_POINT"]) # argcomplete.debug("splitting COMP_LINE for:", comp_line, comp_point) comp_words: t.List[str] try: ( cword_prequote, cword_prefix, cword_suffix, comp_words, last_wordbreak_pos, ) = argcomplete.split_line(comp_line, comp_point) # type:ignore[attr-defined,no-untyped-call] except ModuleNotFoundError: return None # _ARGCOMPLETE is set by the shell script to tell us where comp_words # should start, based on what we're completing. # 1: