Changeset 491 for pkpgcounter/trunk/pkpgpdls/analyzer.py
- Timestamp:
- 11/20/07 00:04:09 (17 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pkpgcounter/trunk/pkpgpdls/analyzer.py
r485 r491 54 54 self.options = options 55 55 self.filename = filename 56 self. infile = None56 self.workfile = None 57 57 self.mustclose = None 58 58 … … 82 82 try : 83 83 pdlhandler = self.detectPDLHandler() 84 (handle, filename) = tempfile.mkstemp(".tmp", "pkpgcounter") 85 os.close(handle) 86 try : 87 pdlhandler.convertToTiffMultiPage24NC(filename, self.options.resolution) 88 result = inkcoverage.getInkCoverage(filename, cspace) 89 finally : 90 try : 91 os.remove(filename) 92 except OSError : 93 sys.stderr.write("Problem when trying to remove temporary file %s\n" % filename) 84 pdlhandler.convertToTiffMultiPage24NC(self.filename, self.options.resolution) 85 result = inkcoverage.getInkCoverage(self.filename, cspace) 94 86 except pdlparser.PDLParserError, msg : 95 87 raise pdlparser.PDLParserError, "Unknown file format for %s (%s)" % (self.filename, msg) … … 100 92 def openFile(self) : 101 93 """Opens the job's data stream for reading.""" 102 self.mustclose = 0# by default we don't want to close the file when finished94 self.mustclose = False # by default we don't want to close the file when finished 103 95 if hasattr(self.filename, "read") and hasattr(self.filename, "seek") : 104 96 # filename is in fact a file-like object … … 109 101 else : 110 102 # normal file 111 self. infile = open(self.filename, "rbU")112 self.mustclose = 1103 self.workfile = open(self.filename, "rb") 104 self.mustclose = True 113 105 return 114 106 115 107 # Use a temporary file, always seekable contrary to standard input. 116 self.infile = tempfile.TemporaryFile(mode="w+b") # TODO : not opened in universal newline mode, Python 2.5 refuses. 117 while 1 : 108 self.workfile = tempfile.NamedTemporaryFile(mode="w+b") 109 self.filename = self.workfile.name 110 while True : 118 111 data = infile.read(pdlparser.MEGABYTE) 119 112 if not data : 120 113 break 121 self. infile.write(data)122 self. infile.flush()123 self. infile.seek(0)114 self.workfile.write(data) 115 self.workfile.flush() 116 self.workfile.seek(0) 124 117 125 118 def closeFile(self) : 126 """Closes the job's data stream if we can close it."""119 """Closes the job's data stream if we have to.""" 127 120 if self.mustclose : 128 self.infile.close() 129 else : 130 # if we don't have to close the file, then 131 # ensure the file pointer is reset to the 132 # start of the file in case the process wants 133 # to read the file again. 134 try : 135 self.infile.seek(0) 136 except IOError : 137 pass # probably stdin, which is not seekable 121 self.workfile.close() 138 122 139 123 def detectPDLHandler(self) : … … 142 126 Returns the correct PDL handler class or None if format is unknown 143 127 """ 144 # Try to detect file type by reading first and last blocks of datas 145 # Each parser can read them automatically, but here we do this only once. 146 self.infile.seek(0) 147 firstblock = self.infile.read(pdlparser.FIRSTBLOCKSIZE) 148 try : 149 self.infile.seek(-pdlparser.LASTBLOCKSIZE, 2) 150 lastblock = self.infile.read(pdlparser.LASTBLOCKSIZE) 151 except IOError : 152 lastblock = "" 153 self.infile.seek(0) 154 if not firstblock : 128 if not os.stat(self.filename).st_size : 155 129 raise pdlparser.PDLParserError, "input file %s is empty !" % str(self.filename) 156 else : 157 # IMPORTANT : the order is important below. FIXME. 158 for module in (postscript, \ 159 pclxl, \ 160 pdf, \ 161 qpdl, \ 162 spl1, \ 163 dvi, \ 164 tiff, \ 165 zjstream, \ 166 ooo, \ 167 hbp, \ 168 lidil, \ 169 pcl345, \ 170 escp2, \ 171 escpages03, \ 172 plain) : # IMPORTANT : don't move this one up ! 173 try : 174 return module.Parser(self.infile, self.options.debug, firstblock, lastblock) 175 except pdlparser.PDLParserError : 176 pass # try next parser 130 # IMPORTANT : the order is important below. FIXME. 131 for module in (postscript, \ 132 pclxl, \ 133 pdf, \ 134 qpdl, \ 135 spl1, \ 136 dvi, \ 137 tiff, \ 138 zjstream, \ 139 ooo, \ 140 hbp, \ 141 lidil, \ 142 pcl345, \ 143 escp2, \ 144 escpages03, \ 145 plain) : # IMPORTANT : don't move this one up ! 146 try : 147 return module.Parser(self.filename, self.options.debug) 148 except pdlparser.PDLParserError : 149 pass # try next parser 177 150 raise pdlparser.PDLParserError, "Analysis of first data block failed." 178 151 … … 254 227 sys.stderr.flush() 255 228 if not options.colorspace : 256 print "% s" % totalsize229 print "%i" % totalsize 257 230 else : 258 231 print "\n".join(lines)