7
7
8
8
9
9
class ErrorCollection (object ):
10
- _errors = []
11
10
12
- def add_object_error (self , error_obj = None , line = '--' , column = '--' , message = '' , level = "ERROR" ):
11
+ def __init__ (self ):
12
+ self ._errors = []
13
+
14
+ def add_object_error (self , error_obj = None , line = '--' , column = '--' , message = '' , level = "ERROR" , allow_repeted = True ):
13
15
if error_obj :
14
16
line = getattr (error_obj , 'line' , line )
15
17
column = getattr (error_obj , 'column' , column )
@@ -22,24 +24,23 @@ def add_object_error(self, error_obj=None, line='--', column='--', message='', l
22
24
'message' : message ,
23
25
'level' : level ,
24
26
}
25
- if error_data not in self ._errors :
27
+ if (error_data not in self ._errors ) or allow_repeted :
28
+ # if error was not included yet, let's append it.
29
+ # if error already included but allow_repeted == True, it's ok to append it again.
26
30
self ._errors .append (error_data )
27
31
28
- def add_exception_error (self , exception_instance ):
32
+ def add_exception_error (self , exception_instance , allow_repeted = True ):
29
33
message = exception_instance .message
30
34
if hasattr (exception_instance , 'position' ):
31
35
line , column = exception_instance .position
32
36
else :
33
37
line , column = None , None
34
- self .add_object_error (error_obj = None , line = line , column = column , message = message )
38
+ self .add_object_error (error_obj = None , line = line , column = column , message = message , allow_repeted = allow_repeted )
35
39
36
40
def add_list_of_errors (self , iterable , allow_repeted = True ):
37
- for error in iterable :
38
- if error not in self ._errors :
39
- self .add_object_error (error )
40
- elif allow_repeted :
41
- # error already exist in _errors list, but is added again
42
- self .add_object_error (error )
41
+ if iterable :
42
+ for error in iterable :
43
+ self .add_object_error (error , allow_repeted = allow_repeted )
43
44
44
45
def get_list (self ):
45
46
return self ._errors
@@ -60,7 +61,6 @@ class StyleCheckerAnalyzer(object):
60
61
_can_be_analyzed = (False , "Can't be analyzed" )
61
62
_can_be_analyzed_as_exception = False
62
63
_annotations = None
63
- _validation_errors = {'results' : [], 'error_lines' : [], }
64
64
_error_collection = None
65
65
66
66
def __init__ (self , target_input ):
@@ -77,12 +77,20 @@ def __init__(self, target_input):
77
77
self ._can_be_analyzed = (False , "IOError while starting Stylechecker.XML(), please verify if the input is correct" )
78
78
except Exception as e :
79
79
self ._can_be_analyzed = (False , "Error while starting Stylechecker.XML()" )
80
+ self ._validation_errors = {'results' : [], 'error_lines' : [], }
80
81
self ._error_collection = ErrorCollection ()
81
82
82
83
83
84
def get_validation_errors (self ):
85
+ """
86
+ returns a dict like { 'results' : ... , 'error_lines': ''}
87
+ 'results' is a dict with a structure necessary to display errors table (error level, line, cols, message)
88
+ 'error_lines' is a coma separated string that list the un-repeated numbers of lines, where the XML has a error annotation
89
+ (this list is used to highlight the line to the user with a plugin).
90
+ """
84
91
self ._validation_errors ['results' ] = self ._error_collection .get_list ()
85
- self ._validation_errors ['error_lines' ] = ", " .join (self ._error_collection .get_lines ())
92
+ lines_distinct = [str (l ) for l in set (self ._error_collection .get_lines ())]
93
+ self ._validation_errors ['error_lines' ] = ", " .join (lines_distinct )
86
94
return self ._validation_errors
87
95
88
96
@@ -95,22 +103,24 @@ def analyze(self):
95
103
if self ._can_be_analyzed_as_exception :
96
104
# in case of exceptions: self._target_data is the exception
97
105
self ._annotations = self ._target_data .message
98
- self ._error_collection .add_exception_error (self ._target_data )
106
+ self ._error_collection .add_exception_error (self ._target_data , allow_repeted = False )
99
107
results ['can_be_analyzed' ] = (True , None )
100
108
elif self ._can_be_analyzed [0 ]:
101
109
try :
102
- # TODO: improve it!
103
110
vs_status , vs_errors = self ._target_data .validate_style ()
104
111
v_status , v_errors = self ._target_data .validate ()
105
112
except Exception as e :
106
113
self ._annotations = e .message
107
- self ._error_collection .add_exception_error (e )
114
+ self ._error_collection .add_exception_error (e , allow_repeted = False )
108
115
results ['can_be_analyzed' ] = (True , None )
109
116
else :
110
117
if not vs_status or not v_status : # have errors
111
118
self ._target_data .annotate_errors ()
112
119
self ._annotations = str (self ._target_data )
120
+ if not vs_status :
113
121
self ._error_collection .add_list_of_errors (vs_errors )
122
+ if not v_status :
123
+ self ._error_collection .add_list_of_errors (v_errors )
114
124
results ['can_be_analyzed' ] = (True , None )
115
125
else :
116
126
results ['can_be_analyzed' ] = self ._can_be_analyzed
0 commit comments