Changeset 493 for pkpgcounter
- Timestamp:
- 11/21/07 16:41:06 (17 years ago)
- Location:
- pkpgcounter/trunk/pkpgpdls
- Files:
-
- 4 modified
Legend:
- Unmodified
- Added
- Removed
-
pkpgcounter/trunk/pkpgpdls/analyzer.py
r492 r493 136 136 if not os.stat(self.filename).st_size : 137 137 raise pdlparser.PDLParserError, "input file %s is empty !" % str(self.filename) 138 139 # Now read first and last block of the input file 140 # to be able to detect the real file format and the parser to use. 141 firstblock = self.workfile.read(pdlparser.FIRSTBLOCKSIZE) 142 try : 143 self.workfile.seek(-pdlparser.LASTBLOCKSIZE, 2) 144 lastblock = self.workfile.read(pdlparser.LASTBLOCKSIZE) 145 except IOError : 146 lastblock = "" 147 138 148 # IMPORTANT : the order is important below. FIXME. 139 149 for module in (postscript, \ … … 153 163 plain) : # IMPORTANT : don't move this one up ! 154 164 try : 155 return module.Parser(self.filename, self.options.debug) 165 return module.Parser(self.filename, firstblock, 166 lastblock, 167 self.options.debug) 156 168 except pdlparser.PDLParserError : 157 169 pass # try next parser -
pkpgcounter/trunk/pkpgpdls/pdlparser.py
r492 r493 43 43 totiffcommands = None # Default command to convert to TIFF 44 44 openmode = "rb" # Default file opening mode 45 def __init__(self, filename, debug=0) :45 def __init__(self, filename, firstblock, lastblock, debug=0) : 46 46 """Initialize the generic parser.""" 47 47 self.filename = filename 48 48 self.debug = debug 49 49 self.infile = None 50 (self.firstblock, self.lastblock) = self.readBlocks() 51 self.infile = open(self.filename, self.openmode) 50 (self.firstblock, self.lastblock) = (firstblock, lastblock) 52 51 if not self.isValid() : 53 52 raise PDLParserError, "Invalid file format !" … … 61 60 # parsing will greatly benefit from this. 62 61 psyco.bind(self.getJobSize) 62 self.infile = open(self.filename, self.openmode) 63 # self.logdebug("Opened %s in '%s' mode." % (self.filename, self.openmode)) 63 64 64 65 def __del__(self) : … … 66 67 if self.infile : 67 68 self.infile.close() 68 69 def readBlocks(self) :70 """Reads first and last block of the input file."""71 infile = open(self.filename, "rb")72 try :73 firstblock = infile.read(FIRSTBLOCKSIZE)74 try :75 infile.seek(-LASTBLOCKSIZE, 2)76 lastblock = infile.read(LASTBLOCKSIZE)77 except IOError :78 lastblock = ""79 finally :80 infile.close()81 return (firstblock, lastblock)82 69 83 70 def logdebug(self, message) : -
pkpgcounter/trunk/pkpgpdls/plain.py
r492 r493 38 38 read the first line, and if it doesn't end in CR or LF 39 39 we consider it's not plain text. 40 41 TODO : use first and last block's content instead of readline(). 40 42 """ 41 43 line = self.infile.readline() -
pkpgcounter/trunk/pkpgpdls/spl1.py
r491 r493 100 100 self.escapedStuff = {} # For escaped datas, mostly PJL commands 101 101 self.bigEndian() 102 102 codesop = chr(0x06) + chr(0x00) + chr(0x00) + chr(0x80) + chr(0x13) + chr(0x40) 103 103 self.isbitmap = False 104 104 pos = 0 … … 114 114 offset = unpack(self.unpackLong, minfile[pos:pos+4])[0] 115 115 sequencenum = unpack(self.unpackShort, minfile[pos+4:pos+6])[0] 116 codesop = " ".join([ "%02x" % ord(v) for v in minfile[pos+6:pos+12]]) 117 if codesop != "06 00 00 80 13 40" : 116 if minfile[pos+6:pos+12] != codesop : 118 117 raise pdlparser.PDLParserError, "Unfortunately SPL1 is incompletely recognized. Parsing aborted. Please report the problem to %s" % version.__authoremail__ 119 118 if not sequencenum :