-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.py
More file actions
83 lines (62 loc) · 2.21 KB
/
filter.py
File metadata and controls
83 lines (62 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from abc import ABC, abstractmethod
from typing import Iterable
import os
class FileFilter(ABC):
@abstractmethod
def matches(self, file_path: str) -> bool:
"""
Determine whether a file satisfies the filter criteria.
Args:
file_path: The file path to evaluate.
Returns:
bool: True if the file matches the filter condition,
otherwise False.
"""
pass
def apply(self, file_paths: Iterable[str]) -> Iterable[str]:
"""
Lazily apply the filter to an iterable collection of files.
Args:
file_paths: An iterable sequence of file paths.
Returns:
Iterable[str]:
A lazy iterable yielding only file paths that satisfy
the filter criteria.
"""
return (file_path for file_path in file_paths if self.matches(file_path))
class ExtensionFileFilter(FileFilter):
"""
Filter files based on their file extensions.
Extensions are normalized internally to ensure
case-insensitive matching and consistent formatting.
"""
def __init__(self, extensions: Iterable[str]):
"""
Initialize the extension filter.
Args:
extensions:
Iterable collection of file extensions
[e.g. 'txt', '.png', 'jpg', 'PNG'].
"""
# Normalize extensions to lowercase and remove any
# leading dots to ensure consistent comparisons.
self.extensions = frozenset(
ext.lower().lstrip('.')
for ext in extensions
)
def matches(self, file_path: str) -> bool:
"""
Check whether the file extension matches
the configured extension set.
Args:
file_path: The file path to evaluate.
Returns:
bool:
True if the file extension is included in the
configured extensions, or if no extensions
were specified, otherwise False.
"""
if not self.extensions:
return True
file_extension = os.path.splitext(file_path)[1].lower().lstrip('.')
return file_extension in self.extensions