Show
Ignore:
Timestamp:
11/28/07 01:08:32 (17 years ago)
Author:
jerome
Message:

More code cleaning : let ghostscript do the job of reading the input
file directly instead of from a pipe.

Files:
1 modified

Legend:

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

    r520 r521  
    5353        """Get the count through GhostScript, useful for non-DSC compliant PS files.""" 
    5454        self.logdebug("Internal parser sucks, using GhostScript instead...") 
    55         command = 'gs -sDEVICE=bbox -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET - 2>&1 | grep -c "%%HiResBoundingBox:" 2>/dev/null' 
     55        infname = self.parent.filename 
     56        command = 'gs -sDEVICE=bbox -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET "%(infname)s" 2>&1 | grep -c "%%HiResBoundingBox:" 2>/dev/null' 
    5657        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.parent.filename, "rb") 
     58        fromchild = os.popen(command % locals(), "r") 
    6059        try : 
    61             child = popen2.Popen4(command) 
    6260            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()) 
     61                pagecount = int(fromchild.readline().strip()) 
    7562            except (IOError, OSError, AttributeError, ValueError), msg : 
    7663                raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg 
    77             child.fromchild.close() 
    78              
    79             try : 
    80                 child.wait() 
    81             except OSError, msg :     
    82                 raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg 
    8364        finally :         
    84             infile.close() 
     65            if fromchild.close() is not None : 
     66                raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document" 
    8567        self.logdebug("GhostScript said : %s pages" % pagecount)     
    8668        return pagecount * self.copies 
     
    9274        oldpagenum = None 
    9375        previousline = "" 
    94         notrust = 0 
    95         prescribe = 0 # Kyocera's Prescribe commands 
    96         acrobatmarker = 0 
     76        notrust = False 
     77        prescribe = False # Kyocera's Prescribe commands 
     78        acrobatmarker = False 
    9779        pagescomment = None 
    9880        for line in self.infile : 
     
    10082            if (not prescribe) and line.startswith(r"%%BeginResource: procset pdf") \ 
    10183               and not acrobatmarker : 
    102                 notrust = 1 # Let this stuff be managed by GhostScript, but we still extract number of copies 
     84                notrust = True # Let this stuff be managed by GhostScript, but we still extract number of copies 
    10385            elif line.startswith(r"%ADOPrintSettings: L") : 
    104                 acrobatmarker = 1 
     86                acrobatmarker = True 
    10587            elif line.startswith("!R!") : 
    106                 prescribe = 1 
     88                prescribe = True 
    10789            elif line.startswith(r"%%Pages: ") : 
    10890                try :