Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3....
File: pipes.py
"""Conversion pipeline templates.
[0] Fix | Delete
[1] Fix | Delete
The problem:
[2] Fix | Delete
------------
[3] Fix | Delete
[4] Fix | Delete
Suppose you have some data that you want to convert to another format,
[5] Fix | Delete
such as from GIF image format to PPM image format. Maybe the
[6] Fix | Delete
conversion involves several steps (e.g. piping it through compress or
[7] Fix | Delete
uuencode). Some of the conversion steps may require that their input
[8] Fix | Delete
is a disk file, others may be able to read standard input; similar for
[9] Fix | Delete
their output. The input to the entire conversion may also be read
[10] Fix | Delete
from a disk file or from an open file, and similar for its output.
[11] Fix | Delete
[12] Fix | Delete
The module lets you construct a pipeline template by sticking one or
[13] Fix | Delete
more conversion steps together. It will take care of creating and
[14] Fix | Delete
removing temporary files if they are necessary to hold intermediate
[15] Fix | Delete
data. You can then use the template to do conversions from many
[16] Fix | Delete
different sources to many different destinations. The temporary
[17] Fix | Delete
file names used are different each time the template is used.
[18] Fix | Delete
[19] Fix | Delete
The templates are objects so you can create templates for many
[20] Fix | Delete
different conversion steps and store them in a dictionary, for
[21] Fix | Delete
instance.
[22] Fix | Delete
[23] Fix | Delete
[24] Fix | Delete
Directions:
[25] Fix | Delete
-----------
[26] Fix | Delete
[27] Fix | Delete
To create a template:
[28] Fix | Delete
t = Template()
[29] Fix | Delete
[30] Fix | Delete
To add a conversion step to a template:
[31] Fix | Delete
t.append(command, kind)
[32] Fix | Delete
where kind is a string of two characters: the first is '-' if the
[33] Fix | Delete
command reads its standard input or 'f' if it requires a file; the
[34] Fix | Delete
second likewise for the output. The command must be valid /bin/sh
[35] Fix | Delete
syntax. If input or output files are required, they are passed as
[36] Fix | Delete
$IN and $OUT; otherwise, it must be possible to use the command in
[37] Fix | Delete
a pipeline.
[38] Fix | Delete
[39] Fix | Delete
To add a conversion step at the beginning:
[40] Fix | Delete
t.prepend(command, kind)
[41] Fix | Delete
[42] Fix | Delete
To convert a file to another file using a template:
[43] Fix | Delete
sts = t.copy(infile, outfile)
[44] Fix | Delete
If infile or outfile are the empty string, standard input is read or
[45] Fix | Delete
standard output is written, respectively. The return value is the
[46] Fix | Delete
exit status of the conversion pipeline.
[47] Fix | Delete
[48] Fix | Delete
To open a file for reading or writing through a conversion pipeline:
[49] Fix | Delete
fp = t.open(file, mode)
[50] Fix | Delete
where mode is 'r' to read the file, or 'w' to write it -- just like
[51] Fix | Delete
for the built-in function open() or for os.popen().
[52] Fix | Delete
[53] Fix | Delete
To create a new template object initialized to a given one:
[54] Fix | Delete
t2 = t.clone()
[55] Fix | Delete
""" # '
[56] Fix | Delete
[57] Fix | Delete
[58] Fix | Delete
import re
[59] Fix | Delete
import os
[60] Fix | Delete
import tempfile
[61] Fix | Delete
# we import the quote function rather than the module for backward compat
[62] Fix | Delete
# (quote used to be an undocumented but used function in pipes)
[63] Fix | Delete
from shlex import quote
[64] Fix | Delete
[65] Fix | Delete
__all__ = ["Template"]
[66] Fix | Delete
[67] Fix | Delete
# Conversion step kinds
[68] Fix | Delete
[69] Fix | Delete
FILEIN_FILEOUT = 'ff' # Must read & write real files
[70] Fix | Delete
STDIN_FILEOUT = '-f' # Must write a real file
[71] Fix | Delete
FILEIN_STDOUT = 'f-' # Must read a real file
[72] Fix | Delete
STDIN_STDOUT = '--' # Normal pipeline element
[73] Fix | Delete
SOURCE = '.-' # Must be first, writes stdout
[74] Fix | Delete
SINK = '-.' # Must be last, reads stdin
[75] Fix | Delete
[76] Fix | Delete
stepkinds = [FILEIN_FILEOUT, STDIN_FILEOUT, FILEIN_STDOUT, STDIN_STDOUT, \
[77] Fix | Delete
SOURCE, SINK]
[78] Fix | Delete
[79] Fix | Delete
[80] Fix | Delete
class Template:
[81] Fix | Delete
"""Class representing a pipeline template."""
[82] Fix | Delete
[83] Fix | Delete
def __init__(self):
[84] Fix | Delete
"""Template() returns a fresh pipeline template."""
[85] Fix | Delete
self.debugging = 0
[86] Fix | Delete
self.reset()
[87] Fix | Delete
[88] Fix | Delete
def __repr__(self):
[89] Fix | Delete
"""t.__repr__() implements repr(t)."""
[90] Fix | Delete
return '<Template instance, steps=%r>' % (self.steps,)
[91] Fix | Delete
[92] Fix | Delete
def reset(self):
[93] Fix | Delete
"""t.reset() restores a pipeline template to its initial state."""
[94] Fix | Delete
self.steps = []
[95] Fix | Delete
[96] Fix | Delete
def clone(self):
[97] Fix | Delete
"""t.clone() returns a new pipeline template with identical
[98] Fix | Delete
initial state as the current one."""
[99] Fix | Delete
t = Template()
[100] Fix | Delete
t.steps = self.steps[:]
[101] Fix | Delete
t.debugging = self.debugging
[102] Fix | Delete
return t
[103] Fix | Delete
[104] Fix | Delete
def debug(self, flag):
[105] Fix | Delete
"""t.debug(flag) turns debugging on or off."""
[106] Fix | Delete
self.debugging = flag
[107] Fix | Delete
[108] Fix | Delete
def append(self, cmd, kind):
[109] Fix | Delete
"""t.append(cmd, kind) adds a new step at the end."""
[110] Fix | Delete
if type(cmd) is not type(''):
[111] Fix | Delete
raise TypeError('Template.append: cmd must be a string')
[112] Fix | Delete
if kind not in stepkinds:
[113] Fix | Delete
raise ValueError('Template.append: bad kind %r' % (kind,))
[114] Fix | Delete
if kind == SOURCE:
[115] Fix | Delete
raise ValueError('Template.append: SOURCE can only be prepended')
[116] Fix | Delete
if self.steps and self.steps[-1][1] == SINK:
[117] Fix | Delete
raise ValueError('Template.append: already ends with SINK')
[118] Fix | Delete
if kind[0] == 'f' and not re.search(r'\$IN\b', cmd):
[119] Fix | Delete
raise ValueError('Template.append: missing $IN in cmd')
[120] Fix | Delete
if kind[1] == 'f' and not re.search(r'\$OUT\b', cmd):
[121] Fix | Delete
raise ValueError('Template.append: missing $OUT in cmd')
[122] Fix | Delete
self.steps.append((cmd, kind))
[123] Fix | Delete
[124] Fix | Delete
def prepend(self, cmd, kind):
[125] Fix | Delete
"""t.prepend(cmd, kind) adds a new step at the front."""
[126] Fix | Delete
if type(cmd) is not type(''):
[127] Fix | Delete
raise TypeError('Template.prepend: cmd must be a string')
[128] Fix | Delete
if kind not in stepkinds:
[129] Fix | Delete
raise ValueError('Template.prepend: bad kind %r' % (kind,))
[130] Fix | Delete
if kind == SINK:
[131] Fix | Delete
raise ValueError('Template.prepend: SINK can only be appended')
[132] Fix | Delete
if self.steps and self.steps[0][1] == SOURCE:
[133] Fix | Delete
raise ValueError('Template.prepend: already begins with SOURCE')
[134] Fix | Delete
if kind[0] == 'f' and not re.search(r'\$IN\b', cmd):
[135] Fix | Delete
raise ValueError('Template.prepend: missing $IN in cmd')
[136] Fix | Delete
if kind[1] == 'f' and not re.search(r'\$OUT\b', cmd):
[137] Fix | Delete
raise ValueError('Template.prepend: missing $OUT in cmd')
[138] Fix | Delete
self.steps.insert(0, (cmd, kind))
[139] Fix | Delete
[140] Fix | Delete
def open(self, file, rw):
[141] Fix | Delete
"""t.open(file, rw) returns a pipe or file object open for
[142] Fix | Delete
reading or writing; the file is the other end of the pipeline."""
[143] Fix | Delete
if rw == 'r':
[144] Fix | Delete
return self.open_r(file)
[145] Fix | Delete
if rw == 'w':
[146] Fix | Delete
return self.open_w(file)
[147] Fix | Delete
raise ValueError('Template.open: rw must be \'r\' or \'w\', not %r'
[148] Fix | Delete
% (rw,))
[149] Fix | Delete
[150] Fix | Delete
def open_r(self, file):
[151] Fix | Delete
"""t.open_r(file) and t.open_w(file) implement
[152] Fix | Delete
t.open(file, 'r') and t.open(file, 'w') respectively."""
[153] Fix | Delete
if not self.steps:
[154] Fix | Delete
return open(file, 'r')
[155] Fix | Delete
if self.steps[-1][1] == SINK:
[156] Fix | Delete
raise ValueError('Template.open_r: pipeline ends width SINK')
[157] Fix | Delete
cmd = self.makepipeline(file, '')
[158] Fix | Delete
return os.popen(cmd, 'r')
[159] Fix | Delete
[160] Fix | Delete
def open_w(self, file):
[161] Fix | Delete
if not self.steps:
[162] Fix | Delete
return open(file, 'w')
[163] Fix | Delete
if self.steps[0][1] == SOURCE:
[164] Fix | Delete
raise ValueError('Template.open_w: pipeline begins with SOURCE')
[165] Fix | Delete
cmd = self.makepipeline('', file)
[166] Fix | Delete
return os.popen(cmd, 'w')
[167] Fix | Delete
[168] Fix | Delete
def copy(self, infile, outfile):
[169] Fix | Delete
return os.system(self.makepipeline(infile, outfile))
[170] Fix | Delete
[171] Fix | Delete
def makepipeline(self, infile, outfile):
[172] Fix | Delete
cmd = makepipeline(infile, self.steps, outfile)
[173] Fix | Delete
if self.debugging:
[174] Fix | Delete
print(cmd)
[175] Fix | Delete
cmd = 'set -x; ' + cmd
[176] Fix | Delete
return cmd
[177] Fix | Delete
[178] Fix | Delete
[179] Fix | Delete
def makepipeline(infile, steps, outfile):
[180] Fix | Delete
# Build a list with for each command:
[181] Fix | Delete
# [input filename or '', command string, kind, output filename or '']
[182] Fix | Delete
[183] Fix | Delete
list = []
[184] Fix | Delete
for cmd, kind in steps:
[185] Fix | Delete
list.append(['', cmd, kind, ''])
[186] Fix | Delete
#
[187] Fix | Delete
# Make sure there is at least one step
[188] Fix | Delete
#
[189] Fix | Delete
if not list:
[190] Fix | Delete
list.append(['', 'cat', '--', ''])
[191] Fix | Delete
#
[192] Fix | Delete
# Take care of the input and output ends
[193] Fix | Delete
#
[194] Fix | Delete
[cmd, kind] = list[0][1:3]
[195] Fix | Delete
if kind[0] == 'f' and not infile:
[196] Fix | Delete
list.insert(0, ['', 'cat', '--', ''])
[197] Fix | Delete
list[0][0] = infile
[198] Fix | Delete
#
[199] Fix | Delete
[cmd, kind] = list[-1][1:3]
[200] Fix | Delete
if kind[1] == 'f' and not outfile:
[201] Fix | Delete
list.append(['', 'cat', '--', ''])
[202] Fix | Delete
list[-1][-1] = outfile
[203] Fix | Delete
#
[204] Fix | Delete
# Invent temporary files to connect stages that need files
[205] Fix | Delete
#
[206] Fix | Delete
garbage = []
[207] Fix | Delete
for i in range(1, len(list)):
[208] Fix | Delete
lkind = list[i-1][2]
[209] Fix | Delete
rkind = list[i][2]
[210] Fix | Delete
if lkind[1] == 'f' or rkind[0] == 'f':
[211] Fix | Delete
(fd, temp) = tempfile.mkstemp()
[212] Fix | Delete
os.close(fd)
[213] Fix | Delete
garbage.append(temp)
[214] Fix | Delete
list[i-1][-1] = list[i][0] = temp
[215] Fix | Delete
#
[216] Fix | Delete
for item in list:
[217] Fix | Delete
[inf, cmd, kind, outf] = item
[218] Fix | Delete
if kind[1] == 'f':
[219] Fix | Delete
cmd = 'OUT=' + quote(outf) + '; ' + cmd
[220] Fix | Delete
if kind[0] == 'f':
[221] Fix | Delete
cmd = 'IN=' + quote(inf) + '; ' + cmd
[222] Fix | Delete
if kind[0] == '-' and inf:
[223] Fix | Delete
cmd = cmd + ' <' + quote(inf)
[224] Fix | Delete
if kind[1] == '-' and outf:
[225] Fix | Delete
cmd = cmd + ' >' + quote(outf)
[226] Fix | Delete
item[1] = cmd
[227] Fix | Delete
#
[228] Fix | Delete
cmdlist = list[0][1]
[229] Fix | Delete
for item in list[1:]:
[230] Fix | Delete
[cmd, kind] = item[1:3]
[231] Fix | Delete
if item[0] == '':
[232] Fix | Delete
if 'f' in kind:
[233] Fix | Delete
cmd = '{ ' + cmd + '; }'
[234] Fix | Delete
cmdlist = cmdlist + ' |\n' + cmd
[235] Fix | Delete
else:
[236] Fix | Delete
cmdlist = cmdlist + '\n' + cmd
[237] Fix | Delete
#
[238] Fix | Delete
if garbage:
[239] Fix | Delete
rmcmd = 'rm -f'
[240] Fix | Delete
for file in garbage:
[241] Fix | Delete
rmcmd = rmcmd + ' ' + quote(file)
[242] Fix | Delete
trapcmd = 'trap ' + quote(rmcmd + '; exit') + ' 1 2 3 13 14 15'
[243] Fix | Delete
cmdlist = trapcmd + '\n' + cmdlist + '\n' + rmcmd
[244] Fix | Delete
#
[245] Fix | Delete
return cmdlist
[246] Fix | Delete
[247] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function