Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Style objects
- class PercentStyle(object):
- default_format = "%(message)"
- def is_asctime_in_format(fmt):
- return fmt.find("%(asctime)") >= 0
- def format_record(fmt, record):
- return fmt % record.__dict__
- class StrFormatStyle(object):
- default_format = "{message}"
- def is_asctime_in_format(fmt):
- return fmt.find("{asctime}") >= 0
- def format_record(fmt, record):
- return fmt.format(**record.__dict__)
- from string import Template # Top level, embedding may cause import
- deadlock issues
- class StringTemplateStyle(object):
- def __init__(self):
- self._fmt = self._tmpl = None # Setup for format_record
- default_format = "${message}"
- def is_asctime_in_format(fmt):
- return fmt.find("$asctime") >= 0 or fmt.find("${asctime}") >= 0
- def format_record(fmt, record):
- if fmt != self._fmt:
- self._fmt = fmt
- self._tmpl = Template(fmt)
- return self._tmpl.substitute(**record.__dict__)
- # Build style map
- _STYLE_CODES = tuple("% { $".split())
- _STYLES = PercentStyle, StrFormatStyle, StringTemplateStyle
- _STYLE_MAP = dict(zip(_STYLE_CODES, _STYLES))
- # Interpretation of the style parameter in Formatter.__init__
- if style not in _STYLE_CODES:
- # Preserve option to allow arbitrary style objects at some point in the future
- raise ValueError("Style must be one of {!r}".format(_STYLE_CODES))
- self._style = _STYLE_MAP[style]()
- # self._fmt initialisation if/elif chain replacement
- self._fmt = self._style.default_format
- # Time check if/elif chain replacement
- result = self._style.is_asctime_in_format(self._fmt)
- # Record formatting if/elif chain replacement
- s = self._style.format_record(self._fmt, record)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement