8000 mpremote: Allow user configuration on Windows by Josverl · Pull Request #9573 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

mpremote: Allow user configuration on Windows #9573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions docs/reference/mpremote.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ The full list of supported commands are:
**Note:** Instead of using the ``connect`` command, there are several
:ref:`pre-defined shortcuts <mpremote_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
Expand Down Expand Up @@ -479,9 +479,16 @@ Shortcuts can be defined using the macro system. Built-in shortcuts are:

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

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
the User Configuration Directory.
The correct location for each OS is determined using the ``platformdirs`` module.

This is typically:
# ``$XDG_CONFIG_HOME/mpremote/config.py``
# ``$HOME/.config/mpremote/config.py``
# ``$env:LOCALAPPDATA/mpremote/config.py``

The ``config.py``` 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
Expand Down
18 changes: 6 additions & 12 deletions tools/mpremote/mpremote/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from collections.abc import Mapping
from textwrap import dedent

import platformdirs

from .commands import (
CommandError,
do_connect,
Expand Down Expand Up @@ -396,18 +398,8 @@ 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 = platformdirs.user_config_dir(appname=_PROG, appauthor=False)
config_file = os.path.join(path, "config.py")

# Check if config file exists.
if not os.path.exists(config_file):
return config

Expand All @@ -416,9 +408,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


Expand Down
1 change: 1 addition & 0 deletions tools/mpremote/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pyserial >= 3.3
importlib_metadata >= 1.4; python_version < "3.8"
platformdirs >= 4.3.7
Loading
0