[200] | 1 | # |
---|
| 2 | # pkpgcounter : a generic Page Description Language parser |
---|
| 3 | # |
---|
[303] | 4 | # (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com> |
---|
[200] | 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 |
---|
[211] | 17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[200] | 18 | # |
---|
| 19 | # $Id$ |
---|
| 20 | # |
---|
| 21 | |
---|
| 22 | import sys |
---|
| 23 | import tempfile |
---|
[348] | 24 | import optparse |
---|
[200] | 25 | |
---|
[235] | 26 | import version, pdlparser, postscript, pdf, pcl345, pclxl, \ |
---|
[328] | 27 | escp2, dvi, tiff, ooo, zjstream |
---|
[200] | 28 | |
---|
| 29 | class PDLAnalyzer : |
---|
| 30 | """Class for PDL autodetection.""" |
---|
| 31 | def __init__(self, filename, debug=0) : |
---|
| 32 | """Initializes the PDL analyzer. |
---|
| 33 | |
---|
| 34 | filename is the name of the file or '-' for stdin. |
---|
| 35 | filename can also be a file-like object which |
---|
| 36 | supports read() and seek(). |
---|
| 37 | """ |
---|
| 38 | self.debug = debug |
---|
| 39 | self.filename = filename |
---|
| 40 | |
---|
| 41 | def getJobSize(self) : |
---|
| 42 | """Returns the job's size.""" |
---|
| 43 | self.openFile() |
---|
| 44 | try : |
---|
| 45 | pdlhandler = self.detectPDLHandler() |
---|
| 46 | except pdlparser.PDLParserError, msg : |
---|
| 47 | self.closeFile() |
---|
[220] | 48 | raise pdlparser.PDLParserError, "Unknown file format for %s (%s)" % (self.filename, msg) |
---|
[200] | 49 | else : |
---|
| 50 | try : |
---|
[220] | 51 | size = pdlhandler.getJobSize() |
---|
[200] | 52 | finally : |
---|
| 53 | self.closeFile() |
---|
| 54 | return size |
---|
| 55 | |
---|
| 56 | def openFile(self) : |
---|
| 57 | """Opens the job's data stream for reading.""" |
---|
| 58 | self.mustclose = 0 # by default we don't want to close the file when finished |
---|
| 59 | if hasattr(self.filename, "read") and hasattr(self.filename, "seek") : |
---|
| 60 | # filename is in fact a file-like object |
---|
| 61 | infile = self.filename |
---|
| 62 | elif self.filename == "-" : |
---|
| 63 | # we must read from stdin |
---|
| 64 | infile = sys.stdin |
---|
| 65 | else : |
---|
| 66 | # normal file |
---|
| 67 | self.infile = open(self.filename, "rb") |
---|
| 68 | self.mustclose = 1 |
---|
| 69 | return |
---|
| 70 | |
---|
| 71 | # Use a temporary file, always seekable contrary to standard input. |
---|
| 72 | self.infile = tempfile.TemporaryFile(mode="w+b") |
---|
| 73 | while 1 : |
---|
[220] | 74 | data = infile.read(pdlparser.MEGABYTE) |
---|
[200] | 75 | if not data : |
---|
| 76 | break |
---|
| 77 | self.infile.write(data) |
---|
| 78 | self.infile.flush() |
---|
| 79 | self.infile.seek(0) |
---|
| 80 | |
---|
| 81 | def closeFile(self) : |
---|
| 82 | """Closes the job's data stream if we can close it.""" |
---|
| 83 | if self.mustclose : |
---|
| 84 | self.infile.close() |
---|
| 85 | else : |
---|
| 86 | # if we don't have to close the file, then |
---|
| 87 | # ensure the file pointer is reset to the |
---|
| 88 | # start of the file in case the process wants |
---|
| 89 | # to read the file again. |
---|
| 90 | try : |
---|
| 91 | self.infile.seek(0) |
---|
| 92 | except : |
---|
| 93 | pass # probably stdin, which is not seekable |
---|
| 94 | |
---|
| 95 | def detectPDLHandler(self) : |
---|
| 96 | """Tries to autodetect the document format. |
---|
| 97 | |
---|
| 98 | Returns the correct PDL handler class or None if format is unknown |
---|
| 99 | """ |
---|
[220] | 100 | # Try to detect file type by reading first and last blocks of datas |
---|
| 101 | # Each parser can read them automatically, but here we do this only once. |
---|
[200] | 102 | self.infile.seek(0) |
---|
[220] | 103 | firstblock = self.infile.read(pdlparser.FIRSTBLOCKSIZE) |
---|
[200] | 104 | try : |
---|
[220] | 105 | self.infile.seek(-pdlparser.LASTBLOCKSIZE, 2) |
---|
| 106 | lastblock = self.infile.read(pdlparser.LASTBLOCKSIZE) |
---|
[200] | 107 | except IOError : |
---|
| 108 | lastblock = "" |
---|
| 109 | self.infile.seek(0) |
---|
[217] | 110 | if not firstblock : |
---|
[220] | 111 | raise pdlparser.PDLParserError, "input file %s is empty !" % str(self.filename) |
---|
[200] | 112 | else : |
---|
[220] | 113 | for module in (postscript, \ |
---|
| 114 | pclxl, \ |
---|
| 115 | pdf, \ |
---|
| 116 | pcl345, \ |
---|
| 117 | escp2, \ |
---|
| 118 | dvi, \ |
---|
[229] | 119 | tiff, \ |
---|
[328] | 120 | zjstream, \ |
---|
[229] | 121 | ooo) : |
---|
[220] | 122 | try : |
---|
[252] | 123 | return module.Parser(self.infile, self.debug, firstblock, lastblock) |
---|
[220] | 124 | except pdlparser.PDLParserError : |
---|
| 125 | pass # try next parser |
---|
[217] | 126 | raise pdlparser.PDLParserError, "Analysis of first data block failed." |
---|
[200] | 127 | |
---|
| 128 | def main() : |
---|
| 129 | """Entry point for PDL Analyzer.""" |
---|
[348] | 130 | parser = optparse.OptionParser(usage="python analyzer.py [options] file1 [file2 ...]") |
---|
| 131 | parser.add_option("-v", "--version", |
---|
| 132 | action="store_true", |
---|
| 133 | dest="version", |
---|
| 134 | help="show pkpgcounter's version number and exit.") |
---|
| 135 | parser.add_option("-d", "--debug", |
---|
| 136 | action="store_true", |
---|
| 137 | dest="debug", |
---|
| 138 | help="activate debug mode.") |
---|
| 139 | (options, arguments) = parser.parse_args() |
---|
| 140 | if options.version : |
---|
[200] | 141 | print "%s" % version.__version__ |
---|
| 142 | else : |
---|
[348] | 143 | if (not arguments) or ((not sys.stdin.isatty()) and ("-" not in arguments)) : |
---|
| 144 | arguments.append("-") |
---|
[200] | 145 | totalsize = 0 |
---|
[348] | 146 | for arg in arguments : |
---|
[200] | 147 | try : |
---|
[348] | 148 | parser = PDLAnalyzer(arg, options.debug) |
---|
[200] | 149 | totalsize += parser.getJobSize() |
---|
[348] | 150 | except (IOError, pdlparser.PDLParserError), msg : |
---|
[200] | 151 | sys.stderr.write("ERROR: %s\n" % msg) |
---|
| 152 | sys.stderr.flush() |
---|
| 153 | print "%s" % totalsize |
---|
| 154 | |
---|
| 155 | if __name__ == "__main__" : |
---|
| 156 | main() |
---|