forked from aicpp/cloudsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.py
102 lines (85 loc) · 2.82 KB
/
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
import datetime
import os
class FileFilterItem(object):
"""
Contains file properties for filter
"""
def __init__(self, name=None, mtime=None, size=None):
self.fileName = name
self.fileModifyTime = mtime
self.fileSize = size
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
# def __eq__(self, other):
# if type(other) is not type(self):
# return False
# if self.fileName != other.fileName:
# return False
# if self.fileModifyTime != other.fileModibyTime:
# return False
# if self.fileSize != other.fileSize:
# return False
# return True
def __ne__(self, other):
return not self.__eq__(other)
class FileFilterBase(object):
"""
Base class to filter files from source list to target
Should get [FileFilterItem] objects
"""
def __init__(self):
pass
def checkItemType(self, fileItem):
""" Check to match type [FileFilterItem] """
if not isinstance(fileItem, FileFilterItem):
raise Exception('invalid type')
def isMatch(self, fileItem):
""" Return true, if file is match filter """
return True
def filterFiles(self, files):
""" Return matched files """
return [f for f in files if self.isMatch(f)]
class FileFilterDays(FileFilterBase):
"""
Match only files which modification time is newer than matchDays days
"""
def __init__(self, matchDays=None):
super(FileFilterDays, self).__init__()
self.matchDays = 0
if matchDays:
self.matchDays = int(matchDays)
def isMatch(self, fileItem):
if not self.matchDays:
return True
self.checkItemType(fileItem)
if fileItem.fileModifyTime:
now = datetime.datetime.now()
diff = now - fileItem.fileModifyTime
diffDays = diff.days
return diffDays < self.matchDays
return True
class FileFilterMask(FileFilterBase):
"""
Exclude temporary files by mask (use fnmatch.fnmatch)
"""
def __init__(self):
super(FileFilterMask, self).__init__()
self.excludeMasks = self.defaultMasks()
def defaultMasks(self):
result = []
result.append('.*') # any hidden files
result.append('~*') # temp files
result.append('thumbs.db') # windows thumbs
return result
def addMask(self, fileMask):
self.excludeMasks.append(fileMask)
def isMatch(self, fileItem):
import fnmatch
self.checkItemType(fileItem)
for fileMask in self.excludeMasks:
if fnmatch.fnmatch(fileItem.fileName, fileMask):
return False
return True