Changeset 491 for pkpgcounter/trunk/pkpgpdls/postscript.py
- Timestamp:
- 11/20/07 00:04:09 (17 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pkpgcounter/trunk/pkpgpdls/postscript.py
r463 r491 34 34 """A parser for PostScript documents.""" 35 35 totiffcommands = [ 'gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r%(dpi)i -sOutputFile="%(fname)s" -' ] 36 openmode = "rU" 36 37 def isValid(self) : 37 38 """Returns True if data is PostScript, else False.""" … … 52 53 """Get the count through GhostScript, useful for non-DSC compliant PS files.""" 53 54 self.logdebug("Internal parser sucks, using GhostScript instead...") 54 self.infile.seek(0)55 55 command = 'gs -sDEVICE=bbox -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET - 2>&1 | grep -c "%%HiResBoundingBox:" 2>/dev/null' 56 child = popen2.Popen4(command) 56 pagecount = 0 57 # we need to reopen the input file in binary mode again, just in case 58 # otherwise we might break the original file's contents. 59 infile = open(self.filename, "rb") 57 60 try : 58 data = self.infile.read(pdlparser.MEGABYTE) 59 while data : 60 child.tochild.write(data) 61 data = self.infile.read(pdlparser.MEGABYTE) 62 child.tochild.flush() 63 child.tochild.close() 64 except (IOError, OSError), msg : 65 raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg 61 child = popen2.Popen4(command) 62 try : 63 data = infile.read(pdlparser.MEGABYTE) 64 while data : 65 child.tochild.write(data) 66 data = infile.read(pdlparser.MEGABYTE) 67 child.tochild.flush() 68 child.tochild.close() 69 except (IOError, OSError), msg : 70 raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg 71 72 pagecount = 0 73 try : 74 pagecount = int(child.fromchild.readline().strip()) 75 except (IOError, OSError, AttributeError, ValueError), msg : 76 raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg 77 child.fromchild.close() 66 78 67 pagecount = 0 68 try : 69 pagecount = int(child.fromchild.readline().strip()) 70 except (IOError, OSError, AttributeError, ValueError), msg : 71 raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg 72 child.fromchild.close() 73 74 try : 75 child.wait() 76 except OSError, msg : 77 raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg 79 try : 80 child.wait() 81 except OSError, msg : 82 raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg 83 finally : 84 infile.close() 78 85 self.logdebug("GhostScript said : %s pages" % pagecount) 79 86 return pagecount * self.copies … … 81 88 def natively(self) : 82 89 """Count pages in a DSC compliant PostScript document.""" 83 self.infile.seek(0)84 90 pagecount = 0 85 91 self.pages = { 0 : { "copies" : 1 } } … … 90 96 acrobatmarker = 0 91 97 pagescomment = None 92 for line in self.infile : 98 for line in self.infile : 99 line = line.strip() 93 100 if (not prescribe) and line.startswith(r"%%BeginResource: procset pdf") \ 94 101 and not acrobatmarker : … … 121 128 elif line.startswith(r"%%Requirements: numcopies(") : 122 129 try : 123 number = int(line.s trip().split('(')[1].split(')')[0])130 number = int(line.split('(')[1].split(')')[0]) 124 131 except : 125 132 pass … … 130 137 # handle # of copies set by some Windows printer driver 131 138 try : 132 number = int(line.s trip().split()[2])139 number = int(line.split()[2]) 133 140 except : 134 141 pass … … 139 146 # handle # of copies set by mozilla/kprinter 140 147 try : 141 number = int(line.s trip().split()[4])148 number = int(line.split()[4]) 142 149 except : 143 150 pass … … 148 155 # handle # of copies set by firefox/kprinter/cups (alternate syntax) 149 156 try : 150 number = int(line.s trip().split()[6])157 number = int(line.split()[6]) 151 158 except : 152 159 pass … … 156 163 elif line.startswith("/languagelevel where{pop languagelevel}{1}ifelse 2 ge{1 dict dup/NumCopies") : 157 164 try : 158 number = int(previousline .strip()[2:])165 number = int(previousline[2:]) 159 166 except : 160 167 pass … … 164 171 elif line.startswith("/#copies ") : 165 172 try : 166 number = int(line.s trip().split()[1])173 number = int(line.split()[1]) 167 174 except : 168 175 pass … … 172 179 elif line.startswith(r"%RBINumCopies: ") : 173 180 try : 174 number = int(line.s trip().split()[1])181 number = int(line.split()[1]) 175 182 except : 176 183 pass … … 207 214 self.logdebug(msg) 208 215 return max(nbpages, newnbpages) 209 210 if __name__ == "__main__" :211 pdlparser.test(Parser)