diff options
author | 2019-09-19 22:59:46 +1000 | |
---|---|---|
committer | 2019-09-19 22:59:46 +1000 | |
commit | f91d49d987065df43cbaa8264d349885f001aa17 (patch) | |
tree | 4f44b8729a19c4020651e261cbda524a7884d87b /bot/utils/persist.py | |
parent | Prevent empty JSON raising an exception. (diff) |
Add persistent datafile utils.
Diffstat (limited to 'bot/utils/persist.py')
-rw-r--r-- | bot/utils/persist.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/bot/utils/persist.py b/bot/utils/persist.py new file mode 100644 index 00000000..ec6f306a --- /dev/null +++ b/bot/utils/persist.py @@ -0,0 +1,24 @@ +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.""" + if not file_path.exists(): + 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) |