-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_filters.py
78 lines (51 loc) · 1.85 KB
/
file_filters.py
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
from typing import Iterable,Callable, Union
from fnmatch import fnmatch
from abc import ABC, abstractmethod
from filesystem import FileSystemObject
class FileFilter(ABC):
def __init__(this) -> None:
this.__hit = 0
def __call__(this,fullpath) -> bool:
check = this.filter(fullpath)
if (check): this.__hit+=1
return check
@abstractmethod
def filter (this,fso:FileSystemObject) -> bool:
...
@property
def hits(this):
return this.__hit
class RemoveHiddenFileFilter(FileFilter):
def filter (this,fso:FileSystemObject) -> bool:
return fso.hidden
def __str__(this) -> str:
return f"Hidden file(s) removed: {this.hits}"
def __repr__(this) -> str:
return str(this)
class UnixPatternExpasionFilter(FileFilter):
def __init__(this, pattern:str) -> None:
super().__init__()
this._pattern:str = pattern
def filter(this,fso:FileSystemObject) -> bool:
return fnmatch(fso.absolute_path,this.pattern)
@property
def pattern(this) -> str:
return this._pattern
def __str__(this) -> str:
return f"Number of file(s) removed matching {this.pattern}: {this.hits}"
def __repr__(this) -> str:
return str(this)
class FilterSet:
def __init__(this,*args):
for i,x in enumerate(args):
if not isinstance(x,FileFilter):
raise TypeError(f"Argument {i+1} is not a (sub)type of FileFilter")
this.filters = args
def __call__(this,q:Iterable,key:Union[Callable|None]=None):
if key is None: key = lambda x:x
return [ itm for itm in q if not this.filter(key(itm)) ]
def filter(this, file:Union[FileSystemObject|None]):
if (file is not None):
for filter in this.filters:
if (filter(file)): return True
return False