[docs]defimport_paths(ctx:Context,param:Parameter,incomplete:str,root:t.Union[t.Callable[[],t.Optional[Path]],t.Optional[Path]]=None,)->t.List[CompletionItem]:""" A completer that completes a python dot import path string based on sys.path. :param ctx: The click context. :param param: The click parameter. :param incomplete: The incomplete string. :param root: The root path to search for modules. :return: A list of available matching import paths """importpkgutilrt=root()ifcallable(root)elserootincomplete=incomplete.strip()completions=[]packages=[pkgforpkginincomplete.split(".")ifpkg]pkg_complete=notincompleteorincomplete.endswith(".")module_import=".".join(packages)ifpkg_completeelse".".join(packages[:-1])module_path=Path(module_import.replace(".","/"))search_paths=[]ifrtand(rt/module_path).exists():search_paths.append(str(rt/module_path))else:forpthinsys.path:if(Path(pth)/module_path).exists():search_paths.append(str(Path(pth)/module_path))prefix=""ifpkg_completeelsepackages[-1]formoduleinpkgutil.iter_modules(path=search_paths):ifmodule.name.startswith(prefix):completions.append(CompletionItem(f"{module_import}{'.'ifmodule_importelse''}{module.name}",type="plain",))iflen(completions)==1andnotcompletions[0].value.endswith("."):returnimport_paths(ctx,param,f"{completions[0].value}.")orcompletionsreturncompletions
[docs]defpaths(ctx:Context,param:Parameter,incomplete:str,dir_only:t.Optional[bool]=None,root:t.Union[t.Callable[[],t.Optional[Path]],t.Optional[Path]]=None,)->t.List[CompletionItem]:""" A completer that completes a path. Relative incomplete paths are interpreted relative to the current working directory. :param ctx: The click context. :param param: The click parameter. :param incomplete: The incomplete string. :param dir_only: Restrict completions to paths to directories only, otherwise complete directories or files. :param root: Restrict completions to this root path. :return: A list of available matching directories """rt=root()ifcallable(root)elserootdefexists(pth:Path)->bool:ifdir_only:returnpth.is_dir()returnpth.exists()orpth.is_symlink()separator=os.sepif"/"inincomplete:if"\\"notinincomplete:separator="/"elif"\\"inincomplete:separator="\\"completions=[]ifrt:incomplete_path=rt/Path(incomplete.replace(separator,os.path.sep).lstrip(os.path.sep))else:incomplete_path=Path(incomplete.replace(separator,os.path.sep))partial_dir=""ifnotexists(incomplete_path)andnotincomplete.endswith(separator):partial_dir=incomplete_path.nameincomplete_path=incomplete_path.parentelifincomplete_path.is_file()andnotdir_only:return[CompletionItem(incomplete,type="file")]ifincomplete_path.is_dir():forchildinos.listdir(incomplete_path):ifnotexists(incomplete_path/child):continueifchild.startswith(partial_dir):to_complete=incomplete[0:(-len(partial_dir)orNone)]sep=(""ifnotto_completeorto_complete.endswith(separator)elseseparator)ctype=("plain"ifrtelse"dir"if(incomplete_path/child).is_dir()else"file")completions.append(CompletionItem(f"{to_complete}{sep}{child}",type=ctype,))if(len(completions)==1andPath(completions[0].value).is_dir()and[childforchildinos.listdir(completions[0].value)ifexists(Path(completions[0].value)/child)]):# recurse because we can go futherreturnpaths(ctx,param,completions[0].value,dir_only=dir_only)returncompletions
directories=partial(paths,dir_only=True)"""A completer that completes a directory path (but not files). Relative incomplete pathsare interpreted relative to the current working directory.:param ctx: The click context.:param param: The click parameter.:param incomplete: The incomplete string.:return: A list of available matching directories"""static_paths=partial(paths,root=partial(_settings_path,name="STATIC_ROOT"))"""Complete static file paths.:param ctx: The click context.:param param: The click parameter.:param incomplete: The incomplete string.:param dir_only: Restrict completions to paths to directories only, otherwise complete directories or files.:return: A list of available matching directories"""media_paths=partial(paths,root=partial(_settings_path,name="MEDIA_ROOT"))"""Complete media file paths.:param ctx: The click context.:param param: The click parameter.:param incomplete: The incomplete string.:param dir_only: Restrict completions to paths to directories only, otherwise complete directories or files.:return: A list of available matching directories"""