aboutsummaryrefslogtreecommitdiffstats
path: root/bot/utils/persist.py
blob: 35e1e41a22e25eb2df44baa1e02dbad00df9248a (plain) (blame)
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
import sqlite3
from pathlib import Path
from shutil import copyfile

DIRECTORY = Path("data")  # directory that has a persistent volume mapped to it


def datafile(file_path: Path) -> Path:
    """
    Copy datafile at the provided file_path to the persistent data directory.

    A persistent data file is needed by some features in order to not lose data
    after bot rebuilds.

    This function will ensure that a clean data file with default schema,
    structure or data is copied over to the persistent volume before returning
    the path to this new persistent version of the file.

    If the persistent file already exists, it won't be overwritten with the
    clean default file, just returning the Path instead to the existing file.

    Example Usage:
    >>> clean_default_datafile = Path("bot", "resources", "datafile.json")
    >>> persistent_file_path = datafile(clean_default_datafile)
    """
    if not file_path.is_file():
        raise OSError(f"File not found at {file_path}.")

    persistant_path = Path(DIRECTORY, file_path.name)

    if not persistant_path.exists():
        copyfile(file_path, persistant_path)

    return persistant_path


def sqlite(db_path: Path) -> sqlite3.Connection:
    """Copy sqlite file to the persistent data directory and return an open connection."""
    persistant_path = datafile(db_path)
    return sqlite3.connect(persistant_path)