Skip to content

Commit 64b3944

Browse files
Josverldpgeorge
authored andcommitted
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 <[email protected]>
1 parent 026a20d commit 64b3944

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

tools/mpremote/mpremote/main.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from collections.abc import Mapping
2323
from textwrap import dedent
2424

25+
import platformdirs
26+
2527
from .commands import (
2628
CommandError,
2729
do_connect,
@@ -439,13 +441,7 @@ def load_user_config():
439441
config.commands = {}
440442

441443
# Get config file name.
442-
path = os.getenv("XDG_CONFIG_HOME")
443-
if path is None:
444-
path = os.getenv("HOME")
445-
if path is None:
446-
return config
447-
path = os.path.join(path, ".config")
448-
path = os.path.join(path, _PROG)
444+
path = platformdirs.user_config_dir(appname=_PROG, appauthor=False)
449445
config_file = os.path.join(path, "config.py")
450446

451447
# Check if config file exists.
@@ -457,6 +453,9 @@ def load_user_config():
457453
config_data = f.read()
458454
prev_cwd = os.getcwd()
459455
os.chdir(path)
456+
# Pass in the config path so that the config file can use it.
457+
config.__dict__["config_path"] = path
458+
config.__dict__["__file__"] = config_file
460459
exec(config_data, config.__dict__)
461460
os.chdir(prev_cwd)
462461

0 commit comments

Comments
 (0)