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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. import os
  6. import tempfile
  7. import string
  8. __all__ = [
  9.     'Template']
  10. FILEIN_FILEOUT = 'ff'
  11. STDIN_FILEOUT = '-f'
  12. FILEIN_STDOUT = 'f-'
  13. STDIN_STDOUT = '--'
  14. SOURCE = '.-'
  15. SINK = '-.'
  16. stepkinds = [
  17.     FILEIN_FILEOUT,
  18.     STDIN_FILEOUT,
  19.     FILEIN_STDOUT,
  20.     STDIN_STDOUT,
  21.     SOURCE,
  22.     SINK]
  23.  
  24. class Template:
  25.     
  26.     def __init__(self):
  27.         self.debugging = 0
  28.         self.reset()
  29.  
  30.     
  31.     def __repr__(self):
  32.         return '<Template instance, steps=%r>' % (self.steps,)
  33.  
  34.     
  35.     def reset(self):
  36.         self.steps = []
  37.  
  38.     
  39.     def clone(self):
  40.         t = Template()
  41.         t.steps = self.steps[:]
  42.         t.debugging = self.debugging
  43.         return t
  44.  
  45.     
  46.     def debug(self, flag):
  47.         self.debugging = flag
  48.  
  49.     
  50.     def append(self, cmd, kind):
  51.         if type(cmd) is not type(''):
  52.             raise TypeError, 'Template.append: cmd must be a string'
  53.         type(cmd) is not type('')
  54.         if kind not in stepkinds:
  55.             raise ValueError, 'Template.append: bad kind %r' % (kind,)
  56.         kind not in stepkinds
  57.         if kind == SOURCE:
  58.             raise ValueError, 'Template.append: SOURCE can only be prepended'
  59.         kind == SOURCE
  60.         if self.steps and self.steps[-1][1] == SINK:
  61.             raise ValueError, 'Template.append: already ends with SINK'
  62.         self.steps[-1][1] == SINK
  63.         if kind[0] == 'f' and not re.search('\\$IN\\b', cmd):
  64.             raise ValueError, 'Template.append: missing $IN in cmd'
  65.         not re.search('\\$IN\\b', cmd)
  66.         if kind[1] == 'f' and not re.search('\\$OUT\\b', cmd):
  67.             raise ValueError, 'Template.append: missing $OUT in cmd'
  68.         not re.search('\\$OUT\\b', cmd)
  69.         self.steps.append((cmd, kind))
  70.  
  71.     
  72.     def prepend(self, cmd, kind):
  73.         if type(cmd) is not type(''):
  74.             raise TypeError, 'Template.prepend: cmd must be a string'
  75.         type(cmd) is not type('')
  76.         if kind not in stepkinds:
  77.             raise ValueError, 'Template.prepend: bad kind %r' % (kind,)
  78.         kind not in stepkinds
  79.         if kind == SINK:
  80.             raise ValueError, 'Template.prepend: SINK can only be appended'
  81.         kind == SINK
  82.         if self.steps and self.steps[0][1] == SOURCE:
  83.             raise ValueError, 'Template.prepend: already begins with SOURCE'
  84.         self.steps[0][1] == SOURCE
  85.         if kind[0] == 'f' and not re.search('\\$IN\\b', cmd):
  86.             raise ValueError, 'Template.prepend: missing $IN in cmd'
  87.         not re.search('\\$IN\\b', cmd)
  88.         if kind[1] == 'f' and not re.search('\\$OUT\\b', cmd):
  89.             raise ValueError, 'Template.prepend: missing $OUT in cmd'
  90.         not re.search('\\$OUT\\b', cmd)
  91.         self.steps.insert(0, (cmd, kind))
  92.  
  93.     
  94.     def open(self, file, rw):
  95.         if rw == 'r':
  96.             return self.open_r(file)
  97.         if rw == 'w':
  98.             return self.open_w(file)
  99.         raise ValueError, "Template.open: rw must be 'r' or 'w', not %r" % (rw,)
  100.  
  101.     
  102.     def open_r(self, file):
  103.         if not self.steps:
  104.             return open(file, 'r')
  105.         if self.steps[-1][1] == SINK:
  106.             raise ValueError, 'Template.open_r: pipeline ends width SINK'
  107.         self.steps[-1][1] == SINK
  108.         cmd = self.makepipeline(file, '')
  109.         return os.popen(cmd, 'r')
  110.  
  111.     
  112.     def open_w(self, file):
  113.         if not self.steps:
  114.             return open(file, 'w')
  115.         if self.steps[0][1] == SOURCE:
  116.             raise ValueError, 'Template.open_w: pipeline begins with SOURCE'
  117.         self.steps[0][1] == SOURCE
  118.         cmd = self.makepipeline('', file)
  119.         return os.popen(cmd, 'w')
  120.  
  121.     
  122.     def copy(self, infile, outfile):
  123.         return os.system(self.makepipeline(infile, outfile))
  124.  
  125.     
  126.     def makepipeline(self, infile, outfile):
  127.         cmd = makepipeline(infile, self.steps, outfile)
  128.         if self.debugging:
  129.             print cmd
  130.             cmd = 'set -x; ' + cmd
  131.         
  132.         return cmd
  133.  
  134.  
  135.  
  136. def makepipeline(infile, steps, outfile):
  137.     list = []
  138.     for cmd, kind in steps:
  139.         list.append([
  140.             '',
  141.             cmd,
  142.             kind,
  143.             ''])
  144.     
  145.     if not list:
  146.         list.append([
  147.             '',
  148.             'cat',
  149.             '--',
  150.             ''])
  151.     
  152.     (cmd, kind) = list[0][1:3]
  153.     if kind[0] == 'f' and not infile:
  154.         list.insert(0, [
  155.             '',
  156.             'cat',
  157.             '--',
  158.             ''])
  159.     
  160.     list[0][0] = infile
  161.     (cmd, kind) = list[-1][1:3]
  162.     if kind[1] == 'f' and not outfile:
  163.         list.append([
  164.             '',
  165.             'cat',
  166.             '--',
  167.             ''])
  168.     
  169.     list[-1][-1] = outfile
  170.     garbage = []
  171.     for i in range(1, len(list)):
  172.         lkind = list[i - 1][2]
  173.         rkind = list[i][2]
  174.         if lkind[1] == 'f' or rkind[0] == 'f':
  175.             (fd, temp) = tempfile.mkstemp()
  176.             os.close(fd)
  177.             garbage.append(temp)
  178.             list[i - 1][-1] = list[i][0] = temp
  179.             continue
  180.     
  181.     for item in list:
  182.         (inf, cmd, kind, outf) = item
  183.         if kind[1] == 'f':
  184.             cmd = 'OUT=' + quote(outf) + '; ' + cmd
  185.         
  186.         if kind[0] == 'f':
  187.             cmd = 'IN=' + quote(inf) + '; ' + cmd
  188.         
  189.         if kind[0] == '-' and inf:
  190.             cmd = cmd + ' <' + quote(inf)
  191.         
  192.         if kind[1] == '-' and outf:
  193.             cmd = cmd + ' >' + quote(outf)
  194.         
  195.         item[1] = cmd
  196.     
  197.     cmdlist = list[0][1]
  198.     for item in list[1:]:
  199.         (cmd, kind) = item[1:3]
  200.         if item[0] == '':
  201.             if 'f' in kind:
  202.                 cmd = '{ ' + cmd + '; }'
  203.             
  204.             cmdlist = cmdlist + ' |\n' + cmd
  205.             continue
  206.         cmdlist = cmdlist + '\n' + cmd
  207.     
  208.     if garbage:
  209.         rmcmd = 'rm -f'
  210.         for file in garbage:
  211.             rmcmd = rmcmd + ' ' + quote(file)
  212.         
  213.         trapcmd = 'trap ' + quote(rmcmd + '; exit') + ' 1 2 3 13 14 15'
  214.         cmdlist = trapcmd + '\n' + cmdlist + '\n' + rmcmd
  215.     
  216.     return cmdlist
  217.  
  218. _safechars = string.ascii_letters + string.digits + '!@%_-+=:,./'
  219. _funnychars = '"`$\\'
  220.  
  221. def quote(file):
  222.     for c in file:
  223.         if c not in _safechars:
  224.             break
  225.             continue
  226.     else:
  227.         return file
  228.     if None not in file:
  229.         return "'" + file + "'"
  230.     res = ''
  231.     for c in file:
  232.         if c in _funnychars:
  233.             c = '\\' + c
  234.         
  235.         res = res + c
  236.     
  237.     return '"' + res + '"'
  238.  
  239.