from logging import Logger from pathlib import PurePath, Path import shutil from time import perf_counter from typing import Callable def get_paths( from_dir_path: PurePath, pattern: str, check_function: Callable = lambda path: True, ) -> list[PurePath]: from_dir_path = Path(from_dir_path) return [ PurePath(path.relative_to(from_dir_path)) for path in from_dir_path.rglob(pattern) if check_function(path) ] def copy_paths( from_dir_path: PurePath, to_dir_path: PurePath, paths: list[PurePath], ): for path in paths: to_file_path = Path(to_dir_path) / path to_file_path.parent.mkdir( parents=True, exist_ok=True, ) shutil.copy2( Path(from_dir_path) / path, to_file_path, ) def log( logger: Logger, start_text: str, success_text: str, ): def decorator(func): def wrapper(*args, **kwargs): logger.info(start_text) start_time = perf_counter() func() end_time = perf_counter() logger.info('%s (%f s)' % ( success_text, end_time - start_time, )) return wrapper return decorator