Skip to content

Лимит на суммарный размер файлов

Bases: BaseFileLimit, FrozenModel

Limits the total size of files handled by [file-downloader][] or [file-mover][].

Calculates the sum of downloaded/moved files size (.stat().st_size), and checks that this sum is less or equal to specified limit.

After limit is reached, no more files will be downloaded/moved.

Doesn't affect directories, paths without .stat() method or files with zero size.

Added in 0.13.0

Note

SI unit prefixes means that 1KB == 1 kilobyte == 1000 bytes. If you need 1024 bytes, use 1 KiB == 1 kibibyte.

Parameters

int or str

Maximum total size of files to be handled. Can be an integer (bytes) or a string like 1GiB.

Examples

Create filter which allows to download/move files with total size up to 1GiB, but not higher:

from onetl.file.limit import MaxFilesCount

limit = TotalFilesSize("1GiB")
Source code in onetl/file/limit/total_files_size.py
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
class TotalFilesSize(BaseFileLimit, FrozenModel):
    """Limits the total size of files handled by [file-downloader][] or [file-mover][].

    Calculates the sum of downloaded/moved files size (`.stat().st_size`),
    and checks that this sum is less or equal to specified limit.

    After limit is reached, no more files will be downloaded/moved.

    Doesn't affect directories, paths without `.stat()` method or files with zero size.

    !!! success "Added in 0.13.0"

    !!! note

        [SI unit prefixes](https://en.wikipedia.org/wiki/Byte#Multiple-byte_units)
        means that `1KB` == `1 kilobyte` == `1000 bytes`.
        If you need `1024 bytes`, use `1 KiB` == `1 kibibyte`.

    Parameters
    ----------

    limit : int or str
        Maximum total size of files to be handled. Can be an integer (bytes) or a string like `1GiB`.

    Examples
    --------

    Create filter which allows to download/move files with total size up to 1GiB, but not higher:

    ```python
    from onetl.file.limit import MaxFilesCount

    limit = TotalFilesSize("1GiB")

    ```
    """

    limit: ByteSize

    _handled: int = 0

    def __init__(self, limit: int | str):
        # this is only to allow passing glob as positional argument
        super().__init__(limit=limit)

    def __repr__(self):
        return f'{self.__class__.__name__}("{self.limit.human_readable()}")'

    @validator("limit")
    def _limit_cannot_be_negative(cls, value):
        if value <= 0:
            msg = "Limit should be positive number"
            raise ValueError(msg)
        return value

    def reset(self):
        self._handled = 0
        return self

    def stops_at(self, path: PathProtocol) -> bool:
        if self.is_reached:
            return True

        if not path.is_file():
            # directories count does not matter
            return False

        if not isinstance(path, PathWithStatsProtocol):
            return False

        self._handled += path.stat().st_size
        return self.is_reached

    @property
    def is_reached(self) -> bool:
        return self._handled > self.limit

is_reached property

reset()

Source code in onetl/file/limit/total_files_size.py
75
76
77
def reset(self):
    self._handled = 0
    return self

stops_at(path)

Source code in onetl/file/limit/total_files_size.py
79
80
81
82
83
84
85
86
87
88
89
90
91
def stops_at(self, path: PathProtocol) -> bool:
    if self.is_reached:
        return True

    if not path.is_file():
        # directories count does not matter
        return False

    if not isinstance(path, PathWithStatsProtocol):
        return False

    self._handled += path.stat().st_size
    return self.is_reached