Show
Ignore:
Timestamp:
11/20/07 00:04:09 (16 years ago)
Author:
jerome
Message:

Major code cleaning. Now clearer, although probably a bit slower since
a file can be opened several times.
Now universal line opening mode is only used when needed (PS, PDF and plain
text), and binary opening mode is used for the other formats.
This mean we will be able to remove mmap calls wherever possible, finally.

Files:
1 modified

Legend:

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

    r485 r491  
    5454        self.options = options 
    5555        self.filename = filename 
    56         self.infile = None 
     56        self.workfile = None  
    5757        self.mustclose = None 
    5858         
     
    8282            try : 
    8383                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) 
    9486            except pdlparser.PDLParserError, msg :     
    9587                raise pdlparser.PDLParserError, "Unknown file format for %s (%s)" % (self.filename, msg) 
     
    10092    def openFile(self) :     
    10193        """Opens the job's data stream for reading.""" 
    102         self.mustclose = 0  # by default we don't want to close the file when finished 
     94        self.mustclose = False  # by default we don't want to close the file when finished 
    10395        if hasattr(self.filename, "read") and hasattr(self.filename, "seek") : 
    10496            # filename is in fact a file-like object  
     
    109101        else :     
    110102            # normal file 
    111             self.infile = open(self.filename, "rbU") 
    112             self.mustclose = 1 
     103            self.workfile = open(self.filename, "rb") 
     104            self.mustclose = True 
    113105            return 
    114106             
    115107        # 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 : 
    118111            data = infile.read(pdlparser.MEGABYTE)  
    119112            if not data : 
    120113                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) 
    124117             
    125118    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.""" 
    127120        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()     
    138122         
    139123    def detectPDLHandler(self) :     
     
    142126           Returns the correct PDL handler class or None if format is unknown 
    143127        """    
    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 : 
    155129            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 
    177150        raise pdlparser.PDLParserError, "Analysis of first data block failed." 
    178151             
     
    254227            sys.stderr.flush() 
    255228        if not options.colorspace :     
    256             print "%s" % totalsize 
     229            print "%i" % totalsize 
    257230        else :     
    258231            print "\n".join(lines)