2 :mod:`pipes` --- Interface to shell pipelines
3 =============================================
7 :synopsis: A Python interface to Unix shell pipelines.
8 .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
11 The :mod:`pipes` module defines a class to abstract the concept of a *pipeline*
12 --- a sequence of converters from one file to another.
14 Because the module uses :program:`/bin/sh` command lines, a POSIX or compatible
15 shell for :func:`os.system` and :func:`os.popen` is required.
17 The :mod:`pipes` module defines the following class:
22 An abstraction of a pipeline.
27 >>> t=pipes.Template()
28 >>> t.append('tr a-z A-Z', '--')
29 >>> f=t.open('/tmp/1', 'w')
30 >>> f.write('hello world')
32 >>> open('/tmp/1').read()
41 Template objects following methods:
44 .. method:: Template.reset()
46 Restore a pipeline template to its initial state.
49 .. method:: Template.clone()
51 Return a new, equivalent, pipeline template.
54 .. method:: Template.debug(flag)
56 If *flag* is true, turn debugging on. Otherwise, turn debugging off. When
57 debugging is on, commands to be executed are printed, and the shell is given
58 ``set -x`` command to be more verbose.
61 .. method:: Template.append(cmd, kind)
63 Append a new action at the end. The *cmd* variable must be a valid bourne shell
64 command. The *kind* variable consists of two letters.
66 The first letter can be either of ``'-'`` (which means the command reads its
67 standard input), ``'f'`` (which means the commands reads a given file on the
68 command line) or ``'.'`` (which means the commands reads no input, and hence
71 Similarly, the second letter can be either of ``'-'`` (which means the command
72 writes to standard output), ``'f'`` (which means the command writes a file on
73 the command line) or ``'.'`` (which means the command does not write anything,
74 and hence must be last.)
77 .. method:: Template.prepend(cmd, kind)
79 Add a new action at the beginning. See :meth:`append` for explanations of the
83 .. method:: Template.open(file, mode)
85 Return a file-like object, open to *file*, but read from or written to by the
86 pipeline. Note that only one of ``'r'``, ``'w'`` may be given.
89 .. method:: Template.copy(infile, outfile)
91 Copy *infile* to *outfile* through the pipe.