[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 |
---|
| 24 | |
---|
[235] | 25 | import version, pdlparser, postscript, pdf, pcl345, pclxl, \ |
---|
[328] | 26 | escp2, dvi, tiff, ooo, zjstream |
---|
[200] | 27 | |
---|
[351] | 28 | class NoOption : |
---|
| 29 | """A class for use as a default parameter to PDLAnalyzer's constructor.""" |
---|
| 30 | debug = None |
---|
| 31 | colorspace = None |
---|
| 32 | resolution = None |
---|
| 33 | |
---|
[200] | 34 | class PDLAnalyzer : |
---|
| 35 | """Class for PDL autodetection.""" |
---|
[351] | 36 | def __init__(self, filename, options=NoOption()) : |
---|
[200] | 37 | """Initializes the PDL analyzer. |
---|
| 38 | |
---|
| 39 | filename is the name of the file or '-' for stdin. |
---|
| 40 | filename can also be a file-like object which |
---|
| 41 | supports read() and seek(). |
---|
| 42 | """ |
---|
[351] | 43 | self.options = options |
---|
[200] | 44 | self.filename = filename |
---|
| 45 | |
---|
| 46 | def getJobSize(self) : |
---|
| 47 | """Returns the job's size.""" |
---|
| 48 | self.openFile() |
---|
| 49 | try : |
---|
| 50 | pdlhandler = self.detectPDLHandler() |
---|
| 51 | except pdlparser.PDLParserError, msg : |
---|
| 52 | self.closeFile() |
---|
[220] | 53 | raise pdlparser.PDLParserError, "Unknown file format for %s (%s)" % (self.filename, msg) |
---|
[200] | 54 | else : |
---|
| 55 | try : |
---|
[220] | 56 | size = pdlhandler.getJobSize() |
---|
[200] | 57 | finally : |
---|
| 58 | self.closeFile() |
---|
| 59 | return size |
---|
| 60 | |
---|
| 61 | def openFile(self) : |
---|
| 62 | """Opens the job's data stream for reading.""" |
---|
| 63 | self.mustclose = 0 # by default we don't want to close the file when finished |
---|
| 64 | if hasattr(self.filename, "read") and hasattr(self.filename, "seek") : |
---|
| 65 | # filename is in fact a file-like object |
---|
| 66 | infile = self.filename |
---|
| 67 | elif self.filename == "-" : |
---|
| 68 | # we must read from stdin |
---|
| 69 | infile = sys.stdin |
---|
| 70 | else : |
---|
| 71 | # normal file |
---|
| 72 | self.infile = open(self.filename, "rb") |
---|
| 73 | self.mustclose = 1 |
---|
| 74 | return |
---|
| 75 | |
---|
| 76 | # Use a temporary file, always seekable contrary to standard input. |
---|
| 77 | self.infile = tempfile.TemporaryFile(mode="w+b") |
---|
| 78 | while 1 : |
---|
[220] | 79 | data = infile.read(pdlparser.MEGABYTE) |
---|
[200] | 80 | if not data : |
---|
| 81 | break |
---|
| 82 | self.infile.write(data) |
---|
| 83 | self.infile.flush() |
---|
| 84 | self.infile.seek(0) |
---|
| 85 | |
---|
| 86 | def closeFile(self) : |
---|
| 87 | """Closes the job's data stream if we can close it.""" |
---|
| 88 | if self.mustclose : |
---|
| 89 | self.infile.close() |
---|
| 90 | else : |
---|
| 91 | # if we don't have to close the file, then |
---|
| 92 | # ensure the file pointer is reset to the |
---|
| 93 | # start of the file in case the process wants |
---|
| 94 | # to read the file again. |
---|
| 95 | try : |
---|
| 96 | self.infile.seek(0) |
---|
| 97 | except : |
---|
| 98 | pass # probably stdin, which is not seekable |
---|
| 99 | |
---|
| 100 | def detectPDLHandler(self) : |
---|
| 101 | """Tries to autodetect the document format. |
---|
| 102 | |
---|
| 103 | Returns the correct PDL handler class or None if format is unknown |
---|
| 104 | """ |
---|
[220] | 105 | # Try to detect file type by reading first and last blocks of datas |
---|
| 106 | # Each parser can read them automatically, but here we do this only once. |
---|
[200] | 107 | self.infile.seek(0) |
---|
[220] | 108 | firstblock = self.infile.read(pdlparser.FIRSTBLOCKSIZE) |
---|
[200] | 109 | try : |
---|
[220] | 110 | self.infile.seek(-pdlparser.LASTBLOCKSIZE, 2) |
---|
| 111 | lastblock = self.infile.read(pdlparser.LASTBLOCKSIZE) |
---|
[200] | 112 | except IOError : |
---|
| 113 | lastblock = "" |
---|
| 114 | self.infile.seek(0) |
---|
[217] | 115 | if not firstblock : |
---|
[220] | 116 | raise pdlparser.PDLParserError, "input file %s is empty !" % str(self.filename) |
---|
[200] | 117 | else : |
---|
[220] | 118 | for module in (postscript, \ |
---|
| 119 | pclxl, \ |
---|
| 120 | pdf, \ |
---|
| 121 | pcl345, \ |
---|
| 122 | escp2, \ |
---|
| 123 | dvi, \ |
---|
[229] | 124 | tiff, \ |
---|
[328] | 125 | zjstream, \ |
---|
[229] | 126 | ooo) : |
---|
[220] | 127 | try : |
---|
[351] | 128 | return module.Parser(self.infile, self.options.debug, firstblock, lastblock) |
---|
[220] | 129 | except pdlparser.PDLParserError : |
---|
| 130 | pass # try next parser |
---|
[217] | 131 | raise pdlparser.PDLParserError, "Analysis of first data block failed." |
---|
[200] | 132 | |
---|
| 133 | def main() : |
---|
| 134 | """Entry point for PDL Analyzer.""" |
---|
[350] | 135 | import optparse |
---|
| 136 | from copy import copy |
---|
| 137 | |
---|
| 138 | def check_cichoice(option, opt, value) : |
---|
| 139 | """To add a CaseIgnore Choice option type.""" |
---|
| 140 | valower = value.lower() |
---|
| 141 | if valower in [v.lower() for v in option.cichoices] : |
---|
| 142 | return valower |
---|
| 143 | else : |
---|
| 144 | choices = ", ".join(map(repr, option.cichoices)) |
---|
| 145 | raise optparse.OptionValueError( |
---|
| 146 | "option %s: invalid choice: %r (choose from %s)" |
---|
| 147 | % (opt, value, choices)) |
---|
| 148 | |
---|
| 149 | class MyOption(optparse.Option) : |
---|
| 150 | """New Option class, with CaseIgnore Choice type.""" |
---|
| 151 | TYPES = optparse.Option.TYPES + ("cichoice",) |
---|
| 152 | ATTRS = optparse.Option.ATTRS + ["cichoices"] |
---|
| 153 | TYPE_CHECKER = copy(optparse.Option.TYPE_CHECKER) |
---|
| 154 | TYPE_CHECKER["cichoice"] = check_cichoice |
---|
| 155 | |
---|
| 156 | parser = optparse.OptionParser(option_class=MyOption, |
---|
| 157 | usage="python analyzer.py [options] file1 [file2 ...]") |
---|
[348] | 158 | parser.add_option("-v", "--version", |
---|
| 159 | action="store_true", |
---|
| 160 | dest="version", |
---|
[350] | 161 | help="Show pkpgcounter's version number and exit.") |
---|
[348] | 162 | parser.add_option("-d", "--debug", |
---|
| 163 | action="store_true", |
---|
| 164 | dest="debug", |
---|
[350] | 165 | help="Activate debug mode.") |
---|
| 166 | parser.add_option("-c", "--colorspace", |
---|
| 167 | dest="colorspace", |
---|
| 168 | type="cichoice", |
---|
| 169 | cichoices=["bw", "cmyk", "cmy", "all"], |
---|
| 170 | help="Activate the computation of ink usage, and defines the colorspace to use. Supported values are 'BW', 'CMYK', 'CMY' and 'ALL'.") |
---|
| 171 | parser.add_option("-r", "--resolution", |
---|
| 172 | type="int", |
---|
| 173 | default=150, |
---|
| 174 | dest="resolution", |
---|
| 175 | help="The resolution in DPI to use when checking ink usage. Lower resolution is faster. Default is 150.") |
---|
[348] | 176 | (options, arguments) = parser.parse_args() |
---|
| 177 | if options.version : |
---|
[200] | 178 | print "%s" % version.__version__ |
---|
| 179 | else : |
---|
[348] | 180 | if (not arguments) or ((not sys.stdin.isatty()) and ("-" not in arguments)) : |
---|
| 181 | arguments.append("-") |
---|
[200] | 182 | totalsize = 0 |
---|
[350] | 183 | try : |
---|
| 184 | for arg in arguments : |
---|
| 185 | try : |
---|
[351] | 186 | parser = PDLAnalyzer(arg, options) |
---|
[350] | 187 | totalsize += parser.getJobSize() |
---|
| 188 | except (IOError, pdlparser.PDLParserError), msg : |
---|
| 189 | sys.stderr.write("ERROR: %s\n" % msg) |
---|
| 190 | sys.stderr.flush() |
---|
| 191 | except KeyboardInterrupt : |
---|
| 192 | sys.stderr.write("WARN: Aborted at user's request.\n") |
---|
| 193 | sys.stderr.flush() |
---|
[200] | 194 | print "%s" % totalsize |
---|
| 195 | |
---|
| 196 | if __name__ == "__main__" : |
---|
| 197 | main() |
---|