In-memory File Manager

Bases: FileManager

In Memory File manager.

This class provides an in-memory implementation of the FileManagerInterface.

Use this class for testing or when you want to store files in memory only. This is the default file manager return by the file_manager_factory method.

Source code in numerous/files/memory.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
class FileManager(FileManagerInterface):

    """
    In Memory File manager.

    This class provides an in-memory implementation of the FileManagerInterface.

    Use this class for testing or when you want to store files in memory only.
    This is the default file manager return by the file_manager_factory method.
    """

    def __init__(self) -> None:
        self._files: dict[str, bytes] = {}

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

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

        """
        with Path.open(Path(src), "rb") as f:
            self._files[dst] = f.read()

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

        Args:
            path: Path to file.

        """
        del self._files[path]

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

        Args:
            path: Path to list files at.

        """
        if path is None:
            return list(self._files.keys())
        return [p for p in self._files if p.startswith(path)]

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

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

        """
        self._files[dst] = self._files[src]
        del self._files[src]

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

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

        """
        self._files[dst] = self._files[src]

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

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

        """
        with Path.open(Path(dest), "wb") as f:
            f.write(self._files[src])

copy(src, dst)

Copy a file from a source to a destination.

Parameters:
  • src (str) –

    Source path.

  • dst (str) –

    Destination path.

Source code in numerous/files/memory.py
69
70
71
72
73
74
75
76
77
78
def copy(self, src: str, dst: str) -> None:
    """
    Copy a file from a source to a destination.

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

    """
    self._files[dst] = self._files[src]

get(src, dest)

Download a file from a source to a destination.

Parameters:
  • src (str) –

    Source path.

  • dest (StrOrPath) –

    Destination path.

Source code in numerous/files/memory.py
80
81
82
83
84
85
86
87
88
89
90
def get(self, src: str, dest: StrOrPath) -> None:
    """
    Download a file from a source to a destination.

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

    """
    with Path.open(Path(dest), "wb") as f:
        f.write(self._files[src])

list(path)

List files at a path.

Parameters:
  • path (str | None) –

    Path to list files at.

Source code in numerous/files/memory.py
45
46
47
48
49
50
51
52
53
54
55
def list(self, path: str|None) -> List[str]:
    """
    List files at a path.

    Args:
        path: Path to list files at.

    """
    if path is None:
        return list(self._files.keys())
    return [p for p in self._files if p.startswith(path)]

move(src, dst)

Move a file from a source to a destination.

Parameters:
  • src (str) –

    Source path.

  • dst (str) –

    Destination path.

Source code in numerous/files/memory.py
57
58
59
60
61
62
63
64
65
66
67
def move(self, src: str, dst: str) -> None:
    """
    Move a file from a source to a destination.

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

    """
    self._files[dst] = self._files[src]
    del self._files[src]

put(src, dst)

Upload a file to a path.

Parameters:
  • src (StrOrPath) –

    Source path.

  • dst (str) –

    Destination path.

Source code in numerous/files/memory.py
23
24
25
26
27
28
29
30
31
32
33
def put(self, src: StrOrPath, dst: str ) -> None:
    """
    Upload a file to a path.

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

    """
    with Path.open(Path(src), "rb") as f:
        self._files[dst] = f.read()

remove(path)

Remove a file at a path.

Parameters:
  • path (str) –

    Path to file.

Source code in numerous/files/memory.py
35
36
37
38
39
40
41
42
43
def remove(self, path: str) -> None:
    """
    Remove a file at a path.

    Args:
        path: Path to file.

    """
    del self._files[path]