Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
pipes Namespace Reference

Data Structures

class  Template
 

Functions

def makepipeline
 
def quote
 
def test
 

Variables

list __all__ = ["Template"]
 
string FILEIN_FILEOUT = 'ff'
 
string STDIN_FILEOUT = '-f'
 
string FILEIN_STDOUT = 'f-'
 
string STDIN_STDOUT = '--'
 
string SOURCE = '.-'
 
string SINK = '-.'
 
list stepkinds
 
string _safechars = string.ascii_letters+string.digits+'!@%_-+=:,./'
 
string _funnychars = '"`$\\'
 

Detailed Description

Conversion pipeline templates.

The problem:
------------

Suppose you have some data that you want to convert to another format,
such as from GIF image format to PPM image format.  Maybe the
conversion involves several steps (e.g. piping it through compress or
uuencode).  Some of the conversion steps may require that their input
is a disk file, others may be able to read standard input; similar for
their output.  The input to the entire conversion may also be read
from a disk file or from an open file, and similar for its output.

The module lets you construct a pipeline template by sticking one or
more conversion steps together.  It will take care of creating and
removing temporary files if they are necessary to hold intermediate
data.  You can then use the template to do conversions from many
different sources to many different destinations.  The temporary
file names used are different each time the template is used.

The templates are objects so you can create templates for many
different conversion steps and store them in a dictionary, for
instance.


Directions:
-----------

To create a template:
t = Template()

To add a conversion step to a template:
   t.append(command, kind)
where kind is a string of two characters: the first is '-' if the
command reads its standard input or 'f' if it requires a file; the
second likewise for the output. The command must be valid /bin/sh
syntax.  If input or output files are required, they are passed as
$IN and $OUT; otherwise, it must be  possible to use the command in
a pipeline.

To add a conversion step at the beginning:
   t.prepend(command, kind)

To convert a file to another file using a template:
  sts = t.copy(infile, outfile)
If infile or outfile are the empty string, standard input is read or
standard output is written, respectively.  The return value is the
exit status of the conversion pipeline.

To open a file for reading or writing through a conversion pipeline:
   fp = t.open(file, mode)
where mode is 'r' to read the file, or 'w' to write it -- just like
for the built-in function open() or for os.popen().

To create a new template object initialized to a given one:
   t2 = t.clone()

For an example, see the function test() at the end of the file.

Function Documentation

def pipes.makepipeline (   infile,
  steps,
  outfile 
)

Definition at line 196 of file pipes.py.

References tempfile.mktemp(), and quote().

197 def makepipeline(infile, steps, outfile):
198  # Build a list with for each command:
199  # [input filename or '', command string, kind, output filename or '']
200 
201  list = []
202  for cmd, kind in steps:
203  list.append(['', cmd, kind, ''])
204  #
205  # Make sure there is at least one step
206  #
207  if not list:
208  list.append(['', 'cat', '--', ''])
209  #
210  # Take care of the input and output ends
211  #
212  [cmd, kind] = list[0][1:3]
213  if kind[0] == 'f' and not infile:
214  list.insert(0, ['', 'cat', '--', ''])
215  list[0][0] = infile
216  #
217  [cmd, kind] = list[-1][1:3]
218  if kind[1] == 'f' and not outfile:
219  list.append(['', 'cat', '--', ''])
220  list[-1][-1] = outfile
221  #
222  # Invent temporary files to connect stages that need files
223  #
224  garbage = []
225  for i in range(1, len(list)):
226  lkind = list[i-1][2]
227  rkind = list[i][2]
228  if lkind[1] == 'f' or rkind[0] == 'f':
229  temp = tempfile.mktemp()
230  garbage.append(temp)
231  list[i-1][-1] = list[i][0] = temp
232  #
233  for item in list:
234  [inf, cmd, kind, outf] = item
235  if kind[1] == 'f':
236  cmd = 'OUT=' + quote(outf) + '; ' + cmd
237  if kind[0] == 'f':
238  cmd = 'IN=' + quote(inf) + '; ' + cmd
239  if kind[0] == '-' and inf:
240  cmd = cmd + ' <' + quote(inf)
241  if kind[1] == '-' and outf:
242  cmd = cmd + ' >' + quote(outf)
243  item[1] = cmd
244  #
245  cmdlist = list[0][1]
246  for item in list[1:]:
247  [cmd, kind] = item[1:3]
248  if item[0] == '':
249  if 'f' in kind:
250  cmd = '{ ' + cmd + '; }'
251  cmdlist = cmdlist + ' |\n' + cmd
252  else:
253  cmdlist = cmdlist + '\n' + cmd
254  #
255  if garbage:
256  rmcmd = 'rm -f'
257  for file in garbage:
258  rmcmd = rmcmd + ' ' + quote(file)
259  trapcmd = 'trap ' + quote(rmcmd + '; exit') + ' 1 2 3 13 14 15'
260  cmdlist = trapcmd + '\n' + cmdlist + '\n' + rmcmd
261  #
262  return cmdlist
263 
264 
265 # Reliably quote a string as a single argument for /bin/sh
def pipes.quote (   file)

Definition at line 269 of file pipes.py.

270 def quote(file):
271  for c in file:
272  if c not in _safechars:
273  break
274  else:
275  return file
276  if '\'' not in file:
277  return '\'' + file + '\''
278  res = ''
279  for c in file:
280  if c in _funnychars:
281  c = '\\' + c
282  res = res + c
283  return '"' + res + '"'
284 
285 
286 # Small test program and example
def pipes.test ( )

Definition at line 287 of file pipes.py.

288 def test():
289  print 'Testing...'
290  t = Template()
291  t.append('togif $IN $OUT', 'ff')
292  t.append('giftoppm', '--')
293  t.append('ppmtogif >$OUT', '-f')
294  t.append('fromgif $IN $OUT', 'ff')
295  t.debug(1)
296  FILE = '/usr/local/images/rgb/rogues/guido.rgb'
297  t.copy(FILE, '@temp')
298  print 'Done.'

Variable Documentation

list __all__ = ["Template"]

Definition at line 68 of file pipes.py.

string _funnychars = '"`$\\'

Definition at line 267 of file pipes.py.

string _safechars = string.ascii_letters+string.digits+'!@%_-+=:,./'

Definition at line 266 of file pipes.py.

string FILEIN_FILEOUT = 'ff'

Definition at line 72 of file pipes.py.

string FILEIN_STDOUT = 'f-'

Definition at line 74 of file pipes.py.

string SINK = '-.'

Definition at line 77 of file pipes.py.

string SOURCE = '.-'

Definition at line 76 of file pipes.py.

string STDIN_FILEOUT = '-f'

Definition at line 73 of file pipes.py.

string STDIN_STDOUT = '--'

Definition at line 75 of file pipes.py.

list stepkinds
Initial value:
1 = [FILEIN_FILEOUT, STDIN_FILEOUT, FILEIN_STDOUT, STDIN_STDOUT, \
2  SOURCE, SINK]

Definition at line 79 of file pipes.py.