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

Skip to content

Commit 46ab6df

Browse files
committed
Re-write in Python for portability. About 30% slower, but who cares?!
1 parent 82ebc27 commit 46ab6df

1 file changed

Lines changed: 61 additions & 26 deletions

File tree

Doc/tools/getpagecounts

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,73 @@
1-
#! /bin/sh
2-
# -*- Ksh -*-
3-
#
4-
# Generate a page count report of the PostScript version of the manuals.
1+
#! /usr/bin/env python
2+
# -*- Python -*-
53

6-
TOTAL=0
4+
"""Generate a page count report of the PostScript version of the manuals."""
75

8-
getpagecount() {
9-
PAGECOUNT=`grep -c '^%%Page:' $1.ps`
10-
echo "$2 $1.ps ($PAGECOUNT pages)"
11-
TOTAL=`expr $TOTAL + $PAGECOUNT`
12-
}
6+
__version__ = '$Revision$'
137

14-
cat <<EOF
15-
This is the PostScript version of the standard Python documentation.
16-
If you plan to print this, be aware that some of the documents are
17-
long. The following manuals are included:
188

19-
EOF
9+
class PageCounter:
10+
def __init__(self):
11+
self.doclist = []
12+
self.total = 0
13+
self.title_width = 0
2014

21-
getpagecount api "Python/C API "
22-
getpagecount ext "Extending and Embedding the Python Interpreter"
23-
getpagecount lib "Python Library Reference "
24-
getpagecount mac "Macintosh Module Reference "
25-
getpagecount ref "Python Reference Manual "
26-
getpagecount tut "Python Tutorial "
27-
getpagecount doc "Documenting Python "
15+
def add_document(self, prefix, title):
16+
count = count_pages(prefix + ".ps")
17+
self.doclist.append((title, prefix, count))
18+
self.title_width = max(self.title_width, len(title))
19+
self.total = self.total + count
2820

29-
echo
30-
echo " Total page count: $TOTAL"
21+
def dump(self):
22+
fmt = "%%-%ds (%%s.ps, %%d pages)" % self.title_width
23+
for item in self.doclist:
24+
print fmt % item
25+
print
26+
print " Total page count: %d" % self.total
3127

32-
cat <<EOF
28+
def run(self):
29+
for prefix, title in [
30+
("api", "Python/C API"),
31+
("ext", "Extending and Embedding the Python Interpreter"),
32+
("lib", "Python Library Reference"),
33+
("mac", "Macintosh Module Reference"),
34+
("ref", "Python Reference Manual"),
35+
("tut", "Python Tutorial"),
36+
("doc", "Documenting Python"),
37+
]:
38+
self.add_document(prefix, title)
39+
print self.PREFIX
40+
self.dump()
41+
print self.SUFFIX
42+
43+
PREFIX = """\
44+
This is the PostScript version of the standard Python documentation.
45+
If you plan to print this, be aware that some of the documents are
46+
long. The following manuals are included:
47+
"""
48+
SUFFIX = """\
3349
3450
3551
If you have any questions, comments, or suggestions regarding these
3652
documents, please send them via email to [email protected].
53+
"""
54+
55+
def count_pages(filename):
56+
fp = open(filename)
57+
count = 0
58+
while 1:
59+
lines = fp.readlines(1024*40)
60+
if not lines:
61+
break
62+
for line in lines:
63+
if line[:7] == "%%Page:":
64+
count = count + 1
65+
fp.close()
66+
return count
67+
68+
69+
def main():
70+
PageCounter().run()
3771

38-
EOF
72+
if __name__ == "__main__":
73+
main()

0 commit comments

Comments
 (0)