8000 tools/mpremote: Add user configuration on Windows %APPDATE%. · micropython/micropython@6db5a2c · GitHub
[go: up one dir, main page]

Skip to content

Commit 6db5a2c

Browse files
committed
tools/mpremote: Add user configuration on Windows %APPDATE%.
Signed-off-by: Jos Verlinde <jos_verlinde@hotmail.com>
1 parent 7d66ae6 commit 6db5a2c

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

docs/reference/mpremote.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,11 @@ Shortcuts can be defined using the macro system. Built-in shortcuts are:
428428
- ``cat``, ``edit``, ``ls``, ``cp``, ``rm``, ``mkdir``, ``rmdir``, ``touch``: Aliases for ``fs <sub-command>``
429429

430430
Additional shortcuts can be defined by in user-configuration files, which is
431-
located at ``.config/mpremote/config.py``. This file should define a
432-
dictionary named ``commands``. The keys of this dictionary are the shortcuts
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``.
433+
434+
For example:
435+
This file should define a dictionary named ``commands``. The keys of this dictionary are the shortcuts
433436
and the values are either a string or a list-of-strings:
434437

435438
.. code-block:: python3

tools/mpremote/mpremote/main.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -355,17 +355,23 @@ def load_user_config():
355355
# Create empty config object.
356356
config = __build_class__(lambda: None, "Config")()
357357
config.commands = {}
358-
359-
# Get config file name.
360-
path = os.getenv("XDG_CONFIG_HOME")
361-
if path is None:
362-
path = os.getenv("HOME")
363-
if path is None:
364-
return config
365-
path = os.path.join(path, ".config")
366-
path = os.path.join(path, _PROG)
358+
# use $XDG_CONFIG_HOME,$HOME $env:USERPROFILE% or $env:APPDATA
359+
path = None
360+
for env_var in ("XDG_CONFIG_HOME", "HOME", "USERPROFILE", "APPDATA"):
361+
path = os.getenv(env_var)
362+
if not path:
363+
continue
364+
if os.path.exists( os.path.join(path,".config", _PROG, "config.py")):
365+
# Unix style
366+
path = os.path.join(path,".config", _PROG, "config.py")
367+
break
368+
elif os.path.exists( os.path.join(path, _PROG, "config.py")):
369+
# Windows style
370+
path = os.path.join(path, _PROG,"config.py")
371+
break
372+
if not path:
373+
return config
367374
config_file = os.path.join(path, "config.py")
368-
369375
# Check if config file exists.
370376
if not os.path.exists(config_file):
371377
return config
@@ -375,6 +381,9 @@ def load_user_config():
375381
config_data = f.read()
376382
prev_cwd = os.getcwd()
377383
os.chdir(path)
384+
# pass in the config path so that the config file can use it
385+
config.__dict__["config_path"] = path
386+
config.__dict__["__file__"] = config_file
378387
exec(config_data, config.__dict__)
379388
os.chdir(prev_cwd)
380389

0 commit comments

Comments
 (0)
0