-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileClass.py
89 lines (74 loc) · 3.3 KB
/
FileClass.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
#FileClass
from FileStatistics import FileStatistics
import os.path
class FileClass(object):
"""docstring for FileClass"""
def __init__(self, name, extensions=[]):
self.name = name
self.extensions = set(extensions)
def addExtension(self, extensions):
if isinstance(extensions, list):
for item in extensions:
self.extensions.add(item)
elif isinstance(extensions, basestring):
self.extensions.add(item)
else:
return False
return True
def getExtensions(self):
return list(self.extensions)
def __str__(self):
returnStr = "FileClass: %s\n" % (self.name)
returnStr += "Contained Extensions: "
if len(self.extensions) == 0:
returnStr += " none\n"
else:
for item in list(self.extensions):
returnStr += "\'%s\' " % (item)
returnStr += "\n"
return returnStr
class FileClassResults(FileClass):
""" Subclass of FileClass, holds the statistics """
def __init__(self, fileClass):
try:
isinstance(fileClass, FileClass)
except:
raise TypeError
FileClass.__init__(self, fileClass.name , fileClass.extensions)
self.files = set([]) # initiate set with empty list
self.statistics = FileStatistics(fileClass.name)
def insertFile(self, pathToFile):
""" Check file name against regexp and add if matched, return true if added """
if isinstance(pathToFile, list):
raise TypeError
elif isinstance(pathToFile, basestring):
if not os.path.isfile(pathToFile):
return False
else:
return self.__addFile(pathToFile)
print "file %s added" % (pathToFile)
else:
return False
def clear(self):
self.statistics.clear()
self.files.clear()
def getFiles(self):
return list(self.files)
def __addFile(self, file):
"""Compare agains the list of regexp and add the file if it matches """
if len(self.extensions) == 0:
self.files.add(file)
self.statistics.addFile(file)
return True
else:
for ext in self.extensions:
fileName, fileExtension = os.path.splitext(file)
if fileExtension == ext:
self.files.add(file)
self.statistics.addFile(file)
return True
return False
def __str__(self):
myString = FileClass.__str__(self)
myString += FileStatistics.__str__(self.statistics)
return myString