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.

130 lines
3.4 KiB

4 years ago
  1. from jedi._compatibility import encoding, is_py3, u
  2. import os
  3. import time
  4. _inited = False
  5. def _lazy_colorama_init():
  6. """
  7. Lazily init colorama if necessary, not to screw up stdout if debugging is
  8. not enabled.
  9. This version of the function does nothing.
  10. """
  11. try:
  12. if os.name == 'nt':
  13. # Does not work on Windows, as pyreadline and colorama interfere
  14. raise ImportError
  15. else:
  16. # Use colorama for nicer console output.
  17. from colorama import Fore, init
  18. from colorama import initialise
  19. def _lazy_colorama_init(): # noqa: F811
  20. """
  21. Lazily init colorama if necessary, not to screw up stdout is
  22. debug not enabled.
  23. This version of the function does init colorama.
  24. """
  25. global _inited
  26. if not _inited:
  27. # pytest resets the stream at the end - causes troubles. Since
  28. # after every output the stream is reset automatically we don't
  29. # need this.
  30. initialise.atexit_done = True
  31. try:
  32. init(strip=False)
  33. except Exception:
  34. # Colorama fails with initializing under vim and is buggy in
  35. # version 0.3.6.
  36. pass
  37. _inited = True
  38. except ImportError:
  39. class Fore(object):
  40. RED = ''
  41. GREEN = ''
  42. YELLOW = ''
  43. MAGENTA = ''
  44. RESET = ''
  45. NOTICE = object()
  46. WARNING = object()
  47. SPEED = object()
  48. enable_speed = False
  49. enable_warning = False
  50. enable_notice = False
  51. # callback, interface: level, str
  52. debug_function = None
  53. _debug_indent = 0
  54. _start_time = time.time()
  55. def reset_time():
  56. global _start_time, _debug_indent
  57. _start_time = time.time()
  58. _debug_indent = 0
  59. def increase_indent(func):
  60. """Decorator for makin """
  61. def wrapper(*args, **kwargs):
  62. global _debug_indent
  63. _debug_indent += 1
  64. try:
  65. return func(*args, **kwargs)
  66. finally:
  67. _debug_indent -= 1
  68. return wrapper
  69. def dbg(message, *args, **kwargs):
  70. """ Looks at the stack, to see if a debug message should be printed. """
  71. # Python 2 compatibility, because it doesn't understand default args
  72. color = kwargs.pop('color', 'GREEN')
  73. assert color
  74. if debug_function and enable_notice:
  75. i = ' ' * _debug_indent
  76. _lazy_colorama_init()
  77. debug_function(color, i + 'dbg: ' + message % tuple(u(repr(a)) for a in args))
  78. def warning(message, *args, **kwargs):
  79. format = kwargs.pop('format', True)
  80. assert not kwargs
  81. if debug_function and enable_warning:
  82. i = ' ' * _debug_indent
  83. if format:
  84. message = message % tuple(u(repr(a)) for a in args)
  85. debug_function('RED', i + 'warning: ' + message)
  86. def speed(name):
  87. if debug_function and enable_speed:
  88. now = time.time()
  89. i = ' ' * _debug_indent
  90. debug_function('YELLOW', i + 'speed: ' + '%s %s' % (name, now - _start_time))
  91. def print_to_stdout(color, str_out):
  92. """
  93. The default debug function that prints to standard out.
  94. :param str color: A string that is an attribute of ``colorama.Fore``.
  95. """
  96. col = getattr(Fore, color)
  97. _lazy_colorama_init()
  98. if not is_py3:
  99. str_out = str_out.encode(encoding, 'replace')
  100. print(col + str_out + Fore.RESET)
  101. # debug_function = print_to_stdout