Skip to content

Commit a8de05b

Browse files
authored
Set config directory according to runtime platform
This commit changes the definition of the persistent state and configuration files (`SOCO_CLI_DIR`) to derive from the output of [platform.system()][1], rather than being fixed to an eponymous, dot-prefixed directory in the top level of the user's `$HOME`. Furthermore, on POSIX-compliant platforms it tests for the presence of the XDG_CONFIG_HOME environment variable and establishes the config directory there, with a fallback to the `$HOME/.config` directory as defined in the [XDG Base Directory Specification][2]. On Microsoft Windows® systems, it places the directory inside the path defined by the `%LOCALAPPDATA%` environment variable, which is the vendor-specified path for all machine- specific application data belonging to the active user. On all other platforms the former behavior is retained. Several individual definitions of this path were consolidated into the `SOCO_CLI_DIR` constant already present in the file, which itself was relocated along with the attendant `_confirm_soco_cli_dir` method earlier in the file, nearer to the other defined constants. 1: https://docs.python.org/3/library/platform.html#platform.system 2: https://specifications.freedesktop.org/basedir-spec/latest/index.html
1 parent a75b427 commit a8de05b

1 file changed

Lines changed: 39 additions & 29 deletions

File tree

soco_cli/utils.py

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
import readline
1111
except ImportError:
1212
pass
13+
import platform
1314
import sys
1415
from collections.abc import Sequence
15-
from platform import python_version
1616
from time import sleep
1717

1818
import soco # type: ignore
@@ -35,6 +35,28 @@ def event_unsubscribe(sub):
3535
logging.info("Unsubscribed")
3636

3737

38+
if platform.system() == "Linux":
39+
if "XDG_CONFIG_HOME" in os.environ:
40+
SOCO_CLI_DIR = os.path.join(os.path.expandvars("${XDG_CONFIG_HOME}"), "soco-cli")
41+
else:
42+
SOCO_CLI_DIR = os.path.join(os.path.expandvars("~user"), ".config", "soco-cli")
43+
elif platform.system() == "Windows":
44+
SOCO_CLI_DIR = os.path.join(os.path.expandvars("%LOCALAPPDATA%"), "soco-cli")
45+
else:
46+
SOCO_CLI_DIR = os.path.join(os.path.expanduser("~user"), ".soco-cli")
47+
48+
49+
def _confirm_soco_cli_dir() -> bool:
50+
if not os.path.exists(SOCO_CLI_DIR):
51+
logging.info("Creating directory '{}'".format(SOCO_CLI_DIR))
52+
try:
53+
os.mkdir(SOCO_CLI_DIR)
54+
return True
55+
except:
56+
error_report("Failed to create directory '{}'".format(SOCO_CLI_DIR))
57+
return False
58+
59+
3860
INTERACTIVE = False
3961
API = False
4062
SINGLE_KEYSTROKE = False
@@ -226,7 +248,7 @@ def convert_true_false(true_or_false, conversion="YesOrNo"):
226248
def version():
227249
print("soco-cli version: {}".format(__version__), flush=True)
228250
print("soco version: {}".format(soco.__version__), flush=True)
229-
print("python version: {}".format(python_version()), flush=True)
251+
print("python version: {}".format(platform.python_version()), flush=True)
230252
print("command path: {}".format(sys.argv[0]), flush=True)
231253

232254

@@ -712,38 +734,38 @@ def check_args(args):
712734
return message
713735

714736

715-
path = os.path.expanduser("~") + "/.soco-cli/"
716-
filename = "saved_search.pickle"
717-
pathname = path + filename
737+
search_pathname = os.path.join(SOCO_CLI_DIR, "saved_search.pickle")
718738

719739

720740
def save_search(result):
721-
if not os.path.exists(path):
722-
os.mkdir(path)
723-
with open(pathname, "wb") as f:
724-
pickle.dump(result, f)
725-
logging.info("Saved search results at {}".format(pathname))
741+
if not _confirm_soco_cli_dir():
742+
return
743+
try:
744+
with open(search_pathname, "wb") as f:
745+
pickle.dump(result, f)
746+
logging.info("Saved search results at {}".format(search_pathname))
747+
except Exception as e:
748+
logging.info("Error saving speaker search results: {}".format(e))
726749
return True
727750

728751

729752
def read_search():
730-
if os.path.exists(pathname):
731-
logging.info("Loading search results from {}".format(pathname))
753+
if os.path.exists(search_pathname):
754+
logging.info("Loading search results from {}".format(search_pathname))
732755
try:
733-
with open(pathname, "rb") as f:
756+
with open(search_pathname, "rb") as f:
734757
return pickle.load(f)
735758
except Exception as e:
736759
logging.info("Failed to load search results: %s", e)
737760
return None
738761

739762

740-
filename = "queue_insertion_position.pickle"
741-
queue_pathname = path + filename
763+
queue_pathname = os.path.join(SOCO_CLI_DIR, "queue_insertion_position.pickle")
742764

743765

744766
def save_queue_insertion_position(queue_position: int):
745-
if not os.path.exists(path):
746-
os.mkdir(path)
767+
if not _confirm_soco_cli_dir:
768+
return
747769
with open(queue_pathname, "wb") as f:
748770
pickle.dump(queue_position, f)
749771
logging.info("Saved queue position at {}".format(queue_pathname))
@@ -765,7 +787,6 @@ def get_queue_insertion_position() -> int:
765787

766788

767789
# Interactive shell history file
768-
SOCO_CLI_DIR = os.path.join(os.path.expanduser("~"), ".soco-cli")
769790
HIST_FILE = os.path.join(SOCO_CLI_DIR, "shell-history.txt")
770791
HIST_LEN = 50
771792

@@ -846,17 +867,6 @@ def playback_state(state):
846867
SUBS_LIST = set()
847868

848869

849-
def _confirm_soco_cli_dir() -> bool:
850-
if not os.path.exists(SOCO_CLI_DIR):
851-
logging.info("Creating directory '{}'".format(SOCO_CLI_DIR))
852-
try:
853-
os.mkdir(SOCO_CLI_DIR)
854-
return True
855-
except:
856-
error_report("Failed to create directory '{}'".format(SOCO_CLI_DIR))
857-
return False
858-
859-
860870
def remember_event_sub(sub):
861871
global SUBS_LIST
862872
logging.info("Adding event subscription record: '{}'".format(sub))

0 commit comments

Comments
 (0)