Skip to main content

File cache for files retrieved from the cloud

Project description

GitHub release; latest by date GitHub Release Date Test Status Documentation Status Code coverage
PyPI - Version PyPI - Format PyPI - Downloads PyPI - Python Version
GitHub commits since latest release GitHub commit activity GitHub last commit
Number of GitHub open issues Number of GitHub closed issues Number of GitHub open pull requests Number of GitHub closed pull requests
GitHub License Number of GitHub stars GitHub forks

Introduction

filecache is a Python module that abstracts away the location where files used or generated by a program are stored. Files can be on the local file system, in Google Cloud Storage, on Amazon Web Services S3, or on a webserver. When files to be read are on the local file system, they are simply accessed in-place. Otherwise, they are downloaded from the remote source to a local temporary directory. When files to be written are on the local file system, they are simply written in-place. Otherwise, they are written to a local temporary directory and then uploaded to the remote location (it is not possible to upload to a webserver). When a cache is no longer needed, it is deleted from the local disk.

filecache is a product of the PDS Ring-Moon Systems Node.

Installation

The filecache module is available via the rms-filecache package on PyPI and can be installed with:

pip install rms-filecache

Getting Started

The top-level file organization is provided by the FileCache class. A FileCache instance is used to specify a particular sharing policy and lifetime. For example, a cache could be private to the current process and group a set of files that all have the same basic purpose. Once these files have been (downloaded and) read, they are deleted as a group. Another cache could be shared among all processes on the current machine and group a set of files that are needed by multiple processes, thus allowing them to be downloaded from a remote source only one time, saving time and bandwidth.

A FileCache can be instantiated either directly or as a context manager. When instantiated directly, the programmer is responsible for calling FileCache.clean_up directly to delete the cache when finished. In addition, a non-shared cache will be deleted on program exit. When instantiated as a context manager, a non-shared cache is deleted on exit from the context. See the class documentation for full details.

Usage examples:

from filecache import FileCache
with FileCache() as fc:  # Use as context manager
    # Also use open() as a context manager
    with fc.open('gs://rms-filecache-tests/subdir1/subdir2a/binary1.bin', 'rb',
                 anonymous=True) as fp:
        bin1 = fp.read()
    with fc.open('s3://rms-filecache-tests/subdir1/subdir2a/binary1.bin', 'rb',
                 anonymous=True) as fp:
        bin2 = fp.read()
    assert bin1 == bin2
# Cache automatically deleted here

fc = FileCache()  # Use without context manager
# Also retrieve file without using open context manager
path1 = fc.retrieve('gs://rms-filecache-tests/subdir1/subdir2a/binary1.bin',
                    anonymous=True)
with open(path1, 'rb') as fp:
    bin1 = fp.read()
path2 = fc.retrieve('s3://rms-filecache-tests/subdir1/subdir2a/binary1.bin',
                    anonymous=True)
with open(path2, 'rb') as fp:
    bin2 = fp.read()
fc.clean_up()  # Cache manually deleted here
assert bin1 == bin2

# Write a file to a bucket and read it back
with FileCache() as fc:
    with fc.open('gs://my-writable-bucket/output.txt', 'w') as fp:
        fp.write('A')
# The cache will be deleted here so the file will have to be downloaded
with FileCache() as fc:
    with fc.open('gs://my-writable-bucket/output.txt', 'r') as fp:
        print(fp.read())

A FileCachePrefix instance can be used to encapsulate the storage prefix string, as well as any subdirectories, plus various arguments such as anonymous and time_out that can be specified to each exists, retrieve, or upload method. Thus using one of these instances can simplify the use of a FileCache by allowing the user to only specify the relative part of the path to be operated on, and to not specify various other parameters at each method call site.

Compare this example to the one above:

from filecache import FileCache
with FileCache() as fc:  # Use as context manager
    # Use GS by specifying the bucket name and one directory level
    pfx1 = fc.new_prefix('gs://rms-filecache-tests/subdir1', anonymous=True)
    # Use S3 by specifying the bucket name and two directory levels
    pfx2 = fc.new_prefix('s3://rms-filecache-tests/subdir1/subdir2a', anonymous=True)
    # Access GS using a directory + filename (since only one directory level
    # was specified by the prefix)
    # Also use open() as a context manager
    with pfx1.open('subdir2a/binary1.bin', 'rb') as fp:
        bin1 = fp.read()
    # Access S3 using a filename only (since two directory levels were already
    # specified by the prefix))
    with pfx2.open('binary1.bin', 'rb') as fp:
        bin2 = fp.read()
    assert bin1 == bin2
# Cache automatically deleted here

A benefit of the abstraction is that different environments can access the same files in different ways without needing to change the program code. For example, consider a program that needs to access the file COISS_2xxx/COISS_2001/voldesc.cat from the NASA PDS archives. This file might be stored on the local disk in the user's home directory in a subdirectory called pds3-holdings. Or if the user does not have a local copy, it is accessible from a webserver at https://pds-rings.seti.org/holdings/volumes/COISS_2xxx/COISS_2001/voldesc.cat. Finally, it could be accessible from Google Cloud Storage from the rms-node-holdings bucket at gs://rms-node-holdings/pds3-holdings/volumes/COISS_2xxx/COISS_2001/voldesc.cat. Before running the program, an environment variable could be set to one of these values::

$ export PDS3_HOLDINGS_SRC="~/pds3-holdings"
$ export PDS3_HOLDINGS_SRC="https://pds-rings.seti.org/holdings"
$ export PDS3_HOLDINGS_SRC="gs://rms-node-holdings/pds3-holdings"

Then the program could be written as:

from filecache import FileCache
import os
with FileCache() as fc:
    pfx = fc.new_prefix(os.getenv('PDS3_HOLDINGS_SRC'))
    with pfx.open('volumes/COISS_2xxx/COISS_2001/voldesc.cat', 'r') as fp:
        contents = fp.read()
# Cache automatically deleted here

If the program was going to be run multiple times in a row, or multiple copies were going to be run simultaneously, marking the cache as shared would allow all of the processes to share the same copy, thus requiring only a single download no matter how many times the program was run:

from filecache import FileCache
import os
with FileCache(shared=True) as fc:
    pfx = fc.new_prefix(os.getenv('PDS3_HOLDINGS_DIR'))
    with pfx.open('volumes/COISS_2xxx/COISS_2001/voldesc.cat', 'r') as fp:
        contents = fp.read()
# Cache not deleted here; must be deleted manually using fc.clean_up(final=True)
# If not deleted manually, the shared cache will persist until the temporary
# directory is purged by the operating system (which may be never)

Finally, there are four classes that allow direct access to the four possible storage locations without invoking any caching behavior: :class:FileCacheSourceLocal, :class:FileCacheSourceHTTP, :class:FileCacheSourceGS, and :class:FileSourceCacheS3:

    from filecache import FileCacheSourceGS
    src = FileCacheSourceGS('gs://rms-filecache-tests', anonymous=True)
    src.retrieve('subdir1/subdir2a/binary1.bin', 'local_file.bin')

Details of each class are available in the module documentation.

Contributing

Information on contributing to this package can be found in the Contributing Guide.

Links

Licensing

This code is licensed under the Apache License v2.0.

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

rms_filecache-1.2.0.tar.gz (185.1 kB view details)

Uploaded Source

Built Distribution

rms_filecache-1.2.0-py3-none-any.whl (26.6 kB view details)

Uploaded Python 3

File details

Details for the file rms_filecache-1.2.0.tar.gz.

File metadata

  • Download URL: rms_filecache-1.2.0.tar.gz
  • Upload date:
  • Size: 185.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for rms_filecache-1.2.0.tar.gz
Algorithm Hash digest
SHA256 f9823dafabfca872223551c713eb4aeb9b825a179656e039178f57af7c01641b
MD5 7368638cce0117406c0e365e1fcbbb9b
BLAKE2b-256 bb34995756ede7ef6468ee565d34c653fe2281596bf98be100f8a5d5cb8ca6cf

See more details on using hashes here.

Provenance

File details

Details for the file rms_filecache-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for rms_filecache-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fb2df5aee8e4eb54a1f1df1ff5baa63d6528d5927816da9a84eff1b590a46b95
MD5 1a5fda1217e5e9e4e243bb748233f8f5
BLAKE2b-256 3f7f79682fb2df43b30f9bdff5de6d5a22f8e6e73764f1d3d1913b6aa6ccdde9

See more details on using hashes here.

Provenance

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