-
Notifications
You must be signed in to change notification settings - Fork 1
/
DirectoryFilter.py
63 lines (47 loc) · 1.99 KB
/
DirectoryFilter.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
import os
class FileFilter(object):
def __init__(self, root_path, file_list, file_filter=None):
self.__root_path = root_path
self.__file_list = file_list
self.__filterFunction = file_filter
if isinstance(file_filter, str):
self.__filterFunction = lambda filename: file_filter in filename
@property
def root_path(self):
return self.__root_path
@property
def files(self):
return self.filter_files(self.__file_list, self.__filterFunction)
@property
def paths(self):
return self.filter_files(self.__file_list, self.__filterFunction, True)
@property
def images(self):
return FileFilter(self.root_path, self.files, lambda filename: "mask" not in filename)
@property
def masks(self):
return FileFilter(self.root_path, self.files, lambda filename: "mask" in filename and "masked" not in filename)
@property
def masked(self):
return FileFilter(self.root_path, self.files, lambda filename: "masked" in filename)
def filter_files(self, file_paths, filter_function, include_dir=False):
return sorted([self.filename_for(filename, include_dir=include_dir) for filename in file_paths
if filter_function(filename)])
def filename_for(self, filename, include_dir=False):
return os.path.join(self.root_path, filename) if include_dir else filename
class DirectoryFilter(object):
def __init__(self, root_path):
self.__root_path = root_path
@property
def root_path(self):
return self.__root_path
@property
def all(self):
return FileFilter(self.root_path, os.listdir(self.root_path), "")
def __getattr__(self, filter_name):
return FileFilter(self.root_path, os.listdir(self.root_path), filter_name)
def dir(self, filter_name):
sub_path = os.path.join(self.root_path, filter_name)
if not os.path.exists(sub_path):
return self
return DirectoryFilter(sub_path)