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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
18 | # |
---|
19 | # $Id$ |
---|
20 | # |
---|
21 | |
---|
22 | import sys |
---|
23 | import tempfile |
---|
24 | |
---|
25 | from pdlanalyzer import version, pdlparser, postscript, pdf, pcl345, pclxl, escp2 |
---|
26 | |
---|
27 | KILOBYTE = 1024 |
---|
28 | MEGABYTE = 1024 * KILOBYTE |
---|
29 | LASTBLOCKSIZE = int(KILOBYTE / 4) |
---|
30 | |
---|
31 | class 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 | |
---|
60 | def getJobSize(self) : |
---|
61 | """Returns the job's size.""" |
---|
62 | self.openFile() |
---|
63 | try : |
---|
64 | pdlhandler = self.detectPDLHandler() |
---|
65 | except pdlparser.PDLParserError, msg : |
---|
66 | self.closeFile() |
---|
67 | raise pdlparser.PDLParserError, "ERROR : Unknown file format for %s (%s)" % (self.filename, msg) |
---|
68 | else : |
---|
69 | try : |
---|
70 | size = pdlhandler(self.infile, self.debug).getJobSize() |
---|
71 | finally : |
---|
72 | self.closeFile() |
---|
73 | return size |
---|
74 | |
---|
75 | def openFile(self) : |
---|
76 | """Opens the job's data stream for reading.""" |
---|
77 | self.mustclose = 0 # by default we don't want to close the file when finished |
---|
78 | if hasattr(self.filename, "read") and hasattr(self.filename, "seek") : |
---|
79 | # filename is in fact a file-like object |
---|
80 | infile = self.filename |
---|
81 | elif self.filename == "-" : |
---|
82 | # we must read from stdin |
---|
83 | infile = sys.stdin |
---|
84 | else : |
---|
85 | # normal file |
---|
86 | self.infile = open(self.filename, "rb") |
---|
87 | self.mustclose = 1 |
---|
88 | return |
---|
89 | |
---|
90 | # Use a temporary file, always seekable contrary to standard input. |
---|
91 | self.infile = tempfile.TemporaryFile(mode="w+b") |
---|
92 | while 1 : |
---|
93 | data = infile.read(MEGABYTE) |
---|
94 | if not data : |
---|
95 | break |
---|
96 | self.infile.write(data) |
---|
97 | self.infile.flush() |
---|
98 | self.infile.seek(0) |
---|
99 | |
---|
100 | def closeFile(self) : |
---|
101 | """Closes the job's data stream if we can close it.""" |
---|
102 | if self.mustclose : |
---|
103 | self.infile.close() |
---|
104 | else : |
---|
105 | # if we don't have to close the file, then |
---|
106 | # ensure the file pointer is reset to the |
---|
107 | # start of the file in case the process wants |
---|
108 | # to read the file again. |
---|
109 | try : |
---|
110 | self.infile.seek(0) |
---|
111 | except : |
---|
112 | pass # probably stdin, which is not seekable |
---|
113 | |
---|
114 | def isPostScript(self, sdata, edata) : |
---|
115 | """Returns 1 if data is PostScript, else 0.""" |
---|
116 | if sdata.startswith("%!") or \ |
---|
117 | sdata.startswith("\004%!") or \ |
---|
118 | sdata.startswith("\033%-12345X%!PS") or \ |
---|
119 | ((sdata[:128].find("\033%-12345X") != -1) and \ |
---|
120 | ((sdata.find("LANGUAGE=POSTSCRIPT") != -1) or \ |
---|
121 | (sdata.find("LANGUAGE = POSTSCRIPT") != -1) or \ |
---|
122 | (sdata.find("LANGUAGE = Postscript") != -1))) or \ |
---|
123 | (sdata.find("%!PS-Adobe") != -1) : |
---|
124 | if self.debug : |
---|
125 | sys.stderr.write("%s is a PostScript file\n" % str(self.filename)) |
---|
126 | return 1 |
---|
127 | else : |
---|
128 | return 0 |
---|
129 | |
---|
130 | def isPDF(self, sdata, edata) : |
---|
131 | """Returns 1 if data is PDF, else 0.""" |
---|
132 | if sdata.startswith("%PDF-") or \ |
---|
133 | sdata.startswith("\033%-12345X%PDF-") or \ |
---|
134 | ((sdata[:128].find("\033%-12345X") != -1) and (sdata.upper().find("LANGUAGE=PDF") != -1)) or \ |
---|
135 | (sdata.find("%PDF-") != -1) : |
---|
136 | if self.debug : |
---|
137 | sys.stderr.write("%s is a PDF file\n" % str(self.filename)) |
---|
138 | return 1 |
---|
139 | else : |
---|
140 | return 0 |
---|
141 | |
---|
142 | def isPCL(self, sdata, edata) : |
---|
143 | """Returns 1 if data is PCL, else 0.""" |
---|
144 | if sdata.startswith("\033E\033") or \ |
---|
145 | (sdata.startswith("\033*rbC") and (not edata[-3:] == "\f\033@")) or \ |
---|
146 | sdata.startswith("\033%8\033") or \ |
---|
147 | (sdata.find("\033%-12345X") != -1) : |
---|
148 | if self.debug : |
---|
149 | sys.stderr.write("%s is a PCL3/4/5 file\n" % str(self.filename)) |
---|
150 | return 1 |
---|
151 | else : |
---|
152 | return 0 |
---|
153 | |
---|
154 | def isPCLXL(self, sdata, edata) : |
---|
155 | """Returns 1 if data is PCLXL aka PCL6, else 0.""" |
---|
156 | if ((sdata[:128].find("\033%-12345X") != -1) and \ |
---|
157 | (sdata.find(" HP-PCL XL;") != -1) and \ |
---|
158 | ((sdata.find("LANGUAGE=PCLXL") != -1) or \ |
---|
159 | (sdata.find("LANGUAGE = PCLXL") != -1))) : |
---|
160 | if self.debug : |
---|
161 | sys.stderr.write("%s is a PCLXL (aka PCL6) file\n" % str(self.filename)) |
---|
162 | return 1 |
---|
163 | else : |
---|
164 | return 0 |
---|
165 | |
---|
166 | def isESCP2(self, sdata, edata) : |
---|
167 | """Returns 1 if data is ESC/P2, else 0.""" |
---|
168 | if sdata.startswith("\033@") or \ |
---|
169 | sdata.startswith("\033*") or \ |
---|
170 | sdata.startswith("\n\033@") or \ |
---|
171 | sdata.startswith("\0\0\0\033\1@EJL") : # ESC/P Raster ??? Seen on Stylus Photo 1284 |
---|
172 | if self.debug : |
---|
173 | sys.stderr.write("%s is an ESC/P2 file\n" % str(self.filename)) |
---|
174 | return 1 |
---|
175 | else : |
---|
176 | return 0 |
---|
177 | |
---|
178 | def detectPDLHandler(self) : |
---|
179 | """Tries to autodetect the document format. |
---|
180 | |
---|
181 | Returns the correct PDL handler class or None if format is unknown |
---|
182 | """ |
---|
183 | # Try to detect file type by reading first block of datas |
---|
184 | self.infile.seek(0) |
---|
185 | firstblock = self.infile.read(4 * KILOBYTE) |
---|
186 | try : |
---|
187 | self.infile.seek(-LASTBLOCKSIZE, 2) |
---|
188 | lastblock = self.infile.read(LASTBLOCKSIZE) |
---|
189 | except IOError : |
---|
190 | lastblock = "" |
---|
191 | self.infile.seek(0) |
---|
192 | if self.isPostScript(firstblock, lastblock) : |
---|
193 | return postscript.PostScriptParser |
---|
194 | elif self.isPCLXL(firstblock, lastblock) : |
---|
195 | return pclxl.PCLXLParser |
---|
196 | elif self.isPDF(firstblock, lastblock) : |
---|
197 | return pdf.PDFParser |
---|
198 | elif self.isPCL(firstblock, lastblock) : |
---|
199 | return pcl345.PCL345Parser |
---|
200 | elif self.isESCP2(firstblock, lastblock) : |
---|
201 | return escp2.ESCP2Parser |
---|
202 | else : |
---|
203 | raise pdlparser.PDLParserError, "Analysis of first data block failed." |
---|
204 | |
---|
205 | def main() : |
---|
206 | """Entry point for PDL Analyzer.""" |
---|
207 | if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) : |
---|
208 | sys.argv.append("-") |
---|
209 | |
---|
210 | if ("-h" in sys.argv[1:]) or ("--help" in sys.argv[1:]) : |
---|
211 | print "usage : pkpgcounter file1 file2 ... fileN" |
---|
212 | elif ("-v" in sys.argv[1:]) or ("--version" in sys.argv[1:]) : |
---|
213 | print "%s" % version.__version__ |
---|
214 | else : |
---|
215 | totalsize = 0 |
---|
216 | debug = 0 |
---|
217 | minindex = 1 |
---|
218 | if sys.argv[1] == "--debug" : |
---|
219 | minindex = 2 |
---|
220 | debug = 1 |
---|
221 | for arg in sys.argv[minindex:] : |
---|
222 | try : |
---|
223 | parser = PDLAnalyzer(arg, debug) |
---|
224 | totalsize += parser.getJobSize() |
---|
225 | except pdlparser.PDLParserError, msg : |
---|
226 | sys.stderr.write("ERROR: %s\n" % msg) |
---|
227 | sys.stderr.flush() |
---|
228 | print "%s" % totalsize |
---|
229 | |
---|
230 | if __name__ == "__main__" : |
---|
231 | main() |
---|