Skip to main content

Cached wrapper for python argcomplete

Project description

cachedcomplete

Cached wrapper for python argcomplete.

Installation

Use the package manager pip to install cachedcomplete.

pip install cachedcomplete
activate-global-python-argcomplete

Explanation

cachedcomplete 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).

Usage

Normal Usage

The normal usage is the same as in argcomplete, only changing the import from argcomplete to cachedcomplete.

Note: The usage of PYTHON_ARGCOMPLETE_OK stays the same as in argcomplete

For Example

Using argcomplete:

# PYTHON_ARGCOMPLETE_OK
import argcomplete

# ...

argcomplete.autocomplete()

Using cachedcomplete:

# PYTHON_ARGCOMPLETE_OK
import cachedcomplete

# ...

cachedcomplete.autocomplete()

Usage of custom subclasses of CompletionFinder

In order to use custom CompletionFinders with cachedcomplete, a usage of the decorator cached_completion_finder is required.

For example

Using argcomplete:

import argcomplete

class CustomCompletionFinder(argcomplete.CompletionFinder):
    # ...

completion_finder = CustomCompletionFinder()
# Instead of argcomplete.autocomplete()
completion_finder()

Using cachedcomplete:

import argcomplete
import cachedcomplete

@cachedcomplete.cached_completion_finder
class CustomCompletionFinder(argcomplete.CompletionFinder):
    # ...

completion_finder = CustomCompletionFinder()
# Instead of argcomplete.autocomplete()
completion_finder()

or:

import argcomplete
import cachedcomplete

class CustomCompletionFinder(argcomplete.CompletionFinder):
    # ...

completion_finder = cachedcomplete.cached_completion_finder(CustomCompletionFinder)()
# Instead of argcomplete.autocomplete()
completion_finder()

Specifying what files to track

In order to specify which files to track changes in, add a comment with the wanted files and the prefix CACHEDCOMPLETE_HASH:

# CACHEDCOMPLETE_HASH: file1.py

It 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: dir

Using 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-defaults

Paths are relative to the calling script, not the current working directory.

cachedcomplete adds an environment variable name pwd to allow accessing the current working directory in the comments. If this variable was already set, it is not overwritten.

# CACHEDCOMPLETE_HASH: $pwd/local-file

Using 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 with CACHEDCOMPLETE_HASH).

Example:

Won't work:

In my_awesome_script.py

# PYTHON_ARGCOMPLETE_OK

import argparse
import cachedcomplete
import json

def json_file(arg):
    with argparse.FileType()(arg) as f:
        return json.load(f)

p = argparse.ArgumentParser()
p.add_argument('settings', type=json_file)
cachedcomplete.autocomplete(p)

Will work:

In my_awesome_type.py

import json

def json_file(arg):
    with argparse.FileType()(arg) as f:
        return json.load(f)

In my_awesome_script.py

# PYTHON_ARGCOMPLETE_OK
# CACHEDCOMPLETE_HASH: my_awesome_type.py

import argparse
import cachedcomplete

p = argparse.ArgumentParser()
p.add_argument('settings', type=json_file)
cachedcomplete.autocomplete(p)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cachedcomplete-1.0.4.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

cachedcomplete-1.0.4-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file cachedcomplete-1.0.4.tar.gz.

File metadata

  • Download URL: cachedcomplete-1.0.4.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for cachedcomplete-1.0.4.tar.gz
Algorithm Hash digest
SHA256 104b6a20f57fa86e6af4fe94a99ad07c834c841764cffe34b3e14eb9c3f49755
MD5 7f56367dbb82c8fdc35ec0bc58506159
BLAKE2b-256 699f95db1eb3edd3e51a6fe4bbff6312b6a3479b40369858171f1dbf388b2e43

See more details on using hashes here.

File details

Details for the file cachedcomplete-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: cachedcomplete-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for cachedcomplete-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 9253bab35de4cdc9e465603c2f2b398a574ea33507501ed2c662327ce870f79a
MD5 80c503dcc80091f68d00305aefbfb1e8
BLAKE2b-256 3ba0cf04a7f3d18ced741388048bc9497e43f139f16a61283ace59795574df18

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page