1 | #! /usr/bin/env python |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | # |
---|
4 | # pkpgcounter : a generic Page Description Language parser |
---|
5 | # |
---|
6 | # (c) 2003,2004,2005 Jerome Alet <alet@librelogiciel.com> |
---|
7 | # This program is free software; you can redistribute it and/or modify |
---|
8 | # it under the terms of the GNU General Public License as published by |
---|
9 | # the Free Software Foundation; either version 2 of the License, or |
---|
10 | # (at your option) any later version. |
---|
11 | # |
---|
12 | # This program is distributed in the hope that it will be useful, |
---|
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | # GNU General Public License for more details. |
---|
16 | # |
---|
17 | # You should have received a copy of the GNU General Public License |
---|
18 | # along with this program; if not, write to the Free Software |
---|
19 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | # |
---|
21 | # $Id$ |
---|
22 | # |
---|
23 | |
---|
24 | import sys |
---|
25 | import popen2 |
---|
26 | |
---|
27 | from pdlanalyzer import pdlparser |
---|
28 | |
---|
29 | class PostScriptParser(pdlparser.PDLParser) : |
---|
30 | """A parser for PostScript documents.""" |
---|
31 | def throughGhostScript(self) : |
---|
32 | """Get the count through GhostScript, useful for non-DSC compliant PS files.""" |
---|
33 | if self.debug : |
---|
34 | sys.stderr.write("Internal parser sucks, using GhostScript instead...\n") |
---|
35 | self.infile.seek(0) |
---|
36 | command = 'gs -sDEVICE=bbox -dNOPAUSE -dBATCH -dQUIET - 2>&1 | grep -c "%%HiResBoundingBox:" 2>/dev/null' |
---|
37 | child = popen2.Popen4(command) |
---|
38 | try : |
---|
39 | data = self.infile.read(MEGABYTE) |
---|
40 | while data : |
---|
41 | child.tochild.write(data) |
---|
42 | data = self.infile.read(MEGABYTE) |
---|
43 | child.tochild.flush() |
---|
44 | child.tochild.close() |
---|
45 | except (IOError, OSError), msg : |
---|
46 | raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg |
---|
47 | |
---|
48 | pagecount = 0 |
---|
49 | try : |
---|
50 | pagecount = int(child.fromchild.readline().strip()) |
---|
51 | except (IOError, OSError, AttributeError, ValueError), msg : |
---|
52 | raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg |
---|
53 | child.fromchild.close() |
---|
54 | |
---|
55 | try : |
---|
56 | child.wait() |
---|
57 | except OSError, msg : |
---|
58 | raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg |
---|
59 | return pagecount * self.copies |
---|
60 | |
---|
61 | def natively(self) : |
---|
62 | """Count pages in a DSC compliant PostScript document.""" |
---|
63 | self.infile.seek(0) |
---|
64 | pagecount = 0 |
---|
65 | for line in self.infile.xreadlines() : |
---|
66 | if line.startswith("%%Page: ") : |
---|
67 | pagecount += 1 |
---|
68 | elif line.startswith("%%Requirements: numcopies(") : |
---|
69 | try : |
---|
70 | number = int(line.strip().split('(')[1].split(')')[0]) |
---|
71 | except : |
---|
72 | pass |
---|
73 | else : |
---|
74 | if number > self.copies : |
---|
75 | self.copies = number |
---|
76 | elif line.startswith("%%BeginNonPPDFeature: NumCopies ") : |
---|
77 | # handle # of copies set by some Windows printer driver |
---|
78 | try : |
---|
79 | number = int(line.strip().split()[2]) |
---|
80 | except : |
---|
81 | pass |
---|
82 | else : |
---|
83 | if number > self.copies : |
---|
84 | self.copies = number |
---|
85 | elif line.startswith("1 dict dup /NumCopies ") : |
---|
86 | # handle # of copies set by mozilla/kprinter |
---|
87 | try : |
---|
88 | number = int(line.strip().split()[4]) |
---|
89 | except : |
---|
90 | pass |
---|
91 | else : |
---|
92 | if number > self.copies : |
---|
93 | self.copies = number |
---|
94 | return pagecount * self.copies |
---|
95 | |
---|
96 | def getJobSize(self) : |
---|
97 | """Count pages in PostScript document.""" |
---|
98 | self.copies = 1 |
---|
99 | return self.natively() or self.throughGhostScript() |
---|
100 | |
---|
101 | def test() : |
---|
102 | """Test function.""" |
---|
103 | if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) : |
---|
104 | sys.argv.append("-") |
---|
105 | totalsize = 0 |
---|
106 | for arg in sys.argv[1:] : |
---|
107 | if arg == "-" : |
---|
108 | infile = sys.stdin |
---|
109 | mustclose = 0 |
---|
110 | else : |
---|
111 | infile = open(arg, "rb") |
---|
112 | mustclose = 1 |
---|
113 | try : |
---|
114 | parser = PostScriptParser(infile, debug=1) |
---|
115 | totalsize += parser.getJobSize() |
---|
116 | except pdlparser.PDLParserError, msg : |
---|
117 | sys.stderr.write("ERROR: %s\n" % msg) |
---|
118 | sys.stderr.flush() |
---|
119 | if mustclose : |
---|
120 | infile.close() |
---|
121 | print "%s" % totalsize |
---|
122 | |
---|
123 | if __name__ == "__main__" : |
---|
124 | test() |
---|