-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathvalidate.py
50 lines (41 loc) · 1.78 KB
/
validate.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
import mmap
import re
import os
from optparse import OptionParser
words = {}
def validate_file(inputfilename):
global words
print "Validating file:%s"%inputfilename
input_file = open(inputfilename)
alllines = input_file.readlines()
line=1
for eachline in alllines:
result = re.findall(words,eachline,flags=re.IGNORECASE)
for eachresult in result:
if eachresult!=None and len(eachresult)!=0:
print "\tLine %d:found \"%s\""%(line,str(eachresult))
line=line+1
def main():
global words
parser = OptionParser()
parser.add_option("-f", "--inputfile", dest="inputfile", action="store", type="string", default="" , help="File to be validated", metavar="FILE")
parser.add_option("-i", "--wordsfile", dest="wordsfile", action="store", type="string", default="prohibited.txt", help="File containing the words to be checked", metavar="FILE")
(options, args) = parser.parse_args()
if len(options.inputfile) <= 0 or len(options.wordsfile) == None:
parser.error("Invalid arguments")
words_file = open(options.wordsfile)
alllines = words_file.read()
words = alllines[:-1].replace("\n","|")
if os.path.isfile(options.inputfile):
validate_file(options.inputfile)
if(os.path.isdir(options.inputfile)):
for root, dirs, files in os.walk(options.inputfile, topdown=False):
for name in files:
file_to_validate=os.path.join(root, name)
if(file_to_validate.endswith(".md")):
validate_file(os.path.join(root, name))
# print(os.path.join(root, name))
#for name in dirs:
# print(os.path.join(root, name))
if __name__ == "__main__":
main()