Skip to content

Filesystems

lume_services.services.files.filesystems.filesystem

Classes

Filesystem

Bases: ABC, BaseModel

Attributes
identifier class-attribute
identifier: str
Functions
dir_exists abstractmethod
dir_exists(dir: str, create_dir: bool = False) -> bool

Check that a directory exists

Parameters:

Name Type Description Default
dir str

Path of directory

required
create_dir bool

Whether to create directory if it does not exist

False

Returns:

Type Description
bool

bool

Source code in lume_services/services/files/filesystems/filesystem.py
10
11
12
13
14
15
16
17
18
19
20
21
@abstractmethod
def dir_exists(self, dir: str, create_dir: bool = False) -> bool:
    """Check that a directory exists

    Args:
        dir (str): Path of directory
        create_dir (bool): Whether to create directory if it does not exist

    Returns:
        bool
    """
    ...
file_exists abstractmethod
file_exists(filepath: str) -> bool

Check that a file exists

Parameters:

Name Type Description Default
filepath str

Path to file

required

Returns:

Type Description
bool

bool

Source code in lume_services/services/files/filesystems/filesystem.py
23
24
25
26
27
28
29
30
31
32
33
34
@abstractmethod
def file_exists(self, filepath: str) -> bool:
    """Check that a file exists

    Args:
        filepath (str): Path to file

    Returns:
        bool

    """
    ...
create_dir abstractmethod
create_dir(dir: str) -> None

Create a directory on the filesystem.

Parameters:

Name Type Description Default
dir str

Directory path to create

required
Source code in lume_services/services/files/filesystems/filesystem.py
36
37
38
39
40
41
42
43
44
@abstractmethod
def create_dir(self, dir: str) -> None:
    """Create a directory on the filesystem.

    Args:
        dir (str): Directory path to create

    """
    ...
read abstractmethod
read(filepath: str, serializer: SerializerBase) -> Any

Read file from the filesystem.

Parameters:

Name Type Description Default
filepath str

Path of file to read

required
serializer SerializerBase

Implementation of lume-base SerializerBase abstract base class

required
Source code in lume_services/services/files/filesystems/filesystem.py
46
47
48
49
50
51
52
53
54
55
56
@abstractmethod
def read(self, filepath: str, serializer: SerializerBase) -> Any:
    """Read file from the filesystem.

    Args:
        filepath (str): Path of file to read
        serializer (SerializerBase): Implementation of lume-base SerializerBase
            abstract base class

    """
    ...
write abstractmethod
write(
    filepath: str,
    object: Any,
    serializer: SerializerBase,
    create_dir: bool = False,
) -> None

Write a file to the filesystem.

Parameters:

Name Type Description Default
filepath str required
object Any

Object to serialize

required
serializer SerializerBase

Implementation of lume-base SerializerBase abstract base class

required
create_dir bool

Whether to create directory in case not implemented

False
Source code in lume_services/services/files/filesystems/filesystem.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
@abstractmethod
def write(
    self,
    filepath: str,
    object: Any,
    serializer: SerializerBase,
    create_dir: bool = False,
) -> None:
    """Write a file to the filesystem.

    Args:
        filepath (str):
        object (Any): Object to serialize
        serializer (SerializerBase): Implementation of lume-base SerializerBase
            abstract base class
        create_dir (bool): Whether to create directory in case not implemented

    """
    ...

lume_services.services.files.filesystems.local

Classes

LocalFilesystem

Bases: Filesystem

Handler for local filesystem.

Attributes
identifier class-attribute
identifier: str = 'local'
Functions
dir_exists
dir_exists(dir: str, create_dir: bool = False) -> bool

Check that a directory exists on the local filesystem.

Parameters:

Name Type Description Default
dir str

Path of directory

required
create_dir bool

Whether to create directory if it does not exist

False

Returns:

Type Description
bool

bool

Source code in lume_services/services/files/filesystems/local.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def dir_exists(self, dir: str, create_dir: bool = False) -> bool:
    """Check that a directory exists on the local filesystem.

    Args:
        dir (str): Path of directory
        create_dir (bool): Whether to create directory if it does not exist

    Returns:
        bool

    """
    # use absolute path
    path = os.path.abspath(dir)
    if os.path.isdir(path):
        logger.info("Found directory %s on local filesystem.", path)
        return True

    # if creating...
    if create_dir:
        self.create_dir(path)
        return True
    else:
        logger.info("Unable to find directory %s on local filesystem.", path)
        return False
file_exists
file_exists(filepath: str) -> bool

Check that a file exists on the local filesystem.

Parameters:

Name Type Description Default
filepath str

Path to file

required

Returns:

Type Description
bool

bool

Source code in lume_services/services/files/filesystems/local.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def file_exists(self, filepath: str) -> bool:
    """Check that a file exists on the local filesystem.

    Args:
        filepath (str): Path to file

    Returns:
        bool

    """
    path = os.path.abspath(filepath)
    if os.path.isfile(path):
        logger.info("Found file %s on local filesystem.", path)
        return True

    else:
        logger.info("Unable to find file %s on local filesystem.", path)
        return False
create_dir
create_dir(dir: str) -> None

Create a directory on the local filesystem.

Parameters:

Name Type Description Default
dir str

Directory path to create

required
Source code in lume_services/services/files/filesystems/local.py
60
61
62
63
64
65
66
67
68
69
70
71
def create_dir(self, dir: str) -> None:
    """Create a directory on the local filesystem.

    Args:
        dir (str): Directory path to create

    """
    try:
        os.makedirs(dir, exist_ok=False)
    except Exception as e:
        logger.error("Unable to create directory %s on local filesystem.", dir)
        raise e
read
read(filepath: str, serializer: SerializerBase) -> Any

Read file from the local filesystem.

Parameters:

Name Type Description Default
filepath str

Path of file to read.

required
serializer SerializerBase

Implementation of lume-base SerializerBase abstract base class.

required
Source code in lume_services/services/files/filesystems/local.py
73
74
75
76
77
78
79
80
81
82
83
84
def read(self, filepath: str, serializer: SerializerBase) -> Any:
    """Read file from the local filesystem.

    Args:
        filepath (str): Path of file to read.
        serializer (SerializerBase): Implementation of lume-base SerializerBase
            abstract base class.

    """
    path = os.path.abspath(filepath)
    content = serializer.deserialize(path)
    return content
write
write(
    filepath: str,
    object: Any,
    serializer: SerializerBase,
    create_dir: bool = False,
) -> None

Write a file to the local filesystem.

Parameters:

Name Type Description Default
filepath str required
object Any

Object to serialize

required
serializer SerializerBase

Implementation of lume-base SerializerBase abstract base class

required
create_dir bool

Whether to create directory in case not implemented

False
Source code in lume_services/services/files/filesystems/local.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
def write(
    self,
    filepath: str,
    object: Any,
    serializer: SerializerBase,
    create_dir: bool = False,
) -> None:
    """Write a file to the local filesystem.

    Args:
        filepath (str):
        object (Any): Object to serialize
        serializer (SerializerBase): Implementation of lume-base SerializerBase
            abstract base class
        create_dir (bool): Whether to create directory in case not implemented

    """
    path = os.path.abspath(filepath)
    dir = os.path.dirname(path)

    if create_dir and not self.dir_exists(dir):
        self.create_dir(dir)

    serializer.serialize(path, object)

lume_services.services.files.filesystems.mounted

Classes

PathNotInMount

PathNotInMount(
    filesystem_identifier: str,
    path: str,
    mount_path: str,
    mount_alias: str,
)

Bases: Exception

Source code in lume_services/services/files/filesystems/mounted.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def __init__(
    self, filesystem_identifier: str, path: str, mount_path: str, mount_alias: str
):
    self.filesystem_identifier = filesystem_identifier
    self.path = (path,)
    self.mount_path = mount_path
    self.mount_alias = mount_alias
    self.message = "Path %s not in mount for mounted filesystem identifier: %s, \
        Mount path: %s, Mount alias: %s"
    super().__init__(
        self.message,
        self.path,
        self.filesystem_identifier,
        self.mount_path,
        self.mount_alias,
    )
Attributes
filesystem_identifier instance-attribute
filesystem_identifier = filesystem_identifier
path instance-attribute
path = (path)
mount_path instance-attribute
mount_path = mount_path
mount_alias instance-attribute
mount_alias = mount_alias
message instance-attribute
message = "Path %s not in mount for mounted filesystem identifier: %s,             Mount path: %s, Mount alias: %s"
Functions

MountedFilesystem

Bases: LocalFilesystem

Handler for mounted filesystem. Modifies the LocalFilesystem to implements checks for mount path modifications. Container and container orchestration tools often allow the ability to provide an alias for a mounted directory. This handler accounts for the mount base and verifies that the file is within the path.

Attributes
identifier class-attribute
identifier: str = 'mounted'
mount_path class-attribute
mount_path: str
mount_alias class-attribute
mount_alias: str
mount_type class-attribute
mount_type: _HostMountLiteral = 'DirectoryOrCreate'
Functions
validate_mount_path
validate_mount_path(v, values)
Source code in lume_services/services/files/filesystems/mounted.py
55
56
57
58
59
60
61
62
@validator("mount_path", pre=True)
def validate_mount_path(cls, v, values):
    mount_type = values.get("mount_type")

    if mount_type == "DirectoryOrCreate":
        os.mkdir(v)

    return v
dir_exists
dir_exists(dir: str, create_dir: bool = False) -> bool

Check that a directory exists on the mounted filesystem.

Parameters:

Name Type Description Default
dir str

Path of directory

required
create_dir bool

Whether to create directory if it does not exist

False

Returns:

Type Description
bool

bool

Source code in lume_services/services/files/filesystems/mounted.py
64
65
66
67
68
69
70
71
72
73
74
75
76
def dir_exists(self, dir: str, create_dir: bool = False) -> bool:
    """Check that a directory exists on the mounted filesystem.

    Args:
        dir (str): Path of directory
        create_dir (bool): Whether to create directory if it does not exist

    Returns:
        bool

    """
    dir = self._check_mounted_path(dir)
    return super().dir_exists(dir, create_dir=create_dir)
file_exists
file_exists(filepath: str) -> bool

Check that a file exists on the mounted filesystem.

Parameters:

Name Type Description Default
filepath str

Path to file

required

Returns:

Type Description
bool

bool

Source code in lume_services/services/files/filesystems/mounted.py
78
79
80
81
82
83
84
85
86
87
88
89
90
def file_exists(self, filepath: str) -> bool:
    """Check that a file exists on the mounted filesystem.

    Args:
        filepath (str): Path to file

    Returns:
        bool

    """

    filepath = self._check_mounted_path(filepath)
    return super().file_exists(filepath)
create_dir
create_dir(dir: str) -> None

Create a directory on the mounted filesystem.

Parameters:

Name Type Description Default
dir str

Directory path to create

required
Source code in lume_services/services/files/filesystems/mounted.py
 92
 93
 94
 95
 96
 97
 98
 99
100
def create_dir(self, dir: str) -> None:
    """Create a directory on the mounted filesystem.

    Args:
        dir (str): Directory path to create

    """
    dir = self._check_mounted_path(dir)
    super().create_dir(dir)
read
read(filepath: str, serializer: SerializerBase) -> Any

Read file from the mounted filesystem.

Parameters:

Name Type Description Default
filepath str

Path of file to read

required
serializer SerializerBase

Implementation of lume-base SerializerBase abstract base class

required
Source code in lume_services/services/files/filesystems/mounted.py
102
103
104
105
106
107
108
109
110
111
112
def read(self, filepath: str, serializer: SerializerBase) -> Any:
    """Read file from the mounted filesystem.

    Args:
        filepath (str): Path of file to read
        serializer (SerializerBase): Implementation of lume-base SerializerBase
            abstract base class

    """
    filepath = self._check_mounted_path(filepath)
    return super().read(filepath, serializer)
write
write(
    filepath: str,
    object: Any,
    serializer: SerializerBase,
    create_dir: bool = False,
) -> None

Write a file to the mounted filesystem.

Parameters:

Name Type Description Default
filepath str required
object Any

Object to serialize

required
serializer SerializerBase

Implementation of lume-base SerializerBase abstract base class

required
create_dir bool

Whether to create directory in case not implemented

False
Source code in lume_services/services/files/filesystems/mounted.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
def write(
    self,
    filepath: str,
    object: Any,
    serializer: SerializerBase,
    create_dir: bool = False,
) -> None:
    """Write a file to the mounted filesystem.

    Args:
        filepath (str):
        object (Any): Object to serialize
        serializer (SerializerBase): Implementation of lume-base SerializerBase
            abstract base class
        create_dir (bool): Whether to create directory in case not implemented

    """
    filepath = self._check_mounted_path(filepath)

    super().write(filepath, object, serializer, create_dir=create_dir)