| 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 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 13 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 | 20 import re, sys |
| 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', | |
| 29 'Used when a warning note as FIXME or XXX is detected.'), | 28 'Used when a warning note as FIXME or XXX is detected.'), |
| 30 'W0512': ('Cannot decode using encoding "%s", unexpected byte at position %d
', | 29 } |
| 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 | |
| 37 | 30 |
| 38 class EncodingChecker(BaseChecker): | 31 class EncodingChecker(BaseChecker): |
| 39 | |
| 40 """checks for: | 32 """checks for: |
| 41 * warning notes in the code like FIXME, XXX | 33 * warning notes in the code like FIXME, XXX |
| 42 * encoding issues. | 34 * PEP 263: source code with non ascii character but no encoding declaration |
| 43 """ | 35 """ |
| 44 __implements__ = IRawChecker | 36 __implements__ = IRawChecker |
| 45 | 37 |
| 46 # configuration section name | 38 # configuration section name |
| 47 name = 'miscellaneous' | 39 name = 'miscellaneous' |
| 48 msgs = MSGS | 40 msgs = MSGS |
| 49 | 41 |
| 50 options = (('notes', | 42 options = (('notes', |
| 51 {'type': 'csv', 'metavar': '<comma separated values>', | 43 {'type' : 'csv', 'metavar' : '<comma separated values>', |
| 52 'default': ('FIXME', 'XXX', 'TODO'), | 44 'default' : ('FIXME', 'XXX', 'TODO'), |
| 53 'help': ('List of note tags to take in consideration, ' | 45 'help' : 'List of note tags to take in consideration, \ |
| 54 'separated by a comma.')}),) | 46 separated by a comma.' |
| 47 }), |
| 48 ) |
| 55 | 49 |
| 56 def _check_note(self, notes, lineno, line): | 50 def __init__(self, linter=None): |
| 57 # First, simply check if the notes are in the line at all. This is an | 51 BaseChecker.__init__(self, linter) |
| 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 | |
| 67 | 52 |
| 68 match = notes.search(line) | 53 def process_module(self, node): |
| 69 if not match: | 54 """inspect the source file to found encoding problem or fixmes like |
| 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 | |
| 82 notes | 55 notes |
| 83 """ | 56 """ |
| 84 stream = module.file_stream | 57 stream = node.file_stream |
| 85 stream.seek(0) # XXX may be removed with astroid > 0.23 | 58 stream.seek(0) # XXX may be removed with astng > 0.23 |
| 86 if self.config.notes: | 59 # warning notes in the code |
| 87 notes = re.compile( | 60 notes = [] |
| 88 r'.*?#\s*(%s)(:*\s*.+)' % "|".join(self.config.notes)) | 61 for note in self.config.notes: |
| 89 else: | 62 notes.append(re.compile(note)) |
| 90 notes = None | 63 linenum = 1 |
| 91 if module.file_encoding: | 64 for line in stream.readlines(): |
| 92 encoding = module.file_encoding | 65 for note in notes: |
| 93 else: | 66 match = note.search(line) |
| 94 encoding = 'ascii' | 67 if match: |
| 68 self.add_message('W0511', args=line[match.start():-1], |
| 69 line=linenum) |
| 70 break |
| 71 linenum += 1 |
| 95 | 72 |
| 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) | |
| 100 | 73 |
| 101 | 74 |
| 102 def register(linter): | 75 def register(linter): |
| 103 """required method to auto register this checker""" | 76 """required method to auto register this checker""" |
| 104 linter.register_checker(EncodingChecker(linter)) | 77 linter.register_checker(EncodingChecker(linter)) |
| OLD | NEW |