tools/mpremote: Locate config.py location across different host OSes.

Use `platformdirs.user_config_dir()` (see
https://platformdirs.readthedocs.io/en/latest/api.html#user-config-directory)
to provide portability across many different OSes and configuration styles.

Signed-off-by: Jos Verlinde <Jos_Verlinde@hotmail.com>
This commit is contained in:
Jos Verlinde
2025-05-01 17:17:06 +02:00
committed by Damien George
parent 026a20da3e
commit 64b3944b01

View File

@@ -22,6 +22,8 @@ import os, sys, time
from collections.abc import Mapping
from textwrap import dedent
import platformdirs
from .commands import (
CommandError,
do_connect,
@@ -439,13 +441,7 @@ def load_user_config():
config.commands = {}
# Get config file name.
path = os.getenv("XDG_CONFIG_HOME")
if path is None:
path = os.getenv("HOME")
if path is None:
return config
path = os.path.join(path, ".config")
path = os.path.join(path, _PROG)
path = platformdirs.user_config_dir(appname=_PROG, appauthor=False)
config_file = os.path.join(path, "config.py")
# Check if config file exists.
@@ -457,6 +453,9 @@ def load_user_config():
config_data = f.read()
prev_cwd = os.getcwd()
os.chdir(path)
# Pass in the config path so that the config file can use it.
config.__dict__["config_path"] = path
config.__dict__["__file__"] = config_file
exec(config_data, config.__dict__)
os.chdir(prev_cwd)