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/pdlparser.py

    r463 r491  
    4242    """Generic PDL parser.""" 
    4343    totiffcommands = None        # Default command to convert to TIFF 
    44     def __init__(self, infile, debug=0, firstblock=None, lastblock=None) : 
     44    openmode = "rb"              # Default file opening mode 
     45    def __init__(self, filename, debug=0) : 
    4546        """Initialize the generic parser.""" 
    46         self.infile = infile 
     47        self.filename = filename 
    4748        self.debug = debug 
    48         if firstblock is None : 
    49             self.infile.seek(0) 
    50             firstblock = self.infile.read(FIRSTBLOCKSIZE) 
    51             try : 
    52                 self.infile.seek(-LASTBLOCKSIZE, 2) 
    53                 lastblock = self.infile.read(LASTBLOCKSIZE) 
    54             except IOError :     
    55                 lastblock = "" 
    56             self.infile.seek(0) 
    57         self.firstblock = firstblock 
    58         self.lastblock = lastblock 
     49        self.infile = None 
     50        (self.firstblock, self.lastblock) = self.readBlocks() 
     51        self.infile = open(self.filename, self.openmode) 
    5952        if not self.isValid() : 
    6053            raise PDLParserError, "Invalid file format !" 
     
    6861            # parsing will greatly benefit from this. 
    6962            psyco.bind(self.getJobSize) 
     63             
     64    def __del__(self) : 
     65        """Ensures the input file gets closed.""" 
     66        if self.infile : 
     67            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)     
    7082             
    7183    def logdebug(self, message) :        
     
    8799        """    
    88100        if self.totiffcommands : 
    89             for totiffcommand in self.totiffcommands : 
    90                 self.infile.seek(0) 
    91                 error = False 
    92                 commandline = totiffcommand % locals() 
    93                 child = popen2.Popen4(commandline) 
    94                 try : 
     101            infile = open(self.filename, "rb") 
     102            try : 
     103                for totiffcommand in self.totiffcommands : 
     104                    infile.seek(0) 
     105                    error = False 
     106                    commandline = totiffcommand % locals() 
     107                    child = popen2.Popen4(commandline) 
    95108                    try : 
    96                         data = self.infile.read(MEGABYTE)     
    97                         while data : 
    98                             child.tochild.write(data) 
    99                             child.tochild.flush() 
    100                             data = self.infile.read(MEGABYTE) 
    101                     except (IOError, OSError) :     
     109                        try : 
     110                            data = infile.read(MEGABYTE)     
     111                            while data : 
     112                                child.tochild.write(data) 
     113                                child.tochild.flush() 
     114                                data = infile.read(MEGABYTE) 
     115                        except (IOError, OSError) :     
     116                            error = True 
     117                    finally :     
     118                        child.tochild.close()     
     119                        dummy = child.fromchild.read() 
     120                        child.fromchild.close() 
     121                         
     122                    try : 
     123                        status = child.wait() 
     124                    except OSError :     
    102125                        error = True 
    103                 finally :     
    104                     child.tochild.close()     
    105                     dummy = child.fromchild.read() 
    106                     child.fromchild.close() 
    107                      
    108                 try : 
    109                     status = child.wait() 
    110                 except OSError :     
    111                     error = True 
    112                 else :     
    113                     if os.WIFEXITED(status) : 
    114                         if os.WEXITSTATUS(status) : 
     126                    else :     
     127                        if os.WIFEXITED(status) : 
     128                            if os.WEXITSTATUS(status) : 
     129                                error = True 
     130                        else :         
    115131                            error = True 
     132                         
     133                    if not os.path.exists(fname) : 
     134                        error = True 
     135                    elif not os.stat(fname).st_size : 
     136                        error = True 
    116137                    else :         
    117                         error = True 
    118                      
    119                 if not os.path.exists(fname) : 
    120                     error = True 
    121                 elif not os.stat(fname).st_size : 
    122                     error = True 
    123                 else :         
    124                     break       # Conversion worked fine it seems. 
    125                 self.logdebug("Command failed : %s" % repr(commandline)) 
     138                        break       # Conversion worked fine it seems. 
     139                    self.logdebug("Command failed : %s" % repr(commandline)) 
     140            finally :         
     141                infile.close() 
    126142            if error : 
    127143                raise PDLParserError, "Problem during conversion to TIFF." 
    128144        else :         
    129145            raise PDLParserError, "Impossible to compute ink coverage for this file format." 
    130              
    131 def test(parserclass) :         
    132     """Test function.""" 
    133     if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) : 
    134         sys.argv.append("-") 
    135     totalsize = 0     
    136     for arg in sys.argv[1:] : 
    137         if arg == "-" : 
    138             infile = sys.stdin 
    139             mustclose = 0 
    140         else :     
    141             infile = open(arg, "rbU") 
    142             mustclose = 1 
    143         try : 
    144             parser = parserclass(infile, debug=1) 
    145             totalsize += parser.getJobSize() 
    146         except PDLParserError, msg :     
    147             sys.stderr.write("ERROR: %s\n" % msg) 
    148             sys.stderr.flush() 
    149         if mustclose :     
    150             infile.close() 
    151     print "%s" % totalsize 
    152