|
1 | 1 | import os |
| 2 | +import sys |
2 | 3 | from collections.abc import Callable |
3 | 4 | from functools import partial |
| 5 | +from stat import FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_SYSTEM, UF_HIDDEN |
4 | 6 | from typing import TYPE_CHECKING, TypeVar |
5 | 7 |
|
6 | 8 | import error_messages |
7 | 9 | from AutoSplitImage import RESET_KEYWORD, START_KEYWORD, AutoSplitImage, ImageType |
8 | 10 | from utils import is_valid_image |
9 | 11 |
|
10 | 12 | if TYPE_CHECKING: |
| 13 | + from _typeshed import StrPath |
| 14 | + |
11 | 15 | from AutoSplit import AutoSplit |
12 | 16 |
|
13 | 17 | ( |
@@ -193,13 +197,40 @@ def validate_before_parsing(autosplit: "AutoSplit", *, show_error: bool = True): |
193 | 197 | return not error |
194 | 198 |
|
195 | 199 |
|
196 | | -def parse_and_validate_images(autosplit: "AutoSplit"): |
197 | | - # Get split images |
198 | | - all_images = [ |
199 | | - AutoSplitImage(os.path.join(autosplit.settings_dict["split_image_directory"], image_name)) |
200 | | - for image_name in os.listdir(autosplit.settings_dict["split_image_directory"]) |
201 | | - ] |
| 200 | +def is_user_file(path: StrPath): |
| 201 | + """Returns False for hidden files, system files and folders.""" |
| 202 | + if os.path.isdir(path) or os.path.basename(path).startswith("."): |
| 203 | + return False |
| 204 | + stat_result = os.stat(path) |
| 205 | + if stat_result.st_mode & UF_HIDDEN: |
| 206 | + return False |
| 207 | + if sys.platform == "win32": |
| 208 | + return not ( |
| 209 | + (stat_result.st_file_attributes & FILE_ATTRIBUTE_SYSTEM) |
| 210 | + | (stat_result.st_file_attributes & FILE_ATTRIBUTE_HIDDEN) |
| 211 | + ) |
| 212 | + return True |
| 213 | + |
202 | 214 |
|
| 215 | +def __get_images_from_directory(directory: StrPath): |
| 216 | + """ |
| 217 | + Returns a list of AutoSplitImage parsed from a directory. |
| 218 | + Hidden files, system files and folders are silently ignored. |
| 219 | + """ |
| 220 | + file_paths = ( |
| 221 | + os.path.join(directory, filename) # format: skip |
| 222 | + for filename in os.listdir(directory) |
| 223 | + ) |
| 224 | + filtered_image_paths = ( |
| 225 | + image_path # format: skip |
| 226 | + for image_path in file_paths |
| 227 | + if is_user_file(image_path) |
| 228 | + ) |
| 229 | + return [AutoSplitImage(image_path) for image_path in filtered_image_paths] |
| 230 | + |
| 231 | + |
| 232 | +def parse_and_validate_images(autosplit: "AutoSplit"): |
| 233 | + all_images = __get_images_from_directory(autosplit.settings_dict["split_image_directory"]) |
203 | 234 | # Find non-split images and then remove them from the list |
204 | 235 | start_image = __pop_image_type(all_images, ImageType.START) |
205 | 236 | reset_image = __pop_image_type(all_images, ImageType.RESET) |
|
0 commit comments