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