home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1966 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  5.8 KB  |  202 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import glob
  5. import os
  6. import time
  7.  
  8. class Logger(object):
  9.     
  10.     def __init__(self, shell, logfname = 'Logger.log', loghead = '', logmode = 'over'):
  11.         (self._i00, self._i, self._ii, self._iii) = ('', '', '', '')
  12.         self.shell = shell
  13.         self.logfname = logfname
  14.         self.loghead = loghead
  15.         self.logmode = logmode
  16.         self.logfile = None
  17.         self.log_raw_input = False
  18.         self.log_output = False
  19.         self.timestamp = False
  20.         self.log_active = False
  21.  
  22.     
  23.     def _set_mode(self, mode):
  24.         if mode not in ('append', 'backup', 'global', 'over', 'rotate'):
  25.             raise ValueError, 'invalid log mode %s given' % mode
  26.         mode not in ('append', 'backup', 'global', 'over', 'rotate')
  27.         self._logmode = mode
  28.  
  29.     
  30.     def _get_mode(self):
  31.         return self._logmode
  32.  
  33.     logmode = property(_get_mode, _set_mode)
  34.     
  35.     def logstart(self, logfname = None, loghead = None, logmode = None, log_output = False, timestamp = False, log_raw_input = False):
  36.         if self.logfile is not None:
  37.             raise RuntimeError('Log file is already active: %s' % self.logfname)
  38.         self.logfile is not None
  39.         self.log_active = True
  40.         if logfname is not None:
  41.             self.logfname = logfname
  42.         
  43.         if loghead is not None:
  44.             self.loghead = loghead
  45.         
  46.         if logmode is not None:
  47.             self.logmode = logmode
  48.         
  49.         self.timestamp = timestamp
  50.         self.log_output = log_output
  51.         self.log_raw_input = log_raw_input
  52.         isfile = os.path.isfile
  53.         logmode = self.logmode
  54.         if logmode == 'append':
  55.             self.logfile = open(self.logfname, 'a')
  56.         elif logmode == 'backup':
  57.             if isfile(self.logfname):
  58.                 backup_logname = self.logfname + '~'
  59.                 if isfile(backup_logname):
  60.                     os.remove(backup_logname)
  61.                 
  62.                 os.rename(self.logfname, backup_logname)
  63.             
  64.             self.logfile = open(self.logfname, 'w')
  65.         elif logmode == 'global':
  66.             self.logfname = os.path.join(self.shell.home_dir, self.logfname)
  67.             self.logfile = open(self.logfname, 'a')
  68.         elif logmode == 'over':
  69.             if isfile(self.logfname):
  70.                 os.remove(self.logfname)
  71.             
  72.             self.logfile = open(self.logfname, 'w')
  73.         elif logmode == 'rotate':
  74.             if isfile(self.logfname):
  75.                 if isfile(self.logfname + '.001~'):
  76.                     old = glob.glob(self.logfname + '.*~')
  77.                     old.sort()
  78.                     old.reverse()
  79.                     for f in old:
  80.                         (root, ext) = os.path.splitext(f)
  81.                         num = int(ext[1:-1]) + 1
  82.                         os.rename(f, root + '.' + `num`.zfill(3) + '~')
  83.                     
  84.                 
  85.                 os.rename(self.logfname, self.logfname + '.001~')
  86.             
  87.             self.logfile = open(self.logfname, 'w')
  88.         
  89.         if logmode != 'append':
  90.             self.logfile.write(self.loghead)
  91.         
  92.         self.logfile.flush()
  93.  
  94.     
  95.     def switch_log(self, val):
  96.         if val not in [
  97.             False,
  98.             True,
  99.             0,
  100.             1]:
  101.             raise ValueError, 'Call switch_log ONLY with a boolean argument, not with:', val
  102.         val not in [
  103.             False,
  104.             True,
  105.             0,
  106.             1]
  107.         label = {
  108.             0: 'OFF',
  109.             1: 'ON',
  110.             False: 'OFF',
  111.             True: 'ON' }
  112.         if self.logfile is None:
  113.             print "\nLogging hasn't been started yet (use logstart for that).\n\n%logon/%logoff are for temporarily starting and stopping logging for a logfile\nwhich already exists. But you must first start the logging process with\n%logstart (optionally giving a logfile name)."
  114.         elif self.log_active == val:
  115.             print 'Logging is already', label[val]
  116.         else:
  117.             print 'Switching logging', label[val]
  118.             self.log_active = not (self.log_active)
  119.             self.log_active_out = self.log_active
  120.  
  121.     
  122.     def logstate(self):
  123.         if self.logfile is None:
  124.             print 'Logging has not been activated.'
  125.         elif not self.log_active or 'active':
  126.             pass
  127.         state = 'temporarily suspended'
  128.         print 'Filename       :', self.logfname
  129.         print 'Mode           :', self.logmode
  130.         print 'Output logging :', self.log_output
  131.         print 'Raw input log  :', self.log_raw_input
  132.         print 'Timestamping   :', self.timestamp
  133.         print 'State          :', state
  134.  
  135.     
  136.     def log(self, line_ori, line_mod, continuation = None):
  137.         
  138.         try:
  139.             input_hist = self.shell.user_ns['_ih']
  140.         except:
  141.             return None
  142.  
  143.         out_cache = self.shell.outputcache
  144.         if out_cache.do_full_cache and out_cache.prompt_count + 1 > len(input_hist):
  145.             input_hist.extend([
  146.                 '\n'] * (out_cache.prompt_count - len(input_hist)))
  147.         
  148.         if not continuation and line_mod:
  149.             self._iii = self._ii
  150.             self._ii = self._i
  151.             self._i = self._i00
  152.             self._i00 = line_mod + '\n'
  153.             input_hist.append(self._i00)
  154.         
  155.         to_main = {
  156.             '_i': self._i,
  157.             '_ii': self._ii,
  158.             '_iii': self._iii }
  159.         if self.shell.outputcache.do_full_cache:
  160.             in_num = self.shell.outputcache.prompt_count
  161.             last_num = len(input_hist) - 1
  162.             if in_num != last_num:
  163.                 in_num = self.shell.outputcache.prompt_count = last_num
  164.             
  165.             new_i = '_i%s' % in_num
  166.             if continuation:
  167.                 self._i00 = '%s%s\n' % (self.shell.user_ns[new_i], line_mod)
  168.                 input_hist[in_num] = self._i00
  169.             
  170.             to_main[new_i] = self._i00
  171.         
  172.         self.shell.user_ns.update(to_main)
  173.         if self.log_raw_input:
  174.             self.log_write(line_ori)
  175.         else:
  176.             self.log_write(line_mod)
  177.  
  178.     
  179.     def log_write(self, data, kind = 'input'):
  180.         if self.log_active and data:
  181.             write = self.logfile.write
  182.             if kind == 'input':
  183.                 if self.timestamp:
  184.                     write(time.strftime('# %a, %d %b %Y %H:%M:%S\n', time.localtime()))
  185.                 
  186.                 write('%s\n' % data)
  187.             elif kind == 'output' and self.log_output:
  188.                 odata = []([ '#[Out]# %s' % s for s in data.split('\n') ])
  189.                 write('%s\n' % odata)
  190.             
  191.             self.logfile.flush()
  192.         
  193.  
  194.     
  195.     def logstop(self):
  196.         self.logfile.close()
  197.         self.logfile = None
  198.         self.log_active = False
  199.  
  200.     close_log = logstop
  201.  
  202.