From 958d4e43db510152a376b686b86614c584414588 Mon Sep 17 00:00:00 2001 From: Rob <5183487+Rexeh@users.noreply.github.com> Date: Wed, 13 Mar 2024 19:46:38 +0000 Subject: [PATCH] Change data storage to AppData --- joystick_diagrams/__main__.py | 10 +++++++++- joystick_diagrams/db/db_connection.py | 5 +++-- joystick_diagrams/db/db_handler.py | 2 +- joystick_diagrams/utils.py | 11 ++++++++++- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/joystick_diagrams/__main__.py b/joystick_diagrams/__main__.py index 183caa2..7f0faa8 100644 --- a/joystick_diagrams/__main__.py +++ b/joystick_diagrams/__main__.py @@ -1,7 +1,12 @@ import logging from logging.handlers import RotatingFileHandler +from pathlib import Path from joystick_diagrams import app_init +from joystick_diagrams.utils import create_directory, data_root + +log_path = Path.joinpath(data_root(), "logs") +create_directory(str(log_path)) logging.basicConfig( level=logging.INFO, @@ -9,7 +14,10 @@ handlers=[ logging.StreamHandler(), RotatingFileHandler( - "application.log", mode="a", maxBytes=5 * 1000000, backupCount=1 + str(Path.joinpath(log_path, "application.log")), + mode="a", + maxBytes=5 * 1000000, + backupCount=1, ), ], ) diff --git a/joystick_diagrams/db/db_connection.py b/joystick_diagrams/db/db_connection.py index a83edbf..329bb57 100644 --- a/joystick_diagrams/db/db_connection.py +++ b/joystick_diagrams/db/db_connection.py @@ -1,11 +1,12 @@ -import os from sqlite3 import Connection, connect +from joystick_diagrams.utils import data_root + DB_DIR = "data" DB_NAME = "joystick_diagrams.db" def connection() -> Connection: - path = os.path.join(os.getcwd(), DB_DIR, DB_NAME) + path = str(data_root().joinpath(DB_DIR, DB_NAME)) connection = connect(path) return connection diff --git a/joystick_diagrams/db/db_handler.py b/joystick_diagrams/db/db_handler.py index b49cf33..6dba9d7 100644 --- a/joystick_diagrams/db/db_handler.py +++ b/joystick_diagrams/db/db_handler.py @@ -21,7 +21,7 @@ def init() -> None: _logger.info("Initialising datastores") - utils.create_directory("data") + utils.create_directory(utils.data_root().joinpath("data")) db_device_management.create_new_db_if_not_exist() db_bind_text.create_new_db_if_not_exist() db_plugin_data.create_new_db_if_not_exist() diff --git a/joystick_diagrams/utils.py b/joystick_diagrams/utils.py index 486bc2e..2d20bcd 100644 --- a/joystick_diagrams/utils.py +++ b/joystick_diagrams/utils.py @@ -5,6 +5,15 @@ _logger = logging.getLogger(__name__) +JOYSTICK_DIAGRAMS_DATA_DIR = "Joystick Diagrams" + + +def data_root() -> Path: + """Returns the user data path for storage of data""" + return Path.joinpath( + Path().home(), "AppData", "Roaming", JOYSTICK_DIAGRAMS_DATA_DIR + ) + def create_directory(directory) -> None: try: @@ -15,7 +24,7 @@ def create_directory(directory) -> None: def install_root() -> str: - """Returns the currently root directory of the package + """Returns the current root directory of the package i.e. installation location "" in local development environments "path_to_frozen_app_exe" in frozen environment