Skip to content

cloudpathlib.enums

Classes

FileCacheMode

Bases: str, Enum

Enumeration of the modes available for for the cloudpathlib file cache.

Attributes:

Name Type Description
persistent str

Cache is not removed by cloudpathlib.

tmp_dir str

Cache is stored in a 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 CLOUDPATHLIB_FILE_CACHE_MODE environment variable.

For more detail, see the caching documentation page.

Source code in cloudpathlib/enums.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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 `CLOUDPATHLIB_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 `CLOUDPATHLIB_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("CLOUDPATHLIB_FILE_CACHE_MODE", "").lower()

        if not env_string:
            return None
        else:
            return cls(env_string)

Attributes

close_file = 'close_file' class-attribute instance-attribute
cloudpath_object = 'cloudpath_object' class-attribute instance-attribute
persistent = 'persistent' class-attribute instance-attribute
tmp_dir = 'tmp_dir' class-attribute instance-attribute

Functions

from_environment() -> Optional[FileCacheMode] classmethod

Parses the environment variable CLOUDPATHLIB_FILE_CACHE_MODE into an instance of this Enum.

Returns:

Type Description
Optional[FileCacheMode]

FileCacheMode enum value if the env var is defined, else None.

Source code in cloudpathlib/enums.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
@classmethod
def from_environment(cls) -> Optional["FileCacheMode"]:
    """Parses the environment variable `CLOUDPATHLIB_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("CLOUDPATHLIB_FILE_CACHE_MODE", "").lower()

    if not env_string:
        return None
    else:
        return cls(env_string)