1 # Copyright (C) 2010 Chris Jerdonek (cjerdonek (at] webkit.org) 2 # 3 # Redistribution and use in source and binary forms, with or without 4 # modification, are permitted provided that the following conditions 5 # are met: 6 # 1. Redistributions of source code must retain the above copyright 7 # notice, this list of conditions and the following disclaimer. 8 # 2. Redistributions in binary form must reproduce the above copyright 9 # notice, this list of conditions and the following disclaimer in the 10 # documentation and/or other materials provided with the distribution. 11 # 12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND 13 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 14 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 15 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR 16 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 17 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 18 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 19 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 20 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 21 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 23 """Supports checking WebKit style in Python files.""" 24 25 from ...style_references import pep8 26 27 28 class PythonChecker(object): 29 30 """Processes text lines for checking style.""" 31 32 def __init__(self, file_path, handle_style_error): 33 self._file_path = file_path 34 self._handle_style_error = handle_style_error 35 36 def check(self, lines): 37 # Initialize pep8.options, which is necessary for 38 # Checker.check_all() to execute. 39 pep8.process_options(arglist=[self._file_path]) 40 41 checker = pep8.Checker(self._file_path) 42 43 def _pep8_handle_error(line_number, offset, text, check): 44 # FIXME: Incorporate the character offset into the error output. 45 # This will require updating the error handler __call__ 46 # signature to include an optional "offset" parameter. 47 pep8_code = text[:4] 48 pep8_message = text[5:] 49 50 category = "pep8/" + pep8_code 51 52 self._handle_style_error(line_number, category, 5, pep8_message) 53 54 checker.report_error = _pep8_handle_error 55 56 errors = checker.check_all() 57