home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyc (Python 2.6) import glob import os import time class Logger(object): def __init__(self, shell, logfname = 'Logger.log', loghead = '', logmode = 'over'): (self._i00, self._i, self._ii, self._iii) = ('', '', '', '') self.shell = shell self.logfname = logfname self.loghead = loghead self.logmode = logmode self.logfile = None self.log_raw_input = False self.log_output = False self.timestamp = False self.log_active = False def _set_mode(self, mode): if mode not in ('append', 'backup', 'global', 'over', 'rotate'): raise ValueError, 'invalid log mode %s given' % mode mode not in ('append', 'backup', 'global', 'over', 'rotate') self._logmode = mode def _get_mode(self): return self._logmode logmode = property(_get_mode, _set_mode) def logstart(self, logfname = None, loghead = None, logmode = None, log_output = False, timestamp = False, log_raw_input = False): if self.logfile is not None: raise RuntimeError('Log file is already active: %s' % self.logfname) self.logfile is not None self.log_active = True if logfname is not None: self.logfname = logfname if loghead is not None: self.loghead = loghead if logmode is not None: self.logmode = logmode self.timestamp = timestamp self.log_output = log_output self.log_raw_input = log_raw_input isfile = os.path.isfile logmode = self.logmode if logmode == 'append': self.logfile = open(self.logfname, 'a') elif logmode == 'backup': if isfile(self.logfname): backup_logname = self.logfname + '~' if isfile(backup_logname): os.remove(backup_logname) os.rename(self.logfname, backup_logname) self.logfile = open(self.logfname, 'w') elif logmode == 'global': self.logfname = os.path.join(self.shell.home_dir, self.logfname) self.logfile = open(self.logfname, 'a') elif logmode == 'over': if isfile(self.logfname): os.remove(self.logfname) self.logfile = open(self.logfname, 'w') elif logmode == 'rotate': if isfile(self.logfname): if isfile(self.logfname + '.001~'): old = glob.glob(self.logfname + '.*~') old.sort() old.reverse() for f in old: (root, ext) = os.path.splitext(f) num = int(ext[1:-1]) + 1 os.rename(f, root + '.' + `num`.zfill(3) + '~') os.rename(self.logfname, self.logfname + '.001~') self.logfile = open(self.logfname, 'w') if logmode != 'append': self.logfile.write(self.loghead) self.logfile.flush() def switch_log(self, val): if val not in [ False, True, 0, 1]: raise ValueError, 'Call switch_log ONLY with a boolean argument, not with:', val val not in [ False, True, 0, 1] label = { 0: 'OFF', 1: 'ON', False: 'OFF', True: 'ON' } if self.logfile is None: 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)." elif self.log_active == val: print 'Logging is already', label[val] else: print 'Switching logging', label[val] self.log_active = not (self.log_active) self.log_active_out = self.log_active def logstate(self): if self.logfile is None: print 'Logging has not been activated.' elif not self.log_active or 'active': pass state = 'temporarily suspended' print 'Filename :', self.logfname print 'Mode :', self.logmode print 'Output logging :', self.log_output print 'Raw input log :', self.log_raw_input print 'Timestamping :', self.timestamp print 'State :', state def log(self, line_ori, line_mod, continuation = None): try: input_hist = self.shell.user_ns['_ih'] except: return None out_cache = self.shell.outputcache if out_cache.do_full_cache and out_cache.prompt_count + 1 > len(input_hist): input_hist.extend([ '\n'] * (out_cache.prompt_count - len(input_hist))) if not continuation and line_mod: self._iii = self._ii self._ii = self._i self._i = self._i00 self._i00 = line_mod + '\n' input_hist.append(self._i00) to_main = { '_i': self._i, '_ii': self._ii, '_iii': self._iii } if self.shell.outputcache.do_full_cache: in_num = self.shell.outputcache.prompt_count last_num = len(input_hist) - 1 if in_num != last_num: in_num = self.shell.outputcache.prompt_count = last_num new_i = '_i%s' % in_num if continuation: self._i00 = '%s%s\n' % (self.shell.user_ns[new_i], line_mod) input_hist[in_num] = self._i00 to_main[new_i] = self._i00 self.shell.user_ns.update(to_main) if self.log_raw_input: self.log_write(line_ori) else: self.log_write(line_mod) def log_write(self, data, kind = 'input'): if self.log_active and data: write = self.logfile.write if kind == 'input': if self.timestamp: write(time.strftime('# %a, %d %b %Y %H:%M:%S\n', time.localtime())) write('%s\n' % data) elif kind == 'output' and self.log_output: odata = []([ '#[Out]# %s' % s for s in data.split('\n') ]) write('%s\n' % odata) self.logfile.flush() def logstop(self): self.logfile.close() self.logfile = None self.log_active = False close_log = logstop