Advertisement
Guest User

Untitled

a guest
Oct 26th, 2010
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. # Style objects
  2. class PercentStyle(object):
  3.  default_format = "%(message)"
  4.  def is_asctime_in_format(fmt):
  5.    return fmt.find("%(asctime)") >= 0
  6.  def format_record(fmt, record):
  7.    return fmt % record.__dict__
  8.  
  9. class StrFormatStyle(object):
  10.  default_format = "{message}"
  11.  def is_asctime_in_format(fmt):
  12.    return fmt.find("{asctime}") >= 0
  13.  def format_record(fmt, record):
  14.    return fmt.format(**record.__dict__)
  15.  
  16. from string import Template # Top level, embedding may cause import
  17. deadlock issues
  18. class StringTemplateStyle(object):
  19.  def __init__(self):
  20.    self._fmt = self._tmpl = None # Setup for format_record
  21.  default_format = "${message}"
  22.  def is_asctime_in_format(fmt):
  23.    return fmt.find("$asctime") >= 0 or fmt.find("${asctime}") >= 0
  24.  def format_record(fmt, record):
  25.    if fmt != self._fmt:
  26.      self._fmt = fmt
  27.      self._tmpl = Template(fmt)
  28.    return self._tmpl.substitute(**record.__dict__)
  29.  
  30. # Build style map
  31. _STYLE_CODES = tuple("% { $".split())
  32. _STYLES = PercentStyle, StrFormatStyle, StringTemplateStyle
  33. _STYLE_MAP = dict(zip(_STYLE_CODES, _STYLES))
  34.  
  35. # Interpretation of the style parameter in Formatter.__init__
  36. if style not in _STYLE_CODES:
  37.  # Preserve option to allow arbitrary style objects at some point in the future
  38.  raise ValueError("Style must be one of {!r}".format(_STYLE_CODES))
  39. self._style = _STYLE_MAP[style]()
  40.  
  41. # self._fmt initialisation if/elif chain replacement
  42. self._fmt = self._style.default_format
  43.  
  44. # Time check if/elif chain replacement
  45. result = self._style.is_asctime_in_format(self._fmt)
  46.  
  47. # Record formatting if/elif chain replacement
  48. s = self._style.format_record(self._fmt, record)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement