Plan 9 from Bell Labs’s /usr/web/sources/contrib/stallion/root/sys/lib/python2.7/pipes.pyo

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


�`^c@s�dZddlZddlZddlZddlZdgZdZdZdZdZ	dZ
d	Zeeee	e
egZddd
��YZ
d�Zeejejd�Zd
�ZdS(s�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()
i�NtTemplatetffs-fsf-s--s.-s-.cBszeZdZd�Zd�Zd�Zd�Zd�Zd�Zd�Z	d�Z
d	�Zd
�Zd�Z
d�ZRS(
s'Class representing a pipeline template.cCsd|_|j�dS(s-Template() returns a fresh pipeline template.iN(t	debuggingtreset(tself((s/sys/lib/python2.7/pipes.pyt__init__Ss	cCsd|jfS(s t.__repr__() implements repr(t).s<Template instance, steps=%r>(tsteps(R((s/sys/lib/python2.7/pipes.pyt__repr__XscCs
g|_dS(s<t.reset() restores a pipeline template to its initial state.N(R(R((s/sys/lib/python2.7/pipes.pyR\scCs&t�}|j|_|j|_|S(sbt.clone() returns a new pipeline template with identical
        initial state as the current one.(RRR(Rtt((s/sys/lib/python2.7/pipes.pytclone`s	
cCs
||_dS(s(t.debug(flag) turns debugging on or off.N(R(Rtflag((s/sys/lib/python2.7/pipes.pytdebughscCs�t|�td�k	r$td�n|tkrCtd|f�n|tkr[td�n|jr�|jddtkr�td�n|dd	kr�tjd
|�r�td�n|dd	kr�jd|�r�d
�n|jj	||f�dS(s/t.append(cmd, kind) adds a new step at the end.ts%Template.append: cmd must be a stringsTemplate.append: bad kind %rs-Template.append: SOURCE can only be prependedi�is'Template.append: already ends with SINKitfs\$IN\bs#Template.append: missing $IN in cmds\$OUT\bs$Template.append: missing $OUT in cmdN(
ttypet	TypeErrort	stepkindst
ValueErrortSOURCERtSINKtretsearchtappend(Rtcmdtkind((s/sys/lib/python2.7/pipes.pyRls&		 	#	#	cCst|�td�k	r$td�n|tkrCtd|f�n|tkr[td�n|jr�|jddtkr�td�n|ddkr�tjd	|�r�td
�n|ddkr�jd|�r�d�n|jj	d||f�d
S(s2t.prepend(cmd, kind) adds a new step at the front.Rs&Template.prepend: cmd must be a stringsTemplate.prepend: bad kind %rs+Template.prepend: SINK can only be appendediis,Template.prepend: already begins with SOURCER
s\$IN\bs$Template.prepend: missing $IN in cmds\$OUT\bs%Template.prepend: missing $OUT in cmdN(
RRRRRRRRRtinsert(RRR((s/sys/lib/python2.7/pipes.pytprepend�s&		 	#	#	cCsF|dkr|j|�S|dkr2|j|�Std|f�dS(s~t.open(file, rw) returns a pipe or file object open for
        reading or writing; the file is the other end of the pipeline.trtws,Template.open: rw must be 'r' or 'w', not %rN(topen_rtopen_wR(Rtfiletrw((s/sys/lib/python2.7/pipes.pytopen�s

cCs[|jst|d�S|jddtkr9td�n|j|d�}tj|d�S(sit.open_r(file) and t.open_w(file) implement
        t.open(file, 'r') and t.open(file, 'w') respectively.Ri�is)Template.open_r: pipeline ends width SINKR(RR!RRtmakepipelinetostpopen(RRR((s/sys/lib/python2.7/pipes.pyR�s	
	cCs[|jst|d�S|jddtkr9td�n|jd|�}tj|d�S(NRiis,Template.open_w: pipeline begins with SOURCER(RR!RRR"R#R$(RRR((s/sys/lib/python2.7/pipes.pyR�s	
	cCstj|j||��S(N(R#tsystemR"(Rtinfiletoutfile((s/sys/lib/python2.7/pipes.pytcopy�scCs4t||j|�}|jr0|GHd|}n|S(Nsset -x; (R"RR(RR&R'R((s/sys/lib/python2.7/pipes.pyR"�s
	
(t__name__t
__module__t__doc__RRRR	RRRR!RRR(R"(((s/sys/lib/python2.7/pipes.pyRPs								
				cCs�g}x-|D]%\}}|jd||dg�q
W|sX|jddddg�n|ddd!\}}|ddkr�|r�|jdddddg�n||dd<|ddd!\}}|ddkr�r�jddddg�n||dd<g}x�tdt|��D]�}||dd	}||d	}	|ddksm|	ddkr'tj�\}
}tj|
�|j|�|||dd<||d<q'q'Wx�|D]�}|\}
}}}|ddkrd
t|�d|}n|ddkr3dt|
�d|}n|dd
kr`|
r`|dt|
�}n|dd
kr�|r�|dt|�}n||d<q�W|dd}xq|dD]e}|dd!\}}|ddkrd|kr�|d}n|d|}q�|d|}q�W|rd}x"|D]}|dt|�}q0Wdt|d�d}|d|d|}n|S(NRtcats--iiiR
i�isOUT=s; sIN=t-s <s >s{ s; }s |
s
srm -ft strap s; exits 1 2 3 13 14 15(	RRtrangetlenttempfiletmkstempR#tclosetquote(R&RR'tlistRRtgarbagetitlkindtrkindtfdttemptitemtinftoutftcmdlisttrmcmdRttrapcmd((s/sys/lib/python2.7/pipes.pyR"�s` 

%

s
@%_-+=:,./cCsGx,|D]}|tkrPqqW|s+dS|Sd|jdd�dS(s2Return a shell-escaped version of the file string.s''t's'"'"'(t
_safecharstreplace(Rtc((s/sys/lib/python2.7/pipes.pyR4s
((R+RR#R1tstringt__all__tFILEIN_FILEOUTt
STDIN_FILEOUTt
FILEIN_STDOUTtSTDIN_STDOUTRRRRR"t	frozensett
ascii_letterstdigitsRCR4(((s/sys/lib/python2.7/pipes.pyt<module>9s"	q	H

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].