From 5d4eedcf9ea1ce30302bea2ff203ade8d9b0d7bb Mon Sep 17 00:00:00 2001 From: Thomas Hahn Date: Sat, 8 Jun 2024 18:39:42 +0200 Subject: [PATCH] refactor functions to get folders --- src/homematicip/cli/hmip.py | 4 +- .../configuration/config_folder.py | 37 +++++++++++++++++++ src/homematicip/configuration/config_io.py | 35 +++--------------- src/homematicip/configuration/log_helper.py | 18 ++------- 4 files changed, 47 insertions(+), 47 deletions(-) create mode 100644 src/homematicip/configuration/config_folder.py diff --git a/src/homematicip/cli/hmip.py b/src/homematicip/cli/hmip.py index fb54442..f7a3dcc 100644 --- a/src/homematicip/cli/hmip.py +++ b/src/homematicip/cli/hmip.py @@ -24,8 +24,8 @@ from homematicip.cli.helper import get_rssi_bar_string from homematicip.cli.hmip_cli_show_targets_helper import build_commands_from_registry, CommandEntry from homematicip.configuration.config import PersistentConfig +from homematicip.configuration.config_folder import get_default_app_config_folder from homematicip.configuration.config_io import ConfigIO -from homematicip.configuration.log_helper import get_home_path from homematicip.connection.rest_connection import ConnectionContext, RestResult from homematicip.events.event_types import ModelUpdateEvent from homematicip.model.anoymizer import handle_config @@ -297,7 +297,7 @@ def version(): click.echo(f"HomematicIP-Rest-Api: {metadata_version("homematicip")}") click.echo(f"Python: {sys.version}") click.echo("") - home_path = get_home_path() + home_path = get_default_app_config_folder() click.echo(f"Logs and config are written to:") click.echo(os.path.abspath(home_path)) click.echo("") diff --git a/src/homematicip/configuration/config_folder.py b/src/homematicip/configuration/config_folder.py new file mode 100644 index 0000000..d54bc11 --- /dev/null +++ b/src/homematicip/configuration/config_folder.py @@ -0,0 +1,37 @@ +import os +import platform +from typing import List + + +def get_well_known_folders() -> List[str]: + """Return a list of folders, where the configuration file can be found.""" + app_name = "homematicip-rest-api" + os_name = platform.system() + + if os_name == 'Linux': + user_path = os.path.join(os.path.expanduser('~'), '.config', app_name) + system_path = os.path.join('/etc', app_name) + elif os_name == 'Darwin': # macOS + user_path = os.path.join(os.path.expanduser('~'), 'Library', 'Application Support', app_name) + system_path = os.path.join('/Library', 'Application Support', app_name) + elif os_name == 'Windows': + user_path = os.path.join(os.getenv('APPDATA'), app_name) + system_path = os.path.join(os.getenv('PROGRAMDATA'), app_name) + else: + raise ValueError(f"Unsupported operating system: {os_name}") + + return [user_path, system_path, os.getcwd()] + + +def get_default_app_config_folder() -> str: + """Return the default application directory.""" + os_name = platform.system() + + if os_name == 'Linux': + return os.path.join(os.path.expanduser('~'), '.config', 'homematicip-rest-api') + elif os_name == 'Darwin': # macOS + return os.path.join(os.path.expanduser('~'), 'Library', 'Application Support', 'homematicip-rest-api') + elif os_name == 'Windows': + return os.path.join(os.getenv('APPDATA'), 'homematicip-rest-api') + else: + raise ValueError(f"Unsupported operating system: {os_name}") diff --git a/src/homematicip/configuration/config_io.py b/src/homematicip/configuration/config_io.py index 60ea99a..c9f4137 100644 --- a/src/homematicip/configuration/config_io.py +++ b/src/homematicip/configuration/config_io.py @@ -1,12 +1,9 @@ import json -import logging import os -import platform -from configparser import ConfigParser from dataclasses import asdict from homematicip.configuration.config import PersistentConfig -from homematicip.configuration.log_helper import get_home_path +from homematicip.configuration.config_folder import get_well_known_folders, get_default_app_config_folder class ConfigIO: @@ -23,31 +20,9 @@ def find_config_in_well_known_locations(cls) -> PersistentConfig | None: @classmethod def _get_well_known_locations(cls) -> list[str]: - """Get the well known locations for the configuration file.""" - config_filename = "config.json" - search_locations = [os.path.join("./", config_filename)] - - os_name = platform.system() - - if os_name == "Windows": - appdata = os.getenv("localappdata") - programdata = os.getenv("programdata") - search_locations.append( - os.path.join(appdata, "homematicip-rest-api", config_filename) - ) - search_locations.append( - os.path.join(programdata, "homematicip-rest-api", config_filename) - ) - elif os_name == "Linux": - search_locations.append(f"~/.homematicip-rest-api/{config_filename}") - search_locations.append(f"/etc/homematicip-rest-api/{config_filename}") - elif os_name == "Darwin": # MAC - # are these folders right? - search_locations.append(f"~/Library/Preferences/homematicip-rest-api/{config_filename}") - search_locations.append( - f"/Library/Application Support/homematicip-rest-api/{config_filename}" - ) - return search_locations + """Return a list of well known locations where the configuration file can be found.""" + folders = get_well_known_folders() + return [os.path.join(folder, "config.json") for folder in folders] @classmethod def from_file(cls, file_path) -> PersistentConfig: @@ -81,7 +56,7 @@ def to_file(cls, config: PersistentConfig) -> str: # 'log_file': config.log_file # } # - filename = os.path.join(get_home_path(), "config.json") + filename = os.path.join(get_default_app_config_folder(), "config.json") with open(filename, 'w', encoding='utf-8') as file: json.dump(asdict(config), file) diff --git a/src/homematicip/configuration/log_helper.py b/src/homematicip/configuration/log_helper.py index 30bfa7b..49994bc 100644 --- a/src/homematicip/configuration/log_helper.py +++ b/src/homematicip/configuration/log_helper.py @@ -1,6 +1,6 @@ -import datetime import os -import platform + +from homematicip.configuration.config_folder import get_default_app_config_folder def get_logger_filename() -> str: @@ -21,17 +21,5 @@ def ensure_log_path_exists(path: str): def get_target_log_path() -> str: """Depending on the operating system a path is returned where the log file should be stored. :return: the path to the log directory.""" - return os.path.join(get_home_path(), "logs") - + return os.path.join(get_default_app_config_folder(), "logs") -def get_home_path() -> str: - """Depending on the operating system a path is returned where the log file should be stored. - :return: the path to the log directory. - """ - os_name = platform.system() - if os_name == "Windows": - return os.path.join(os.getenv("LOCALAPPDATA"), "homematicip-rest-api") - elif os_name == "Linux": - return os.path.expanduser("~/.homematicip-rest-api") - elif os_name == "Darwin": - return "~/Library/Preferences/homematicip-rest-api"