diff --git a/docs/reference/mpremote.rst b/docs/reference/mpremote.rst index 08ef5d31ff..47a9e14836 100644 --- a/docs/reference/mpremote.rst +++ b/docs/reference/mpremote.rst @@ -107,7 +107,7 @@ The full list of supported commands are: **Note:** Instead of using the ``connect`` command, there are several :ref:`pre-defined shortcuts ` for common device paths. For example the ``a0`` shortcut command is equivalent to - ``connect /dev/ttyACM0`` (Linux), or ``c0`` for ``COM0`` (Windows). + ``connect /dev/ttyACM0`` (Linux), or ``c1`` for ``COM1`` (Windows). **Note:** The ``auto`` option will only detect USB serial ports, i.e. a serial port that has an associated USB VID/PID (i.e. CDC/ACM or FTDI-style @@ -429,9 +429,14 @@ Shortcuts can be defined using the macro system. Built-in shortcuts are: - ``cat``, ``edit``, ``ls``, ``cp``, ``rm``, ``mkdir``, ``rmdir``, ``touch``: Aliases for ``fs `` -Additional shortcuts can be defined by in user-configuration files, which is -located at ``.config/mpremote/config.py``. This file should define a -dictionary named ``commands``. The keys of this dictionary are the shortcuts +Additional shortcuts can be defined by in the user configuration file ``mpremote/config.py``, located in: + # ``$XDG_CONFIG_HOME/mpremote/config.py`` + # ``$HOME/.config/mpremote/config.py`` + # ``%APPDATA%/mpremote/config.py`` + # ``%USERPROFILE%/mpremote/config.py`` + searched in that order on all platforms. + +This file should define a dictionary named ``commands``. The keys of this dictionary are the shortcuts and the values are either a string or a list-of-strings: .. code-block:: python3 diff --git a/tools/mpremote/mpremote/main.py b/tools/mpremote/mpremote/main.py index eeb9cbd989..0a39bd7dd5 100644 --- a/tools/mpremote/mpremote/main.py +++ b/tools/mpremote/mpremote/main.py @@ -341,10 +341,12 @@ _BUILTIN_COMMAND_EXPANSIONS = { "--version": "version", } -# Add "a0", "a1", ..., "u0", "u1", ..., "c0", "c1", ... as aliases +# Add "a0", "a1", ..., "u0", "u1", ..., "c1", "c2", ... as aliases # for "connect /dev/ttyACMn" (and /dev/ttyUSBn, COMn) etc. for port_num in range(4): for prefix, port in [("a", "/dev/ttyACM"), ("u", "/dev/ttyUSB"), ("c", "COM")]: + if port_num == 0 and port == "COM": + continue # skip COM0 as it does not exist on Windows _BUILTIN_COMMAND_EXPANSIONS["{}{}".format(prefix, port_num)] = { "command": "connect {}{}".format(port, port_num), "help": 'connect to serial port "{}{}"'.format(port, port_num), @@ -355,18 +357,22 @@ def load_user_config(): # Create empty config object. config = __build_class__(lambda: None, "Config")() config.commands = {} - - # Get config file name. - path = os.getenv("XDG_CONFIG_HOME") - if path is None: - path = os.getenv("HOME") - if path is None: - return config - path = os.path.join(path, ".config") - path = os.path.join(path, _PROG) + path = None + for env_var in ("XDG_CONFIG_HOME", "HOME", "APPDATA", "USERPROFILE"): + path = os.getenv(env_var) + if not path: + continue + if env_var == "HOME" and os.path.exists(os.path.join(path, ".config", _PROG, "config.py")): + # Unix style + path = os.path.join(path, ".config", _PROG) + break + elif os.path.exists(os.path.join(path, _PROG, "config.py")): + # Windows style + path = os.path.join(path, _PROG) + break + if not path: + return config config_file = os.path.join(path, "config.py") - - # Check if config file exists. if not os.path.exists(config_file): return config @@ -375,9 +381,11 @@ def load_user_config(): config_data = f.read() prev_cwd = os.getcwd() os.chdir(path) + # pass in the config path so that the config file can use it + config.__dict__["config_path"] = path + config.__dict__["__file__"] = config_file exec(config_data, config.__dict__) os.chdir(prev_cwd) - return config