tools/mpremote: Add user configuration on Windows %APPDATE%.

Signed-off-by: Jos Verlinde <jos_verlinde@hotmail.com>
pull/9573/head
Jos Verlinde 2023-11-27 18:07:42 +01:00 zatwierdzone przez Jos Verlinde
rodzic 094b52b8ad
commit 7ae7d127b7
2 zmienionych plików z 24 dodań i 12 usunięć

Wyświetl plik

@ -430,8 +430,11 @@ 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
located at ``.config/mpremote/config.py`` relative to the ``XDG_CONFIG_HOME`` or ``HOME`` environment variable on unix systems
, or on Windows relative to ``HOME``, ``USERPROFILE`` or ``APPDATA``.
For example:
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

Wyświetl plik

@ -355,17 +355,23 @@ 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)
# use $XDG_CONFIG_HOME,$HOME $env:USERPROFILE% or $env:APPDATA
path = None
for env_var in ("XDG_CONFIG_HOME", "HOME", "USERPROFILE", "APPDATA"):
path = os.getenv(env_var)
if not path:
continue
if os.path.exists( os.path.join(path,".config", _PROG, "config.py")):
# Unix style
path = os.path.join(path,".config", _PROG, "config.py")
break
elif os.path.exists( os.path.join(path, _PROG, "config.py")):
# Windows style
path = os.path.join(path, _PROG,"config.py")
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,6 +381,9 @@ 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)