Skip to content

Commit 7083967

Browse files
authored
Ignore folders, system and hidden files (#297)
1 parent c392b9f commit 7083967

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

Diff for: .pre-commit-config.yaml

+1-1
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]

Diff for: scripts/requirements-dev.txt

+1-1
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'

Diff for: src/split_parser.py

+41-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import os
2+
import sys
23
from collections.abc import Callable
34
from functools import partial
5+
from stat import 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

12+
if sys.platform == "win32":
13+
from stat import FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_SYSTEM
14+
15+
1016
if TYPE_CHECKING:
17+
from _typeshed import StrPath
18+
1119
from AutoSplit import AutoSplit
1220

1321
(
@@ -193,13 +201,40 @@ def validate_before_parsing(autosplit: "AutoSplit", *, show_error: bool = True):
193201
return not error
194202

195203

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
202217

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

0 commit comments

Comments
 (0)