Only expand '~' at the start of config paths

Every '~' in inputs and backup_dir was replaced with '~user', so
paths like 'notes~' or '~otheruser' did not match. Expand only a
leading '~' or '~/'.
This commit is contained in:
Fuxino
2026-10-08 18:04:25 +02:00
parent d79ea56c0f
commit fc165fccbf
+13 -4
View File
@@ -697,6 +697,15 @@ def _parse_arguments() -> argparse.Namespace:
return args return args
def _expand_home(path: str, user: str) -> str:
"""Expand a leading '~' to the home directory of user (other '~' are left as they are)"""
if path == '~' or path.startswith('~/'):
path = f'~{user}{path[1:]}'
return os.path.expanduser(path)
def _expand_inputs(inputs, user: Optional[str] = None): def _expand_inputs(inputs, user: Optional[str] = None):
expanded_inputs = [] expanded_inputs = []
@@ -705,11 +714,11 @@ def _expand_inputs(inputs, user: Optional[str] = None):
continue continue
if user is not None: if user is not None:
i_ex = glob(os.path.expanduser(i.replace('~', f'~{user}'))) i_ex = glob(_expand_home(i, user))
else: else:
i_ex = glob(i) i_ex = glob(i)
if '~' in i: if i.startswith('~'):
logger.warning('Cannot expand \'~\'. No user specified') logger.warning('Cannot expand \'~\'. No user specified')
if len(i_ex) == 0: if len(i_ex) == 0:
@@ -762,8 +771,8 @@ def _read_config(config_file, user: Optional[str] = None):
output = config.get(section, 'backup_dir') output = config.get(section, 'backup_dir')
if user is not None: if user is not None:
output = os.path.expanduser(output.replace('~', f'~{user}')) output = _expand_home(output, user)
elif user is None and '~' in output: elif user is None and output.startswith('~'):
logger.warning('Cannot expand \'~\', no user specified') logger.warning('Cannot expand \'~\', no user specified')
config_args['output'] = output config_args['output'] = output