Skip to content

Commit 0e8157d

Browse files
committed
Ignore folders, system and hidden files
1 parent c392b9f commit 0e8157d

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ repos:
1919
- id: pretty-format-ini
2020
args: [--autofix]
2121
- repo: https://github.com/astral-sh/ruff-pre-commit
22-
rev: v0.6.1 # Must match requirements-dev.txt
22+
rev: v0.6.8 # Must match requirements-dev.txt
2323
hooks:
2424
- id: ruff
2525
args: [--fix]

scripts/requirements-dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
-r requirements.txt
1313
#
1414
# Linters & Formatters
15-
ruff>=0.6.1 # Pre-commit fix # Must match .pre-commit-config.yaml
15+
ruff>=0.6.8 # Pre-commit fix # Must match .pre-commit-config.yaml
1616
#
1717
# Types
1818
types-D3DShot ; sys_platform == 'win32'

src/split_parser.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import os
2+
import sys
23
from collections.abc import Callable
34
from functools import partial
5+
from stat import FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_SYSTEM, UF_HIDDEN
46
from typing import TYPE_CHECKING, TypeVar
57

68
import error_messages
79
from AutoSplitImage import RESET_KEYWORD, START_KEYWORD, AutoSplitImage, ImageType
810
from utils import is_valid_image
911

1012
if TYPE_CHECKING:
13+
from _typeshed import StrPath
14+
1115
from AutoSplit import AutoSplit
1216

1317
(
@@ -193,13 +197,40 @@ def validate_before_parsing(autosplit: "AutoSplit", *, show_error: bool = True):
193197
return not error
194198

195199

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+
202214

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"])
203234
# Find non-split images and then remove them from the list
204235
start_image = __pop_image_type(all_images, ImageType.START)
205236
reset_image = __pop_image_type(all_images, ImageType.RESET)

0 commit comments

Comments
 (0)