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