Skip to content

Commit 3af2879

Browse files
committed
tools/mpremote: Allow user configuration on Windows.
Signed-off-by: Jos Verlinde <jos_verlinde@hotmail.com>
1 parent c2771df commit 3af2879

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed

docs/reference/mpremote.rst

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

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

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

430-
Additional shortcuts can be defined by in user-configuration files, which is
431-
located at ``.config/mpremote/config.py`` relative to the ``XDG_CONFIG_HOME`` or ``HOME`` environment variable on unix systems
432-
, or on Windows relative to ``HOME``, ``USERPROFILE`` or ``APPDATA``.
430+
Additional shortcuts can be defined by in the user configuration file ``mpremote/config.py``, located in:
431+
# ``$XDG_CONFIG_HOME/mpremote/config.py``
432+
# ``$HOME/.config/mpremote/config.py``
433+
# ``%APPDATA%/mpremote/config.py``
434+
# ``%USERPROFILE%/mpremote/config.py``
435+
searched in that order on all platforms.
433436

434-
For example:
435-
This file should define a dictionary named ``commands``. The keys of this dictionary are the shortcuts
437+
This file should define a dictionary named ``commands``. The keys of this dictionary are the shortcuts
436438
and the values are either a string or a list-of-strings:
437439

438440
.. code-block:: python3

tools/mpremote/mpremote/main.py

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

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

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

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

101105

@@ -133,7 +137,9 @@ def argparse_repl():
133137
required=False,
134138
help="saves a copy of the REPL session to the specified path",
135139
)
136-
cmd_parser.add_argument("--inject-code", type=str, required=False, help="code to be run when Ctrl-J is pressed")
140+
cmd_parser.add_argument(
141+
"--inject-code", type=str, required=False, help="code to be run when Ctrl-J is pressed"
142+
)
137143
cmd_parser.add_argument(
138144
"--inject-file",
139145
type=str,
@@ -151,14 +157,18 @@ def argparse_eval():
151157

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

158166

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

@@ -179,15 +189,21 @@ def argparse_filesystem():
179189
None,
180190
"enable verbose output (defaults to True for all commands except cat)",
181191
)
182-
cmd_parser.add_argument("command", nargs=1, help="filesystem command (e.g. cat, cp, ls, rm, touch)")
192+
cmd_parser.add_argument(
193+
"command", nargs=1, help="filesystem command (e.g. cat, cp, ls, rm, touch)"
194+
)
183195
cmd_parser.add_argument("path", nargs="+", help="local and remote paths")
184196
return cmd_parser
185197

186198

187199
def argparse_mip():
188-
cmd_parser = argparse.ArgumentParser(description="install packages from micropython-lib or third-party sources")
200+
cmd_parser = argparse.ArgumentParser(
201+
description="install packages from micropython-lib or third-party sources"
202+
)
189203
_bool_flag(cmd_parser, "mpy", "m", True, "download as compiled .mpy files (default)")
190-
cmd_parser.add_argument("--target", type=str, required=False, help="destination direction on the device")
204+
cmd_parser.add_argument(
205+
"--target", type=str, required=False, help="destination direction on the device"
206+
)
191207
cmd_parser.add_argument(
192208
"--index",
193209
type=str,
@@ -330,36 +346,33 @@ def argparse_none(description):
330346
for port_num in range(4):
331347
for prefix, port in [("a", "/dev/ttyACM"), ("u", "/dev/ttyUSB"), ("c", "COM")]:
332348
if port_num == 0 and port == "COM":
333-
continue # skip COM0 as it does not exist
334-
_BUILTIN_COMMAND_EXPANSIONS[f"{prefix}{port_num}"] = {
335-
"command": f"connect {port}{port_num}",
336-
"help": f'connect to serial port "{port}{port_num}"',
349+
continue # skip COM0 as it does not exist on Windows
350+
_BUILTIN_COMMAND_EXPANSIONS["{}{}".format(prefix, port_num)] = {
351+
"command": "connect {}{}".format(port, port_num),
352+
"help": 'connect to serial port "{}{}"'.format(port, port_num),
337353
}
338354

339355

340356
def load_user_config():
341357
# Create empty config object.
342358
config = __build_class__(lambda: None, "Config")()
343359
config.commands = {}
344-
# use $XDG_CONFIG_HOME,$HOME $env:USERPROFILE% or $env:APPDATA
345360
path = None
346-
for env_var in ("XDG_CONFIG_HOME", "HOME", "USERPROFILE", "APPDATA"):
361+
for env_var in ("XDG_CONFIG_HOME", "HOME", "APPDATA", "USERPROFILE"):
347362
path = os.getenv(env_var)
348363
if not path:
349364
continue
350-
if os.path.exists(os.path.join(path, ".config", _PROG, "config.py")):
365+
if env_var == "HOME" and os.path.exists(os.path.join(path, ".config", _PROG, "config.py")):
351366
# Unix style
352-
path = os.path.join(path, ".config", _PROG, "config.py")
367+
path = os.path.join(path, ".config", _PROG)
353368
break
354369
elif os.path.exists(os.path.join(path, _PROG, "config.py")):
355370
# Windows style
356-
path = os.path.join(path, _PROG, "config.py")
371+
path = os.path.join(path, _PROG)
357372
break
358373
if not path:
359374
return config
360-
361375
config_file = os.path.join(path, "config.py")
362-
# Check if config file exists.
363376
if not os.path.exists(config_file):
364377
return config
365378

@@ -514,7 +527,9 @@ def main():
514527
cmd_parser = parser_func()
515528
cmd_parser.prog = cmd
516529
# Catch all for unhandled positional arguments (this is the next command).
517-
cmd_parser.add_argument("next_command", nargs=argparse.REMAINDER, help=f"Next {_PROG} command")
530+
cmd_parser.add_argument(
531+
"next_command", nargs=argparse.REMAINDER, help=f"Next {_PROG} command"
532+
)
518533
args = cmd_parser.parse_args(command_args)
519534

520535
# Execute command.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy