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 1361 2008-07-13 18:12:40Z 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, defaultloglevel=logging.INFO,
     31                   raiseExceptions=True):
     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 raised e.g. during building
     41              - False: Errors will be written to the log, this is the
     42                default behaviour when parsing
     43          """
     44          if log:
     45              self._log = log
     46          else:
     47              import sys
     48              self._log = logging.getLogger('CSSUTILS')
     49              hdlr = logging.StreamHandler(sys.stderr)
     50              formatter = logging.Formatter('%(levelname)s\t%(message)s')
     51              hdlr.setFormatter(formatter)
     52              self._log.addHandler(hdlr)
     53              self._log.setLevel(defaultloglevel)
     54
     55          self.raiseExceptions = raiseExceptions
     56
     57 -    def __getattr__(self, name):
     58          "use self._log items"
     59          calls = ('debug', 'info', 'warn', 'error', 'critical', 'fatal')
     60          other = ('setLevel', 'getEffectiveLevel', 'addHandler', 'removeHandler')
     61
     62          if name in calls:
     63              self._logcall = getattr(self._log, name)
     64              return self.__handle
     65          elif name in other:
     66              return getattr(self._log, name)
     67          else:
     68              raise AttributeError(
     69                  '(errorhandler) No Attribute %r found' % name)
     70
     71 -    def __handle(self, msg=u'', token=None, error=xml.dom.SyntaxErr,
     72                   neverraise=False, args=None):
     73          """
     74          handles all calls
     75          logs or raises exception
     76          """
     77          if token:
     78              if isinstance(token, tuple):
     79                  msg = u'%s [%s:%s: %s]' % (
     80                      msg, token[2], token[3], token[1])
     81              else:
     82                  msg = u'%s [%s:%s: %s]' % (
     83                      msg, token.line, token.col, token.value)
     84
     85          if error and self.raiseExceptions and not neverraise:
     86              if isinstance(error, urllib2.HTTPError) or isinstance(error, urllib2.URLError):
     87                  raise error
     88              else:
     89                  raise error(msg)
     90          else:
     91              self._logcall(msg)
     92
     93 -    def setLog(self, log):
     94          """set log of errorhandler's log"""
     95          self._log = log
     96
     97      @Deprecated('Use setLog() instead.')
     98 -    def setlog(self, log):
     99          self.setLog(log)
    100
    101      @Deprecated('Use setLevel() instead.')
    102 -    def setloglevel(self, level):
    103          self.setLevel(level)
    104
    105
    106 -class ErrorHandler(_ErrorHandler):
    107      "Singleton, see _ErrorHandler"
    108      instance = None
    109
    110 -    def __init__(self,
    111              log=None, defaultloglevel=logging.INFO, raiseExceptions=True):
    112
    113          if ErrorHandler.instance is None:
    114              ErrorHandler.instance = _ErrorHandler(log=log,
    115                                          defaultloglevel=defaultloglevel,
    116                                          raiseExceptions=raiseExceptions)
    117          self.__dict__ = ErrorHandler.instance.__dict__
    118