-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordfinder.py
199 lines (182 loc) · 6.71 KB
/
wordfinder.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#-*- coding: utf-8 -*-
import codecs
import getopt
import io
import os
import sys
class WordFinder:
def __init__(self):
self.__COUNT = "count"
self.__LINENO = "lineno"
self.__OCCURRED = "occurred"
self.__path = os.getcwd()
self.__findmode = self.__LINENO
self.__fullword = False
self.__recursive = False
self.__storeresult = False
self.__casesensitive = False
self.__SetSuffixs("c,cpp,h,hpp,txt,xml,py,proto")
def Find(self, line):
word = ""
optionstart = line.find("-")
if optionstart == -1:
word = line
else:
word = line[0 : optionstart - 1]
args = line[optionstart : ].split()
if not self.__ParseOptions(args):
return
if not os.path.exists(self.__path):
print("'" + self.__path + "'" + " is not exists!")
return
print(os.path.abspath(self.__path))
path = os.path.normpath(os.path.abspath(self.__path))
filecount = 0
wordcount = 0
output = io.StringIO()
if os.path.isdir(path):
filecount, wordcount = self.__FindInDir(path, word, output)
elif os.path.isfile(path):
wordcount = FindInFile(path, word)
filecount = 1 if wordcount > 0 else 0
outputline = "file-count:" + str(filecount)
if self.__findmode != self.__OCCURRED:
outputline += " word-count:" + str(wordcount)
print(outputline)
output.write(outputline + os.linesep)
if self.__storeresult:
outputfile = open("wordfinder.txt", "w")
outputfile.writelines(output.getvalue())
outputfile.close()
output.close()
def __SetSuffixs(self, line):
self.__ignoresuffix = False
self.__emptysuffix = False
self.__suffixs = ()
words = line.split(',')
if '*' in words:
self.__ignoresuffix = True
return
suffixwords = set()
for word in words:
if word == "":
self.__emptysuffix = True
else:
suffixwords.add("." + word)
self.__suffixs = tuple(suffixwords)
def __FindInFile(self, path, word, output):
assert(os.path.isfile(path))
if (not os.path.exists(path)):
return 0
count = 0
lineno = 0
inputfile = codecs.open(path, "r", "utf-8", "ignore")
for line in inputfile.readlines():
lineno += 1
cur_count = self.__CountWord(line, word)
if cur_count > 0:
if self.__findmode == self.__OCCURRED:
print(path)
output.write(path + os.linesep)
return 1
elif self.__findmode == self.__LINENO:
outputline = path + " line:" + str(lineno) + " count:" + str(cur_count)
print(outputline)
output.write(outputline + os.linesep)
count += cur_count
if count > 0 and self.__findmode == self.__COUNT:
outputline = path + " count:" + str(count)
output.write(outputline + os.linesep)
return count
def __FindInDir(self, path, word, output):
assert(os.path.isdir(path))
if (not os.path.exists(path)):
return 0, 0
filecount = 0
wordcount = 0
for child in os.listdir(path):
childpath = os.path.join(path, child)
if os.path.isdir(childpath) and self.__recursive:
fc, wc = self.__FindInDir(childpath, word, output)
filecount += fc
wordcount += wc
elif os.path.isfile(childpath) and self.__CheckSuffix(childpath):
wc = self.__FindInFile(childpath, word, output)
if wc > 0:
wordcount += wc
filecount += 1
return filecount, wordcount
def __CheckSuffix(self, path):
assert(os.path.isfile(path))
if self.__ignoresuffix:
return True
elif self.__emptysuffix and not "." in os.path.basename(path):
return True
else:
return path.endswith(self.__suffixs)
def PrintUsage(self):
print("example input: word [-p, -m, --suffixs=]")
print(" word: target word")
print(" -p: target dir or file")
print(" -m: 1 for occurred, 2 for word count, 3 for line no(default)")
print(" --store: if specified, the result will be stored in 'wordfinder.txt'")
print(" --rec: if specified, program will search the subdir recursively")
print(" --case: if specified, the word is case sensitive")
print(" --full: if specified, the word is full word")
print(" --suffixs: specified the target files's suffixs, such as 'c,cpp,xml'")
def __ParseOptions(self, args):
try:
options, _ = getopt.getopt(args, "p:m", ["rec", "store", "case", "full", "suffixs="])
except:
print("unknown option")
return False
for name, value in options:
if name == "-m":
if value == "1":
self.__findmode = self.__OCCURRED
elif value == "2":
self.__findmode = self.__COUNT
else:
self.__findmode = self.__LINENO
elif name == "-p":
self.__path = value
elif name == "--rec":
self.__recursive = True
elif name == "--store":
self.__storeresult = True
elif name == "--case":
self.__casesensitive = True
elif name == "--full":
self.__fullword = True
elif name == "--suffixs":
self.__SetSuffixs(value)
return True
def __CountWord(self, line, word):
if not self.__casesensitive:
line = line.upper()
word = word.upper()
count = 0
wordlen = len(word)
linelen = len(line)
pos = line.find(word, 0)
while pos != -1:
if self.__fullword:
if (pos > 0 and line[pos].isalpha()) or (pos + wordlen + 1 < linelen and line[pos + wordlen + 1].isalpha()):
pos = line.find(word, pos + 1)
continue
count += 1
pos = line.find(word, pos + wordlen + 1)
return count
def main():
finder = WordFinder()
finder.PrintUsage()
while True:
try:
line = input("input: ")
except:
sys.exit()
if line == "exit" or line == "q":
break;
finder.Find(line)
if __name__ == "__main__":
main()