Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2006, 2010, 2012-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  3. # Copyright (c) 2012-2014 Google, Inc.
  4. # Copyright (c) 2012 FELD Boris <lothiraldan@gmail.com>
  5. # Copyright (c) 2014-2017 Claudiu Popa <pcmanticore@gmail.com>
  6. # Copyright (c) 2014 Brett Cannon <brett@python.org>
  7. # Copyright (c) 2014 Ricardo Gemignani <ricardo.gemignani@gmail.com>
  8. # Copyright (c) 2014 Arun Persaud <arun@nubati.net>
  9. # Copyright (c) 2015 Simu Toni <simutoni@gmail.com>
  10. # Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
  11. # Copyright (c) 2017 Kári Tristan Helgason <kthelgason@gmail.com>
  12. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  13. # For details: https://github.com/PyCQA/pylint/blob/master/COPYING
  14. """utilities methods and classes for reporters"""
  15. from __future__ import print_function
  16. import sys
  17. import locale
  18. import os
  19. import warnings
  20. import six
  21. CMPS = ['=', '-', '+']
  22. # py3k has no more cmp builtin
  23. if sys.version_info >= (3, 0):
  24. def cmp(a, b): # pylint: disable=redefined-builtin
  25. return (a > b) - (a < b)
  26. def diff_string(old, new):
  27. """given a old and new int value, return a string representing the
  28. difference
  29. """
  30. diff = abs(old - new)
  31. diff_str = "%s%s" % (CMPS[cmp(old, new)], diff and ('%.2f' % diff) or '')
  32. return diff_str
  33. class BaseReporter(object):
  34. """base class for reporters
  35. symbols: show short symbolic names for messages.
  36. """
  37. extension = ''
  38. def __init__(self, output=None):
  39. self.linter = None
  40. self.section = 0
  41. self.out = None
  42. self.out_encoding = None
  43. self.set_output(output)
  44. # Build the path prefix to strip to get relative paths
  45. self.path_strip_prefix = os.getcwd() + os.sep
  46. def handle_message(self, msg):
  47. """Handle a new message triggered on the current file."""
  48. def set_output(self, output=None):
  49. """set output stream"""
  50. self.out = output or sys.stdout
  51. if six.PY3:
  52. encode = lambda self, string: string
  53. else:
  54. def encode(self, string):
  55. if not isinstance(string, six.text_type):
  56. return string
  57. encoding = (getattr(self.out, 'encoding', None) or
  58. locale.getpreferredencoding(do_setlocale=False) or
  59. sys.getdefaultencoding())
  60. # errors=replace, we don't want to crash when attempting to show
  61. # source code line that can't be encoded with the current locale
  62. # settings
  63. return string.encode(encoding, 'replace')
  64. def writeln(self, string=''):
  65. """write a line in the output buffer"""
  66. print(self.encode(string), file=self.out)
  67. def display_reports(self, layout):
  68. """display results encapsulated in the layout tree"""
  69. self.section = 0
  70. if hasattr(layout, 'report_id'):
  71. layout.children[0].children[0].data += ' (%s)' % layout.report_id
  72. self._display(layout)
  73. def _display(self, layout):
  74. """display the layout"""
  75. raise NotImplementedError()
  76. def display_messages(self, layout):
  77. """Hook for displaying the messages of the reporter
  78. This will be called whenever the underlying messages
  79. needs to be displayed. For some reporters, it probably
  80. doesn't make sense to display messages as soon as they
  81. are available, so some mechanism of storing them could be used.
  82. This method can be implemented to display them after they've
  83. been aggregated.
  84. """
  85. # Event callbacks
  86. def on_set_current_module(self, module, filepath):
  87. """Hook called when a module starts to be analysed."""
  88. def on_close(self, stats, previous_stats):
  89. """Hook called when a module finished analyzing."""
  90. class CollectingReporter(BaseReporter):
  91. """collects messages"""
  92. name = 'collector'
  93. def __init__(self):
  94. BaseReporter.__init__(self)
  95. self.messages = []
  96. def handle_message(self, msg):
  97. self.messages.append(msg)
  98. _display = None
  99. def initialize(linter):
  100. """initialize linter with reporters in this package """
  101. from pylint import utils
  102. utils.register_plugins(linter, __path__[0])