| OLD | NEW |
| 1 # Copyright (c) 2003-2013 LOGILAB S.A. (Paris, FRANCE). |
| 2 # http://www.logilab.fr/ -- mailto:contact@logilab.fr |
| 3 # |
| 1 # This program is free software; you can redistribute it and/or modify it under | 4 # This program is free software; you can redistribute it and/or modify it under |
| 2 # the terms of the GNU General Public License as published by the Free Software | 5 # the terms of the GNU General Public License as published by the Free Software |
| 3 # Foundation; either version 2 of the License, or (at your option) any later | 6 # Foundation; either version 2 of the License, or (at your option) any later |
| 4 # version. | 7 # version. |
| 5 # | 8 # |
| 6 # This program is distributed in the hope that it will be useful, but WITHOUT | 9 # This program is distributed in the hope that it will be useful, but WITHOUT |
| 7 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 8 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | 11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 9 # | 12 # |
| 10 # You should have received a copy of the GNU General Public License along with | 13 # You should have received a copy of the GNU General Public License along with |
| 11 # this program; if not, write to the Free Software Foundation, Inc., | 14 # this program; if not, write to the Free Software Foundation, Inc., |
| 12 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 13 """ Copyright (c) 2003-2010 LOGILAB S.A. (Paris, FRANCE). | 16 """ Copyright (c) 2003-2010 LOGILAB S.A. (Paris, FRANCE). |
| 14 http://www.logilab.fr/ -- mailto:contact@logilab.fr | 17 http://www.logilab.fr/ -- mailto:contact@logilab.fr |
| 15 | 18 |
| 16 Raw metrics checker | 19 Raw metrics checker |
| 17 """ | 20 """ |
| 18 | 21 |
| 19 import tokenize | 22 import tokenize |
| 20 | 23 |
| 21 # pylint now requires pylint >= 2.2, so this is no longer necessary | 24 # pylint now requires pylint >= 2.2, so this is no longer necessary |
| 22 #if not hasattr(tokenize, 'NL'): | 25 #if not hasattr(tokenize, 'NL'): |
| 23 # raise ValueError("tokenize.NL doesn't exist -- tokenize module too old") | 26 # raise ValueError("tokenize.NL doesn't exist -- tokenize module too old") |
| 24 | 27 |
| 25 from logilab.common.ureports import Table | 28 from logilab.common.ureports import Table |
| 26 | 29 |
| 27 from pylint.interfaces import IRawChecker | 30 from pylint.interfaces import ITokenChecker |
| 28 from pylint.checkers import BaseRawChecker, EmptyReport | 31 from pylint.utils import EmptyReport |
| 32 from pylint.checkers import BaseTokenChecker |
| 29 from pylint.reporters import diff_string | 33 from pylint.reporters import diff_string |
| 30 | 34 |
| 31 def report_raw_stats(sect, stats, old_stats): | 35 def report_raw_stats(sect, stats, old_stats): |
| 32 """calculate percentage of code / doc / comment / empty | 36 """calculate percentage of code / doc / comment / empty |
| 33 """ | 37 """ |
| 34 total_lines = stats['total_lines'] | 38 total_lines = stats['total_lines'] |
| 35 if not total_lines: | 39 if not total_lines: |
| 36 raise EmptyReport() | 40 raise EmptyReport() |
| 37 sect.description = '%s lines have been analyzed' % total_lines | 41 sect.description = '%s lines have been analyzed' % total_lines |
| 38 lines = ('type', 'number', '%', 'previous', 'difference') | 42 lines = ('type', 'number', '%', 'previous', 'difference') |
| 39 for node_type in ('code', 'docstring', 'comment', 'empty'): | 43 for node_type in ('code', 'docstring', 'comment', 'empty'): |
| 40 key = node_type + '_lines' | 44 key = node_type + '_lines' |
| 41 total = stats[key] | 45 total = stats[key] |
| 42 percent = float(total * 100) / total_lines | 46 percent = float(total * 100) / total_lines |
| 43 old = old_stats.get(key, None) | 47 old = old_stats.get(key, None) |
| 44 if old is not None: | 48 if old is not None: |
| 45 diff_str = diff_string(old, total) | 49 diff_str = diff_string(old, total) |
| 46 else: | 50 else: |
| 47 old, diff_str = 'NC', 'NC' | 51 old, diff_str = 'NC', 'NC' |
| 48 lines += (node_type, str(total), '%.2f' % percent, | 52 lines += (node_type, str(total), '%.2f' % percent, |
| 49 str(old), diff_str) | 53 str(old), diff_str) |
| 50 sect.append(Table(children=lines, cols=5, rheaders=1)) | 54 sect.append(Table(children=lines, cols=5, rheaders=1)) |
| 51 | 55 |
| 52 | 56 |
| 53 class RawMetricsChecker(BaseRawChecker): | 57 class RawMetricsChecker(BaseTokenChecker): |
| 54 """does not check anything but gives some raw metrics : | 58 """does not check anything but gives some raw metrics : |
| 55 * total number of lines | 59 * total number of lines |
| 56 * total number of code lines | 60 * total number of code lines |
| 57 * total number of docstring lines | 61 * total number of docstring lines |
| 58 * total number of comments lines | 62 * total number of comments lines |
| 59 * total number of empty lines | 63 * total number of empty lines |
| 60 """ | 64 """ |
| 61 | 65 |
| 62 __implements__ = (IRawChecker,) | 66 __implements__ = (ITokenChecker,) |
| 63 | 67 |
| 64 # configuration section name | 68 # configuration section name |
| 65 name = 'metrics' | 69 name = 'metrics' |
| 66 # configuration options | 70 # configuration options |
| 67 options = ( ) | 71 options = () |
| 68 # messages | 72 # messages |
| 69 msgs = {} | 73 msgs = {} |
| 70 # reports | 74 # reports |
| 71 reports = ( ('RP0701', 'Raw metrics', report_raw_stats), ) | 75 reports = (('RP0701', 'Raw metrics', report_raw_stats),) |
| 72 | 76 |
| 73 def __init__(self, linter): | 77 def __init__(self, linter): |
| 74 BaseRawChecker.__init__(self, linter) | 78 BaseTokenChecker.__init__(self, linter) |
| 75 self.stats = None | 79 self.stats = None |
| 76 | 80 |
| 77 def open(self): | 81 def open(self): |
| 78 """init statistics""" | 82 """init statistics""" |
| 79 self.stats = self.linter.add_stats(total_lines=0, code_lines=0, | 83 self.stats = self.linter.add_stats(total_lines=0, code_lines=0, |
| 80 empty_lines=0, docstring_lines=0, | 84 empty_lines=0, docstring_lines=0, |
| 81 comment_lines=0) | 85 comment_lines=0) |
| 82 | 86 |
| 83 def process_tokens(self, tokens): | 87 def process_tokens(self, tokens): |
| 84 """update stats""" | 88 """update stats""" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 line_type = 'empty_lines' | 120 line_type = 'empty_lines' |
| 117 elif i < len(tokens) and tok_type == tokenize.NEWLINE: | 121 elif i < len(tokens) and tok_type == tokenize.NEWLINE: |
| 118 i += 1 | 122 i += 1 |
| 119 return i, pos[0] - start[0] + 1, line_type | 123 return i, pos[0] - start[0] + 1, line_type |
| 120 | 124 |
| 121 | 125 |
| 122 def register(linter): | 126 def register(linter): |
| 123 """ required method to auto register this checker """ | 127 """ required method to auto register this checker """ |
| 124 linter.register_checker(RawMetricsChecker(linter)) | 128 linter.register_checker(RawMetricsChecker(linter)) |
| 125 | 129 |
| OLD | NEW |