root / pkpgcounter / trunk / pdlanalyzer / analyzer.py @ 216

Revision 216, 9.4 kB (checked in by jerome, 19 years ago)

Added support for the DVI format

  • Property svn:keywords set to Auth Date Id Rev
Line 
1#
2# pkpgcounter : a generic Page Description Language parser
3#
4# (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com>
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18#
19# $Id$
20#
21
22import sys
23import tempfile
24
25from pdlanalyzer import version, pdlparser, postscript, pdf, pcl345, pclxl, escp2, dvi
26
27KILOBYTE = 1024   
28MEGABYTE = 1024 * KILOBYTE   
29LASTBLOCKSIZE = int(KILOBYTE / 4)
30
31class PDLAnalyzer :   
32    """Class for PDL autodetection."""
33    def __init__(self, filename, debug=0) :
34        """Initializes the PDL analyzer.
35       
36           filename is the name of the file or '-' for stdin.
37           filename can also be a file-like object which
38           supports read() and seek().
39        """
40        self.debug = debug
41        self.filename = filename
42        try :
43            import psyco 
44        except ImportError :   
45            sys.stderr.write("pkpgcounter : you should install psyco if possible, this would greatly speedup parsing.\n")
46            pass # Psyco is not installed
47        else :   
48            # Psyco is installed, tell it to compile
49            # the CPU intensive methods : PCL and PCLXL
50            # parsing will greatly benefit from this,
51            # for PostScript and PDF the difference is
52            # barely noticeable since they are already
53            # almost optimal, and much more speedy anyway.
54            psyco.bind(postscript.PostScriptParser.getJobSize)
55            psyco.bind(pdf.PDFParser.getJobSize)
56            psyco.bind(escp2.ESCP2Parser.getJobSize)
57            psyco.bind(pcl345.PCL345Parser.getJobSize)
58            psyco.bind(pclxl.PCLXLParser.getJobSize)
59            psyco.bind(dvi.DVIParser.getJobSize)
60       
61    def getJobSize(self) :   
62        """Returns the job's size."""
63        self.openFile()
64        try :
65            pdlhandler = self.detectPDLHandler()
66        except pdlparser.PDLParserError, msg :   
67            self.closeFile()
68            raise pdlparser.PDLParserError, "ERROR : Unknown file format for %s (%s)" % (self.filename, msg)
69        else :
70            try :
71                size = pdlhandler(self.infile, self.debug).getJobSize()
72            finally :   
73                self.closeFile()
74            return size
75       
76    def openFile(self) :   
77        """Opens the job's data stream for reading."""
78        self.mustclose = 0  # by default we don't want to close the file when finished
79        if hasattr(self.filename, "read") and hasattr(self.filename, "seek") :
80            # filename is in fact a file-like object
81            infile = self.filename
82        elif self.filename == "-" :
83            # we must read from stdin
84            infile = sys.stdin
85        else :   
86            # normal file
87            self.infile = open(self.filename, "rb")
88            self.mustclose = 1
89            return
90           
91        # Use a temporary file, always seekable contrary to standard input.
92        self.infile = tempfile.TemporaryFile(mode="w+b")
93        while 1 :
94            data = infile.read(MEGABYTE) 
95            if not data :
96                break
97            self.infile.write(data)
98        self.infile.flush()   
99        self.infile.seek(0)
100           
101    def closeFile(self) :       
102        """Closes the job's data stream if we can close it."""
103        if self.mustclose :
104            self.infile.close()   
105        else :   
106            # if we don't have to close the file, then
107            # ensure the file pointer is reset to the
108            # start of the file in case the process wants
109            # to read the file again.
110            try :
111                self.infile.seek(0)
112            except :   
113                pass    # probably stdin, which is not seekable
114       
115    def isPostScript(self, sdata, edata) :   
116        """Returns 1 if data is PostScript, else 0."""
117        if sdata.startswith("%!") or \
118           sdata.startswith("\004%!") or \
119           sdata.startswith("\033%-12345X%!PS") or \
120           ((sdata[:128].find("\033%-12345X") != -1) and \
121             ((sdata.find("LANGUAGE=POSTSCRIPT") != -1) or \
122              (sdata.find("LANGUAGE = POSTSCRIPT") != -1) or \
123              (sdata.find("LANGUAGE = Postscript") != -1))) or \
124              (sdata.find("%!PS-Adobe") != -1) :
125            if self.debug : 
126                sys.stderr.write("%s is a PostScript file\n" % str(self.filename))
127            return 1
128        else :   
129            return 0
130       
131    def isPDF(self, sdata, edata) :   
132        """Returns 1 if data is PDF, else 0."""
133        if sdata.startswith("%PDF-") or \
134           sdata.startswith("\033%-12345X%PDF-") or \
135           ((sdata[:128].find("\033%-12345X") != -1) and (sdata.upper().find("LANGUAGE=PDF") != -1)) or \
136           (sdata.find("%PDF-") != -1) :
137            if self.debug : 
138                sys.stderr.write("%s is a PDF file\n" % str(self.filename))
139            return 1
140        else :   
141            return 0
142       
143    def isPCL(self, sdata, edata) :   
144        """Returns 1 if data is PCL, else 0."""
145        if sdata.startswith("\033E\033") or \
146           (sdata.startswith("\033*rbC") and (not edata[-3:] == "\f\033@")) or \
147           sdata.startswith("\033%8\033") or \
148           (sdata.find("\033%-12345X") != -1) :
149            if self.debug : 
150                sys.stderr.write("%s is a PCL3/4/5 file\n" % str(self.filename))
151            return 1
152        else :   
153            return 0
154       
155    def isPCLXL(self, sdata, edata) :   
156        """Returns 1 if data is PCLXL aka PCL6, else 0."""
157        if ((sdata[:128].find("\033%-12345X") != -1) and \
158             (sdata.find(" HP-PCL XL;") != -1) and \
159             ((sdata.find("LANGUAGE=PCLXL") != -1) or \
160              (sdata.find("LANGUAGE = PCLXL") != -1))) :
161            if self.debug : 
162                sys.stderr.write("%s is a PCLXL (aka PCL6) file\n" % str(self.filename))
163            return 1
164        else :   
165            return 0
166           
167    def isESCP2(self, sdata, edata) :       
168        """Returns 1 if data is ESC/P2, else 0."""
169        if sdata.startswith("\033@") or \
170           sdata.startswith("\033*") or \
171           sdata.startswith("\n\033@") or \
172           sdata.startswith("\0\0\0\033\1@EJL") : # ESC/P Raster ??? Seen on Stylus Photo 1284
173            if self.debug : 
174                sys.stderr.write("%s is an ESC/P2 file\n" % str(self.filename))
175            return 1
176        else :   
177            return 0
178           
179    def isDVI(self, sdata, edata) :       
180        """Returns 1 if data is DVI, else 0."""
181        if (ord(sdata[0]) == 0xf7) and (ord(edata[-1]) == 0xdf) :
182            if self.debug : 
183                sys.stderr.write("%s is an DVI file\n" % str(self.filename))
184            return 1
185        else :   
186            return 0
187   
188    def detectPDLHandler(self) :   
189        """Tries to autodetect the document format.
190       
191           Returns the correct PDL handler class or None if format is unknown
192        """   
193        # Try to detect file type by reading first block of datas   
194        self.infile.seek(0)
195        firstblock = self.infile.read(16 * KILOBYTE)
196        try :
197            self.infile.seek(-LASTBLOCKSIZE, 2)
198            lastblock = self.infile.read(LASTBLOCKSIZE)
199        except IOError :   
200            lastblock = ""
201        self.infile.seek(0)
202        if self.isPostScript(firstblock, lastblock) :
203            return postscript.PostScriptParser
204        elif self.isPCLXL(firstblock, lastblock) :   
205            return pclxl.PCLXLParser
206        elif self.isPDF(firstblock, lastblock) :   
207            return pdf.PDFParser
208        elif self.isPCL(firstblock, lastblock) :   
209            return pcl345.PCL345Parser
210        elif self.isESCP2(firstblock, lastblock) :   
211            return escp2.ESCP2Parser
212        elif self.isDVI(firstblock, lastblock) :   
213            return dvi.DVIParser
214        else :   
215            raise pdlparser.PDLParserError, "Analysis of first data block failed."
216           
217def main() :   
218    """Entry point for PDL Analyzer."""
219    if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) :
220        sys.argv.append("-")
221       
222    if ("-h" in sys.argv[1:]) or ("--help" in sys.argv[1:]) :
223        print "usage : pkpgcounter file1 file2 ... fileN"
224    elif ("-v" in sys.argv[1:]) or ("--version" in sys.argv[1:]) :
225        print "%s" % version.__version__
226    else :
227        totalsize = 0   
228        debug = 0
229        minindex = 1
230        if sys.argv[1] == "--debug" :
231            minindex = 2
232            debug = 1
233        for arg in sys.argv[minindex:] :
234            try :
235                parser = PDLAnalyzer(arg, debug)
236                totalsize += parser.getJobSize()
237            except pdlparser.PDLParserError, msg :   
238                sys.stderr.write("ERROR: %s\n" % msg)
239                sys.stderr.flush()
240        print "%s" % totalsize
241   
242if __name__ == "__main__" :   
243    main()
Note: See TracBrowser for help on using the browser.