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

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  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.         if kind not in stepkinds:
  54.             raise ValueError, 'Template.append: bad kind %r' % (kind,)
  55.         if kind == SOURCE:
  56.             raise ValueError, 'Template.append: SOURCE can only be prepended'
  57.         if self.steps and self.steps[-1][1] == SINK:
  58.             raise ValueError, 'Template.append: already ends with SINK'
  59.         if kind[0] == 'f' and not re.search('\\$IN\\b', cmd):
  60.             raise ValueError, 'Template.append: missing $IN in cmd'
  61.         if kind[1] == 'f' and not re.search('\\$OUT\\b', cmd):
  62.             raise ValueError, 'Template.append: missing $OUT in cmd'
  63.         self.steps.append((cmd, kind))
  64.  
  65.     
  66.     def prepend(self, cmd, kind):
  67.         if type(cmd) is not type(''):
  68.             raise TypeError, 'Template.prepend: cmd must be a string'
  69.         if kind not in stepkinds:
  70.             raise ValueError, 'Template.prepend: bad kind %r' % (kind,)
  71.         if kind == SINK:
  72.             raise ValueError, 'Template.prepend: SINK can only be appended'
  73.         if self.steps and self.steps[0][1] == SOURCE:
  74.             raise ValueError, 'Template.prepend: already begins with SOURCE'
  75.         if kind[0] == 'f' and not re.search('\\$IN\\b', cmd):
  76.             raise ValueError, 'Template.prepend: missing $IN in cmd'
  77.         if kind[1] == 'f' and not re.search('\\$OUT\\b', cmd):
  78.             raise ValueError, 'Template.prepend: missing $OUT in cmd'
  79.         self.steps.insert(0, (cmd, kind))
  80.  
  81.     
  82.     def open(self, file, rw):
  83.         if rw == 'r':
  84.             return self.open_r(file)
  85.         if None == 'w':
  86.             return self.open_w(file)
  87.         raise None, "Template.open: rw must be 'r' or 'w', not %r" % (rw,)
  88.  
  89.     
  90.     def open_r(self, file):
  91.         if not self.steps:
  92.             return open(file, 'r')
  93.         if None.steps[-1][1] == SINK:
  94.             raise ValueError, 'Template.open_r: pipeline ends width SINK'
  95.         cmd = self.makepipeline(file, '')
  96.         return os.popen(cmd, 'r')
  97.  
  98.     
  99.     def open_w(self, file):
  100.         if not self.steps:
  101.             return open(file, 'w')
  102.         if None.steps[0][1] == SOURCE:
  103.             raise ValueError, 'Template.open_w: pipeline begins with SOURCE'
  104.         cmd = self.makepipeline('', file)
  105.         return os.popen(cmd, 'w')
  106.  
  107.     
  108.     def copy(self, infile, outfile):
  109.         return os.system(self.makepipeline(infile, outfile))
  110.  
  111.     
  112.     def makepipeline(self, infile, outfile):
  113.         cmd = makepipeline(infile, self.steps, outfile)
  114.         if self.debugging:
  115.             print cmd
  116.             cmd = 'set -x; ' + cmd
  117.         return cmd
  118.  
  119.  
  120.  
  121. def makepipeline(infile, steps, outfile):
  122.     list = []
  123.     for cmd, kind in steps:
  124.         list.append([
  125.             '',
  126.             cmd,
  127.             kind,
  128.             ''])
  129.     
  130.     if not list:
  131.         list.append([
  132.             '',
  133.             'cat',
  134.             '--',
  135.             ''])
  136.     (cmd, kind) = list[0][1:3]
  137.     if kind[0] == 'f' and not infile:
  138.         list.insert(0, [
  139.             '',
  140.             'cat',
  141.             '--',
  142.             ''])
  143.     list[0][0] = infile
  144.     (cmd, kind) = list[-1][1:3]
  145.     if kind[1] == 'f' and not outfile:
  146.         list.append([
  147.             '',
  148.             'cat',
  149.             '--',
  150.             ''])
  151.     list[-1][-1] = outfile
  152.     garbage = []
  153.     for i in range(1, len(list)):
  154.         lkind = list[i - 1][2]
  155.         rkind = list[i][2]
  156.         if not lkind[1] == 'f':
  157.             if rkind[0] == 'f':
  158.                 (fd, temp) = tempfile.mkstemp()
  159.                 os.close(fd)
  160.                 garbage.append(temp)
  161.                 list[i - 1][-1] = list[i][0] = temp
  162.                 continue
  163.             for item in list:
  164.                 (inf, cmd, kind, outf) = item
  165.                 if kind[1] == 'f':
  166.                     cmd = 'OUT=' + quote(outf) + '; ' + cmd
  167.                 if kind[0] == 'f':
  168.                     cmd = 'IN=' + quote(inf) + '; ' + cmd
  169.                 if kind[0] == '-' and inf:
  170.                     cmd = cmd + ' <' + quote(inf)
  171.                 if kind[1] == '-' and outf:
  172.                     cmd = cmd + ' >' + quote(outf)
  173.                 item[1] = cmd
  174.             
  175.     cmdlist = list[0][1]
  176.     for item in list[1:]:
  177.         (cmd, kind) = item[1:3]
  178.         if item[0] == '':
  179.             if 'f' in kind:
  180.                 cmd = '{ ' + cmd + '; }'
  181.             cmdlist = cmdlist + ' |\n' + cmd
  182.             continue
  183.         cmdlist = cmdlist + '\n' + cmd
  184.     
  185.     if garbage:
  186.         rmcmd = 'rm -f'
  187.         for file in garbage:
  188.             rmcmd = rmcmd + ' ' + quote(file)
  189.         
  190.         trapcmd = 'trap ' + quote(rmcmd + '; exit') + ' 1 2 3 13 14 15'
  191.         cmdlist = trapcmd + '\n' + cmdlist + '\n' + rmcmd
  192.     return cmdlist
  193.  
  194. _safechars = frozenset(string.ascii_letters + string.digits + '@%_-+=:,./')
  195.  
  196. def quote(file):
  197.     for c in file:
  198.         if c not in _safechars:
  199.             break
  200.             continue
  201.         if not file:
  202.             return "''"
  203.         return None
  204.         return "'" + file.replace("'", '\'"\'"\'') + "'"
  205.  
  206.