Package: cachedcomplete
cachedcompleteCached wrapper for python argcomplete.InstallationUse the package manager pip to install cachedcomplete.pipinstallcachedcomplete
activate-global-python-argcompleteExplanationcachedcomplete wraps argcomplete, and allows caching of its parsers
in order to save up time.cachedcomplete re-caches every time a change was made in itself,
or in the files specified by the user.cachedcomplete caches the parsers using pickle, which implies the parser
must be able to be serialized and de-serialized using pickle.Without any interference argparse's parsers cannot be serialized using pickle,
because the default type is defined within the parser's__init__function.
cachedcomplete replaces the default type with an identical type that can be pickled
(this is only done when cachedcomplete detects that the default type is still the default
identity function).UsageNormal UsageThe normal usage is the same as in argcomplete, only changing the import from argcomplete to cachedcomplete.Note:The usage ofPYTHON_ARGCOMPLETE_OKstays the same as in argcompleteFor ExampleUsing argcomplete:# PYTHON_ARGCOMPLETE_OKimportargcomplete# ...argcomplete.autocomplete()Using cachedcomplete:# PYTHON_ARGCOMPLETE_OKimportcachedcomplete# ...cachedcomplete.autocomplete()Usage of custom subclasses of CompletionFinderIn order to use custom CompletionFinders with cachedcomplete,
a usage of the decorator cached_completion_finder is required.For exampleUsing argcomplete:importargcompleteclassCustomCompletionFinder(argcomplete.CompletionFinder):# ...completion_finder=CustomCompletionFinder()# Instead of argcomplete.autocomplete()completion_finder()Using cachedcomplete:importargcompleteimportcachedcomplete@cachedcomplete.cached_completion_finderclassCustomCompletionFinder(argcomplete.CompletionFinder):# ...completion_finder=CustomCompletionFinder()# Instead of argcomplete.autocomplete()completion_finder()or:importargcompleteimportcachedcompleteclassCustomCompletionFinder(argcomplete.CompletionFinder):# ...completion_finder=cachedcomplete.cached_completion_finder(CustomCompletionFinder)()# Instead of argcomplete.autocomplete()completion_finder()Specifying what files to trackIn order to specify which files to track changes in,
add a comment with the wanted files and the prefix CACHEDCOMPLETE_HASH:# CACHEDCOMPLETE_HASH: file1.pyIt also allows multiple files split to multiple comments, or within one comment.# CACHEDCOMPLETE_HASH: file1.py file2.json# CACHEDCOMPLETE_HASH: file3.py# CACHEDCOMPLETE_HASH: "file with spaces.txt"And also allows passing up a directory to track all the files within# CACHEDCOMPLETE_HASH: dirUsing environment variables and user directories is also allowed:# CACHEDCOMPLETE_HASH: $HOME/.cache/my-cache /tmp/${USERNAME}.ini# CACHEDCOMPLETE_HASH: ~/.cache/my-cache ~gdm/greeter-dconf-defaultsPaths are relative to the calling script, not the current working directory.cachedcomplete adds an environment variable namepwdto allow accessing the current
working directory in the comments. If this variable was already set, it isnotoverwritten.# CACHEDCOMPLETE_HASH: $pwd/local-fileUsing custom types, completers, actions, etc...In order to cache a parser that uses your own custom types and functions,
they must be defined in a separate module than the main script (that defines the parser).Note:It is recommended to add these separate modules to the tracked files (As specified withCACHEDCOMPLETE_HASH).Example:Won't work:Inmy_awesome_script.py# PYTHON_ARGCOMPLETE_OKimportargparseimportcachedcompleteimportjsondefjson_file(arg):withargparse.FileType()(arg)asf:returnjson.load(f)p=argparse.ArgumentParser()p.add_argument('settings',type=json_file)cachedcomplete.autocomplete(p)Will work:Inmy_awesome_type.pyimportjsondefjson_file(arg):withargparse.FileType()(arg)asf:returnjson.load(f)Inmy_awesome_script.py# PYTHON_ARGCOMPLETE_OK# CACHEDCOMPLETE_HASH: my_awesome_type.pyimportargparseimportcachedcompletep=argparse.ArgumentParser()p.add_argument('settings',type=json_file)cachedcomplete.autocomplete(p)