From e720828645a5cc4598e02e30a719e6a69db2d7b6 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Fri, 6 Mar 2020 20:55:16 +0000 Subject: [PATCH] support list of PathIsh in common.py --- my/common.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/my/common.py b/my/common.py index 807ce60..ba8c07b 100644 --- a/my/common.py +++ b/my/common.py @@ -1,6 +1,6 @@ from pathlib import Path import functools -from typing import Union, Callable, Dict, List, Iterable, TypeVar +from typing import Union, Callable, Dict, List, Iterable, TypeVar, Sequence, List # some helper functions @@ -83,22 +83,32 @@ from .kython.klogging import setup_logger, LazyLogger PathIsh = Union[Path, str] +Paths = Union[Sequence[PathIsh], PathIsh] -def get_files(pp: PathIsh, glob: str, sort=True) -> List[Path]: +def get_files(pp: Paths, glob: str, sort=True) -> List[Path]: """ Helper function to avoid boilerplate. """ # TODO FIXME mm, some wrapper to assert iterator isn't empty? - path = Path(pp) - if path.is_dir(): - gp: Iterable[Path] = path.glob(glob) - if sort: - gp = sorted(gp) - return list(gp) + sources: List[Path] = [] + if isinstance(pp, (str, Path)): + sources.append(Path(pp)) else: - assert path.is_file(), path - # TODO FIXME assert matches glob?? - return [path] + sources.extend(map(Path, pp)) + + paths: List[Path] = [] + for src in sources: + if src.is_dir(): + gp: Iterable[Path] = src.glob(glob) + paths.extend(gp) + else: + assert src.is_file(), src + # TODO FIXME assert matches glob?? + paths.append(src) + + if sort: + paths = list(sorted(paths)) + return paths def mcachew(*args, **kwargs):