Show
Ignore:
Timestamp:
07/22/04 15:49:51 (20 years ago)
Author:
jalet
Message:

Added support for binary PostScript? through GhostScript? if native DSC
compliant PostScript? analyzer doesn't find any page. This is much
slower though, so native analyzer is tried first.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/pdlanalyzer.py

    r1599 r1622  
    2222# 
    2323# $Log$ 
     24# Revision 1.26  2004/07/22 13:49:51  jalet 
     25# Added support for binary PostScript through GhostScript if native DSC 
     26# compliant PostScript analyzer doesn't find any page. This is much 
     27# slower though, so native analyzer is tried first. 
     28# 
    2429# Revision 1.25  2004/07/10 14:06:36  jalet 
    2530# Fix for Python2.1 incompatibilities 
     
    108113import tempfile 
    109114import mmap 
     115import popen2 
    110116     
    111117KILOBYTE = 1024     
     
    125131        """Initialize PostScript Analyzer.""" 
    126132        self.infile = infile 
    127          
    128     def getJobSize(self) :     
     133        
     134    def throughGhostScript(self) : 
     135        """Get the count through GhostScript, useful for non-DSC compliant PS files.""" 
     136        self.infile.seek(0) 
     137        command = 'gs -sDEVICE=bbox -dNOPAUSE -dBATCH -dQUIET - 2>&1 | grep -c "%%HiResBoundingBox:" 2>/dev/null' 
     138        child = popen2.Popen4(command) 
     139        try : 
     140            data = self.infile.read(MEGABYTE)     
     141            while data : 
     142                child.tochild.write(data) 
     143                data = self.infile.read(MEGABYTE) 
     144            child.tochild.flush() 
     145            child.tochild.close()     
     146        except (IOError, OSError), msg :     
     147            raise PDLAnalyzerError, "Problem during analysis of Binary PostScript document." 
     148             
     149        pagecount = 0 
     150        try : 
     151            pagecount = int(child.fromchild.readline().strip()) 
     152        except (IOError, OSError, AttributeError, ValueError) : 
     153            raise PDLAnalyzerError, "Problem during analysis of Binary PostScript document." 
     154        child.fromchild.close() 
     155         
     156        try : 
     157            retcode = child.wait() 
     158        except OSError, msg :     
     159            raise PDLAnalyzerError, "Problem during analysis of Binary PostScript document." 
     160        return pagecount 
     161         
     162    def natively(self) : 
    129163        """Count pages in a DSC compliant PostScript document.""" 
     164        self.infile.seek(0) 
    130165        pagecount = 0 
    131166        for line in self.infile.xreadlines() :  
     
    133168                pagecount += 1 
    134169        return pagecount 
     170         
     171    def getJobSize(self) :     
     172        """Count pages in PostScript document.""" 
     173        return self.natively() or self.throughGhostScript() 
    135174         
    136175class PDFAnalyzer :