Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 749f943

Browse files
committed
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.
1 parent 7c8754f commit 749f943

1 file changed

Lines changed: 28 additions & 19 deletions

File tree

Doc/tools/indfix.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,20 @@ def dump_entries(write, entries):
4343
breakable_re = re.compile(
4444
r" \\item (.*) [(](.*)[)]((?:(?:, \d+)|(?:, \\[a-z]*\{\d+\}))+)")
4545

46-
def main():
47-
import getopt
48-
outfile = None
49-
opts, args = getopt.getopt(sys.argv[1:], "o:")
50-
for opt, val in opts:
51-
if opt in ("-o", "--output"):
52-
outfile = val
53-
filename = args[0]
54-
outfile = outfile or filename
55-
if filename == "-":
56-
fp = sys.stdin
46+
47+
def process(ifn, ofn=None):
48+
if ifn == "-":
49+
ifp = sys.stdin
5750
else:
58-
fp = open(filename)
51+
ifp = open(ifn)
52+
if ofn is None:
53+
ofn = ifn
5954
ofp = StringIO.StringIO()
6055
entries = []
6156
match = breakable_re.match
6257
write = ofp.write
6358
while 1:
64-
line = fp.readline()
59+
line = ifp.readline()
6560
if not line:
6661
break
6762
m = match(line)
@@ -79,13 +74,27 @@ def main():
7974
write(line)
8075
del write
8176
del match
82-
fp.close()
83-
if outfile == "-":
84-
fp = sys.stdout
77+
ifp.close()
78+
data = ofp.getvalue()
79+
ofp.close()
80+
if ofn == "-":
81+
ofp = sys.stdout
8582
else:
86-
fp = open(outfile, "w")
87-
fp.write(ofp.getvalue())
88-
fp.close()
83+
ofp = open(ofn, "w")
84+
ofp.write(data)
85+
ofp.close()
86+
87+
88+
def main():
89+
import getopt
90+
outfile = None
91+
opts, args = getopt.getopt(sys.argv[1:], "o:")
92+
for opt, val in opts:
93+
if opt in ("-o", "--output"):
94+
outfile = val
95+
filename = args[0]
96+
outfile = outfile or filename
97+
process(filename, outfile)
8998

9099

91100
if __name__ == "__main__":

0 commit comments

Comments
 (0)