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