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: 2008-02-11 22:08:18 +0100 (Mo, 11 Feb 2008) $'
     21  __version__ = '$LastChangedRevision: 1008 $'
     22
     23  import logging
     24  import xml.dom
     25
     26 -class _ErrorHandler(object):
     27      """
     28      handles all errors and log messages
     29      """
     30 -    def __init__(self, log,
     31                   defaultloglevel=logging.DEBUG, raiseExceptions=False):
     32          """
     33          inits log if none given
     34
     35          log
     36              for parse messages, default logs to sys.stderr
     37          defaultloglevel
     38              if none give this is logging.DEBUG
     39          raiseExceptions
     40              - True: Errors will be reported to the calling app,
     41                e.g. during building
     42              - False: Errors will be written to the log, this is the
     43                default behaviour when parsing
     44          """
     45          if log:
     46              self._log = log
     47          else:
     48              import sys
     49              self._log = logging.getLogger('CSSUTILS')
     50              hdlr = logging.StreamHandler(sys.stderr)
     51              formatter = logging.Formatter('%(levelname)s\t%(message)s')
     52              hdlr.setFormatter(formatter)
     53              self._log.addHandler(hdlr)
     54              self._log.setLevel(defaultloglevel)
     55
     56          self.raiseExceptions = raiseExceptions
     57
     58 -    def __getattr__(self, name):
     59          # here if new log has been set
     60          _logcalls = {
     61              u'debug': self._log.debug,
     62              u'info': self._log.info,
     63              u'warn': self._log.warn,
     64              u'critical': self._log.critical,
     65              u'fatal': self._log.fatal,
     66              u'error': self._log.error
     67              }
     68
     69          if name in _logcalls.keys():
     70              self._logcall = _logcalls[name]
     71              return self.__handle
     72          else:
     73              raise AttributeError(
     74                  '(errorhandler) No Attribute "%s" found' % name)
     75
     76 -    def setlog(self, log):
     77          """set log of errorhandler's log"""
     78          self._log = log
     79
     80 -    def setloglevel(self, level):
     81          """set level of errorhandler's log"""
     82          self._log.setLevel(level)
     83
     84 -    def __handle(self, msg=u'', token=None, error=xml.dom.SyntaxErr,
     85                   neverraise=False):
     86          """
     87          handles all calls
     88          logs or raises exception
     89          """
     90          if token:
     91              if isinstance(token, tuple):
     92                  msg = u'%s [%s:%s: %s]' % (
     93                      msg, token[2], token[3], token[1])
     94              else:
     95                  msg = u'%s [%s:%s: %s]' % (
     96                      msg, token.line, token.col, token.value)
     97
     98          if error and self.raiseExceptions and not neverraise:
     99              raise error(msg)
    100          else:
    101              self._logcall(msg)
    102
    103
    104 -class ErrorHandler(_ErrorHandler):
    105      "Singleton, see _ErrorHandler"
    106      instance = None
    107
    108 -    def __init__(self,
    109              log=None, defaultloglevel=logging.INFO, raiseExceptions=True):
    110
    111          if ErrorHandler.instance is None:
    112              ErrorHandler.instance = _ErrorHandler(log=log,
    113                                          defaultloglevel=defaultloglevel,
    114                                          raiseExceptions=raiseExceptions)
    115          self.__dict__ = ErrorHandler.instance.__dict__
    116