blob: 7e1ff9e11959dbc5c58d71b10f2c8933bdb7a821 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`pipes` --- Interface to shell pipelines
2=============================================
3
4.. module:: pipes
5 :platform: Unix
6 :synopsis: A Python interface to Unix shell pipelines.
7.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
8
9
10The :mod:`pipes` module defines a class to abstract the concept of a *pipeline*
11--- a sequence of converters from one file to another.
12
13Because the module uses :program:`/bin/sh` command lines, a POSIX or compatible
14shell for :func:`os.system` and :func:`os.popen` is required.
15
16The :mod:`pipes` module defines the following class:
17
18
19.. class:: Template()
20
21 An abstraction of a pipeline.
22
23Example::
24
25 >>> import pipes
26 >>> t=pipes.Template()
27 >>> t.append('tr a-z A-Z', '--')
28 >>> f=t.open('/tmp/1', 'w')
29 >>> f.write('hello world')
30 >>> f.close()
31 >>> open('/tmp/1').read()
32 'HELLO WORLD'
33
34
35.. _template-objects:
36
37Template Objects
38----------------
39
40Template objects following methods:
41
42
43.. method:: Template.reset()
44
45 Restore a pipeline template to its initial state.
46
47
48.. method:: Template.clone()
49
50 Return a new, equivalent, pipeline template.
51
52
53.. method:: Template.debug(flag)
54
55 If *flag* is true, turn debugging on. Otherwise, turn debugging off. When
56 debugging is on, commands to be executed are printed, and the shell is given
57 ``set -x`` command to be more verbose.
58
59
60.. method:: Template.append(cmd, kind)
61
62 Append a new action at the end. The *cmd* variable must be a valid bourne shell
63 command. The *kind* variable consists of two letters.
64
65 The first letter can be either of ``'-'`` (which means the command reads its
66 standard input), ``'f'`` (which means the commands reads a given file on the
67 command line) or ``'.'`` (which means the commands reads no input, and hence
68 must be first.)
69
70 Similarly, the second letter can be either of ``'-'`` (which means the command
71 writes to standard output), ``'f'`` (which means the command writes a file on
72 the command line) or ``'.'`` (which means the command does not write anything,
73 and hence must be last.)
74
75
76.. method:: Template.prepend(cmd, kind)
77
78 Add a new action at the beginning. See :meth:`append` for explanations of the
79 arguments.
80
81
82.. method:: Template.open(file, mode)
83
84 Return a file-like object, open to *file*, but read from or written to by the
85 pipeline. Note that only one of ``'r'``, ``'w'`` may be given.
86
87
88.. method:: Template.copy(infile, outfile)
89
90 Copy *infile* to *outfile* through the pipe.
91