Kaydet (Commit) 749f943a authored tarafından Fred Drake's avatar Fred Drake

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.
üst 7c8754fa
...@@ -43,25 +43,20 @@ def dump_entries(write, entries): ...@@ -43,25 +43,20 @@ def dump_entries(write, entries):
breakable_re = re.compile( breakable_re = re.compile(
r" \\item (.*) [(](.*)[)]((?:(?:, \d+)|(?:, \\[a-z]*\{\d+\}))+)") r" \\item (.*) [(](.*)[)]((?:(?:, \d+)|(?:, \\[a-z]*\{\d+\}))+)")
def main():
import getopt def process(ifn, ofn=None):
outfile = None if ifn == "-":
opts, args = getopt.getopt(sys.argv[1:], "o:") ifp = sys.stdin
for opt, val in opts:
if opt in ("-o", "--output"):
outfile = val
filename = args[0]
outfile = outfile or filename
if filename == "-":
fp = sys.stdin
else: else:
fp = open(filename) ifp = open(ifn)
if ofn is None:
ofn = ifn
ofp = StringIO.StringIO() ofp = StringIO.StringIO()
entries = [] entries = []
match = breakable_re.match match = breakable_re.match
write = ofp.write write = ofp.write
while 1: while 1:
line = fp.readline() line = ifp.readline()
if not line: if not line:
break break
m = match(line) m = match(line)
...@@ -79,13 +74,27 @@ def main(): ...@@ -79,13 +74,27 @@ def main():
write(line) write(line)
del write del write
del match del match
fp.close() ifp.close()
if outfile == "-": data = ofp.getvalue()
fp = sys.stdout ofp.close()
if ofn == "-":
ofp = sys.stdout
else: else:
fp = open(outfile, "w") ofp = open(ofn, "w")
fp.write(ofp.getvalue()) ofp.write(data)
fp.close() 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__": if __name__ == "__main__":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment