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