Skip to content

Commit

Permalink
Add per-platform logic to define path to CONFIG_DIR
Browse files Browse the repository at this point in the history
Use platform module from Python standard library to define the
path to the appropriate location for user configuration files, with
fallback to former default path.
  • Loading branch information
jlmello57 committed May 31, 2023
1 parent 79b0bcf commit 4a28d99
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions soco_cli/aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

import logging
import pickle
from os import mkdir, path
import platform
from os import environ, mkdir, path
from typing import List, Tuple, Union

CONFIG_DIR = path.join(path.expanduser("~"), ".soco-cli")
if platform.system() == "Linux":
if "XDG_CONFIG_HOME" in environ:
CONFIG_DIR = path.join(path.expandvars("${XDG_CONFIG_HOME}"), "soco-cli")
else:
CONFIG_DIR = path.join(path.expanduser("~user"), ".config", "soco-cli")
elif platform.system() == "Windows":
CONFIG_DIR = path.join(path.expandvars("%LOCALAPPDATA%"), "soco-cli")
else:
CONFIG_DIR = path.join(path.expanduser("~user"), ".soco-cli")
ALIAS_FILE = path.join(CONFIG_DIR, "aliases.pickle")


Expand Down Expand Up @@ -45,13 +54,19 @@ def remove_alias(self, alias_name: str) -> bool:
def alias_names(self) -> List[str]:
return list(self._aliases.keys())

def _confirm_config_dir() -> bool:
if not path.exists(CONFIG_DIR):
logging.info("Creating directory '{}'".format(SOCO_CLI_DIR))
try:
mkdir(CONFIG_DIR)
return True
except:
error_report("Failed to create directory '{}'".format(CONFIG_DIR))
return False

def save_aliases(self) -> None:
if not path.exists(CONFIG_DIR):
try:
logging.info("Creating directory '{}'".format(CONFIG_DIR))
mkdir(CONFIG_DIR)
except:
pass
if not _confirm_config_dir():
pass
with open(ALIAS_FILE, "wb") as f:
logging.info("Saving aliases")
pickle.dump(self._aliases, f)
Expand Down

0 comments on commit 4a28d99

Please sign in to comment.