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