[3034] | 1 | # PyKota |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | # |
---|
| 4 | # PyKota - Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
[3133] | 6 | # (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com> |
---|
[3034] | 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" } , |
---|
[3122] | 35 | "GC" : { "G" : "grayscale", "C" : "colored" } , |
---|
[3034] | 36 | } |
---|
| 37 | def computeJobSize(self) : |
---|
| 38 | """Do ink accounting for a print job.""" |
---|
| 39 | if (not self.isPreAccounter) and \ |
---|
| 40 | (self.filter.accounter.arguments == self.filter.preaccounter.arguments) : |
---|
| 41 | # if precomputing has been done and both accounter and preaccounter are |
---|
| 42 | # configured the same, no need to launch a second pass since we already |
---|
| 43 | # know the result. |
---|
| 44 | self.filter.logdebug("Precomputing pass told us that job is %s pages long." % self.filter.softwareJobSize) |
---|
[3036] | 45 | self.inkUsage = self.filter.preaccounter.inkUsage # Optimize : already computed ! |
---|
| 46 | return self.filter.softwareJobSize # Optimize : already computed ! |
---|
[3034] | 47 | |
---|
[3036] | 48 | parameters = [p.strip() for p in self.arguments.split(',')] |
---|
| 49 | if len(parameters) == 1 : |
---|
| 50 | parameters.append("72") |
---|
| 51 | (colorspace, resolution) = parameters |
---|
[3034] | 52 | colorspace = colorspace.lower() |
---|
[3122] | 53 | if colorspace not in ("cmyk", "bw", "cmy", "rgb", "gc") : |
---|
[3034] | 54 | raise PyKotaAccounterError, "Invalid parameters for ink accounter : [%s]" % self.arguments |
---|
| 55 | |
---|
| 56 | try : |
---|
| 57 | resolution = int(resolution) |
---|
| 58 | except ValueError : |
---|
| 59 | raise PyKotaAccounterError, "Invalid parameters for ink accounter : [%s]" % self.arguments |
---|
| 60 | |
---|
| 61 | self.filter.logdebug("Using internal parser to compute job's size and ink usage.") |
---|
| 62 | |
---|
| 63 | jobsize = 0 |
---|
| 64 | if self.filter.JobSizeBytes : |
---|
| 65 | try : |
---|
| 66 | from pkpgpdls import analyzer, pdlparser |
---|
| 67 | except ImportError : |
---|
| 68 | self.filter.printInfo("pkpgcounter is now distributed separately, please grab it from http://www.pykota.com/software/pkpgcounter", "error") |
---|
| 69 | self.filter.printInfo("Precomputed job size will be forced to 0 pages.", "error") |
---|
| 70 | else : |
---|
| 71 | options = analyzer.AnalyzerOptions(colorspace=colorspace, resolution=resolution) |
---|
| 72 | try : |
---|
[3042] | 73 | parser = analyzer.PDLAnalyzer(self.filter.DataFile, options) |
---|
| 74 | (cspace, pages) = parser.getInkCoverage() |
---|
[3046] | 75 | except pdlparser.PDLParserError, msg : |
---|
| 76 | # Here we just log the failure, but |
---|
| 77 | # we finally ignore it and return 0 since this |
---|
| 78 | # computation is just an indication of what the |
---|
| 79 | # job's size MAY be. |
---|
| 80 | self.filter.printInfo(_("Unable to precompute the job's size and ink coverage with the generic PDL analyzer : %s") % msg, "warn") |
---|
| 81 | else : |
---|
[3042] | 82 | cspacelabels = self.cspaceExpanded[cspace] |
---|
| 83 | for page in pages : |
---|
| 84 | colordict = {} |
---|
| 85 | for color in page.keys() : |
---|
| 86 | colordict[cspacelabels[color]] = page[color] |
---|
| 87 | self.inkUsage.append(colordict) |
---|
| 88 | jobsize = len(pages) |
---|
| 89 | if self.filter.InputFile is not None : |
---|
| 90 | # when a filename is passed as an argument, the backend |
---|
| 91 | # must generate the correct number of copies. |
---|
| 92 | jobsize *= self.filter.Copies |
---|
[3046] | 93 | self.inkUsage *= self.filter.Copies |
---|
| 94 | self.filter.logdebug("Ink usage : %s ===> %s" % (cspace, repr(self.inkUsage))) |
---|
[3034] | 95 | return jobsize |
---|