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  __version__ = '$Id: errorhandler.py 1324 2008-07-06 18:17:56Z cthedot $'
     20
     21  import logging
     22  import urllib2
     23  import xml.dom
     24  from helper import Deprecated
     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          "use self._log items"
     60          calls = ('debug', 'info', 'warn', 'error', 'critical', 'fatal')
     61          other = ('setLevel', 'getEffectiveLevel', 'addHandler', 'removeHandler')
     62
     63          if name in calls:
     64              self._logcall = getattr(self._log, name)
     65              return self.__handle
     66          elif name in other:
     67              return getattr(self._log, name)
     68          else:
     69              raise AttributeError(
     70                  '(errorhandler) No Attribute %r found' % name)
     71
     72 -    def __handle(self, msg=u'', token=None, error=xml.dom.SyntaxErr,
     73                   neverraise=False, args=None):
     74          """
     75          handles all calls
     76          logs or raises exception
     77          """
     78          if token:
     79              if isinstance(token, tuple):
     80                  msg = u'%s [%s:%s: %s]' % (
     81                      msg, token[2], token[3], token[1])
     82              else:
     83                  msg = u'%s [%s:%s: %s]' % (
     84                      msg, token.line, token.col, token.value)
     85
     86          if error and self.raiseExceptions and not neverraise:
     87              if isinstance(error, urllib2.HTTPError) or isinstance(error, urllib2.URLError):
     88                  raise error
     89              else:
     90                  raise error(msg)
     91          else:
     92              self._logcall(msg)
     93
     94 -    def setLog(self, log):
     95          """set log of errorhandler's log"""
     96          self._log = log
     97
     98      @Deprecated('Use setLog() instead.')
     99 -    def setlog(self, log):
    100          self.setLog(log)
    101
    102      @Deprecated('Use setLevel() instead.')
    103 -    def setloglevel(self, level):
    104          self.setLevel(level)
    105
    106
    107 -class ErrorHandler(_ErrorHandler):
    108      "Singleton, see _ErrorHandler"
    109      instance = None
    110
    111 -    def __init__(self,
    112              log=None, defaultloglevel=logging.INFO, raiseExceptions=True):
    113
    114          if ErrorHandler.instance is None:
    115              ErrorHandler.instance = _ErrorHandler(log=log,
    116                                          defaultloglevel=defaultloglevel,
    117                                          raiseExceptions=raiseExceptions)
    118          self.__dict__ = ErrorHandler.instance.__dict__
    119