[3034] | 1 | # PyKota |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | # |
---|
| 4 | # PyKota - Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
| 6 | # (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com> |
---|
| 7 | # This program is free software; you can redistribute it and/or modify |
---|
| 8 | # it under the terms of the GNU General Public License as published by |
---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 10 | # (at your option) any later version. |
---|
| 11 | # |
---|
| 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
| 18 | # along with this program; if not, write to the Free Software |
---|
| 19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
| 23 | # |
---|
| 24 | |
---|
| 25 | import os |
---|
| 26 | from pykota.accounter import AccounterBase, PyKotaAccounterError |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | class Accounter(AccounterBase) : |
---|
| 30 | cspaceExpanded = { |
---|
| 31 | "CMYK" : { "C" : "cyan", "M" : "magenta", "Y" : "yellow", "K" : "black" } , |
---|
| 32 | "CMY" : { "C" : "cyan", "M" : "magenta", "Y" : "yellow" } , |
---|
| 33 | "RGB" : { "R" : "red", "G" : "green", "B" : "blue" } , |
---|
| 34 | "BW" : { "B" : "black", "W" : "white" } , |
---|
| 35 | } |
---|
| 36 | def computeJobSize(self) : |
---|
| 37 | """Do ink accounting for a print job.""" |
---|
| 38 | if (not self.isPreAccounter) and \ |
---|
| 39 | (self.filter.accounter.arguments == self.filter.preaccounter.arguments) : |
---|
| 40 | # if precomputing has been done and both accounter and preaccounter are |
---|
| 41 | # configured the same, no need to launch a second pass since we already |
---|
| 42 | # know the result. |
---|
| 43 | self.filter.logdebug("Precomputing pass told us that job is %s pages long." % self.filter.softwareJobSize) |
---|
| 44 | return self.filter.softwareJobSize # Optimize : already computed ! |
---|
| 45 | |
---|
| 46 | (colorspace, resolution) = [p.strip() for p in self.arguments.split(',')] |
---|
| 47 | colorspace = colorspace.lower() |
---|
| 48 | if colorspace not in ("cmyk", "bw", "cmy", "rgb") : |
---|
| 49 | raise PyKotaAccounterError, "Invalid parameters for ink accounter : [%s]" % self.arguments |
---|
| 50 | |
---|
| 51 | try : |
---|
| 52 | resolution = int(resolution) |
---|
| 53 | except ValueError : |
---|
| 54 | raise PyKotaAccounterError, "Invalid parameters for ink accounter : [%s]" % self.arguments |
---|
| 55 | |
---|
| 56 | self.filter.logdebug("Using internal parser to compute job's size and ink usage.") |
---|
| 57 | |
---|
| 58 | jobsize = 0 |
---|
| 59 | self.inkUsage = [] |
---|
| 60 | if self.filter.JobSizeBytes : |
---|
| 61 | try : |
---|
| 62 | from pkpgpdls import analyzer, pdlparser |
---|
| 63 | except ImportError : |
---|
| 64 | self.filter.printInfo("pkpgcounter is now distributed separately, please grab it from http://www.pykota.com/software/pkpgcounter", "error") |
---|
| 65 | self.filter.printInfo("Precomputed job size will be forced to 0 pages.", "error") |
---|
| 66 | else : |
---|
| 67 | options = analyzer.AnalyzerOptions(colorspace=colorspace, resolution=resolution) |
---|
| 68 | infile = open(self.filter.DataFile, "rb") |
---|
| 69 | try : |
---|
| 70 | try : |
---|
| 71 | parser = analyzer.PDLAnalyzer(infile, options) |
---|
| 72 | (cspace, pages) = parser.getInkCoverage() |
---|
| 73 | cspacelabels = self.cspaceExpanded[cspace] |
---|
| 74 | for page in pages : |
---|
| 75 | colordict = {} |
---|
| 76 | for color in page.keys() : |
---|
| 77 | colordict[cspacelabels[color]] = page[color] |
---|
| 78 | self.inkUsage.append(colordict) |
---|
| 79 | jobsize = len(pages) |
---|
| 80 | self.filter.logdebug("Ink usage : %s ===> %s" % (cspace, repr(self.inkUsage))) |
---|
| 81 | except pdlparser.PDLParserError, msg : |
---|
| 82 | # Here we just log the failure, but |
---|
| 83 | # we finally ignore it and return 0 since this |
---|
| 84 | # computation is just an indication of what the |
---|
| 85 | # job's size MAY be. |
---|
| 86 | self.filter.printInfo(_("Unable to precompute the job's size with the generic PDL analyzer : %s") % msg, "warn") |
---|
| 87 | else : |
---|
| 88 | if self.filter.InputFile is not None : |
---|
| 89 | # when a filename is passed as an argument, the backend |
---|
| 90 | # must generate the correct number of copies. |
---|
| 91 | jobsize *= self.filter.Copies |
---|
| 92 | finally : |
---|
| 93 | infile.close() |
---|
| 94 | return jobsize |
---|