Package cssutils :: Module errorhandler |
[hide private]
[frames] | no frames]

# Source Code for Module cssutils.errorhandler

      1  #!/usr/bin/env python
      2  """cssutils ErrorHandler
      3
      4  ErrorHandler
      5      used as log with usual levels (debug, info, warn, error)
      6
      7      if instanciated with ``raiseExceptions=True`` raises exeptions instead
      8      of logging
      9
     10  log
     11      defaults to instance of ErrorHandler for any kind of log message from
     12      lexerm, parser etc.
     13
     14      - raiseExceptions = [False, True]
     15      - setloglevel(loglevel)
     16  """
     17  __all__ = ['ErrorHandler']
     18  __docformat__ = 'restructuredtext'
     19  __author__ = '$LastChangedBy: cthedot $'
     20  __date__ = '$LastChangedDate: 2007-10-18 22:43:15 +0200 (Do, 18 Okt 2007) $'
     21  __version__ = '$LastChangedRevision: 503 $'
     22
     23  import logging
     24  import xml.dom
     25
     26 -class _ErrorHandler(object):
     27      """
     28      handles all errors and log messages
     29      """
     30
     31 -    def __init__(self, log,
     32                   defaultloglevel=logging.DEBUG, raiseExceptions=False):
     33          """
     34          inits log if none given
     35
     36          log
     37              for parse messages, default logs to sys.stderr
     38          defaultloglevel
     39              if none give this is logging.DEBUG
     40          raiseExceptions
     41              - True: Errors will be reported to the calling app,
     42                e.g. during building
     43              - False: Errors will be written to the log, this is the
     44                default behaviour when parsing
     45          """
     46          if log:
     47              self._log = log
     48          else:
     49              import sys
     50              self._log = logging.getLogger('CSSUTILS')
     51              hdlr = logging.StreamHandler(sys.stderr)
     52              formatter = logging.Formatter('%(levelname)s\t%(message)s')
     53              hdlr.setFormatter(formatter)
     54              self._log.addHandler(hdlr)
     55              self._log.setLevel(defaultloglevel)
     56
     57          self.raiseExceptions = raiseExceptions
     58
     59
     60 -    def __getattr__(self, name):
     61          # here if new log has been set
     62          _logcalls = {
     63              u'debug': self._log.debug,
     64              u'info': self._log.info,
     65              u'warn': self._log.warn,
     66              u'critical': self._log.critical,
     67              u'fatal': self._log.fatal,
     68              u'error': self._log.error
     69              }
     70
     71          if name in _logcalls.keys():
     72              self._logcall = _logcalls[name]
     73              return self.__handle
     74          else:
     75              raise AttributeError(
     76                  '(errorhandler) No Attribute "%s" found' % name)
     77
     78
     79 -    def setlog(self, log):
     80          """set log of errorhandler's log"""
     81          self._log = log
     82
     83
     84 -    def setloglevel(self, level):
     85          """set level of errorhandler's log"""
     86          self._log.setLevel(level)
     87
     88
     89 -    def __handle(self, msg=u'', token=None, error=xml.dom.SyntaxErr,
     90                   neverraise=False):
     91          """
     92          handles all calls
     93          logs or raises exception
     94          """
     95          if token:
     96              if isinstance(token, tuple):
     97                  msg = u'%s [%s:%s: %s]' % (
     98                      msg, token[2], token[3], token[1])
     99              else:
    100                  msg = u'%s [%s:%s: %s]' % (
    101                      msg, token.line, token.col, token.value)
    102
    103          if error and self.raiseExceptions and not neverraise:
    104              raise error(msg)
    105          else:
    106              self._logcall(msg)
    107
    108
    109 -class ErrorHandler(_ErrorHandler):
    110      "Singleton, see _ErrorHandler"
    111
    112      instance = None
    113
    114 -    def __init__(self,
    115              log=None, defaultloglevel=logging.INFO, raiseExceptions=True):
    116
    117          if ErrorHandler.instance is None:
    118              ErrorHandler.instance = _ErrorHandler(log=log,
    119                                          defaultloglevel=defaultloglevel,
    120                                          raiseExceptions=raiseExceptions)
    121          self.__dict__ = ErrorHandler.instance.__dict__
    122