__init__.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright (c) 2003-2010 Sylvain Thenault (thenault@gmail.com).
  2. # Copyright (c) 2003-2012 LOGILAB S.A. (Paris, FRANCE).
  3. # This program is free software; you can redistribute it and/or modify it under
  4. # the terms of the GNU General Public License as published by the Free Software
  5. # Foundation; either version 2 of the License, or (at your option) any later
  6. # version.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License along with
  13. # this program; if not, write to the Free Software Foundation, Inc.,
  14. # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. """utilities methods and classes for reporters"""
  16. import sys, locale
  17. CMPS = ['=', '-', '+']
  18. def diff_string(old, new):
  19. """given a old and new int value, return a string representing the
  20. difference
  21. """
  22. diff = abs(old - new)
  23. diff_str = "%s%s" % (CMPS[cmp(old, new)], diff and ('%.2f' % diff) or '')
  24. return diff_str
  25. class EmptyReport(Exception):
  26. """raised when a report is empty and so should not be displayed"""
  27. class BaseReporter:
  28. """base class for reporters"""
  29. extension = ''
  30. def __init__(self, output=None):
  31. self.linter = None
  32. self.include_ids = None
  33. self.section = 0
  34. self.out = None
  35. self.out_encoding = None
  36. self.set_output(output)
  37. def set_output(self, output=None):
  38. """set output stream"""
  39. self.out = output or sys.stdout
  40. # py3k streams handle their encoding :
  41. if sys.version_info >= (3, 0):
  42. self.encode = lambda x: x
  43. return
  44. def encode(string):
  45. if not isinstance(string, unicode):
  46. return string
  47. encoding = (getattr(self.out, 'encoding', None) or
  48. locale.getdefaultlocale()[1] or
  49. sys.getdefaultencoding())
  50. return string.encode(encoding)
  51. self.encode = encode
  52. def writeln(self, string=''):
  53. """write a line in the output buffer"""
  54. print >> self.out, self.encode(string)
  55. def display_results(self, layout):
  56. """display results encapsulated in the layout tree"""
  57. self.section = 0
  58. if self.include_ids and hasattr(layout, 'report_id'):
  59. layout.children[0].children[0].data += ' (%s)' % layout.report_id
  60. self._display(layout)
  61. def _display(self, layout):
  62. """display the layout"""
  63. raise NotImplementedError()
  64. # Event callbacks
  65. def on_set_current_module(self, module, filepath):
  66. """starting analyzis of a module"""
  67. pass
  68. def on_close(self, stats, previous_stats):
  69. """global end of analyzis"""
  70. pass