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

    r463 r491  
    3434    """A parser for PostScript documents.""" 
    3535    totiffcommands = [ 'gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r%(dpi)i -sOutputFile="%(fname)s" -' ] 
     36    openmode = "rU" 
    3637    def isValid(self) :     
    3738        """Returns True if data is PostScript, else False.""" 
     
    5253        """Get the count through GhostScript, useful for non-DSC compliant PS files.""" 
    5354        self.logdebug("Internal parser sucks, using GhostScript instead...") 
    54         self.infile.seek(0) 
    5555        command = 'gs -sDEVICE=bbox -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET - 2>&1 | grep -c "%%HiResBoundingBox:" 2>/dev/null' 
    56         child = popen2.Popen4(command) 
     56        pagecount = 0 
     57        # we need to reopen the input file in binary mode again, just in case 
     58        # otherwise we might break the original file's contents. 
     59        infile = open(self.filename, "rb") 
    5760        try : 
    58             data = self.infile.read(pdlparser.MEGABYTE)     
    59             while data : 
    60                 child.tochild.write(data) 
    61                 data = self.infile.read(pdlparser.MEGABYTE) 
    62             child.tochild.flush() 
    63             child.tochild.close()     
    64         except (IOError, OSError), msg :     
    65             raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg 
     61            child = popen2.Popen4(command) 
     62            try : 
     63                data = infile.read(pdlparser.MEGABYTE)     
     64                while data : 
     65                    child.tochild.write(data) 
     66                    data = infile.read(pdlparser.MEGABYTE) 
     67                child.tochild.flush() 
     68                child.tochild.close()     
     69            except (IOError, OSError), msg :     
     70                raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg 
     71                 
     72            pagecount = 0 
     73            try : 
     74                pagecount = int(child.fromchild.readline().strip()) 
     75            except (IOError, OSError, AttributeError, ValueError), msg : 
     76                raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg 
     77            child.fromchild.close() 
    6678             
    67         pagecount = 0 
    68         try : 
    69             pagecount = int(child.fromchild.readline().strip()) 
    70         except (IOError, OSError, AttributeError, ValueError), msg : 
    71             raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg 
    72         child.fromchild.close() 
    73          
    74         try : 
    75             child.wait() 
    76         except OSError, msg :     
    77             raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg 
     79            try : 
     80                child.wait() 
     81            except OSError, msg :     
     82                raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg 
     83        finally :         
     84            infile.close() 
    7885        self.logdebug("GhostScript said : %s pages" % pagecount)     
    7986        return pagecount * self.copies 
     
    8188    def natively(self) : 
    8289        """Count pages in a DSC compliant PostScript document.""" 
    83         self.infile.seek(0) 
    8490        pagecount = 0 
    8591        self.pages = { 0 : { "copies" : 1 } } 
     
    9096        acrobatmarker = 0 
    9197        pagescomment = None 
    92         for line in self.infile :  
     98        for line in self.infile : 
     99            line = line.strip() 
    93100            if (not prescribe) and line.startswith(r"%%BeginResource: procset pdf") \ 
    94101               and not acrobatmarker : 
     
    121128            elif line.startswith(r"%%Requirements: numcopies(") :     
    122129                try : 
    123                     number = int(line.strip().split('(')[1].split(')')[0]) 
     130                    number = int(line.split('(')[1].split(')')[0]) 
    124131                except :      
    125132                    pass 
     
    130137                # handle # of copies set by some Windows printer driver 
    131138                try : 
    132                     number = int(line.strip().split()[2]) 
     139                    number = int(line.split()[2]) 
    133140                except :      
    134141                    pass 
     
    139146                # handle # of copies set by mozilla/kprinter 
    140147                try : 
    141                     number = int(line.strip().split()[4]) 
     148                    number = int(line.split()[4]) 
    142149                except :      
    143150                    pass 
     
    148155                # handle # of copies set by firefox/kprinter/cups (alternate syntax) 
    149156                try : 
    150                     number = int(line.strip().split()[6]) 
     157                    number = int(line.split()[6]) 
    151158                except : 
    152159                    pass 
     
    156163            elif line.startswith("/languagelevel where{pop languagelevel}{1}ifelse 2 ge{1 dict dup/NumCopies") : 
    157164                try : 
    158                     number = int(previousline.strip()[2:]) 
     165                    number = int(previousline[2:]) 
    159166                except : 
    160167                    pass 
     
    164171            elif line.startswith("/#copies ") : 
    165172                try : 
    166                     number = int(line.strip().split()[1]) 
     173                    number = int(line.split()[1]) 
    167174                except :      
    168175                    pass 
     
    172179            elif line.startswith(r"%RBINumCopies: ") :    
    173180                try : 
    174                     number = int(line.strip().split()[1]) 
     181                    number = int(line.split()[1]) 
    175182                except :      
    176183                    pass 
     
    207214                self.logdebug(msg) 
    208215        return max(nbpages, newnbpages)     
    209          
    210 if __name__ == "__main__" :     
    211     pdlparser.test(Parser)