mirror of
https://github.com/python/cpython.git
synced 2025-11-09 18:11:38 +00:00
Clean up some argument profiles, enrich the docstring.
This commit is contained in:
parent
d0688ebc10
commit
29bb115bae
2 changed files with 41 additions and 32 deletions
|
|
@ -31,7 +31,7 @@ simply passed as the first argument to the transformation function.
|
||||||
Typically it is a string that names the filter and is used in
|
Typically it is a string that names the filter and is used in
|
||||||
generating error messages, but it could be arbitrary data.
|
generating error messages, but it could be arbitrary data.
|
||||||
|
|
||||||
The second argument, is interpreted as a list of filenames. The files
|
The second argument is interpreted as a list of filenames. The files
|
||||||
are transformed in left to right order in the list. A filename
|
are transformed in left to right order in the list. A filename
|
||||||
consisting of a dash is interpreted as a directive to read from
|
consisting of a dash is interpreted as a directive to read from
|
||||||
standard input (this can be useful in pipelines).
|
standard input (this can be useful in pipelines).
|
||||||
|
|
@ -59,27 +59,27 @@ translation of that file has completed. Any tempfiles are removed
|
||||||
automatically on any exception thrown by the translation function,
|
automatically on any exception thrown by the translation function,
|
||||||
and the exception is then passed upwards.
|
and the exception is then passed upwards.
|
||||||
|
|
||||||
\begin{funcdesc}{filefilter}{name, arguments, trans_data\optional{,trans_file}}
|
\begin{funcdesc}{filefilter}{name, file, arguments, trans_data\optional{,trans_file}}
|
||||||
Filter using a function taking the name and two file-object
|
Filter using a function taking the name, filename, and two file-object
|
||||||
arguments. The function is expected to read data from the input file
|
arguments. The function is expected to read data from the input file
|
||||||
object, transform it, and write the data to the output file object.
|
object, transform it, and write the data to the output file object.
|
||||||
When the function terminates, the translation is done. The return
|
When the function terminates, the translation is done. The return
|
||||||
value of the transformation function is not used.
|
value of the transformation function is not used.
|
||||||
\end{funcdesc}
|
\end{funcdesc}
|
||||||
|
|
||||||
\begin{funcdesc}{linefilter}{name,arguments,trans_data\optional{,trans_file}}
|
\begin{funcdesc}{linefilter}{name, file, arguments,trans_data\optional{,trans_file}}
|
||||||
Filter using a function taking the name and a string argument. The return
|
Filter using a function taking the name, the filename, and a string
|
||||||
value of the function should be a string. This function is applied to
|
argument. The return value of the function should be a string. This
|
||||||
each line in the input file in turn; the return values become the
|
function is applied to each line in the input file in turn; the return
|
||||||
lines of the transformed file.
|
values become the lines of the transformed file.
|
||||||
\end{funcdesc}
|
\end{funcdesc}
|
||||||
|
|
||||||
\begin{funcdesc}{sponge}{name, arguments, trans_data\optional{, trans_file}}
|
\begin{funcdesc}{sponge}{name, file, arguments, trans_data\optional{, trans_file}}
|
||||||
Filter using a function taking the name and a string argument. The
|
Filter using a function taking the name, the filename, and a string
|
||||||
return value of the function should be a string. The function will be
|
argument. The return value of the function should be a string. The
|
||||||
passed the entire contents of the input file as a string. The string
|
function will be passed the entire contents of the input file as a
|
||||||
return value of the function will become the entire contents of the
|
string. The string return value of the function will become the
|
||||||
transformed file.
|
entire contents of the transformed file.
|
||||||
\end{funcdesc}
|
\end{funcdesc}
|
||||||
|
|
||||||
# End
|
# End
|
||||||
|
|
|
||||||
|
|
@ -7,12 +7,23 @@
|
||||||
line, but otherwise transforms each file individually into a corresponding
|
line, but otherwise transforms each file individually into a corresponding
|
||||||
output file.
|
output file.
|
||||||
|
|
||||||
This module provides framework and glue code to make such programs easy
|
This module provides framework and glue code to make such programs
|
||||||
to write. You supply a function to massage the file data; depending
|
easy to write. You supply a function to massage the file data. It
|
||||||
on which entry point you use, it can take input and output file pointers,
|
always takes initial name and filename arguments; depending on which
|
||||||
or it can take a string consisting of the entire file's data and return
|
entry point you use, it can also take input and output file pointers,
|
||||||
a replacement, or it can take in succession strings consisting of each
|
or it can take a string consisting of the entire file's data and
|
||||||
of the file's lines and return a translated line for each.
|
return a replacement, or it can take in succession strings consisting
|
||||||
|
of each of the file's lines and return a translated line for each.
|
||||||
|
|
||||||
|
The fourth, optional argument of each entry point is a name
|
||||||
|
transformation function or name suffix string. If it is of string
|
||||||
|
type, the shortest suffix of each filename beginning with the first
|
||||||
|
character of the argument string is stripped off. If the first
|
||||||
|
character of the argument does not occur in the filename, no suffix is
|
||||||
|
removed. Then the name suffix argument is concatenated to the end of
|
||||||
|
the stripped filename. (Thus, a name suffix argument of ".x" will
|
||||||
|
cause the filenames foo.c and bar.d to be transformed to foo.x and
|
||||||
|
bar.x respectively.)
|
||||||
|
|
||||||
Argument files are transformed in left to right order in the argument list.
|
Argument files are transformed in left to right order in the argument list.
|
||||||
A filename consisting of a dash is interpreted as a directive to read from
|
A filename consisting of a dash is interpreted as a directive to read from
|
||||||
|
|
@ -25,14 +36,12 @@
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Requires Python 2.
|
# Requires Python 2.
|
||||||
from __future__ import nested_scopes
|
|
||||||
|
|
||||||
import sys, os, filecmp, traceback
|
import sys, os, filecmp, traceback
|
||||||
|
|
||||||
def filefilter(name, arguments, trans_data, trans_filename=None):
|
def filefilter(name, arguments, trans_data, trans_filename=None):
|
||||||
"Filter stdin to stdout, or file arguments to renamed files."
|
"Filter stdin to stdout, or file arguments to renamed files."
|
||||||
if not arguments:
|
if not arguments:
|
||||||
trans_data("stdin", sys.stdin, sys.stdout)
|
trans_data(name, "stdin", sys.stdin, sys.stdout)
|
||||||
else:
|
else:
|
||||||
for file in arguments:
|
for file in arguments:
|
||||||
if file == '-': # - is conventional for stdin
|
if file == '-': # - is conventional for stdin
|
||||||
|
|
@ -43,7 +52,7 @@ def filefilter(name, arguments, trans_data, trans_filename=None):
|
||||||
tempfile = file + ".~%s-%d~" % (name, os.getpid())
|
tempfile = file + ".~%s-%d~" % (name, os.getpid())
|
||||||
outfp = open(tempfile, "w")
|
outfp = open(tempfile, "w")
|
||||||
try:
|
try:
|
||||||
trans_data(file, infp, outfp)
|
trans_data(name, file, infp, outfp)
|
||||||
except:
|
except:
|
||||||
os.remove(tempfile)
|
os.remove(tempfile)
|
||||||
# Pass the exception upwards
|
# Pass the exception upwards
|
||||||
|
|
@ -62,26 +71,26 @@ def filefilter(name, arguments, trans_data, trans_filename=None):
|
||||||
else:
|
else:
|
||||||
os.rename(tempfile, trans_filename(file))
|
os.rename(tempfile, trans_filename(file))
|
||||||
|
|
||||||
def line_by_line(name, infp, outfp, translate_line):
|
def line_by_line(name, file, infp, outfp, translate_line):
|
||||||
"Hook to do line-by-line translation for filters."
|
"Hook to do line-by-line translation for filters."
|
||||||
while 1:
|
while 1:
|
||||||
line = infp.readline()
|
line = infp.readline()
|
||||||
if line == "":
|
if line == "":
|
||||||
break
|
break
|
||||||
elif line: # None returns are skipped
|
elif line: # None returns are skipped
|
||||||
outfp.write(translate_line(name, line))
|
outfp.write(translate_line(name, file, line))
|
||||||
|
|
||||||
def linefilter(name, arguments, trans_data, trans_filename=None):
|
def linefilter(name, arguments, trans_data, trans_filename=None):
|
||||||
"Filter framework for line-by-line transformation."
|
"Filter framework for line-by-line transformation."
|
||||||
return filefilter(name,
|
return filefilter(name,
|
||||||
arguments,
|
arguments,
|
||||||
lambda name, infp, outfp: line_by_line(name, infp, outfp, trans_data),
|
lambda name, file, infp, outfp: line_by_line(name, file, infp, outfp, trans_data),
|
||||||
trans_filename)
|
trans_filename)
|
||||||
|
|
||||||
def sponge(name, arguments, trans_data, trans_filename=None):
|
def sponge(name, arguments, trans_data, trans_filename=None):
|
||||||
"Read input sources entire and transform them in memory."
|
"Read input sources entire and transform them in memory."
|
||||||
if not arguments:
|
if not arguments:
|
||||||
sys.stdout.write(trans_data(name, sys.stdin.read()))
|
sys.stdout.write(trans_data(name, "stdin", sys.stdin.read()))
|
||||||
else:
|
else:
|
||||||
for file in arguments:
|
for file in arguments:
|
||||||
infp = open(file)
|
infp = open(file)
|
||||||
|
|
@ -94,7 +103,7 @@ def sponge(name, arguments, trans_data, trans_filename=None):
|
||||||
sys.stderr.write("%s: can't open tempfile" % name)
|
sys.stderr.write("%s: can't open tempfile" % name)
|
||||||
return 1
|
return 1
|
||||||
try:
|
try:
|
||||||
outdoc = trans_data(name, indoc)
|
outdoc = trans_data(name, file, indoc)
|
||||||
except:
|
except:
|
||||||
os.remove(tempfile)
|
os.remove(tempfile)
|
||||||
# Pass the exception upwards
|
# Pass the exception upwards
|
||||||
|
|
@ -120,7 +129,7 @@ def sponge(name, arguments, trans_data, trans_filename=None):
|
||||||
def nametrans(name):
|
def nametrans(name):
|
||||||
return name + ".out"
|
return name + ".out"
|
||||||
|
|
||||||
def filefilter_test(name, infp, outfp):
|
def filefilter_test(name, file, infp, outfp):
|
||||||
"Test hook for filefilter entry point -- put dashes before blank lines."
|
"Test hook for filefilter entry point -- put dashes before blank lines."
|
||||||
while 1:
|
while 1:
|
||||||
line = infp.readline()
|
line = infp.readline()
|
||||||
|
|
@ -130,11 +139,11 @@ def filefilter_test(name, infp, outfp):
|
||||||
outfp.write("------------------------------------------\n")
|
outfp.write("------------------------------------------\n")
|
||||||
outfp.write(line)
|
outfp.write(line)
|
||||||
|
|
||||||
def linefilter_test(name, data):
|
def linefilter_test(name, file, data):
|
||||||
"Test hook for linefilter entry point -- wrap lines in brackets."
|
"Test hook for linefilter entry point -- wrap lines in brackets."
|
||||||
return "<" + data[:-1] + ">\n"
|
return "<" + data[:-1] + ">\n"
|
||||||
|
|
||||||
def sponge_test(name, data):
|
def sponge_test(name, file, data):
|
||||||
"Test hook for the sponge entry point -- reverse file lines."
|
"Test hook for the sponge entry point -- reverse file lines."
|
||||||
lines = data.split("\n")
|
lines = data.split("\n")
|
||||||
lines.reverse()
|
lines.reverse()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue