support list of PathIsh in common.py
This commit is contained in:
parent
55fd26acfc
commit
e720828645
1 changed files with 21 additions and 11 deletions
30
my/common.py
30
my/common.py
|
@ -1,6 +1,6 @@
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import functools
|
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
|
# some helper functions
|
||||||
|
|
||||||
|
@ -83,22 +83,32 @@ from .kython.klogging import setup_logger, LazyLogger
|
||||||
|
|
||||||
PathIsh = Union[Path, str]
|
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.
|
Helper function to avoid boilerplate.
|
||||||
"""
|
"""
|
||||||
# TODO FIXME mm, some wrapper to assert iterator isn't empty?
|
# TODO FIXME mm, some wrapper to assert iterator isn't empty?
|
||||||
path = Path(pp)
|
sources: List[Path] = []
|
||||||
if path.is_dir():
|
if isinstance(pp, (str, Path)):
|
||||||
gp: Iterable[Path] = path.glob(glob)
|
sources.append(Path(pp))
|
||||||
if sort:
|
|
||||||
gp = sorted(gp)
|
|
||||||
return list(gp)
|
|
||||||
else:
|
else:
|
||||||
assert path.is_file(), 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??
|
# TODO FIXME assert matches glob??
|
||||||
return [path]
|
paths.append(src)
|
||||||
|
|
||||||
|
if sort:
|
||||||
|
paths = list(sorted(paths))
|
||||||
|
return paths
|
||||||
|
|
||||||
|
|
||||||
def mcachew(*args, **kwargs):
|
def mcachew(*args, **kwargs):
|
||||||
|
|
Loading…
Add table
Reference in a new issue