~afrantzis/bzr-text-checker/empty-line-at-eof

« back to all changes in this revision

Viewing changes to text_checker.py

  • Committer: Alexandros Frantzis
  • Date: 2012-11-12 10:44:50 UTC
  • Revision ID: alexandros.frantzis@canonical.com-20121112104450-vmx8pdpt2es4hdfz
Add empty_line_at_eof check

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
        self.long_lines = {}
44
44
        self.no_newline_at_eof = []
45
45
        self.wrong_eol = {}
 
46
        self.empty_line_at_eof = {}
46
47
        self.problems = {'warn':[], 'fail':[]}
47
48
 
48
49
    def _reset_actions(self):
55
56
        self.long_line_length = DEFAULT_LONGLINE_LENGTH
56
57
        self.check_eol_action = 'ignore'
57
58
        self.check_eol_type = 'lf'
 
59
        self.empty_line_at_eof_action = 'ignore'
58
60
 
59
61
    def _push_file_line(self, dict_, fname, line_no):
60
62
        if fname not in dict_:
83
85
        rules_seq = tuple(tree.iter_search_rules((path,),
84
86
            pref_names=('trailing_whitespace', 'tabs', 'newline_at_eof',
85
87
                'long_lines', 'long_line_length', 'long_line_exceptions',
86
 
                'check_eol', 'check_eol_type'
 
88
                'check_eol', 'check_eol_type', 'empty_line_at_eof'
87
89
                ), **kwargs))[0]
88
90
        if len(rules_seq) >= 3:
89
91
            self._set_action_val(rules_seq, 0, 'trailing_whitespace_action')
101
103
                self.long_line_exceptions = [self.long_line_exceptions,]
102
104
            self._set_action_val(rules_seq, 6, 'check_eol_action')
103
105
            self._set_action_val(rules_seq, 7, 'check_eol_type')
 
106
            self._set_action_val(rules_seq, 8, 'empty_line_at_eof_action')
104
107
            if self.check_eol_type == 'lf':
105
108
                self.eol_regex = r'^[^\r\n]*\n$'
106
109
            elif self.check_eol_type == 'crlf':
161
164
                    if len(newlines) == j2 and not newlines[j2-1].endswith(
162
165
                            '\n'):
163
166
                        self.no_newline_at_eof.append(new_filename)
 
167
                    if len(newlines) == j2 and newlines[j2-1] == '\n':
 
168
                        self._push_file_line(self.empty_line_at_eof, new_filename, j2)
164
169
 
165
170
    def _append_problem(self, action, msg):
166
171
        if action in ('warn', 'fail'):
199
204
                    'following files:'
200
205
                )
201
206
            )
 
207
        if self.empty_line_at_eof:
 
208
            self._append_problem(self.empty_line_at_eof_action,
 
209
                self._format_message(self.empty_line_at_eof,
 
210
                    'New empty lines at EOF were found in the '
 
211
                    'following files:'
 
212
                )
 
213
            )
202
214
        if _text_check_warn_only:
203
215
            self.problems['warn'].extend(self.problems['fail'])
204
216
            self.problems['fail'] = []