-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaticCodeAnalyzer - level3.py
More file actions
421 lines (333 loc) · 11.7 KB
/
Copy pathstaticCodeAnalyzer - level3.py
File metadata and controls
421 lines (333 loc) · 11.7 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import os
import sys
# write your code here
class TooLongError(Exception):
def __init__(self, lineNumber, filePath):
self.lineNumber = str(lineNumber)
self.filePath = filePath
def __str__(self):
return f'{self.filePath}: Line {self.lineNumber}: S001 Too long'
class IndentationError(Exception):
def __init__(self, lineNumber, filePath):
self.lineNumber = lineNumber
self.filePath = filePath
def __str__(self):
return f'{self.filePath}: Line {self.lineNumber}: S002 Indentation not a multiple of four'
class RedundantSemicolonError(Exception):
def __init__(self, lineNumber, filePath):
self.lineNumber = lineNumber
self.filePath = filePath
def __str__(self):
return f'{self.filePath}: Line {self.lineNumber}: S003 Unnecessary semicolon'
class InlineSpaceError(Exception):
def __init__(self, lineNumber, filePath):
self.lineNumber = lineNumber
self.filePath = filePath
def __str__(self):
return f'{self.filePath}: Line {self.lineNumber}: S004 At least two spaces required before inline comments'
class TODOError(Exception):
def __init__(self, lineNumber, filePath):
self.lineNumber = lineNumber
self.filePath = filePath
def __str__(self):
return f'{self.filePath}: Line {self.lineNumber}: S005 TODO found'
class BlankLinesError(Exception):
def __init__(self, lineNumber, filePath):
self.lineNumber = lineNumber
self.filePath = filePath
def __str__(self):
return f'{self.filePath}: Line {self.lineNumber}: S006 More than two blank lines used before this line'
def checkFileOrFolder():
global linesList
global filePath
global fileList
global joinedPath
global dir
global halfPath
global user
user = sys.argv[1]
joinedPath = halfPath + user
if user[0] == 'C':
joinedPath = user
if '.py' in joinedPath:
filePath = joinedPath
dir = False
with open(joinedPath, 'r') as pythonFile:
linesList = pythonFile.readlines()
else:
scrapList = os.listdir(joinedPath)
dir = True
for file in scrapList:
if '.py' in file:
fileList.append(file)
def fileOpener(fullFilePath):
global linesList
with open(fullFilePath, 'r') as pythonFile:
linesList = pythonFile.readlines()
def getMaxLines(): # call only for multiple files
global fileList
global joinedPath
global maxLines
lineNumberList = []
for file in fileList:
fullFilePath = joinedPath + '\\' + file
with open(fullFilePath, 'r') as pythonFile:
localLinesList = pythonFile.readlines()
lineNumberList.append(len(localLinesList))
maxLines = max(lineNumberList)
def createDict():
global maxLines
global fileErrorDict
for file in fileList:
fileErrorDict.update({file: {}})
for lineNo in range(1, (maxLines + 1)):
fileErrorDict[file].update({lineNo: []})
dir = None
fileErrorDict = {}
errorDict = {}
errorNumDict = {}
user = ' '
halfPath = 'C:/Users/upend/PycharmProjects/Static Code Analyzer/Static Code Analyzer/task/'
joinedPath = ' '
filePath = ' '
fileList = []
maxLines = -1
linesList = []
emptySpace = [' ']
indents = []
hashtagIndex = -1 # fake value
hashFound = 0
firstCharIndex = ''
semiColonIndex = -1
def indexFinder(line):
global hashtagIndex
global firstCharIndex
global semiColonIndex
if "#" in line: # either whole line is comment or there is an inline comment
hashtagIndex = line.index("#")
for index, char in enumerate(line): # to look for empty spaces and first character index
if char in emptySpace:
continue
elif char != "#":
firstCharIndex = index
break
for colonIndex, char in enumerate(line):
if char == ';':
semiColonIndex = colonIndex
# elif char == '#':
# print(semiColonIndex)
def tooLongError(lineNumber, line, fileName=0):
global dir
global fileErrorDict
global user
try:
if len(line.strip('\n')) > 79:
if not dir:
raise TooLongError(lineNumber, user)
else:
fileErrorDict[fileName][lineNumber].append(1)
except TooLongError as error:
print(error)
def indentationError(lineNumber, line, fileName=0):
global hashtagIndex
global firstCharIndex
global dir
global fileErrorDict
global user
try:
if (hashtagIndex < firstCharIndex) and (hashtagIndex >= 0):
pass
# print('whole Line is comment', lineNumber)
else:
if hashtagIndex == -1: # comment not found
if firstCharIndex == 0: # starts without indentation all kul
pass
# print('all well, line', lineNumber)
elif firstCharIndex > 0:
if firstCharIndex % 4 == 0:
pass
# print('indent kul', lineNumber)
else:
if not dir:
raise IndentationError(lineNumber, user)
else:
fileErrorDict[fileName][lineNumber].append(2)
else: # hashtag is there... but inline
pass
# print('comment is inline', lineNumber)
except IndentationError as error: # only for debugging
print(error)
def redundantSemicolonError(lineNumber, line, fileName=0): # remove try except block and keep if else onli
global semiColonIndex
global dir
global fileErrorDict
global user
if semiColonIndex >= 0 and hashtagIndex >= 0:
if semiColonIndex < hashtagIndex:
gaps = line[(semiColonIndex + 1):hashtagIndex]
gapsLen = len(gaps)
gapsList = [' '] * gapsLen
try:
if gaps == ''.join(gapsList):
if not dir:
raise RedundantSemicolonError(lineNumber, user)
else:
fileErrorDict[fileName][lineNumber].append(3)
except RedundantSemicolonError as error:
print(error)
if len(line) >= 2 and hashtagIndex == -1:
try:
if line[-2] == ";":
if not dir:
raise RedundantSemicolonError(lineNumber, user)
else:
fileErrorDict[fileName][lineNumber].append(3)
except RedundantSemicolonError as error:
print(error)
# print(errorDict)
def inlineSpaceError(lineNumber, line, fileName=0):
global hashtagIndex
global firstCharIndex
global fileErrorDict
global dir
global user
if firstCharIndex < hashtagIndex:
spaces = line[hashtagIndex - 1] + line[hashtagIndex - 2]
try:
if spaces != " ":
if not dir:
raise InlineSpaceError(lineNumber, user)
else:
fileErrorDict[fileName][lineNumber].append(4)
except InlineSpaceError as error:
print(error)
def tODOError(lineNumber, line, fileName=0):
global hashtagIndex
global errorDict
global dir
global fileErrorDict
global user
def tryExceptTODO(hashtagIndex, todoIndex):
try:
if hashtagIndex >= 0 and hashtagIndex < todoIndex:
if not dir:
raise TODOError(lineNumber, user)
else:
fileErrorDict[fileName][lineNumber].append(5)
except TODOError as error:
print(error)
todoWord = ''
todoList = ['todo', 'Todo', 'tOdo', 'toDo', 'todO', 'TOdo', 'ToDo', 'TodO', 'TODo', 'TOdO', 'ToDO', 'TODO']
for TODOs in todoList:
if TODOs in line:
todoWord = TODOs
todoIndex = line.index(todoWord)
tryExceptTODO(hashtagIndex, todoIndex)
def blankLinesError(lineNumber, fileName=0):
global linesList
global dir
global fileErrorDict
global user
if lineNumber >= 4:
try:
if linesList[lineNumber - 4] == linesList[lineNumber - 3] == linesList[lineNumber - 2] == '\n' and linesList[lineNumber - 1] != '\n':
if not dir:
raise BlankLinesError(lineNumber, user)
elif dir:
fileErrorDict[fileName][lineNumber].append(6)
except BlankLinesError as error:
print(error)
def indexSubtractor():
global hashtagIndex
global semiColonIndex
hashtagIndex = -1
semiColonIndex = -1
def errorParser(errorCode, lineNum, absFilePath):
try:
if errorCode == 1:
raise TooLongError(lineNum, absFilePath)
elif errorCode == 2:
raise IndentationError(lineNum, absFilePath)
elif errorCode == 3:
raise RedundantSemicolonError(lineNum, absFilePath)
elif errorCode == 4:
raise InlineSpaceError(lineNum, absFilePath)
elif errorCode == 5:
raise TODOError(lineNum, absFilePath)
elif errorCode == 6:
raise BlankLinesError(lineNum, absFilePath)
except TooLongError as error:
print(error)
except IndentationError as error:
print(error)
except RedundantSemicolonError as error:
print(error)
except InlineSpaceError as error:
print(error)
except TODOError as error:
print(error)
except BlankLinesError as error:
print(error)
def catchErrors():
global fileErrorDict
global dir
global linesList
global hashtagIndex
global firstCharIndex
checkFileOrFolder()
if not dir:
for lineNumber, line in enumerate(linesList, start=1):
indexFinder(line)
tooLongError(lineNumber, line)
indentationError(lineNumber, line)
redundantSemicolonError(lineNumber, line)
inlineSpaceError(lineNumber, line)
tODOError(lineNumber, line)
blankLinesError(lineNumber)
indexSubtractor()
# print(errorDict)
elif dir:
getMaxLines()
createDict()
for file in fileList:
absFilePath = joinedPath + '\\' + file
fileOpener(absFilePath)
for lineNumber, line in enumerate(linesList, start=1):
indexFinder(line)
tooLongError(lineNumber, line, file)
indentationError(lineNumber, line, file)
redundantSemicolonError(lineNumber, line, file)
inlineSpaceError(lineNumber, line, file)
tODOError(lineNumber, line, file)
blankLinesError(lineNumber, file)
indexSubtractor()
# print(dict(reversed(list(fileErrorDict.items()))))
revFileList = fileList[::-1]
for file in fileList:
if file == 'tests.py':
continue
absFilePath = joinedPath + '\\' + file
for lineNum in range(1, (maxLines + 1)):
errorList = fileErrorDict[file][lineNum]
if len(errorList) >= 1:
# print(errorList)
for errorCode in errorList:
errorParser(errorCode, lineNum, absFilePath)
def debugger(line):
indexFinder(line)
# redundantSemicolonError(3, line)
# blankLinesError(11)
# inlineSpaceError(1, "print('What\'s your name?') # reading an input\n", 27, 0)
redundantSemicolonError(3, line)
# indentationError(12, " very_big_number")
# CHECK = TODOError(1, 3)
# TooLongError()
# indexFinder("he")
catchErrors()
# checkFileOrFolder()
# getMaxLines()
# createDict()
# print(fileErrorDict['__init__.py'][1])
# debugger(" print = he;")
# redundantSemicolonError(3, " print = he;")
# test 2, comment semi colons