-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement fine-grained file cache control options (#314)
* Implementation of cache options * Cache tests * Cache mode documentation * changelog and version bump * Update tests * Hide input output for jupyter cells * cleanup client in any non-persistent cahce mode * simplify test suite; add missing tests * stabailize test * configuration changes * autoformat notebooks * Add nbautoexport for docs * Update docs * Reorder kwargs * Update typos in caching docs * Add feedback to docs * Update docs/docs/script/caching.py Co-authored-by: Jay Qi <[email protected]> * update notebook as well --------- Co-authored-by: Jay Qi <[email protected]>
- Loading branch information
Showing
26 changed files
with
1,626 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from enum import Enum | ||
import os | ||
from typing import Optional | ||
|
||
|
||
class FileCacheMode(str, Enum): | ||
"""Enumeration of the modes available for for the cloudpathlib file cache. | ||
Attributes: | ||
persistent (str): Cache is not removed by `cloudpathlib`. | ||
tmp_dir (str): Cache is stored in a | ||
[`TemporaryDirectory`](https://docs.python.org/3/library/tempfile.html#tempfile.TemporaryDirectory) | ||
which is removed when the Client object is garbage collected (or by the OS at some point if not). | ||
cloudpath_object (str): Cache for a `CloudPath` object is removed when `__del__` for that object is | ||
called by Python garbage collection. | ||
close_file (str): Cache for a `CloudPath` file is removed as soon as the file is closed. Note: you must | ||
use `CloudPath.open` whenever opening the file for this method to function. | ||
Modes can be set by passing them to the Client or by setting the `CLOUPATHLIB_FILE_CACHE_MODE` | ||
environment variable. | ||
For more detail, see the [caching documentation page](../../caching). | ||
""" | ||
|
||
persistent = "persistent" # cache stays as long as dir on OS does | ||
tmp_dir = "tmp_dir" # DEFAULT: handled by deleting client, Python, or OS (usually on machine restart) | ||
cloudpath_object = "cloudpath_object" # __del__ called on the CloudPath object | ||
close_file = "close_file" # cache is cleared when file is closed | ||
|
||
@classmethod | ||
def from_environment(cls) -> Optional["FileCacheMode"]: | ||
"""Parses the environment variable `CLOUPATHLIB_FILE_CACHE_MODE` into | ||
an instance of this Enum. | ||
Returns: | ||
FileCacheMode enum value if the env var is defined, else None. | ||
""" | ||
env_string = os.environ.get("CLOUPATHLIB_FILE_CACHE_MODE", "").lower() | ||
|
||
if not env_string: | ||
return None | ||
else: | ||
return cls(env_string) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.