[193] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
[191] | 3 | # |
---|
| 4 | # pkpgcounter : a generic Page Description Language parser |
---|
| 5 | # |
---|
[443] | 6 | # (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com> |
---|
[463] | 7 | # This program is free software: you can redistribute it and/or modify |
---|
[191] | 8 | # it under the terms of the GNU General Public License as published by |
---|
[463] | 9 | # the Free Software Foundation, either version 3 of the License, or |
---|
[191] | 10 | # (at your option) any later version. |
---|
[463] | 11 | # |
---|
[191] | 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 |
---|
[463] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[191] | 19 | # |
---|
| 20 | # $Id$ |
---|
| 21 | # |
---|
[193] | 22 | |
---|
[357] | 23 | """This modules implements a page counter for PostScript documents.""" |
---|
| 24 | |
---|
[193] | 25 | import sys |
---|
[283] | 26 | import os |
---|
| 27 | import tempfile |
---|
[193] | 28 | import popen2 |
---|
| 29 | |
---|
[235] | 30 | import pdlparser |
---|
[283] | 31 | import inkcoverage |
---|
[193] | 32 | |
---|
[220] | 33 | class Parser(pdlparser.PDLParser) : |
---|
[193] | 34 | """A parser for PostScript documents.""" |
---|
[428] | 35 | totiffcommands = [ 'gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r%(dpi)i -sOutputFile="%(fname)s" -' ] |
---|
[220] | 36 | def isValid(self) : |
---|
[387] | 37 | """Returns True if data is PostScript, else False.""" |
---|
[220] | 38 | if self.firstblock.startswith("%!") or \ |
---|
| 39 | self.firstblock.startswith("\004%!") or \ |
---|
| 40 | self.firstblock.startswith("\033%-12345X%!PS") or \ |
---|
| 41 | ((self.firstblock[:128].find("\033%-12345X") != -1) and \ |
---|
| 42 | ((self.firstblock.find("LANGUAGE=POSTSCRIPT") != -1) or \ |
---|
| 43 | (self.firstblock.find("LANGUAGE = POSTSCRIPT") != -1) or \ |
---|
| 44 | (self.firstblock.find("LANGUAGE = Postscript") != -1))) or \ |
---|
| 45 | (self.firstblock.find("%!PS-Adobe") != -1) : |
---|
[252] | 46 | self.logdebug("DEBUG: Input file is in the PostScript format.") |
---|
[387] | 47 | return True |
---|
[220] | 48 | else : |
---|
[387] | 49 | return False |
---|
[220] | 50 | |
---|
[193] | 51 | def throughGhostScript(self) : |
---|
| 52 | """Get the count through GhostScript, useful for non-DSC compliant PS files.""" |
---|
[252] | 53 | self.logdebug("Internal parser sucks, using GhostScript instead...") |
---|
[193] | 54 | self.infile.seek(0) |
---|
[283] | 55 | command = 'gs -sDEVICE=bbox -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET - 2>&1 | grep -c "%%HiResBoundingBox:" 2>/dev/null' |
---|
[193] | 56 | child = popen2.Popen4(command) |
---|
| 57 | try : |
---|
[237] | 58 | data = self.infile.read(pdlparser.MEGABYTE) |
---|
[193] | 59 | while data : |
---|
| 60 | child.tochild.write(data) |
---|
[237] | 61 | data = self.infile.read(pdlparser.MEGABYTE) |
---|
[193] | 62 | child.tochild.flush() |
---|
| 63 | child.tochild.close() |
---|
| 64 | except (IOError, OSError), msg : |
---|
[200] | 65 | raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg |
---|
[193] | 66 | |
---|
| 67 | pagecount = 0 |
---|
| 68 | try : |
---|
| 69 | pagecount = int(child.fromchild.readline().strip()) |
---|
| 70 | except (IOError, OSError, AttributeError, ValueError), msg : |
---|
[200] | 71 | raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg |
---|
[193] | 72 | child.fromchild.close() |
---|
| 73 | |
---|
| 74 | try : |
---|
| 75 | child.wait() |
---|
| 76 | except OSError, msg : |
---|
[200] | 77 | raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg |
---|
[273] | 78 | self.logdebug("GhostScript said : %s pages" % pagecount) |
---|
[193] | 79 | return pagecount * self.copies |
---|
| 80 | |
---|
| 81 | def natively(self) : |
---|
| 82 | """Count pages in a DSC compliant PostScript document.""" |
---|
| 83 | self.infile.seek(0) |
---|
| 84 | pagecount = 0 |
---|
[273] | 85 | self.pages = { 0 : { "copies" : 1 } } |
---|
[263] | 86 | oldpagenum = None |
---|
[252] | 87 | previousline = "" |
---|
[273] | 88 | notrust = 0 |
---|
[314] | 89 | prescribe = 0 # Kyocera's Prescribe commands |
---|
[323] | 90 | acrobatmarker = 0 |
---|
[444] | 91 | pagescomment = None |
---|
[450] | 92 | for line in self.infile : |
---|
[321] | 93 | if (not prescribe) and line.startswith(r"%%BeginResource: procset pdf") \ |
---|
[323] | 94 | and not acrobatmarker : |
---|
[273] | 95 | notrust = 1 # Let this stuff be managed by GhostScript, but we still extract number of copies |
---|
[334] | 96 | elif line.startswith(r"%ADOPrintSettings: L") : |
---|
[323] | 97 | acrobatmarker = 1 |
---|
[314] | 98 | elif line.startswith("!R!") : |
---|
| 99 | prescribe = 1 |
---|
[444] | 100 | elif line.startswith(r"%%Pages: ") : |
---|
| 101 | try : |
---|
[447] | 102 | pagescomment = max(pagescomment or 0, int(line.split()[1])) |
---|
[444] | 103 | except ValueError : |
---|
| 104 | pass # strange, to say the least |
---|
[272] | 105 | elif line.startswith(r"%%Page: ") or line.startswith(r"(%%[Page: ") : |
---|
[263] | 106 | proceed = 1 |
---|
| 107 | try : |
---|
[437] | 108 | # treats both "%%Page: x x" and "%%Page: (x-y) z" (probably N-up mode) |
---|
| 109 | newpagenum = int(line.split(']')[0].split()[-1]) |
---|
[263] | 110 | except : |
---|
[323] | 111 | notinteger = 1 # It seems that sometimes it's not an integer but an EPS file name |
---|
[263] | 112 | else : |
---|
[323] | 113 | notinteger = 0 |
---|
[263] | 114 | if newpagenum == oldpagenum : |
---|
| 115 | proceed = 0 |
---|
| 116 | else : |
---|
| 117 | oldpagenum = newpagenum |
---|
[323] | 118 | if proceed and not notinteger : |
---|
[263] | 119 | pagecount += 1 |
---|
[273] | 120 | self.pages[pagecount] = { "copies" : self.pages[pagecount-1]["copies"] } |
---|
[208] | 121 | elif line.startswith(r"%%Requirements: numcopies(") : |
---|
[193] | 122 | try : |
---|
| 123 | number = int(line.strip().split('(')[1].split(')')[0]) |
---|
| 124 | except : |
---|
| 125 | pass |
---|
| 126 | else : |
---|
[273] | 127 | if number > self.pages[pagecount]["copies"] : |
---|
| 128 | self.pages[pagecount]["copies"] = number |
---|
[208] | 129 | elif line.startswith(r"%%BeginNonPPDFeature: NumCopies ") : |
---|
[193] | 130 | # handle # of copies set by some Windows printer driver |
---|
| 131 | try : |
---|
| 132 | number = int(line.strip().split()[2]) |
---|
| 133 | except : |
---|
| 134 | pass |
---|
| 135 | else : |
---|
[273] | 136 | if number > self.pages[pagecount]["copies"] : |
---|
| 137 | self.pages[pagecount]["copies"] = number |
---|
[193] | 138 | elif line.startswith("1 dict dup /NumCopies ") : |
---|
| 139 | # handle # of copies set by mozilla/kprinter |
---|
| 140 | try : |
---|
| 141 | number = int(line.strip().split()[4]) |
---|
| 142 | except : |
---|
| 143 | pass |
---|
| 144 | else : |
---|
[273] | 145 | if number > self.pages[pagecount]["copies"] : |
---|
| 146 | self.pages[pagecount]["copies"] = number |
---|
[389] | 147 | elif line.startswith("{ pop 1 dict dup /NumCopies ") : |
---|
| 148 | # handle # of copies set by firefox/kprinter/cups (alternate syntax) |
---|
| 149 | try : |
---|
| 150 | number = int(line.strip().split()[6]) |
---|
| 151 | except : |
---|
| 152 | pass |
---|
| 153 | else : |
---|
| 154 | if number > self.pages[pagecount]["copies"] : |
---|
| 155 | self.pages[pagecount]["copies"] = number |
---|
[248] | 156 | elif line.startswith("/languagelevel where{pop languagelevel}{1}ifelse 2 ge{1 dict dup/NumCopies") : |
---|
| 157 | try : |
---|
| 158 | number = int(previousline.strip()[2:]) |
---|
| 159 | except : |
---|
| 160 | pass |
---|
| 161 | else : |
---|
[273] | 162 | if number > self.pages[pagecount]["copies"] : |
---|
| 163 | self.pages[pagecount]["copies"] = number |
---|
[290] | 164 | elif line.startswith("/#copies ") : |
---|
| 165 | try : |
---|
| 166 | number = int(line.strip().split()[1]) |
---|
| 167 | except : |
---|
| 168 | pass |
---|
| 169 | else : |
---|
| 170 | if number > self.pages[pagecount]["copies"] : |
---|
| 171 | self.pages[pagecount]["copies"] = number |
---|
[448] | 172 | elif line.startswith(r"%RBINumCopies: ") : |
---|
| 173 | try : |
---|
| 174 | number = int(line.strip().split()[1]) |
---|
| 175 | except : |
---|
| 176 | pass |
---|
| 177 | else : |
---|
| 178 | if number > self.pages[pagecount]["copies"] : |
---|
| 179 | self.pages[pagecount]["copies"] = number |
---|
[248] | 180 | previousline = line |
---|
| 181 | |
---|
| 182 | # extract max number of copies to please the ghostscript parser, just |
---|
| 183 | # in case we will use it later |
---|
[273] | 184 | self.copies = max([ v["copies"] for (k, v) in self.pages.items() ]) |
---|
[193] | 185 | |
---|
[248] | 186 | # now apply the number of copies to each page |
---|
[448] | 187 | if not pagecount and pagescomment : |
---|
| 188 | pagecount = pagescomment |
---|
[248] | 189 | for pnum in range(1, pagecount + 1) : |
---|
[448] | 190 | page = self.pages.get(pnum, self.pages.get(1, self.pages.get(0, { "copies" : 1 }))) |
---|
[248] | 191 | copies = page["copies"] |
---|
| 192 | pagecount += (copies - 1) |
---|
[252] | 193 | self.logdebug("%s * page #%s" % (copies, pnum)) |
---|
[444] | 194 | |
---|
[273] | 195 | self.logdebug("Internal parser said : %s pages" % pagecount) |
---|
[384] | 196 | return (pagecount, notrust) |
---|
[273] | 197 | |
---|
[193] | 198 | def getJobSize(self) : |
---|
| 199 | """Count pages in PostScript document.""" |
---|
[202] | 200 | self.copies = 1 |
---|
[384] | 201 | (nbpages, notrust) = self.natively() |
---|
| 202 | newnbpages = nbpages |
---|
[444] | 203 | if notrust or not nbpages : |
---|
[384] | 204 | try : |
---|
| 205 | newnbpages = self.throughGhostScript() |
---|
| 206 | except pdlparser.PDLParserError, msg : |
---|
| 207 | self.logdebug(msg) |
---|
| 208 | return max(nbpages, newnbpages) |
---|
[283] | 209 | |
---|
[193] | 210 | if __name__ == "__main__" : |
---|
[415] | 211 | pdlparser.test(Parser) |
---|