|
9 | 9 |
|
10 | 10 | from .constants import default_json_dir, ts
|
11 | 11 |
|
| 12 | + |
12 | 13 | def export_json(
|
13 | 14 | input: Union[str, list[list, dict], dict[str, Any]] = None,
|
14 | 15 | output_dir: str = default_json_dir,
|
@@ -132,3 +133,34 @@ def crawl_dir(
|
132 | 133 | return_obj: dict[str, list[Path]] = {"files": files, "dirs": dirs}
|
133 | 134 |
|
134 | 135 | return return_obj
|
| 136 | + |
| 137 | + |
| 138 | +def list_files(in_dir: str = None, ext_filter: str = None) -> list[Path]: |
| 139 | + """Return list of all files in a path, optionally filtering by file extension.""" |
| 140 | + if not in_dir: |
| 141 | + raise ValueError("Missing input directory to search") |
| 142 | + if ext_filter is not None: |
| 143 | + if not ext_filter.startswith("."): |
| 144 | + ext_filter = f".{ext_filter}" |
| 145 | + |
| 146 | + if ext_filter: |
| 147 | + search_str: str = f"**/{ext_filter}" |
| 148 | + else: |
| 149 | + search_str: str = "**/*" |
| 150 | + |
| 151 | + return_files: list[Path] = [] |
| 152 | + |
| 153 | + try: |
| 154 | + for _p in Path(in_dir).glob(search_str): |
| 155 | + if _p.is_file(): |
| 156 | + return_files.append(_p) |
| 157 | + except FileNotFoundError as fnf: |
| 158 | + raise FileNotFoundError(f"Could not find input path: {in_dir}. Details: {fnf}") |
| 159 | + except PermissionError as perm: |
| 160 | + raise PermissionError(f"Could not open path: {in_dir}. Details: {perm}") |
| 161 | + except Exception as exc: |
| 162 | + raise Exception( |
| 163 | + f"Unhandled exception looping input path: {in_dir}. Details: {exc}" |
| 164 | + ) |
| 165 | + |
| 166 | + return return_files |
0 commit comments