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.
 
 
 

136 lines
3.9 KiB

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