Show
Ignore:
Timestamp:
11/21/07 16:41:06 (17 years ago)
Author:
jerome
Message:

Re-optimize disk access by not reopening and re-reading first and last block
more than once.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pkpgcounter/trunk/pkpgpdls/analyzer.py

    r492 r493  
    136136        if not os.stat(self.filename).st_size : 
    137137            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             
    138148        # IMPORTANT : the order is important below. FIXME. 
    139149        for module in (postscript, \ 
     
    153163                       plain) :     # IMPORTANT : don't move this one up ! 
    154164            try :                
    155                 return module.Parser(self.filename, self.options.debug) 
     165                return module.Parser(self.filename, firstblock, 
     166                                                    lastblock, 
     167                                                    self.options.debug) 
    156168            except pdlparser.PDLParserError : 
    157169                pass # try next parser