[193] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
[191] | 3 | # |
---|
| 4 | # pkpgcounter : a generic Page Description Language parser |
---|
| 5 | # |
---|
[211] | 6 | # (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com> |
---|
[191] | 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 |
---|
[211] | 19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[191] | 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
[193] | 23 | |
---|
| 24 | import sys |
---|
| 25 | import popen2 |
---|
| 26 | |
---|
[235] | 27 | import pdlparser |
---|
[193] | 28 | |
---|
[220] | 29 | class Parser(pdlparser.PDLParser) : |
---|
[193] | 30 | """A parser for PostScript documents.""" |
---|
[220] | 31 | def isValid(self) : |
---|
| 32 | """Returns 1 if data is PostScript, else 0.""" |
---|
| 33 | if self.firstblock.startswith("%!") or \ |
---|
| 34 | self.firstblock.startswith("\004%!") or \ |
---|
| 35 | self.firstblock.startswith("\033%-12345X%!PS") or \ |
---|
| 36 | ((self.firstblock[:128].find("\033%-12345X") != -1) and \ |
---|
| 37 | ((self.firstblock.find("LANGUAGE=POSTSCRIPT") != -1) or \ |
---|
| 38 | (self.firstblock.find("LANGUAGE = POSTSCRIPT") != -1) or \ |
---|
| 39 | (self.firstblock.find("LANGUAGE = Postscript") != -1))) or \ |
---|
| 40 | (self.firstblock.find("%!PS-Adobe") != -1) : |
---|
[252] | 41 | self.logdebug("DEBUG: Input file is in the PostScript format.") |
---|
[220] | 42 | return 1 |
---|
| 43 | else : |
---|
| 44 | return 0 |
---|
| 45 | |
---|
[193] | 46 | def throughGhostScript(self) : |
---|
| 47 | """Get the count through GhostScript, useful for non-DSC compliant PS files.""" |
---|
[252] | 48 | self.logdebug("Internal parser sucks, using GhostScript instead...") |
---|
[193] | 49 | self.infile.seek(0) |
---|
| 50 | command = 'gs -sDEVICE=bbox -dNOPAUSE -dBATCH -dQUIET - 2>&1 | grep -c "%%HiResBoundingBox:" 2>/dev/null' |
---|
| 51 | child = popen2.Popen4(command) |
---|
| 52 | try : |
---|
[237] | 53 | data = self.infile.read(pdlparser.MEGABYTE) |
---|
[193] | 54 | while data : |
---|
| 55 | child.tochild.write(data) |
---|
[237] | 56 | data = self.infile.read(pdlparser.MEGABYTE) |
---|
[193] | 57 | child.tochild.flush() |
---|
| 58 | child.tochild.close() |
---|
| 59 | except (IOError, OSError), msg : |
---|
[200] | 60 | raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg |
---|
[193] | 61 | |
---|
| 62 | pagecount = 0 |
---|
| 63 | try : |
---|
| 64 | pagecount = int(child.fromchild.readline().strip()) |
---|
| 65 | except (IOError, OSError, AttributeError, ValueError), msg : |
---|
[200] | 66 | raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg |
---|
[193] | 67 | child.fromchild.close() |
---|
| 68 | |
---|
| 69 | try : |
---|
| 70 | child.wait() |
---|
| 71 | except OSError, msg : |
---|
[200] | 72 | raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg |
---|
[193] | 73 | return pagecount * self.copies |
---|
| 74 | |
---|
| 75 | def natively(self) : |
---|
| 76 | """Count pages in a DSC compliant PostScript document.""" |
---|
| 77 | self.infile.seek(0) |
---|
| 78 | pagecount = 0 |
---|
[248] | 79 | pages = {} |
---|
| 80 | pages[0] = { "copies" : 1 } |
---|
[252] | 81 | previousline = "" |
---|
[193] | 82 | for line in self.infile.xreadlines() : |
---|
[208] | 83 | if line.startswith(r"%%Page: ") : |
---|
[193] | 84 | pagecount += 1 |
---|
[261] | 85 | pages[pagecount] = { "copies" : pages[pagecount-1]["copies"] } |
---|
[256] | 86 | elif line.startswith(r"(%%[Page: ") : |
---|
| 87 | pagecount += 1 |
---|
[261] | 88 | pages[pagecount] = { "copies" : pages[pagecount-1]["copies"] } |
---|
[208] | 89 | elif line.startswith(r"%%Requirements: numcopies(") : |
---|
[193] | 90 | try : |
---|
| 91 | number = int(line.strip().split('(')[1].split(')')[0]) |
---|
| 92 | except : |
---|
| 93 | pass |
---|
| 94 | else : |
---|
[248] | 95 | if number > pages[pagecount]["copies"] : |
---|
| 96 | pages[pagecount]["copies"] = number |
---|
[208] | 97 | elif line.startswith(r"%%BeginNonPPDFeature: NumCopies ") : |
---|
[193] | 98 | # handle # of copies set by some Windows printer driver |
---|
| 99 | try : |
---|
| 100 | number = int(line.strip().split()[2]) |
---|
| 101 | except : |
---|
| 102 | pass |
---|
| 103 | else : |
---|
[248] | 104 | if number > pages[pagecount]["copies"] : |
---|
| 105 | pages[pagecount]["copies"] = number |
---|
[193] | 106 | elif line.startswith("1 dict dup /NumCopies ") : |
---|
| 107 | # handle # of copies set by mozilla/kprinter |
---|
| 108 | try : |
---|
| 109 | number = int(line.strip().split()[4]) |
---|
| 110 | except : |
---|
| 111 | pass |
---|
| 112 | else : |
---|
[248] | 113 | if number > pages[pagecount]["copies"] : |
---|
| 114 | pages[pagecount]["copies"] = number |
---|
| 115 | elif line.startswith("/languagelevel where{pop languagelevel}{1}ifelse 2 ge{1 dict dup/NumCopies") : |
---|
| 116 | try : |
---|
| 117 | number = int(previousline.strip()[2:]) |
---|
| 118 | except : |
---|
| 119 | pass |
---|
| 120 | else : |
---|
| 121 | if number > pages[pagecount]["copies"] : |
---|
| 122 | pages[pagecount]["copies"] = number |
---|
| 123 | previousline = line |
---|
| 124 | |
---|
| 125 | # extract max number of copies to please the ghostscript parser, just |
---|
| 126 | # in case we will use it later |
---|
| 127 | self.copies = max([ v["copies"] for (k, v) in pages.items() ]) |
---|
[193] | 128 | |
---|
[248] | 129 | # now apply the number of copies to each page |
---|
| 130 | for pnum in range(1, pagecount + 1) : |
---|
| 131 | page = pages.get(pnum, pages.get(1, { "copies" : 1 })) |
---|
| 132 | copies = page["copies"] |
---|
| 133 | pagecount += (copies - 1) |
---|
[252] | 134 | self.logdebug("%s * page #%s" % (copies, pnum)) |
---|
[248] | 135 | return pagecount |
---|
| 136 | |
---|
[193] | 137 | def getJobSize(self) : |
---|
| 138 | """Count pages in PostScript document.""" |
---|
[202] | 139 | self.copies = 1 |
---|
[193] | 140 | return self.natively() or self.throughGhostScript() |
---|
| 141 | |
---|
| 142 | def test() : |
---|
| 143 | """Test function.""" |
---|
[196] | 144 | if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) : |
---|
| 145 | sys.argv.append("-") |
---|
| 146 | totalsize = 0 |
---|
| 147 | for arg in sys.argv[1:] : |
---|
| 148 | if arg == "-" : |
---|
| 149 | infile = sys.stdin |
---|
| 150 | mustclose = 0 |
---|
| 151 | else : |
---|
| 152 | infile = open(arg, "rb") |
---|
| 153 | mustclose = 1 |
---|
| 154 | try : |
---|
[220] | 155 | parser = Parser(infile, debug=1) |
---|
[196] | 156 | totalsize += parser.getJobSize() |
---|
[200] | 157 | except pdlparser.PDLParserError, msg : |
---|
[196] | 158 | sys.stderr.write("ERROR: %s\n" % msg) |
---|
| 159 | sys.stderr.flush() |
---|
| 160 | if mustclose : |
---|
| 161 | infile.close() |
---|
| 162 | print "%s" % totalsize |
---|
[193] | 163 | |
---|
| 164 | if __name__ == "__main__" : |
---|
| 165 | test() |
---|