misc.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. """ Copyright (c) 2000-2010 LOGILAB S.A. (Paris, FRANCE).
  15. http://www.logilab.fr/ -- mailto:contact@logilab.fr
  16. Check source code is ascii only or has an encoding declaration (PEP 263)
  17. """
  18. import re, sys
  19. from pylint.interfaces import IRawChecker
  20. from pylint.checkers import BaseChecker
  21. MSGS = {
  22. 'W0511': ('%s',
  23. 'Used when a warning note as FIXME or XXX is detected.'),
  24. }
  25. class EncodingChecker(BaseChecker):
  26. """checks for:
  27. * warning notes in the code like FIXME, XXX
  28. * PEP 263: source code with non ascii character but no encoding declaration
  29. """
  30. __implements__ = IRawChecker
  31. # configuration section name
  32. name = 'miscellaneous'
  33. msgs = MSGS
  34. options = (('notes',
  35. {'type' : 'csv', 'metavar' : '<comma separated values>',
  36. 'default' : ('FIXME', 'XXX', 'TODO'),
  37. 'help' : 'List of note tags to take in consideration, \
  38. separated by a comma.'
  39. }),
  40. )
  41. def __init__(self, linter=None):
  42. BaseChecker.__init__(self, linter)
  43. def process_module(self, node):
  44. """inspect the source file to found encoding problem or fixmes like
  45. notes
  46. """
  47. stream = node.file_stream
  48. stream.seek(0) # XXX may be removed with astng > 0.23
  49. # warning notes in the code
  50. notes = []
  51. for note in self.config.notes:
  52. notes.append(re.compile(note))
  53. linenum = 1
  54. for line in stream.readlines():
  55. for note in notes:
  56. match = note.search(line)
  57. if match:
  58. self.add_message('W0511', args=line[match.start():-1],
  59. line=linenum)
  60. break
  61. linenum += 1
  62. def register(linter):
  63. """required method to auto register this checker"""
  64. linter.register_checker(EncodingChecker(linter))