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

Skip to content

Commit 8a54028

Browse files
committed
mpremote: Allow user configuration on Windows based on USERPROFILE.
1 parent 8e912a5 commit 8a54028

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
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: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,14 @@
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

25-
from .commands import (
26-
CommandError,
27-
do_connect,
28-
do_disconnect,
29-
do_edit,
30-
do_filesystem,
31-
do_mount,
32-
do_umount,
33-
do_exec,
34-
do_eval,
35-
do_run,
36-
do_resume,
37-
do_soft_reset,
38-
)
26+
from .commands import (CommandError, do_connect, do_disconnect, do_edit,
27+
do_eval, do_exec, do_filesystem, do_mount, do_resume,
28+
do_run, do_soft_reset, do_umount)
3929
from .mip import do_mip
4030
from .repl import do_repl
4131

@@ -49,8 +39,12 @@ def print_commands_help(cmds, help_key):
4939
help_message_lines = dedent(help_key(cmds[cmd])).split("\n")
5040
help_message = help_message_lines[0]
5141
for line in help_message_lines[1:]:
52-
help_message = "{}\n{}{}".format(help_message, " " * (max_command_len + 4), line)
53-
print(" ", cmd, " " * (max_command_len - len(cmd) + 2), help_message, sep="")
42+
help_message = "{}\n{}{}".format(
43+
help_message, " " * (max_command_len + 4), line
44+
)
45+
print(
46+
" ", cmd, " " * (max_command_len - len(cmd) + 2), help_message, sep=""
47+
)
5448

5549
print(_PROG, "-- MicroPython remote control")
5650
print("See https://docs.micropython.org/en/latest/reference/mpremote.html")
@@ -61,7 +55,9 @@ def print_commands_help(cmds, help_key):
6155
) # extract description from argparse
6256

6357
print("\nList of shortcuts:")
64-
print_commands_help(_command_expansions, lambda x: x[2]) # (args, sub, help_message)
58+
print_commands_help(
59+
_command_expansions, lambda x: x[2]
60+
) # (args, sub, help_message)
6561

6662
sys.exit(0)
6763

@@ -93,7 +89,9 @@ def _bool_flag(cmd_parser, name, short_name, default, description):
9389
def argparse_connect():
9490
cmd_parser = argparse.ArgumentParser(description="connect to given device")
9591
cmd_parser.add_argument(
96-
"device", nargs=1, help="Either list, auto, id:x, port:x, or any valid device name/path"
92+
"device",
93+
nargs=1,
94+
help="Either list, auto, id:x, port:x, or any valid device name/path",
9795
)
9896
return cmd_parser
9997

@@ -126,7 +124,10 @@ def argparse_repl():
126124
help="saves a copy of the REPL session to the specified path",
127125
)
128126
cmd_parser.add_argument(
129-
"--inject-code", type=str, required=False, help="code to be run when Ctrl-J is pressed"
127+
"--inject-code",
128+
type=str,
129+
required=False,
130+
help="code to be run when Ctrl-J is pressed",
130131
)
131132
cmd_parser.add_argument(
132133
"--inject-file",
@@ -146,7 +147,11 @@ def argparse_eval():
146147
def argparse_exec():
147148
cmd_parser = argparse.ArgumentParser(description="execute the string")
148149
_bool_flag(
149-
cmd_parser, "follow", "f", True, "follow output until the expression completes (default)"
150+
cmd_parser,
151+
"follow",
152+
"f",
153+
True,
154+
"follow output until the expression completes (default)",
150155
)
151156
cmd_parser.add_argument("expr", nargs=1, help="expression to execute")
152157
return cmd_parser
@@ -155,15 +160,23 @@ def argparse_exec():
155160
def argparse_run():
156161
cmd_parser = argparse.ArgumentParser(description="run the given local script")
157162
_bool_flag(
158-
cmd_parser, "follow", "f", True, "follow output until the script completes (default)"
163+
cmd_parser,
164+
"follow",
165+
"f",
166+
True,
167+
"follow output until the script completes (default)",
159168
)
160169
cmd_parser.add_argument("path", nargs=1, help="path to script to execute")
161170
return cmd_parser
162171

163172

164173
def argparse_filesystem():
165-
cmd_parser = argparse.ArgumentParser(description="execute filesystem commands on the device")
166-
_bool_flag(cmd_parser, "recursive", "r", False, "recursive copy (for cp command only)")
174+
cmd_parser = argparse.ArgumentParser(
175+
description="execute filesystem commands on the device"
176+
)
177+
_bool_flag(
178+
cmd_parser, "recursive", "r", False, "recursive copy (for cp command only)"
179+
)
167180
_bool_flag(
168181
cmd_parser,
169182
"verbose",
@@ -182,7 +195,9 @@ def argparse_mip():
182195
cmd_parser = argparse.ArgumentParser(
183196
description="install packages from micropython-lib or third-party sources"
184197
)
185-
_bool_flag(cmd_parser, "mpy", "m", True, "download as compiled .mpy files (default)")
198+
_bool_flag(
199+
cmd_parser, "mpy", "m", True, "download as compiled .mpy files (default)"
200+
)
186201
cmd_parser.add_argument(
187202
"--target", type=str, required=False, help="destination direction on the device"
188203
)
@@ -333,13 +348,15 @@ def load_user_config():
333348
config.commands = {}
334349

335350
# Get config file name.
336-
path = os.getenv("XDG_CONFIG_HOME")
351+
path = None
352+
for var in ["XDG_CONFIG_HOME", "HOME", "USERPROFILE"]:
353+
path = os.getenv(var)
354+
if path:
355+
path = os.path.join(path, ".config")
356+
path = os.path.join(path, _PROG)
357+
break
337358
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)
359+
return config
343360
config_file = os.path.join(path, "config.py")
344361

345362
# Check if config file exists.

0 commit comments

Comments
 (0)
0