xiwrap

slightly higher-level container setup utility
git clone https://git.ce9e.org/xiwrap.git

commit
285e0a348907e6c6efcb7734a3786a9e6e59f186
parent
3415a18cdabadf12971b087d8711e2a08eb087b2
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2025-10-11 16:32
refactor expandvars()

Diffstat

M xiwrap.py 57 +++++++++++++++++++++------------------------------------

1 files changed, 21 insertions, 36 deletions


diff --git a/xiwrap.py b/xiwrap.py

@@ -50,6 +50,14 @@ DBUS_SESSION_DEST = '$XDG_RUNTIME_DIR/bus'
   50    50 DBUS_SYSTEM_SRC = f'{os.environ["XDG_RUNTIME_DIR"]}/dbus-system-proxy-{os.getpid()}'
   51    51 DBUS_SYSTEM_DEST = '/run/dbus/system_bus_socket'
   52    52 
   -1    53 # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
   -1    54 XDG_DEFAULTS = {
   -1    55     'XDG_DATA_HOME': '.local/share',
   -1    56     'XDG_CONFIG_HOME': '.config',
   -1    57     'XDG_STATE_HOME': '.local/state',
   -1    58     'XDG_CACHE_HOME': '.cache',
   -1    59 }
   -1    60 
   53    61 
   54    62 class RuleError(ValueError):
   55    63     def __init__(self, key, args):
@@ -57,43 +65,19 @@ class RuleError(ValueError):
   57    65         super().__init__(f'Invalid rule: {rule}')
   58    66 
   59    67 
   60    -1 def xdg_expandvars(path, env):
   61    -1     # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
   62    -1 
   63    -1     if not path.startswith('$'):
   64    -1         return path
   65    -1 
   66    -1     for var, default in [
   67    -1         ('HOME', ''),
   68    -1         ('XDG_DATA_HOME', '.local/share'),
   69    -1         ('XDG_CONFIG_HOME', '.config'),
   70    -1         ('XDG_STATE_HOME', '.local/state'),
   71    -1         ('XDG_CACHE_HOME', '.cache'),
   72    -1         ('XDG_RUNTIME_DIR', None),
   73    -1     ]:
   74    -1         if path.startswith(f'${var}'):
   75    -1             if env.get(var):
   76    -1                 head = Path(env.get(var))
   77    -1             elif default is not None and 'HOME' in env:
   78    -1                 head = Path(env['HOME']) / default
   79    -1             else:
   80    -1                 raise ValueError(
   81    -1                     f'Invalid path {path}: {var} is not defined in this context.'
   82    -1                 )
   83    -1             if '/' in path:
   84    -1                 tail = path.removeprefix(f'${var}/')
   85    -1                 return str(head / tail)
   86    -1             elif path == f'${var}':
   87    -1                 return str(head)
   88    -1             else:
   89    -1                 raise ValueError(f'Invalid path {path}')
   90    -1 
   91    -1     raise ValueError(f'Invalid path {path}')
   -1    68 def expandvars(path, env):
   -1    69     original = os.environ
   92    70 
   -1    71     os.environ = {**env}
   -1    72     if 'HOME' in env:
   -1    73         for var, default in XDG_DEFAULTS.items():
   -1    74             if var not in env:
   -1    75                 os.environ[var] = str(Path(env['HOME']) / default)
   93    76 
   94    -1 def expandvars(path, env):
   95    -1     path = xdg_expandvars(path, env)
   96    -1     return os.path.expandvars(path)
   -1    77     try:
   -1    78         return os.path.expandvars(path)
   -1    79     finally:
   -1    80         os.environ = original
   97    81 
   98    82 
   99    83 class DBusProxy:
@@ -203,7 +187,8 @@ class RuleSet:
  203   187             self.dbus_system[args[0]] = key.removeprefix('dbus-system-')
  204   188         elif key == 'setenv':
  205   189             var, value = self.parse_env(key, args)
  206    -1             self.env[var] = value
   -1   190             if value is not None:
   -1   191                 self.env[var] = value
  207   192         elif key in [
  208   193             'bind',
  209   194             'bind-try',