diff options
author | Fred Drake <fdrake@acm.org> | 1999-03-03 19:36:23 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 1999-03-03 19:36:23 (GMT) |
commit | 749f943a6b4c628f29f1d9321ba6644570fa274e (patch) | |
tree | c11985b7e692580d3117cba74449e2278a7a58b2 | |
parent | 7c8754fafb9de59d7c33bbb69941c41340ab2f1e (diff) | |
download | cpython-749f943a6b4c628f29f1d9321ba6644570fa274e.zip cpython-749f943a6b4c628f29f1d9321ba6644570fa274e.tar.gz cpython-749f943a6b4c628f29f1d9321ba6644570fa274e.tar.bz2 |
process(): New function that contains the "orchestration" of the
actual work.
main(): Just handle the command line and filename determination,
calling process() to do the work.
These changes make this more import-friendly.
-rwxr-xr-x | Doc/tools/indfix.py | 47 |
1 files changed, 28 insertions, 19 deletions
diff --git a/Doc/tools/indfix.py b/Doc/tools/indfix.py index 395f095..fbf81e6 100755 --- a/Doc/tools/indfix.py +++ b/Doc/tools/indfix.py @@ -43,25 +43,20 @@ def dump_entries(write, entries): breakable_re = re.compile( r" \\item (.*) [(](.*)[)]((?:(?:, \d+)|(?:, \\[a-z]*\{\d+\}))+)") -def main(): - import getopt - outfile = None - opts, args = getopt.getopt(sys.argv[1:], "o:") - for opt, val in opts: - if opt in ("-o", "--output"): - outfile = val - filename = args[0] - outfile = outfile or filename - if filename == "-": - fp = sys.stdin + +def process(ifn, ofn=None): + if ifn == "-": + ifp = sys.stdin else: - fp = open(filename) + ifp = open(ifn) + if ofn is None: + ofn = ifn ofp = StringIO.StringIO() entries = [] match = breakable_re.match write = ofp.write while 1: - line = fp.readline() + line = ifp.readline() if not line: break m = match(line) @@ -79,13 +74,27 @@ def main(): write(line) del write del match - fp.close() - if outfile == "-": - fp = sys.stdout + ifp.close() + data = ofp.getvalue() + ofp.close() + if ofn == "-": + ofp = sys.stdout else: - fp = open(outfile, "w") - fp.write(ofp.getvalue()) - fp.close() + ofp = open(ofn, "w") + ofp.write(data) + ofp.close() + + +def main(): + import getopt + outfile = None + opts, args = getopt.getopt(sys.argv[1:], "o:") + for opt, val in opts: + if opt in ("-o", "--output"): + outfile = val + filename = args[0] + outfile = outfile or filename + process(filename, outfile) if __name__ == "__main__": |