home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / code.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  4.9 KB  |  199 lines

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