8000 tools/mpremote/main: Locate config.py location across Host OSes. · micropython/micropython@77b368b · GitHub
[go: up one dir, main page]

Skip to content

Commit 77b368b

Browse files
committed
tools/mpremote/main: Locate config.py location across Host OSes.
Use [platformdirs.user_config_dir()](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>
1 parent 0e2efe8 commit 77b368b

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

tools/mpremote/mpremote/main.py

Lines changed: 6 additions & 12 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,
@@ -396,18 +398,8 @@ def load_user_config():
396398
# Create empty config object.
397399
config = __build_class__(lambda: None, "Config")()
398400
config.commands = {}
399-
400-
# Get config file name.
401-
path = os.getenv("XDG_CONFIG_HOME")
402-
if path is None:
403-
path = os.getenv("HOME")
404-
if path is None:
405-
return config
406-
path = os.path.join(path, ".config")
407-
path = os.path.join(path, _PROG)
401+
path = platformdirs.user_config_dir(appname=_PROG, appauthor=False)
408402
config_file = os.path.join(path, "config.py")
409-
410-
# Check if config file exists.
411403
if not os.path.exists(config_file):
412404
return config
413405

@@ -416,9 +408,11 @@ def load_user_config():
416408
config_data = f.read()
417409
prev_cwd = os.getcwd()
418410
os.chdir(path)
411+
# pass in the config path so that the config file can use it
412+
config.__dict__["config_path"] = path
413+
config.__dict__["__file__"] = config_file
419414
exec(config_data, config.__dict__)
420415
os.chdir(prev_cwd)
421-
422416
return config
423417

424418

0 commit comments

Comments
 (0)
0