| 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 # 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 | 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 import six |
| 24 | 25 |
| 25 | 26 |
| 26 MSGS = { | 27 MSGS = { |
| 27 'W0511': ('%s', | 28 'W0511': ('%s', |
| 28 'fixme', | 29 'fixme', |
| 29 'Used when a warning note as FIXME or XXX is detected.'), | 30 'Used when a warning note as FIXME or XXX is detected.'), |
| 30 'W0512': ('Cannot decode using encoding "%s", unexpected byte at position %d
', | 31 'W0512': ('Cannot decode using encoding "%s", unexpected byte at position %d
', |
| 31 'invalid-encoded-data', | 32 'invalid-encoded-data', |
| 32 'Used when a source line cannot be decoded using the specified ' | 33 'Used when a source line cannot be decoded using the specified ' |
| 33 'source file encoding.', | 34 'source file encoding.', |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 else: | 66 else: |
| 66 return | 67 return |
| 67 | 68 |
| 68 match = notes.search(line) | 69 match = notes.search(line) |
| 69 if not match: | 70 if not match: |
| 70 return | 71 return |
| 71 self.add_message('fixme', args=line[match.start(1):-1], line=lineno) | 72 self.add_message('fixme', args=line[match.start(1):-1], line=lineno) |
| 72 | 73 |
| 73 def _check_encoding(self, lineno, line, file_encoding): | 74 def _check_encoding(self, lineno, line, file_encoding): |
| 74 try: | 75 try: |
| 75 return unicode(line, file_encoding) | 76 return six.text_type(line, file_encoding) |
| 76 except UnicodeDecodeError, ex: | 77 except UnicodeDecodeError as ex: |
| 77 self.add_message('invalid-encoded-data', line=lineno, | 78 self.add_message('invalid-encoded-data', line=lineno, |
| 78 args=(file_encoding, ex.args[2])) | 79 args=(file_encoding, ex.args[2])) |
| 79 | 80 |
| 80 def process_module(self, module): | 81 def process_module(self, module): |
| 81 """inspect the source file to find encoding problem or fixmes like | 82 """inspect the source file to find encoding problem or fixmes like |
| 82 notes | 83 notes |
| 83 """ | 84 """ |
| 84 stream = module.file_stream | 85 stream = module.file_stream |
| 85 stream.seek(0) # XXX may be removed with astroid > 0.23 | 86 stream.seek(0) # XXX may be removed with astroid > 0.23 |
| 86 if self.config.notes: | 87 if self.config.notes: |
| 87 notes = re.compile( | 88 notes = re.compile( |
| 88 r'.*?#\s*(%s)(:*\s*.+)' % "|".join(self.config.notes)) | 89 r'.*?#\s*(%s)(:*\s*.+)' % "|".join(self.config.notes)) |
| 89 else: | 90 else: |
| 90 notes = None | 91 notes = None |
| 91 if module.file_encoding: | 92 if module.file_encoding: |
| 92 encoding = module.file_encoding | 93 encoding = module.file_encoding |
| 93 else: | 94 else: |
| 94 encoding = 'ascii' | 95 encoding = 'ascii' |
| 95 | 96 |
| 96 for lineno, line in enumerate(stream): | 97 for lineno, line in enumerate(stream): |
| 97 line = self._check_encoding(lineno + 1, line, encoding) | 98 line = self._check_encoding(lineno + 1, line, encoding) |
| 98 if line is not None and notes: | 99 if line is not None and notes: |
| 99 self._check_note(notes, lineno + 1, line) | 100 self._check_note(notes, lineno + 1, line) |
| 100 | 101 |
| 101 | 102 |
| 102 def register(linter): | 103 def register(linter): |
| 103 """required method to auto register this checker""" | 104 """required method to auto register this checker""" |
| 104 linter.register_checker(EncodingChecker(linter)) | 105 linter.register_checker(EncodingChecker(linter)) |
| OLD | NEW |