8000 mpremote: Allow user configuration on Windows based on USERPROFILE. · micropython/micropython@dce2f76 · GitHub
[go: up one dir, main page]

Skip to content

Commit dce2f76

Browse files
committed
mpremote: Allow user configuration on Windows based on USERPROFILE.
Signed-off-by: Jos Verlinde <Jos.Verlinde@Microsoft.com>
1 parent 8e912a5 commit dce2f76

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

docs/reference/mpremote.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ Shortcuts can be defined using the macro system. Built-in shortcuts are::
222222
- ``bootloader``: make the device enter its bootloader
223223

224224
Any user configuration, including user-defined shortcuts, can be placed in the file
225-
``.config/mpremote/config.py``. For example:
225+
``.config/mpremote/config.py`` (relative to the ``XDG_CONFIG_HOME``, ``HOME``, or ``USERPROFILE`` environment variable).
226+
For example:
226227

227228
.. code-block:: python3
228229

tools/mpremote/mpremote/main.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"""
1919

2020
import argparse
21-
import os, sys
21+
import os
22+
import sys
2223
from collections.abc import Mapping
2324
from textwrap import dedent
2425

@@ -27,14 +28,14 @@
2728
do_connect,
2829
do_disconnect,
2930
do_edit,
31+
do_eval,
32+
do_exec,
3033
do_filesystem,
3134
do_mount,
32-
do_umount,
33-
do_exec,
34-
do_eval,
35-
do_run,
3635
do_resume,
36+
do_run,
3737
do_soft_reset,
38+
do_umount,
3839
)
3940
from .mip import do_mip
4041
from .repl import do_repl
@@ -93,7 +94,9 @@ def _bool_flag(cmd_parser, name, short_name, default, description):
9394
def argparse_connect():
9495
cmd_parser = argparse.ArgumentParser(description="connect to given device")
9596
cmd_parser.add_argument(
96-
"device", nargs=1, help="Either list, auto, id:x, port:x, or any valid device name/path"
97+
"device",
98+
nargs=1,
99+
help="Either list, auto, id:x, port:x, or any valid device name/path",
97100
)
98101
return cmd_parser
99102

@@ -126,7 +129,10 @@ def argparse_repl():
126129
help="saves a copy of the REPL session to the specified path",
127130
)
128131
cmd_parser.add_argument(
129-
"--inject-code", type=str, required=False, help="code to be run when Ctrl-J is pressed"
132+
"--inject-code",
133+
type=str,
134+
required=False,
135+
help="code to be run when Ctrl-J is pressed",
130136
)
131137
cmd_parser.add_argument(
132138
"--inject-file",
@@ -146,7 +152,11 @@ def argparse_eval():
146152
def argparse_exec():
147153
cmd_parser = argparse.ArgumentParser(description="execute the string")
148154
_bool_flag(
149-
cmd_parser, "follow", "f", True, "follow output until the expression completes (default)"
155+
cmd_parser,
156+
"follow",
157+
"f",
158+
True,
159+
"follow output until the expression completes (default)",
150160
)
151161
cmd_parser.add_argument("expr", nargs=1, help="expression to execute")
152162
return cmd_parser
@@ -155,7 +165,11 @@ def argparse_exec():
155165
def argparse_run():
156166
cmd_parser = argparse.ArgumentParser(description="run the given local script")
157167
_bool_flag(
158-
cmd_parser, "follow", "f", True, "follow output until the script completes (default)"
168+
cmd_parser,
169+
"follow",
170+
"f",
171+
True,
172+
"follow output until the script completes (default)",
159173
)
160174
cmd_parser.add_argument("path", nargs=1, help="path to script to execute")
161175
return cmd_parser
@@ -333,21 +347,21 @@ def load_user_config():
333347
config.commands = {}
334348

335349
# Get config file name.
336-
path = os.getenv("XDG_CONFIG_HOME")
350+
path = None
351+
for var in ["XDG_CONFIG_HOME", "HOME", "USERPROFILE"]:
352+
path = os.getenv(var)
353+
if path:
354+
break
337355
if path is None:
338-
path = os.getenv("HOME")
339-
if path is None:
340-
return config
341-
path = os.path.join(path, ".config")
342-
path = os.path.join(path, _PROG)
343-
config_file = os.path.join(path, "config.py")
356+
return config
357+
config_file = os.path.join(path, ".config", _PROG, "config.py")
344358

345359
# Check if config file exists.
346360
if not os.path.exists(config_file):
347361
return config
348362

349363
# Exec the config file in its directory.
350-
with open(config_file) as f:
364+
with open(config_file, encoding="utf-8") as f:
351365
config_data = f.read()
352366
prev_cwd = os.getcwd()
353367
os.chdir(path)

0 commit comments

Comments
 (0)
0