| OLD | NEW |
| (Empty) | |
| 1 # 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 |
| 3 # Foundation; either version 2 of the License, or (at your option) any later |
| 4 # version. |
| 5 # |
| 6 # 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 |
| 8 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 9 # |
| 10 # 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., |
| 12 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 13 """utilities methods and classes for reporters |
| 14 |
| 15 Copyright (c) 2000-2003 LOGILAB S.A. (Paris, FRANCE). |
| 16 http://www.logilab.fr/ -- mailto:contact@logilab.fr |
| 17 """ |
| 18 |
| 19 import sys, locale |
| 20 |
| 21 CMPS = ['=', '-', '+'] |
| 22 |
| 23 def diff_string(old, new): |
| 24 """given a old and new int value, return a string representing the |
| 25 difference |
| 26 """ |
| 27 diff = abs(old - new) |
| 28 diff_str = "%s%s" % (CMPS[cmp(old, new)], diff and ('%.2f' % diff) or '') |
| 29 return diff_str |
| 30 |
| 31 |
| 32 class EmptyReport(Exception): |
| 33 """raised when a report is empty and so should not be displayed""" |
| 34 |
| 35 class BaseReporter: |
| 36 """base class for reporters""" |
| 37 |
| 38 extension = '' |
| 39 |
| 40 def __init__(self, output=None): |
| 41 self.linter = None |
| 42 self.include_ids = None |
| 43 self.section = 0 |
| 44 self.out = None |
| 45 self.out_encoding = None |
| 46 self.set_output(output) |
| 47 |
| 48 def set_output(self, output=None): |
| 49 """set output stream""" |
| 50 self.out = output or sys.stdout |
| 51 # py3k streams handle their encoding : |
| 52 if sys.version_info >= (3, 0): |
| 53 self.encode = lambda x: x |
| 54 return |
| 55 |
| 56 def encode(string): |
| 57 if not isinstance(string, unicode): |
| 58 return string |
| 59 encoding = (getattr(self.out, 'encoding', None) or |
| 60 locale.getdefaultlocale()[1] or |
| 61 sys.getdefaultencoding()) |
| 62 return string.encode(encoding) |
| 63 self.encode = encode |
| 64 |
| 65 def writeln(self, string=''): |
| 66 """write a line in the output buffer""" |
| 67 print >> self.out, self.encode(string) |
| 68 |
| 69 def display_results(self, layout): |
| 70 """display results encapsulated in the layout tree""" |
| 71 self.section = 0 |
| 72 if self.include_ids and hasattr(layout, 'report_id'): |
| 73 layout.children[0].children[0].data += ' (%s)' % layout.report_id |
| 74 self._display(layout) |
| 75 |
| 76 def _display(self, layout): |
| 77 """display the layout""" |
| 78 raise NotImplementedError() |
| 79 |
| OLD | NEW |