1
 2
 3
 4
 5
 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
"""
byceps.util.upload
~~~~~~~~~~~~~~~~~~

:Copyright: 2014-2025 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

from pathlib import Path
from shutil import copyfileobj
from typing import Any, IO


def store(
    source: IO[Any],
    target_path: Path,
    *,
    create_parent_path_if_nonexistent: bool = False,
) -> None:
    """Copy source data to the target path."""
    if target_path.exists():
        raise FileExistsError

    if create_parent_path_if_nonexistent:
        _create_path_if_nonexistent(target_path.parent)

    with target_path.open('wb') as f:
        copyfileobj(source, f)


def delete(path: Path) -> None:
    """Delete the path."""
    try:
        path.unlink()
    except OSError:
        pass


def _create_path_if_nonexistent(path: Path) -> None:
    """Create the path (and its parent paths) if it does not exist."""
    if not path.exists():
        path.mkdir(parents=True)