A fork of hyde, the static site generation. Some patches will be pushed upstream.
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.
 
 
 

125 lines
3.6 KiB

  1. """
  2. Module for python 2.6 compatibility.
  3. """
  4. import logging
  5. import sys
  6. from itertools import ifilter, izip, tee
  7. try:
  8. from logging import NullHandler
  9. except:
  10. class NullHandler(logging.Handler):
  11. """
  12. NOOP handler for libraries.
  13. """
  14. def emit(self, record):
  15. """
  16. /dev/null
  17. """
  18. pass
  19. def getLoggerWithConsoleHandler(logger_name):
  20. logger = logging.getLogger(logger_name)
  21. logger.setLevel(logging.INFO)
  22. if not logger.handlers:
  23. handler = logging.StreamHandler(sys.stdout)
  24. if sys.platform == 'win32':
  25. formatter = logging.Formatter(
  26. fmt="%(asctime)s %(name)s %(message)s",
  27. datefmt='%H:%M:%S')
  28. else:
  29. formatter = ColorFormatter(fmt="$RESET %(asctime)s "
  30. "$BOLD$COLOR%(name)s$RESET "
  31. "%(message)s", datefmt='%H:%M:%S')
  32. handler.setFormatter(formatter)
  33. logger.addHandler(handler)
  34. return logger
  35. def getLoggerWithNullHandler(logger_name):
  36. """
  37. Gets the logger initialized with the `logger_name`
  38. and a NullHandler.
  39. """
  40. logger = logging.getLogger(logger_name)
  41. if not logger.handlers:
  42. logger.addHandler(NullHandler())
  43. return logger
  44. ## Code stolen from :
  45. ## http://stackoverflow.com/questions/384076/how-can-i-make-the-python-logging-output-to-be-colored/2532931#2532931
  46. ##
  47. BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
  48. COLORS = {
  49. 'WARNING' : YELLOW,
  50. 'INFO' : WHITE,
  51. 'DEBUG' : BLUE,
  52. 'CRITICAL' : YELLOW,
  53. 'ERROR' : RED,
  54. 'RED' : RED,
  55. 'GREEN' : GREEN,
  56. 'YELLOW' : YELLOW,
  57. 'BLUE' : BLUE,
  58. 'MAGENTA' : MAGENTA,
  59. 'CYAN' : CYAN,
  60. 'WHITE' : WHITE,
  61. }
  62. RESET_SEQ = "\033[0m"
  63. COLOR_SEQ = "\033[1;%dm"
  64. BOLD_SEQ = "\033[1m"
  65. class ColorFormatter(logging.Formatter):
  66. def __init__(self, *args, **kwargs):
  67. # can't do super(...) here because Formatter is an old school class
  68. logging.Formatter.__init__(self, *args, **kwargs)
  69. def format(self, record):
  70. levelname = record.levelname
  71. color = COLOR_SEQ % (30 + COLORS[levelname])
  72. message = logging.Formatter.format(self, record)
  73. message = message.replace("$RESET", RESET_SEQ)\
  74. .replace("$BOLD", BOLD_SEQ)\
  75. .replace("$COLOR", color)
  76. for k,v in COLORS.items():
  77. message = message.replace("$" + k, COLOR_SEQ % (v+30))\
  78. .replace("$BG" + k, COLOR_SEQ % (v+40))\
  79. .replace("$BG-" + k, COLOR_SEQ % (v+40))
  80. return message + RESET_SEQ
  81. logging.ColorFormatter = ColorFormatter
  82. def make_method(method_name, method_):
  83. def method__(*args, **kwargs):
  84. return method_(*args, **kwargs)
  85. method__.__name__ = method_name
  86. return method__
  87. def add_property(obj, method_name, method_, *args, **kwargs):
  88. from functools import partial
  89. m = make_method(method_name, partial(method_, *args, **kwargs))
  90. setattr(obj, method_name, property(m))
  91. def add_method(obj, method_name, method_, *args, **kwargs):
  92. from functools import partial
  93. m = make_method(method_name, partial(method_, *args, **kwargs))
  94. setattr(obj, method_name, m)
  95. def pairwalk(iterable):
  96. a, b = tee(iterable)
  97. next(b, None)
  98. return izip(a, b)
  99. def first_match(predicate, iterable):
  100. """
  101. Gets the first element matched by the predicate
  102. in the iterable.
  103. """
  104. for item in iterable:
  105. if predicate(item):
  106. return item
  107. return None