blob: de25fb56969f4e87cdff92879dce21bfbd60f31d [file] [log] [blame]
Fred Drake4755e7d1999-06-21 18:25:49 +00001\section{\module{pipes} ---
2 Interface to shell pipelines}
3
4\declaremodule{standard}{pipes}
5 \platform{Unix}
Fred Drake57657bc2000-12-01 15:25:23 +00006\sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il}
Fred Drakec116b822001-05-09 15:50:17 +00007\modulesynopsis{A Python interface to \UNIX\ shell pipelines.}
Fred Drake4755e7d1999-06-21 18:25:49 +00008
9
10The \module{pipes} module defines a class to abstract the concept of
Georg Brandlcaa94bd2006-01-23 22:00:17 +000011a \emph{pipeline} --- a sequence of converters from one file to
Fred Drake4755e7d1999-06-21 18:25:49 +000012another.
13
14Because the module uses \program{/bin/sh} command lines, a \POSIX{} or
15compatible shell for \function{os.system()} and \function{os.popen()}
16is required.
17
18The \module{pipes} module defines the following class:
19
20\begin{classdesc}{Template}{}
21An abstraction of a pipeline.
Fred Draked5d55ea1999-06-21 18:36:09 +000022\end{classdesc}
Fred Drake4755e7d1999-06-21 18:25:49 +000023
24Example:
25
26\begin{verbatim}
27>>> import pipes
28>>> t=pipes.Template()
29>>> t.append('tr a-z A-Z', '--')
30>>> f=t.open('/tmp/1', 'w')
31>>> f.write('hello world')
32>>> f.close()
33>>> open('/tmp/1').read()
34'HELLO WORLD'
35\end{verbatim}
36
37
38\subsection{Template Objects \label{template-objects}}
39
40Template objects following methods:
41
Guido van Rossumd8faa362007-04-27 19:54:29 +000042\begin{methoddesc}[Template]{reset}{}
Fred Drake4755e7d1999-06-21 18:25:49 +000043Restore a pipeline template to its initial state.
44\end{methoddesc}
45
Guido van Rossumd8faa362007-04-27 19:54:29 +000046\begin{methoddesc}[Template]{clone}{}
Fred Drake4755e7d1999-06-21 18:25:49 +000047Return a new, equivalent, pipeline template.
48\end{methoddesc}
49
Guido van Rossumd8faa362007-04-27 19:54:29 +000050\begin{methoddesc}[Template]{debug}{flag}
Fred Drake4755e7d1999-06-21 18:25:49 +000051If \var{flag} is true, turn debugging on. Otherwise, turn debugging
52off. When debugging is on, commands to be executed are printed, and
53the shell is given \code{set -x} command to be more verbose.
54\end{methoddesc}
55
Guido van Rossumd8faa362007-04-27 19:54:29 +000056\begin{methoddesc}[Template]{append}{cmd, kind}
Fred Drake4755e7d1999-06-21 18:25:49 +000057Append a new action at the end. The \var{cmd} variable must be a valid
58bourne shell command. The \var{kind} variable consists of two letters.
59
60The first letter can be either of \code{'-'} (which means the command
61reads its standard input), \code{'f'} (which means the commands reads
62a given file on the command line) or \code{'.'} (which means the commands
63reads no input, and hence must be first.)
64
Thomas Woutersf8316632000-07-16 19:01:10 +000065Similarly, the second letter can be either of \code{'-'} (which means
Fred Drake4755e7d1999-06-21 18:25:49 +000066the command writes to standard output), \code{'f'} (which means the
67command writes a file on the command line) or \code{'.'} (which means
68the command does not write anything, and hence must be last.)
69\end{methoddesc}
70
Guido van Rossumd8faa362007-04-27 19:54:29 +000071\begin{methoddesc}[Template]{prepend}{cmd, kind}
Fred Drake4755e7d1999-06-21 18:25:49 +000072Add a new action at the beginning. See \method{append()} for explanations
73of the arguments.
74\end{methoddesc}
75
Guido van Rossumd8faa362007-04-27 19:54:29 +000076\begin{methoddesc}[Template]{open}{file, mode}
Fred Drake4755e7d1999-06-21 18:25:49 +000077Return a file-like object, open to \var{file}, but read from or
78written to by the pipeline. Note that only one of \code{'r'},
79\code{'w'} may be given.
80\end{methoddesc}
81
Guido van Rossumd8faa362007-04-27 19:54:29 +000082\begin{methoddesc}[Template]{copy}{infile, outfile}
Fred Drake4755e7d1999-06-21 18:25:49 +000083Copy \var{infile} to \var{outfile} through the pipe.
84\end{methoddesc}