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 1234 2008-05-22 20:26:12Z cthedot $'
     20
     21  import logging
     22  import urllib2
     23  import xml.dom
     24
     25 -class _ErrorHandler(object):
     26      """
     27      handles all errors and log messages
     28      """
     29 -    def __init__(self, log,
     30                   defaultloglevel=logging.DEBUG, raiseExceptions=False):
     31          """
     32          inits log if none given
     33
     34          log
     35              for parse messages, default logs to sys.stderr
     36          defaultloglevel
     37              if none give this is logging.DEBUG
     38          raiseExceptions
     39              - True: Errors will be reported to the calling app,
     40                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          # here if new log has been set
     59          _logcalls = {
     60              u'debug': self._log.debug,
     61              u'info': self._log.info,
     62              u'warn': self._log.warn,
     63              u'critical': self._log.critical,
     64              u'fatal': self._log.fatal,
     65              u'error': self._log.error
     66              }
     67
     68          if name in _logcalls.keys():
     69              self._logcall = _logcalls[name]
     70              return self.__handle
     71          else:
     72              raise AttributeError(
     73                  '(errorhandler) No Attribute "%s" found' % name)
     74
     75 -    def setlog(self, log):
     76          """set log of errorhandler's log"""
     77          self._log = log
     78
     79 -    def setloglevel(self, level):
     80          """set level of errorhandler's log"""
     81          self._log.setLevel(level)
     82
     83 -    def __handle(self, msg=u'', token=None, error=xml.dom.SyntaxErr,
     84                   neverraise=False, args=None):
     85          """
     86          handles all calls
     87          logs or raises exception
     88          """
     89          if token:
     90              if isinstance(token, tuple):
     91                  msg = u'%s [%s:%s: %s]' % (
     92                      msg, token[2], token[3], token[1])
     93              else:
     94                  msg = u'%s [%s:%s: %s]' % (
     95                      msg, token.line, token.col, token.value)
     96
     97          if error and self.raiseExceptions and not neverraise:
     98              if isinstance(error, urllib2.HTTPError) or isinstance(error, urllib2.URLError):
     99                  raise error
    100              else:
    101                  raise error(msg)
    102          else:
    103              self._logcall(msg)
    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