Skip to content

Commit

Permalink
refactor functions to get folders
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-th committed Jun 8, 2024
1 parent d65bb18 commit 5d4eedc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/homematicip/cli/hmip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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("")
Expand Down
37 changes: 37 additions & 0 deletions src/homematicip/configuration/config_folder.py
Original file line number Diff line number Diff line change
@@ -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}")
35 changes: 5 additions & 30 deletions src/homematicip/configuration/config_io.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 3 additions & 15 deletions src/homematicip/configuration/log_helper.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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"

0 comments on commit 5d4eedc

Please sign in to comment.