8000 tools/mpremote: Allow user configuration on Windows. · micropython/micropython@493b932 · GitHub
[go: up one dir, main page]

Skip to content

Commit 493b932

Browse files
committed
tools/mpremote: Allow user config 8000 uration on Windows.
Signed-off-by: Jos Verlinde <jos_verlinde@hotmail.com>
1 parent f871bf3 commit 493b932

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

docs/reference/mpremote.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ The full list of supported commands are:
108108
**Note:** Instead of using the ``connect`` command, there are several
109109
:ref:`pre-defined shortcuts <mpremote_shortcuts>` for common device paths. For
110110
example the ``a0`` shortcut command is equivalent to
111-
``connect /dev/ttyACM0`` (Linux), or ``c0`` for ``COM0`` (Windows).
111+
``connect /dev/ttyACM0`` (Linux), or ``c1`` for ``COM1`` (Windows).
112112

113113
**Note:** The ``auto`` option will only detect USB serial ports, i.e. a serial
114114
port that has an associated USB VID/PID (i.e. CDC/ACM or FTDI-style
@@ -479,12 +479,14 @@ Shortcuts can be defined using the macro system. Built-in shortcuts are:
479479

480480
- ``cat``, ``edit``, ``ls``, ``cp``, ``rm``, ``mkdir``, ``rmdir``, ``touch``: Aliases for ``fs <sub-command>``
481481

482-
Additional shortcuts can be defined by in user-configuration files, which is
483-
located at ``.config/mpremote/config.py`` relative to the ``XDG_CONFIG_HOME`` or ``HOME`` environment variable on unix systems
484-
, or on Windows relative to ``HOME``, ``USERPROFILE`` or ``APPDATA``.
482+
Additional shortcuts can be defined by in the user configuration file ``mpremote/config.py``, located in:
483+
# ``$XDG_CONFIG_HOME/mpremote/config.py``
484+
# ``$HOME/.config/mpremote/config.py``
485+
# ``%APPDATA%/mpremote/config.py``
486+
# ``%USERPROFILE%/mpremote/config.py``
487+
searched in that order on all platforms.
485488

486-
For example:
487-
This file should define a dictionary named ``commands``. The keys of this dictionary are the shortcuts
489+
This file should define a dictionary named ``commands``. The keys of this dictionary are the shortcuts
488490
and the values are either a string or a list-of-strings:
489491

490492
.. code-block:: python3

tools/mpremote/mpremote/main.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ def print_commands_help(cmds, help_key):
6262
print("See https://docs.micropython.org/en/latest/reference/mpremote.html")
6363

6464
print("\nList of commands:")
65-
print_commands_help(_COMMANDS, lambda x: x[1]().description) # extract description from argparse
65+
print_commands_help(
66+
_COMMANDS, lambda x: x[1]().description
67+
) # extract description from argparse
6668

6769
print("\nList of shortcuts:")
6870
print_commands_help(_command_expansions, lambda x: x[2]) # (args, sub, help_message)
@@ -96,7 +98,9 @@ def _bool_flag(cmd_parser, name, short_name, default, description):
9698

9799
def argparse_connect():
98100
cmd_parser = argparse.ArgumentParser(description="connect to given device")
99-
cmd_parser.add_argument("device", nargs=1, help="Either list, auto, id:x, port:x, or any valid device name/path")
101+
cmd_parser.add_argument(
102+
"device", nargs=1, help="Either list, auto, id:x, port:x, or any valid device name/path"
103+
)
100104
return cmd_parser
101105

102106

@@ -134,7 +138,9 @@ def argparse_repl():
134138
required=False,
135139
help="saves a copy of the REPL session to the specified path",
136140
)
137-
cmd_parser.add_argument("--inject-code", type=str, required=False, help="code to be run when Ctrl-J is pressed")
141+
cmd_parser.add_argument(
142+
"--inject-code", type=str, required=False, help="code to be run when Ctrl-J is pressed"
143+
)
138144
cmd_parser.add_argument(
139145
"--inject-file",
140146
type=str,
@@ -152,14 +158,18 @@ def argparse_eval():
152158

153159
def argparse_exec():
154160
cmd_parser = argparse.ArgumentParser(description="execute the string")
155-
_bool_flag(cmd_parser, "follow", "f", True, "follow output until the expression completes (default)")
161+
_bool_flag(
162+
cmd_parser, "follow", "f", True, "follow output until the expression completes (default)"
163+
)
156164
cmd_parser.add_argument("expr", nargs=1, help="expression to execute")
157165
return cmd_parser
158166

159167

160168
def argparse_run():
161169
cmd_parser = argparse.ArgumentParser(description="run the given local script")
162-
_bool_flag(cmd_parser, "follow", "f", True, "follow output until the script completes (default)")
170+
_bool_flag(
171+
cmd_parser, "follow", "f", True, "follow output until the script completes (default)"
172+
)
163173
cmd_parser.add_argument("path", nargs=1, help="path to script to execute")
164174
return cmd_parser
165175

@@ -197,9 +207,13 @@ def argparse_filesystem():
197 9E88 207

198208

199209
def argparse_mip():
200-
cmd_parser = argparse.ArgumentParser(description="install packages from micropython-lib or third-party sources")
210+
cmd_parser = argparse.ArgumentParser(
211+
description="install packages from micropython-lib or third-party sources"
212+
)
201213
_bool_flag(cmd_parser, "mpy", "m", True, "download as compiled .mpy files (default)")
202-
cmd_parser.add_argument("--target", type=str, required=False, help="destination direction on the device")
214+
cmd_parser.add_argument(
215+
"--target", type=str, required=False, help="destination direction on the device"
216+
)
203217
cmd_parser.add_argument(
204218
"--index",
205219
type=str,
@@ -373,36 +387,33 @@ def argparse_none(description):
373387
for port_num in range(4):
374388
for prefix, port in [("a", "/dev/ttyACM"), ("u", "/dev/ttyUSB"), ("c", "COM")]:
375389
if port_num == 0 and port == "COM":
376-
continue # skip COM0 as it does not exist
377-
_BUILTIN_COMMAND_EXPANSIONS[f"{prefix}{port_num}"] = {
378-
"command": f"connect {port}{port_num}",
379-
"help": f'connect to serial port "{port}{port_num}"',
390+
continue # skip COM0 as it does not exist on Windows
391+
_BUILTIN_COMMAND_EXPANSIONS["{}{}".format(prefix, port_num)] = {
392+
"command": "connect {}{}".format(port, port_num),
393+
"help": 'connect to serial port "{}{}"'.format(port, port_num),
380394
}
381395

382396

383397
def load_user_config():
384398
# Create empty config object.
385399
config = __build_class__(lambda: None, "Config")()
386400
config.commands = {}
387-
# use $XDG_CONFIG_HOME,$HOME $env:USERPROFILE% or $env:APPDATA
388401
path = None
389-
for env_var in ("XDG_CONFIG_HOME", "HOME", "USERPROFILE", "APPDATA"):
402+
for env_var in ("XDG_CONFIG_HOME", "HOME", "APPDATA", "USERPROFILE"):
390403
path = os.getenv(env_var)
391404
if not path:
392405
continue
393-
if os.path.exists(os.path.join(path, ".config", _PROG, "config.py")):
406+
if env_var == "HOME" and os.path.exists(os.path.join(path, ".config", _PROG, "config.py")):
394407
# Unix style
395-
path = os.path.join(path, ".config", _PROG, "config.py")
408+
path = os.path.join(path, ".config", _PROG)
396409
break
397410
elif os.path.exists(os.path.join(path, _PROG, "config.py")):
398411
# Windows style
399-
path = os.path.join(path, _PROG, "config.py")
412+
path = os.path.join(path, _PROG)
400413
break
401414
if not path:
402415
return config
403-
404416
config_file = os.path.join(path, "config.py")
405-
# Check if config file exists.
406417
if not os.path.exists(config_file):
407418
return config
408419

@@ -557,7 +568,9 @@ def main():
557568
cmd_parser = parser_func()
558569
cmd_parser.prog = cmd
559570
# Catch all for unhandled positional arguments (this is the next command).
560-
cmd_parser.add_argument("next_command", nargs=argparse.REMAINDER, help=f"Next {_PROG} command")
571+
cmd_parser.add_argument(
572+
"next_command", nargs=argparse.REMAINDER, help=f"Next {_PROG} command"
573+
)
561574
args = cmd_parser.parse_args(command_args)
562575

563576
# Execute command.

0 commit comments

Comments
 (0)
0