OLD | NEW |
1 # pylint: disable=W0511 | 1 # pylint: disable=W0511 |
2 # This program is free software; you can redistribute it and/or modify it under | 2 # This program is free software; you can redistribute it and/or modify it under |
3 # the terms of the GNU General Public License as published by the Free Software | 3 # the terms of the GNU General Public License as published by the Free Software |
4 # Foundation; either version 2 of the License, or (at your option) any later | 4 # Foundation; either version 2 of the License, or (at your option) any later |
5 # version. | 5 # version. |
6 # | 6 # |
7 # This program is distributed in the hope that it will be useful, but WITHOUT | 7 # This program is distributed in the hope that it will be useful, but WITHOUT |
8 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 8 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
9 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | 9 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
10 # | 10 # |
11 # You should have received a copy of the GNU General Public License along with | 11 # You should have received a copy of the GNU General Public License along with |
12 # this program; if not, write to the Free Software Foundation, Inc., | 12 # this program; if not, write to the Free Software Foundation, Inc., |
13 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 13 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
14 """ Copyright (c) 2000-2010 LOGILAB S.A. (Paris, FRANCE). | 14 """ Copyright (c) 2000-2010 LOGILAB S.A. (Paris, FRANCE). |
15 http://www.logilab.fr/ -- mailto:contact@logilab.fr | 15 http://www.logilab.fr/ -- mailto:contact@logilab.fr |
16 | 16 |
17 Check source code is ascii only or has an encoding declaration (PEP 263) | 17 Check source code is ascii only or has an encoding declaration (PEP 263) |
18 """ | 18 """ |
19 | 19 |
20 import re, sys | 20 import re |
21 | 21 |
22 from pylint.interfaces import IRawChecker | 22 from pylint.interfaces import IRawChecker |
23 from pylint.checkers import BaseChecker | 23 from pylint.checkers import BaseChecker |
24 | 24 |
25 | 25 |
26 MSGS = { | 26 MSGS = { |
27 'W0511': ('%s', | 27 'W0511': ('%s', |
| 28 'fixme', |
28 'Used when a warning note as FIXME or XXX is detected.'), | 29 'Used when a warning note as FIXME or XXX is detected.'), |
29 } | 30 'W0512': ('Cannot decode using encoding "%s", unexpected byte at position %d
', |
| 31 'invalid-encoded-data', |
| 32 'Used when a source line cannot be decoded using the specified ' |
| 33 'source file encoding.', |
| 34 {'maxversion': (3, 0)}), |
| 35 } |
| 36 |
30 | 37 |
31 class EncodingChecker(BaseChecker): | 38 class EncodingChecker(BaseChecker): |
| 39 |
32 """checks for: | 40 """checks for: |
33 * warning notes in the code like FIXME, XXX | 41 * warning notes in the code like FIXME, XXX |
34 * PEP 263: source code with non ascii character but no encoding declaration | 42 * encoding issues. |
35 """ | 43 """ |
36 __implements__ = IRawChecker | 44 __implements__ = IRawChecker |
37 | 45 |
38 # configuration section name | 46 # configuration section name |
39 name = 'miscellaneous' | 47 name = 'miscellaneous' |
40 msgs = MSGS | 48 msgs = MSGS |
41 | 49 |
42 options = (('notes', | 50 options = (('notes', |
43 {'type' : 'csv', 'metavar' : '<comma separated values>', | 51 {'type': 'csv', 'metavar': '<comma separated values>', |
44 'default' : ('FIXME', 'XXX', 'TODO'), | 52 'default': ('FIXME', 'XXX', 'TODO'), |
45 'help' : 'List of note tags to take in consideration, \ | 53 'help': ('List of note tags to take in consideration, ' |
46 separated by a comma.' | 54 'separated by a comma.')}),) |
47 }), | |
48 ) | |
49 | 55 |
50 def __init__(self, linter=None): | 56 def _check_note(self, notes, lineno, line): |
51 BaseChecker.__init__(self, linter) | 57 # First, simply check if the notes are in the line at all. This is an |
| 58 # optimisation to prevent using the regular expression on every line, |
| 59 # but rather only on lines which may actually contain one of the notes. |
| 60 # This prevents a pathological problem with lines that are hundreds |
| 61 # of thousands of characters long. |
| 62 for note in self.config.notes: |
| 63 if note in line: |
| 64 break |
| 65 else: |
| 66 return |
52 | 67 |
53 def process_module(self, node): | 68 match = notes.search(line) |
54 """inspect the source file to found encoding problem or fixmes like | 69 if not match: |
| 70 return |
| 71 self.add_message('fixme', args=line[match.start(1):-1], line=lineno) |
| 72 |
| 73 def _check_encoding(self, lineno, line, file_encoding): |
| 74 try: |
| 75 return unicode(line, file_encoding) |
| 76 except UnicodeDecodeError, ex: |
| 77 self.add_message('invalid-encoded-data', line=lineno, |
| 78 args=(file_encoding, ex.args[2])) |
| 79 |
| 80 def process_module(self, module): |
| 81 """inspect the source file to find encoding problem or fixmes like |
55 notes | 82 notes |
56 """ | 83 """ |
57 stream = node.file_stream | 84 stream = module.file_stream |
58 stream.seek(0) # XXX may be removed with astng > 0.23 | 85 stream.seek(0) # XXX may be removed with astroid > 0.23 |
59 # warning notes in the code | 86 if self.config.notes: |
60 notes = [] | 87 notes = re.compile( |
61 for note in self.config.notes: | 88 r'.*?#\s*(%s)(:*\s*.+)' % "|".join(self.config.notes)) |
62 notes.append(re.compile(note)) | 89 else: |
63 linenum = 1 | 90 notes = None |
64 for line in stream.readlines(): | 91 if module.file_encoding: |
65 for note in notes: | 92 encoding = module.file_encoding |
66 match = note.search(line) | 93 else: |
67 if match: | 94 encoding = 'ascii' |
68 self.add_message('W0511', args=line[match.start():-1], | |
69 line=linenum) | |
70 break | |
71 linenum += 1 | |
72 | 95 |
| 96 for lineno, line in enumerate(stream): |
| 97 line = self._check_encoding(lineno + 1, line, encoding) |
| 98 if line is not None and notes: |
| 99 self._check_note(notes, lineno + 1, line) |
73 | 100 |
74 | 101 |
75 def register(linter): | 102 def register(linter): |
76 """required method to auto register this checker""" | 103 """required method to auto register this checker""" |
77 linter.register_checker(EncodingChecker(linter)) | 104 linter.register_checker(EncodingChecker(linter)) |
OLD | NEW |