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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import traceback
  6. from codeop import CommandCompiler, compile_command
  7. __all__ = [
  8.     'InteractiveInterpreter',
  9.     'InteractiveConsole',
  10.     'interact',
  11.     'compile_command']
  12.  
  13. def softspace(file, newvalue):
  14.     oldvalue = 0
  15.     
  16.     try:
  17.         oldvalue = file.softspace
  18.     except AttributeError:
  19.         pass
  20.  
  21.     
  22.     try:
  23.         file.softspace = newvalue
  24.     except (AttributeError, TypeError):
  25.         pass
  26.  
  27.     return oldvalue
  28.  
  29.  
  30. class InteractiveInterpreter:
  31.     
  32.     def __init__(self, locals = None):
  33.         if locals is None:
  34.             locals = {
  35.                 '__name__': '__console__',
  36.                 '__doc__': None }
  37.         
  38.         self.locals = locals
  39.         self.compile = CommandCompiler()
  40.  
  41.     
  42.     def runsource(self, source, filename = '<input>', symbol = 'single'):
  43.         
  44.         try:
  45.             code = self.compile(source, filename, symbol)
  46.         except (OverflowError, SyntaxError, ValueError):
  47.             self.showsyntaxerror(filename)
  48.             return False
  49.  
  50.         if code is None:
  51.             return True
  52.         self.runcode(code)
  53.         return False
  54.  
  55.     
  56.     def runcode(self, code):
  57.         
  58.         try:
  59.             exec code in self.locals
  60.         except SystemExit:
  61.             raise 
  62.         except:
  63.             self.showtraceback()
  64.  
  65.         if softspace(sys.stdout, 0):
  66.             print 
  67.         
  68.  
  69.     
  70.     def showsyntaxerror(self, filename = None):
  71.         (type, value, sys.last_traceback) = sys.exc_info()
  72.         sys.last_type = type
  73.         sys.last_value = value
  74.         if filename and type is SyntaxError:
  75.             
  76.             try:
  77.                 (dummy_filename, lineno, offset, line) = (msg,)
  78.             except:
  79.                 pass
  80.  
  81.             value = SyntaxError(msg, (filename, lineno, offset, line))
  82.             sys.last_value = value
  83.         
  84.         list = traceback.format_exception_only(type, value)
  85.         map(self.write, list)
  86.  
  87.     
  88.     def showtraceback(self):
  89.         
  90.         try:
  91.             (type, value, tb) = sys.exc_info()
  92.             sys.last_type = type
  93.             sys.last_value = value
  94.             sys.last_traceback = tb
  95.             tblist = traceback.extract_tb(tb)
  96.             del tblist[:1]
  97.             list = traceback.format_list(tblist)
  98.             if list:
  99.                 list.insert(0, 'Traceback (most recent call last):\n')
  100.             
  101.             list[len(list):] = traceback.format_exception_only(type, value)
  102.         finally:
  103.             tblist = None
  104.             tb = None
  105.  
  106.         map(self.write, list)
  107.  
  108.     
  109.     def write(self, data):
  110.         sys.stderr.write(data)
  111.  
  112.  
  113.  
  114. class InteractiveConsole(InteractiveInterpreter):
  115.     
  116.     def __init__(self, locals = None, filename = '<console>'):
  117.         InteractiveInterpreter.__init__(self, locals)
  118.         self.filename = filename
  119.         self.resetbuffer()
  120.  
  121.     
  122.     def resetbuffer(self):
  123.         self.buffer = []
  124.  
  125.     
  126.     def interact(self, banner = None):
  127.         
  128.         try:
  129.             sys.ps1
  130.         except AttributeError:
  131.             sys.ps1 = '>>> '
  132.  
  133.         
  134.         try:
  135.             sys.ps2
  136.         except AttributeError:
  137.             sys.ps2 = '... '
  138.  
  139.         cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
  140.         if banner is None:
  141.             self.write('Python %s on %s\n%s\n(%s)\n' % (sys.version, sys.platform, cprt, self.__class__.__name__))
  142.         else:
  143.             self.write('%s\n' % str(banner))
  144.         more = 0
  145.         while None:
  146.             
  147.             try:
  148.                 if more:
  149.                     prompt = sys.ps2
  150.                 else:
  151.                     prompt = sys.ps1
  152.                 
  153.                 try:
  154.                     line = self.raw_input(prompt)
  155.                     encoding = getattr(sys.stdin, 'encoding', None)
  156.                     if encoding and not isinstance(line, unicode):
  157.                         line = line.decode(encoding)
  158.                 except EOFError:
  159.                     self.write('\n')
  160.                     break
  161.  
  162.                 more = self.push(line)
  163.             continue
  164.             except KeyboardInterrupt:
  165.                 self.write('\nKeyboardInterrupt\n')
  166.                 self.resetbuffer()
  167.                 more = 0
  168.                 continue
  169.             
  170.  
  171.             return None
  172.  
  173.     
  174.     def push(self, line):
  175.         self.buffer.append(line)
  176.         source = '\n'.join(self.buffer)
  177.         more = self.runsource(source, self.filename)
  178.         if not more:
  179.             self.resetbuffer()
  180.         
  181.         return more
  182.  
  183.     
  184.     def raw_input(self, prompt = ''):
  185.         return raw_input(prompt)
  186.  
  187.  
  188.  
  189. def interact(banner = None, readfunc = None, local = None):
  190.     console = InteractiveConsole(local)
  191.     if readfunc is not None:
  192.         console.raw_input = readfunc
  193.     else:
  194.         
  195.         try:
  196.             import readline
  197.         except ImportError:
  198.             pass
  199.  
  200.     console.interact(banner)
  201.  
  202. if __name__ == '__main__':
  203.     import pdb
  204.     pdb.run('interact()\n')
  205.  
  206.