Skip to content

Commit 8675d41

Browse files
committed
Ignore folders, system and hidden files
1 parent 5cba9b4 commit 8675d41

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

src/split_parser.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import os
22
from collections.abc import Callable
33
from functools import partial
4+
import sys
45
from typing import TYPE_CHECKING, TypeVar
56

67
import error_messages
78
from AutoSplitImage import RESET_KEYWORD, START_KEYWORD, AutoSplitImage, ImageType
89
from utils import is_valid_image
10+
from stat import FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_SYSTEM, UF_HIDDEN
911

1012
if TYPE_CHECKING:
1113
from AutoSplit import AutoSplit
14+
from _typeshed import StrPath
1215

1316
(
1417
DUMMY_FLAG,
@@ -193,13 +196,39 @@ def validate_before_parsing(autosplit: "AutoSplit", *, show_error: bool = True):
193196
return not error
194197

195198

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-
]
199+
def is_user_file(path: StrPath):
200+
"""Returns False for hidden files, system files and folders."""
201+
if os.path.isdir(path) or os.path.basename(path).startswith("."):
202+
return False
203+
if sys.platform == "darwin":
204+
return not (os.stat(path).st_mode & UF_HIDDEN)
205+
if sys.platform == "win32":
206+
st_file_attributes = os.stat(path).st_file_attributes
207+
return not (
208+
(st_file_attributes & FILE_ATTRIBUTE_SYSTEM)
209+
| (st_file_attributes & FILE_ATTRIBUTE_HIDDEN)
210+
)
211+
212+
213+
def __get_images_from_directory(directory: StrPath):
214+
"""Returns a list of AutoSplitImage parsed from a directory.
215+
Hidden files, system files and folders are silently ignored."""
216+
file_paths = (
217+
os.path.join(directory, filename) # format: skip
218+
for filename in os.listdir(directory)
219+
)
220+
filtered_image_paths = (
221+
image_path # format: skip
222+
for image_path in file_paths
223+
if is_user_file(image_path)
224+
)
225+
return [AutoSplitImage(image_path) for image_path in filtered_image_paths]
202226

227+
228+
def parse_and_validate_images(autosplit: "AutoSplit"):
229+
all_images = __get_images_from_directory(
230+
autosplit.settings_dict["split_image_directory"]
231+
)
203232
# Find non-split images and then remove them from the list
204233
start_image = __pop_image_type(all_images, ImageType.START)
205234
reset_image = __pop_image_type(all_images, ImageType.RESET)
@@ -245,12 +274,16 @@ def parse_and_validate_images(autosplit: "AutoSplit"):
245274

246275
# Check that there's only one Reset Image
247276
if image.image_type == ImageType.RESET:
248-
error_message = lambda: error_messages.multiple_keyword_images(RESET_KEYWORD) # noqa: E731
277+
error_message = lambda: error_messages.multiple_keyword_images(
278+
RESET_KEYWORD
279+
) # noqa: E731
249280
break
250281

251282
# Check that there's only one Start Image
252283
if image.image_type == ImageType.START:
253-
error_message = lambda: error_messages.multiple_keyword_images(START_KEYWORD) # noqa: E731
284+
error_message = lambda: error_messages.multiple_keyword_images(
285+
START_KEYWORD
286+
) # noqa: E731
254287
break
255288

256289
if error_message:

0 commit comments

Comments
 (0)