[200] | 1 | # |
---|
| 2 | # pkpgcounter : a generic Page Description Language parser |
---|
| 3 | # |
---|
[443] | 4 | # (c) 2003, 2004, 2005, 2006, 2007 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 | |
---|
[353] | 22 | """This is the main module of pkpgcounter. |
---|
| 23 | |
---|
| 24 | It defines the PDLAnalyzer class, which provides a generic way to parse |
---|
| 25 | input files, by automatically detecting the best parser to use.""" |
---|
| 26 | |
---|
[200] | 27 | import sys |
---|
[363] | 28 | import os |
---|
[200] | 29 | import tempfile |
---|
| 30 | |
---|
[235] | 31 | import version, pdlparser, postscript, pdf, pcl345, pclxl, \ |
---|
[412] | 32 | escp2, dvi, tiff, ooo, zjstream, qpdl, spl1, plain |
---|
[363] | 33 | import inkcoverage |
---|
[200] | 34 | |
---|
[358] | 35 | class AnalyzerOptions : |
---|
[353] | 36 | """A class for use as the options parameter to PDLAnalyzer's constructor.""" |
---|
| 37 | def __init__(self, debug=None, |
---|
| 38 | colorspace=None, |
---|
| 39 | resolution=None) : |
---|
| 40 | """Sets initial attributes.""" |
---|
| 41 | self.debug = debug |
---|
| 42 | self.colorspace = colorspace |
---|
| 43 | self.resolution = resolution |
---|
[351] | 44 | |
---|
[353] | 45 | |
---|
[200] | 46 | class PDLAnalyzer : |
---|
| 47 | """Class for PDL autodetection.""" |
---|
[358] | 48 | def __init__(self, filename, options=AnalyzerOptions()) : |
---|
[200] | 49 | """Initializes the PDL analyzer. |
---|
| 50 | |
---|
| 51 | filename is the name of the file or '-' for stdin. |
---|
| 52 | filename can also be a file-like object which |
---|
| 53 | supports read() and seek(). |
---|
| 54 | """ |
---|
[351] | 55 | self.options = options |
---|
[200] | 56 | self.filename = filename |
---|
[353] | 57 | self.infile = None |
---|
| 58 | self.mustclose = None |
---|
[200] | 59 | |
---|
| 60 | def getJobSize(self) : |
---|
| 61 | """Returns the job's size.""" |
---|
[370] | 62 | size = 0 |
---|
[200] | 63 | self.openFile() |
---|
| 64 | try : |
---|
| 65 | try : |
---|
[411] | 66 | pdlhandler = self.detectPDLHandler() |
---|
[220] | 67 | size = pdlhandler.getJobSize() |
---|
[411] | 68 | except pdlparser.PDLParserError, msg : |
---|
| 69 | raise pdlparser.PDLParserError, "Unknown file format for %s (%s)" % (self.filename, msg) |
---|
| 70 | finally : |
---|
| 71 | self.closeFile() |
---|
[370] | 72 | return size |
---|
[363] | 73 | |
---|
| 74 | def getInkCoverage(self, colorspace=None, resolution=None) : |
---|
| 75 | """Extracts the percents of ink coverage from the input file.""" |
---|
| 76 | result = None |
---|
| 77 | cspace = colorspace or self.options.colorspace |
---|
| 78 | res = resolution or self.options.resolution |
---|
| 79 | if (not cspace) or (not res) : |
---|
| 80 | raise ValueError, "Invalid colorspace (%s) or resolution (%s)" % (cspace, res) |
---|
| 81 | self.openFile() |
---|
| 82 | try : |
---|
| 83 | try : |
---|
[411] | 84 | pdlhandler = self.detectPDLHandler() |
---|
[434] | 85 | (handle, filename) = tempfile.mkstemp(".tmp", "pkpgcounter") |
---|
| 86 | os.close(handle) |
---|
[363] | 87 | try : |
---|
[434] | 88 | pdlhandler.convertToTiffMultiPage24NC(filename, self.options.resolution) |
---|
| 89 | result = inkcoverage.getInkCoverage(filename, cspace) |
---|
[427] | 90 | finally : |
---|
| 91 | try : |
---|
[434] | 92 | os.remove(filename) |
---|
[427] | 93 | except OSError : |
---|
[434] | 94 | sys.stderr.write("Problem when trying to remove temporary file %s\n" % filename) |
---|
[411] | 95 | except pdlparser.PDLParserError, msg : |
---|
| 96 | raise pdlparser.PDLParserError, "Unknown file format for %s (%s)" % (self.filename, msg) |
---|
| 97 | finally : |
---|
| 98 | self.closeFile() |
---|
[363] | 99 | return result |
---|
[200] | 100 | |
---|
| 101 | def openFile(self) : |
---|
| 102 | """Opens the job's data stream for reading.""" |
---|
| 103 | self.mustclose = 0 # by default we don't want to close the file when finished |
---|
| 104 | if hasattr(self.filename, "read") and hasattr(self.filename, "seek") : |
---|
| 105 | # filename is in fact a file-like object |
---|
| 106 | infile = self.filename |
---|
| 107 | elif self.filename == "-" : |
---|
| 108 | # we must read from stdin |
---|
| 109 | infile = sys.stdin |
---|
| 110 | else : |
---|
| 111 | # normal file |
---|
| 112 | self.infile = open(self.filename, "rb") |
---|
| 113 | self.mustclose = 1 |
---|
| 114 | return |
---|
| 115 | |
---|
| 116 | # Use a temporary file, always seekable contrary to standard input. |
---|
| 117 | self.infile = tempfile.TemporaryFile(mode="w+b") |
---|
| 118 | while 1 : |
---|
[220] | 119 | data = infile.read(pdlparser.MEGABYTE) |
---|
[200] | 120 | if not data : |
---|
| 121 | break |
---|
| 122 | self.infile.write(data) |
---|
| 123 | self.infile.flush() |
---|
| 124 | self.infile.seek(0) |
---|
| 125 | |
---|
| 126 | def closeFile(self) : |
---|
| 127 | """Closes the job's data stream if we can close it.""" |
---|
| 128 | if self.mustclose : |
---|
| 129 | self.infile.close() |
---|
| 130 | else : |
---|
| 131 | # if we don't have to close the file, then |
---|
| 132 | # ensure the file pointer is reset to the |
---|
| 133 | # start of the file in case the process wants |
---|
| 134 | # to read the file again. |
---|
| 135 | try : |
---|
| 136 | self.infile.seek(0) |
---|
[353] | 137 | except IOError : |
---|
[200] | 138 | pass # probably stdin, which is not seekable |
---|
| 139 | |
---|
| 140 | def detectPDLHandler(self) : |
---|
| 141 | """Tries to autodetect the document format. |
---|
| 142 | |
---|
| 143 | Returns the correct PDL handler class or None if format is unknown |
---|
| 144 | """ |
---|
[220] | 145 | # Try to detect file type by reading first and last blocks of datas |
---|
| 146 | # Each parser can read them automatically, but here we do this only once. |
---|
[200] | 147 | self.infile.seek(0) |
---|
[220] | 148 | firstblock = self.infile.read(pdlparser.FIRSTBLOCKSIZE) |
---|
[200] | 149 | try : |
---|
[220] | 150 | self.infile.seek(-pdlparser.LASTBLOCKSIZE, 2) |
---|
| 151 | lastblock = self.infile.read(pdlparser.LASTBLOCKSIZE) |
---|
[200] | 152 | except IOError : |
---|
| 153 | lastblock = "" |
---|
| 154 | self.infile.seek(0) |
---|
[217] | 155 | if not firstblock : |
---|
[220] | 156 | raise pdlparser.PDLParserError, "input file %s is empty !" % str(self.filename) |
---|
[200] | 157 | else : |
---|
[386] | 158 | # IMPORTANT : the order is important below. FIXME. |
---|
[220] | 159 | for module in (postscript, \ |
---|
| 160 | pclxl, \ |
---|
| 161 | pdf, \ |
---|
[386] | 162 | qpdl, \ |
---|
[410] | 163 | spl1, \ |
---|
[220] | 164 | dvi, \ |
---|
[229] | 165 | tiff, \ |
---|
[328] | 166 | zjstream, \ |
---|
[412] | 167 | ooo, \ |
---|
[416] | 168 | pcl345, \ |
---|
| 169 | escp2, \ |
---|
[412] | 170 | plain) : # IMPORTANT : don't move this one up ! |
---|
[220] | 171 | try : |
---|
[351] | 172 | return module.Parser(self.infile, self.options.debug, firstblock, lastblock) |
---|
[220] | 173 | except pdlparser.PDLParserError : |
---|
| 174 | pass # try next parser |
---|
[217] | 175 | raise pdlparser.PDLParserError, "Analysis of first data block failed." |
---|
[200] | 176 | |
---|
| 177 | def main() : |
---|
| 178 | """Entry point for PDL Analyzer.""" |
---|
[350] | 179 | import optparse |
---|
| 180 | from copy import copy |
---|
| 181 | |
---|
| 182 | def check_cichoice(option, opt, value) : |
---|
| 183 | """To add a CaseIgnore Choice option type.""" |
---|
| 184 | valower = value.lower() |
---|
| 185 | if valower in [v.lower() for v in option.cichoices] : |
---|
| 186 | return valower |
---|
| 187 | else : |
---|
[353] | 188 | choices = ", ".join([repr(o) for o in option.cichoices]) |
---|
[350] | 189 | raise optparse.OptionValueError( |
---|
| 190 | "option %s: invalid choice: %r (choose from %s)" |
---|
| 191 | % (opt, value, choices)) |
---|
| 192 | |
---|
| 193 | class MyOption(optparse.Option) : |
---|
| 194 | """New Option class, with CaseIgnore Choice type.""" |
---|
| 195 | TYPES = optparse.Option.TYPES + ("cichoice",) |
---|
| 196 | ATTRS = optparse.Option.ATTRS + ["cichoices"] |
---|
| 197 | TYPE_CHECKER = copy(optparse.Option.TYPE_CHECKER) |
---|
| 198 | TYPE_CHECKER["cichoice"] = check_cichoice |
---|
| 199 | |
---|
| 200 | parser = optparse.OptionParser(option_class=MyOption, |
---|
| 201 | usage="python analyzer.py [options] file1 [file2 ...]") |
---|
[348] | 202 | parser.add_option("-v", "--version", |
---|
| 203 | action="store_true", |
---|
| 204 | dest="version", |
---|
[350] | 205 | help="Show pkpgcounter's version number and exit.") |
---|
[348] | 206 | parser.add_option("-d", "--debug", |
---|
| 207 | action="store_true", |
---|
| 208 | dest="debug", |
---|
[350] | 209 | help="Activate debug mode.") |
---|
| 210 | parser.add_option("-c", "--colorspace", |
---|
| 211 | dest="colorspace", |
---|
| 212 | type="cichoice", |
---|
[439] | 213 | cichoices=["bw", "rgb", "cmyk", "cmy", "gc"], |
---|
| 214 | help="Activate the computation of ink usage, and defines the colorspace to use. Supported values are 'BW', 'RGB', 'CMYK', 'CMY', and 'GC'.") |
---|
[350] | 215 | parser.add_option("-r", "--resolution", |
---|
| 216 | type="int", |
---|
[367] | 217 | default=72, |
---|
[350] | 218 | dest="resolution", |
---|
[367] | 219 | help="The resolution in DPI to use when checking ink usage. Lower resolution is faster but less accurate. Default is 72 dpi.") |
---|
[348] | 220 | (options, arguments) = parser.parse_args() |
---|
| 221 | if options.version : |
---|
[200] | 222 | print "%s" % version.__version__ |
---|
[358] | 223 | elif not (72 <= options.resolution <= 1200) : |
---|
[367] | 224 | sys.stderr.write("ERROR: the argument to the --resolution command line option must be between 72 and 1200.\n") |
---|
[358] | 225 | sys.stderr.flush() |
---|
[200] | 226 | else : |
---|
[348] | 227 | if (not arguments) or ((not sys.stdin.isatty()) and ("-" not in arguments)) : |
---|
| 228 | arguments.append("-") |
---|
[200] | 229 | totalsize = 0 |
---|
[377] | 230 | lines = [] |
---|
[350] | 231 | try : |
---|
| 232 | for arg in arguments : |
---|
| 233 | try : |
---|
[351] | 234 | parser = PDLAnalyzer(arg, options) |
---|
[363] | 235 | if not options.colorspace : |
---|
| 236 | totalsize += parser.getJobSize() |
---|
| 237 | else : |
---|
[376] | 238 | (cspace, pages) = parser.getInkCoverage() |
---|
| 239 | for page in pages : |
---|
[377] | 240 | lineparts = [] |
---|
| 241 | for k in cspace : # NB : this way we preserve the order of the planes |
---|
[376] | 242 | try : |
---|
[383] | 243 | lineparts.append("%s : %s%%" % (k, ("%f" % page[k]).rjust(10))) |
---|
[376] | 244 | except KeyError : |
---|
| 245 | pass |
---|
[377] | 246 | lines.append(" ".join(lineparts)) |
---|
[350] | 247 | except (IOError, pdlparser.PDLParserError), msg : |
---|
| 248 | sys.stderr.write("ERROR: %s\n" % msg) |
---|
| 249 | sys.stderr.flush() |
---|
| 250 | except KeyboardInterrupt : |
---|
| 251 | sys.stderr.write("WARN: Aborted at user's request.\n") |
---|
| 252 | sys.stderr.flush() |
---|
[377] | 253 | if not options.colorspace : |
---|
| 254 | print "%s" % totalsize |
---|
| 255 | else : |
---|
| 256 | print "\n".join(lines) |
---|
[200] | 257 | |
---|
| 258 | if __name__ == "__main__" : |
---|
| 259 | main() |
---|