Local File Manager

Bases: FileManager

Local Folder File manager.

This class provides an local folder implementation of the FileManagerInterface.

Use this class for testing or when you want to store files in a local folder only.

Source code in numerous/files/local.py
  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
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class FileManager(FileManagerInterface):

    """
    Local Folder File manager.

    This class provides an local folder implementation of the FileManagerInterface.

    Use this class for testing or when you want to store files in a local folder only.
    """

    def __init__(self, workfolder: StrOrPath="./tmp") -> None:
        """
        Initialize the LocalFolderFileManager.

        Args:
            workfolder: Path to the working folder.

        """
        self._workfolder = Path(workfolder)

        # Ensure the workfolder exists.
        self._workfolder.mkdir(parents=True, exist_ok=True)

    def put(self, src: StrOrPath, dst: StrOrPath ) -> None:
        """
        Upload a file to a path.

        Args:
            src: Source path.
            dst: Destination path.

        """
        _path = self._workfolder / Path(dst)
        # Ensure the parent folder exists.
        _path.parent.mkdir(parents=True, exist_ok=True)

        with Path.open(Path(src), "rb") as f:
            shutil.copyfileobj(f, Path.open(_path, "wb"))

    def remove(self, path: StrOrPath) -> None:
        """
        Remove a file at a path.

        Args:
            path: Path to file.

        """
        _path = self._workfolder / Path(path)
        Path(_path).unlink()

    def list(self, path: StrOrPath|None) -> list[str]:
        """
        List files at a path.

        Args:
            path: Path to list files at.

        """
        _path = self._workfolder if path is None else self._workfolder / Path(path)

        # Make all paths relative to the workfolder.
        return [str(p.relative_to(self._workfolder))
                for p in _path.rglob("*") if p.is_file()]

    def move(self, src: StrOrPath, dst: StrOrPath) -> None:
        """
        Move a file from a source to a destination.

        Args:
            src: Source path.
            dst: Destination path.

        """
        _src = self._workfolder / Path(src)
        _dst = self._workfolder / Path(dst)
        # Ensure the parent folder exists.
        _dst.parent.mkdir(parents=True, exist_ok=True)
        shutil.move(_src, _dst)


    def copy(self, src: StrOrPath, dst: StrOrPath) -> None:
        """
        Copy a file from a source to a destination.

        Args:
            src: Source path.
            dst: Destination path.

        """
        _src = self._workfolder / Path(src)
        _dst = self._workfolder / Path(dst)
        # Ensure the parent folder exists.
        _dst.parent.mkdir(parents=True, exist_ok=True)
        shutil.copy(_src, _dst)

    def get(self, src: StrOrPath, dest: StrOrPath) -> None:
        """
        Download a file from a source to a destination.

        Args:
            src: Source path.
            dest: Destination path.

        """
        _src = self._workfolder / Path(src)
        # Ensure the parent folder exists.
        Path(dest).parent.mkdir(parents=True, exist_ok=True)
        shutil.copy(_src, dest)

__init__(workfolder='./tmp')

Initialize the LocalFolderFileManager.

Parameters:
  • workfolder (StrOrPath, default: './tmp' ) –

    Path to the working folder.

Source code in numerous/files/local.py
19
20
21
22
23
24
25
26
27
28
29
30
def __init__(self, workfolder: StrOrPath="./tmp") -> None:
    """
    Initialize the LocalFolderFileManager.

    Args:
        workfolder: Path to the working folder.

    """
    self._workfolder = Path(workfolder)

    # Ensure the workfolder exists.
    self._workfolder.mkdir(parents=True, exist_ok=True)

copy(src, dst)

Copy a file from a source to a destination.

Parameters:
  • src (StrOrPath) –

    Source path.

  • dst (StrOrPath) –

    Destination path.

Source code in numerous/files/local.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def copy(self, src: StrOrPath, dst: StrOrPath) -> None:
    """
    Copy a file from a source to a destination.

    Args:
        src: Source path.
        dst: Destination path.

    """
    _src = self._workfolder / Path(src)
    _dst = self._workfolder / Path(dst)
    # Ensure the parent folder exists.
    _dst.parent.mkdir(parents=True, exist_ok=True)
    shutil.copy(_src, _dst)

get(src, dest)

Download a file from a source to a destination.

Parameters:
  • src (StrOrPath) –

    Source path.

  • dest (StrOrPath) –

    Destination path.

Source code in numerous/files/local.py
104
105
106
107
108
109
110
111
112
113
114
115
116
def get(self, src: StrOrPath, dest: StrOrPath) -> None:
    """
    Download a file from a source to a destination.

    Args:
        src: Source path.
        dest: Destination path.

    """
    _src = self._workfolder / Path(src)
    # Ensure the parent folder exists.
    Path(dest).parent.mkdir(parents=True, exist_ok=True)
    shutil.copy(_src, dest)

list(path)

List files at a path.

Parameters:
  • path (StrOrPath | None) –

    Path to list files at.

Source code in numerous/files/local.py
59
60
61
62
63
64
65
66
67
68
69
70
71
def list(self, path: StrOrPath|None) -> list[str]:
    """
    List files at a path.

    Args:
        path: Path to list files at.

    """
    _path = self._workfolder if path is None else self._workfolder / Path(path)

    # Make all paths relative to the workfolder.
    return [str(p.relative_to(self._workfolder))
            for p in _path.rglob("*") if p.is_file()]

move(src, dst)

Move a file from a source to a destination.

Parameters:
  • src (StrOrPath) –

    Source path.

  • dst (StrOrPath) –

    Destination path.

Source code in numerous/files/local.py
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def move(self, src: StrOrPath, dst: StrOrPath) -> None:
    """
    Move a file from a source to a destination.

    Args:
        src: Source path.
        dst: Destination path.

    """
    _src = self._workfolder / Path(src)
    _dst = self._workfolder / Path(dst)
    # Ensure the parent folder exists.
    _dst.parent.mkdir(parents=True, exist_ok=True)
    shutil.move(_src, _dst)

put(src, dst)

Upload a file to a path.

Parameters:
  • src (StrOrPath) –

    Source path.

  • dst (StrOrPath) –

    Destination path.

Source code in numerous/files/local.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def put(self, src: StrOrPath, dst: StrOrPath ) -> None:
    """
    Upload a file to a path.

    Args:
        src: Source path.
        dst: Destination path.

    """
    _path = self._workfolder / Path(dst)
    # Ensure the parent folder exists.
    _path.parent.mkdir(parents=True, exist_ok=True)

    with Path.open(Path(src), "rb") as f:
        shutil.copyfileobj(f, Path.open(_path, "wb"))

remove(path)

Remove a file at a path.

Parameters:
  • path (StrOrPath) –

    Path to file.

Source code in numerous/files/local.py
48
49
50
51
52
53
54
55
56
57
def remove(self, path: StrOrPath) -> None:
    """
    Remove a file at a path.

    Args:
        path: Path to file.

    """
    _path = self._workfolder / Path(path)
    Path(_path).unlink()