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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import optparse
  5. import os
  6. import re
  7. import sys
  8. import tempfile
  9. from IPython import irunner
  10. from IPython.genutils import fatal
  11.  
  12. class IndentOut(object):
  13.     
  14.     def __init__(self, out = sys.stdout, indent = 4):
  15.         self.indent_text = ' ' * indent
  16.         self.indent = re.compile('^', re.MULTILINE).sub
  17.         self.out = out
  18.         self._write = out.write
  19.         self.buffer = []
  20.         self._closed = False
  21.  
  22.     
  23.     def write(self, data):
  24.         if self._closed:
  25.             raise ValueError('I/O operation on closed file')
  26.         self._closed
  27.         self.buffer.append(data)
  28.  
  29.     
  30.     def flush(self):
  31.         if self.buffer:
  32.             data = ''.join(self.buffer)
  33.             self.buffer[:] = []
  34.             self._write(self.indent(self.indent_text, data))
  35.         
  36.  
  37.     
  38.     def close(self):
  39.         self.flush()
  40.         self._closed = True
  41.  
  42.  
  43.  
  44. class RunnerFactory(object):
  45.     
  46.     def __init__(self, out = sys.stdout):
  47.         self.out = out
  48.         self.runner = None
  49.         self.runnerClass = None
  50.  
  51.     
  52.     def _makeRunner(self, runnerClass):
  53.         self.runnerClass = runnerClass
  54.         self.runner = runnerClass(out = self.out)
  55.         return self.runner
  56.  
  57.     
  58.     def __call__(self, fname):
  59.         if fname.endswith('.py'):
  60.             runnerClass = irunner.PythonRunner
  61.         elif fname.endswith('.ipy'):
  62.             runnerClass = irunner.IPythonRunner
  63.         else:
  64.             raise ValueError('Unknown file type for Runner: %r' % fname)
  65.         if fname.endswith('.py').runner is None:
  66.             return self._makeRunner(runnerClass)
  67.         if runnerClass == self.runnerClass:
  68.             return self.runner
  69.         e = 'A runner of type %r can not run file %r' % (self.runnerClass, fname)
  70.         raise ValueError(e)
  71.  
  72.  
  73. TPL = '\n=========================\n Auto-generated doctests\n=========================\n\nThis file was auto-generated by IPython in its entirety.  If you need finer\ncontrol over the contents, simply make a manual template.  See the\nmkdoctests.py script for details.\n\n%%run %s\n'
  74.  
  75. def main():
  76.     parser = optparse.OptionParser(usage = __doc__)
  77.     newopt = parser.add_option
  78.     newopt('-f', '--force', action = 'store_true', dest = 'force', default = False, help = 'Force overwriting of the output file.')
  79.     newopt('-s', '--stdout', action = 'store_true', dest = 'stdout', default = False, help = 'Use stdout instead of a file for output.')
  80.     (opts, args) = parser.parse_args()
  81.     if len(args) < 1:
  82.         parser.error('incorrect number of arguments')
  83.     
  84.     fname = args[0]
  85.     auto_gen_output = False
  86.     
  87.     try:
  88.         outfname = args[1]
  89.     except IndexError:
  90.         outfname = None
  91.  
  92.     if fname.endswith('.tpl.txt') and outfname is None:
  93.         outfname = fname.replace('.tpl.txt', '.txt')
  94.     else:
  95.         (bname, ext) = os.path.splitext(fname)
  96.         if ext in ('.py', '.ipy'):
  97.             auto_gen_output = True
  98.         
  99.         if outfname is None:
  100.             outfname = bname + '.txt'
  101.         
  102.     if auto_gen_output:
  103.         infile = tempfile.TemporaryFile()
  104.         infile.write(TPL % fname)
  105.         infile.flush()
  106.         infile.seek(0)
  107.     else:
  108.         infile = open(fname)
  109.     if opts.stdout:
  110.         outfile = sys.stdout
  111.     elif os.path.isfile(outfname) and not (opts.force):
  112.         fatal('Output file %r exists, use --force (-f) to overwrite.' % outfname)
  113.     
  114.     outfile = open(outfname, 'w')
  115.     indentOut = IndentOut(outfile, 4)
  116.     getRunner = RunnerFactory(indentOut)
  117.     rst_transition = '\n' + '-' * 76 + '\n\n'
  118.     write = outfile.write
  119.     for line in infile:
  120.         if line.startswith('%run '):
  121.             incfname = line.split()[1]
  122.             write(rst_transition)
  123.             write('Begin included file %s::\n\n' % incfname)
  124.             getRunner(incfname).run_file(incfname)
  125.             write('\nEnd included file %s\n' % incfname)
  126.             write(rst_transition)
  127.             continue
  128.         write(line)
  129.     
  130.     infile.close()
  131.     if outfile is not sys.stdout:
  132.         outfile.close()
  133.     
  134.  
  135. if __name__ == '__main__':
  136.     main()
  137.  
  138.