home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1406 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  4.4 KB  |  117 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL 3'
  6. __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
  7. __docformat__ = 'restructuredtext en'
  8. DEBUG = 0
  9. INFO = 1
  10. WARN = 2
  11. ERROR = 3
  12. import sys
  13. import traceback
  14. from functools import partial
  15.  
  16. class Stream(object):
  17.     
  18.     def __init__(self, stream):
  19.         prints = prints
  20.         import calibre
  21.         self._prints = partial(prints, safe_encode = True)
  22.         self.stream = stream
  23.  
  24.     
  25.     def flush(self):
  26.         self.stream.flush()
  27.  
  28.  
  29.  
  30. class ANSIStream(Stream):
  31.     
  32.     def __init__(self, stream = sys.stdout):
  33.         Stream.__init__(self, stream)
  34.         TerminalController = TerminalController
  35.         import calibre.utils.terminfo
  36.         tc = TerminalController(stream)
  37.         self.color = {
  38.             DEBUG: tc.GREEN,
  39.             INFO: '',
  40.             WARN: tc.YELLOW,
  41.             ERROR: tc.RED }
  42.         self.normal = tc.NORMAL
  43.  
  44.     
  45.     def prints(self, level, *args, **kwargs):
  46.         self.stream.write(self.color[level])
  47.         kwargs['file'] = self.stream
  48.         self._prints(*args, **kwargs)
  49.         self.stream.write(self.normal)
  50.  
  51.     
  52.     def flush(self):
  53.         self.stream.flush()
  54.  
  55.  
  56.  
  57. class HTMLStream(Stream):
  58.     
  59.     def __init__(self, stream = sys.stdout):
  60.         Stream.__init__(self, stream)
  61.         self.color = {
  62.             DEBUG: '<span style="color:green">',
  63.             INFO: '<span>',
  64.             WARN: '<span style="color:yellow">',
  65.             ERROR: '<span style="color:red">' }
  66.         self.normal = '</span>'
  67.  
  68.     
  69.     def prints(self, level, *args, **kwargs):
  70.         self.stream.write(self.color[level])
  71.         kwargs['file'] = self.stream
  72.         self._prints(*args, **kwargs)
  73.         self.stream.write(self.normal)
  74.  
  75.     
  76.     def flush(self):
  77.         self.stream.flush()
  78.  
  79.  
  80.  
  81. class Log(object):
  82.     DEBUG = DEBUG
  83.     INFO = INFO
  84.     WARN = WARN
  85.     ERROR = ERROR
  86.     
  87.     def __init__(self, level = INFO):
  88.         self.filter_level = level
  89.         default_output = ANSIStream()
  90.         self.outputs = [
  91.             default_output]
  92.         self.debug = partial(self.prints, DEBUG)
  93.         self.info = partial(self.prints, INFO)
  94.         self.warn = self.warning = partial(self.prints, WARN)
  95.         self.error = partial(self.prints, ERROR)
  96.  
  97.     
  98.     def prints(self, level, *args, **kwargs):
  99.         if level < self.filter_level:
  100.             return None
  101.         for output in self.outputs:
  102.             output.prints(level, *args, **kwargs)
  103.         
  104.  
  105.     
  106.     def exception(self, *args, **kwargs):
  107.         limit = kwargs.pop('limit', None)
  108.         self.prints(ERROR, *args, **kwargs)
  109.         self.prints(DEBUG, traceback.format_exc(limit))
  110.  
  111.     
  112.     def __call__(self, *args, **kwargs):
  113.         self.prints(INFO, *args, **kwargs)
  114.  
  115.  
  116. default_log = Log()
  117.