Skip to content

Commit 4a28d99

Browse files
authored
Add per-platform logic to define path to CONFIG_DIR
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.
1 parent 79b0bcf commit 4a28d99

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

soco_cli/aliases.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@
22

33
import logging
44
import pickle
5-
from os import mkdir, path
5+
import platform
6+
from os import environ, mkdir, path
67
from typing import List, Tuple, Union
78

8-
CONFIG_DIR = path.join(path.expanduser("~"), ".soco-cli")
9+
if platform.system() == "Linux":
10+
if "XDG_CONFIG_HOME" in environ:
11+
CONFIG_DIR = path.join(path.expandvars("${XDG_CONFIG_HOME}"), "soco-cli")
12+
else:
13+
CONFIG_DIR = path.join(path.expanduser("~user"), ".config", "soco-cli")
14+
elif platform.system() == "Windows":
15+
CONFIG_DIR = path.join(path.expandvars("%LOCALAPPDATA%"), "soco-cli")
16+
else:
17+
CONFIG_DIR = path.join(path.expanduser("~user"), ".soco-cli")
918
ALIAS_FILE = path.join(CONFIG_DIR, "aliases.pickle")
1019

1120

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

57+
def _confirm_config_dir() -> bool:
58+
if not path.exists(CONFIG_DIR):
59+
logging.info("Creating directory '{}'".format(SOCO_CLI_DIR))
60+
try:
61+
mkdir(CONFIG_DIR)
62+
return True
63+
except:
64+
error_report("Failed to create directory '{}'".format(CONFIG_DIR))
65+
return False
66+
4867
def save_aliases(self) -> None:
49-
if not path.exists(CONFIG_DIR):
50-
try:
51-
logging.info("Creating directory '{}'".format(CONFIG_DIR))
52-
mkdir(CONFIG_DIR)
53-
except:
54-
pass
68+
if not _confirm_config_dir():
69+
pass
5570
with open(ALIAS_FILE, "wb") as f:
5671
logging.info("Saving aliases")
5772
pickle.dump(self._aliases, f)

0 commit comments

Comments
 (0)