logging.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. __package__ = 'archivebox.misc'
  2. # TODO: merge/dedupe this file with archivebox/logging_util.py
  3. import sys
  4. from typing import Optional, Union, Tuple, List
  5. from collections import defaultdict
  6. from random import randint
  7. from benedict import benedict
  8. from rich.console import Console
  9. from rich.highlighter import Highlighter
  10. # SETUP RICH CONSOLE / TTY detection / COLOR / PROGRESS BARS
  11. CONSOLE = Console()
  12. STDERR = Console(stderr=True)
  13. IS_TTY = CONSOLE.is_interactive
  14. class RainbowHighlighter(Highlighter):
  15. def highlight(self, text):
  16. for index in range(len(text)):
  17. text.stylize(f"color({randint(90, 98)})", index, index + 1)
  18. rainbow = RainbowHighlighter()
  19. DEFAULT_CLI_COLORS = benedict(
  20. {
  21. "reset": "\033[00;00m",
  22. "lightblue": "\033[01;30m",
  23. "lightyellow": "\033[01;33m",
  24. "lightred": "\033[01;35m",
  25. "red": "\033[01;31m",
  26. "green": "\033[01;32m",
  27. "blue": "\033[01;34m",
  28. "white": "\033[01;37m",
  29. "black": "\033[01;30m",
  30. }
  31. )
  32. ANSI = benedict({k: '' for k in DEFAULT_CLI_COLORS.keys()})
  33. COLOR_DICT = defaultdict(lambda: [(0, 0, 0), (0, 0, 0)], {
  34. '00': [(0, 0, 0), (0, 0, 0)],
  35. '30': [(0, 0, 0), (0, 0, 0)],
  36. '31': [(255, 0, 0), (128, 0, 0)],
  37. '32': [(0, 200, 0), (0, 128, 0)],
  38. '33': [(255, 255, 0), (128, 128, 0)],
  39. '34': [(0, 0, 255), (0, 0, 128)],
  40. '35': [(255, 0, 255), (128, 0, 128)],
  41. '36': [(0, 255, 255), (0, 128, 128)],
  42. '37': [(255, 255, 255), (255, 255, 255)],
  43. })
  44. # Logging Helpers (DEPRECATED, use rich.print instead going forward)
  45. def stdout(*args, color: Optional[str]=None, prefix: str='', config: Optional[benedict]=None) -> None:
  46. ansi = DEFAULT_CLI_COLORS if (config or {}).get('USE_COLOR') else ANSI
  47. if color:
  48. strs = [ansi[color], ' '.join(str(a) for a in args), ansi['reset'], '\n']
  49. else:
  50. strs = [' '.join(str(a) for a in args), '\n']
  51. sys.stdout.write(prefix + ''.join(strs))
  52. def stderr(*args, color: Optional[str]=None, prefix: str='', config: Optional[benedict]=None) -> None:
  53. ansi = DEFAULT_CLI_COLORS if (config or {}).get('USE_COLOR') else ANSI
  54. if color:
  55. strs = [ansi[color], ' '.join(str(a) for a in args), ansi['reset'], '\n']
  56. else:
  57. strs = [' '.join(str(a) for a in args), '\n']
  58. sys.stderr.write(prefix + ''.join(strs))
  59. def hint(text: Union[Tuple[str, ...], List[str], str], prefix=' ', config: Optional[benedict]=None) -> None:
  60. ansi = DEFAULT_CLI_COLORS if (config or {}).get('USE_COLOR') else ANSI
  61. if isinstance(text, str):
  62. stderr('{}{lightred}Hint:{reset} {}'.format(prefix, text, **ansi))
  63. else:
  64. stderr('{}{lightred}Hint:{reset} {}'.format(prefix, text[0], **ansi))
  65. for line in text[1:]:
  66. stderr('{} {}'.format(prefix, line))