diff --git a/simple_backup/simple_backup.py b/simple_backup/simple_backup.py index 1a289b8..2636ad2 100755 --- a/simple_backup/simple_backup.py +++ b/simple_backup/simple_backup.py @@ -697,6 +697,15 @@ def _parse_arguments() -> argparse.Namespace: 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): expanded_inputs = [] @@ -705,11 +714,11 @@ def _expand_inputs(inputs, user: Optional[str] = None): continue if user is not None: - i_ex = glob(os.path.expanduser(i.replace('~', f'~{user}'))) + i_ex = glob(_expand_home(i, user)) else: i_ex = glob(i) - if '~' in i: + if i.startswith('~'): logger.warning('Cannot expand \'~\'. No user specified') if len(i_ex) == 0: @@ -762,8 +771,8 @@ def _read_config(config_file, user: Optional[str] = None): output = config.get(section, 'backup_dir') if user is not None: - output = os.path.expanduser(output.replace('~', f'~{user}')) - elif user is None and '~' in output: + output = _expand_home(output, user) + elif user is None and output.startswith('~'): logger.warning('Cannot expand \'~\', no user specified') config_args['output'] = output