def __call__(self, parser, namespace, values, option_string=None): if isinstance(values, (str, Path)): values = [values] normalized_values = [ Path(expand_path_vars(path)).resolve() for path in values ] previous_values = getattr(namespace, self.dest, []) setattr(namespace, self.dest, previous_values + normalized_values)
def __call__( self, parser: argparse.ArgumentParser, namespace: Namespace, values: Union[str, Sequence[Any], None], option_string: Optional[str] = None, ) -> None: if isinstance(values, (str, Path)): values = [values] if values: normalized_values = [ Path(expand_path_vars(str(path))).resolve() for path in values ] previous_values = getattr(namespace, self.dest, []) setattr(namespace, self.dest, previous_values + normalized_values)
def expand_to_normalized_paths(config: dict, base_dir: str = None) -> None: # config can be None (-c /dev/null) if not config: return base_dir = base_dir or os.getcwd() for paths_var in _PATH_VARS: if paths_var not in config: continue # Cause we don't want to add a variable not present normalized_paths = [] for path in config.pop(paths_var): normalized_path = abspath(expand_path_vars(path), base_dir=base_dir) normalized_paths.append(normalized_path) config[paths_var] = normalized_paths
def test_expand_path_vars(monkeypatch): """Ensure that tilde and env vars are expanded in paths.""" test_path = '/test/path' monkeypatch.setenv('TEST_PATH', test_path) assert expand_path_vars('~') == os.path.expanduser('~') assert expand_path_vars('$TEST_PATH') == test_path