-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileworker.py
63 lines (51 loc) · 1.9 KB
/
fileworker.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
import os
class FileWorker:
def __init__(self, path=os.getcwd(), suffixs="h,c,hpp,cpp,inl", recursive=True):
self.SetPath(path)
self.SetSuffixs(suffixs)
self.SetRecursive(recursive)
def SetPath(self, path):
self.__path = path
def SetSuffixs(self, suffixs):
self.__suffixs = []
self.__emptysuffix = False
self.__ignoresuffix = False
words = suffixs.split(",")
for word in words:
if word == "":
self.__emptysuffix = True
elif word == "*":
self.__ignoresuffix = True
else:
self.__suffixs.append("." + word)
def SetRecursive(self, recursive):
self.__recursive = recursive
def Work(self, job):
if os.path.isdir(self.__path):
self.__WorkDir(self.__path, job)
elif os.path.isfile(self.__path):
self.__WorkFile(self.__path, job)
def PrintUsage(self):
print("revisenewline.py [-p path] [-s suffixs] [-r yes|no] [-h|--help]")
def __WorkDir(self, dirname, job):
assert(os.path.isdir(dirname))
for child in os.listdir(dirname):
childpath = os.path.join(dirname, child)
if self.__recursive and os.path.isdir(childpath):
self.__WorkDir(childpath, job)
elif os.path.isfile(childpath):
self.__WorkFile(childpath, job)
def __WorkFile(self, filename, job):
assert(os.path.isfile(filename))
if self.__CheckSuffix(filename):
job.Process(filename)
def __CheckSuffix(self, filename):
if self.__ignoresuffix:
return True
elif self.__emptysuffix and not filename.endswith("."):
return True
else:
for suffix in self.__suffixs:
if filename.endswith(suffix):
return True
return False