root / pykota / trunk / pykota / accounters / ink.py @ 3413

Revision 3413, 4.9 kB (checked in by jerome, 16 years ago)

Removed unnecessary spaces at EOL.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[3411]1# -*- coding: utf-8 -*-*-
[3034]2#
[3260]3# PyKota : Print Quotas for CUPS
[3034]4#
[3275]5# (c) 2003, 2004, 2005, 2006, 2007, 2008 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.
[3413]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.
[3413]15#
[3034]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
[3288]23"""This module handles ink accounting in PyKota."""
24
[3034]25import os
26
[3288]27from pykota.errors import PyKotaAccounterError
28from pykota.accounter import AccounterBase
[3034]29
30class Accounter(AccounterBase) :
31    cspaceExpanded = {
32                        "CMYK" : { "C" : "cyan", "M" : "magenta", "Y" : "yellow", "K" : "black" } ,
33                        "CMY" : { "C" : "cyan", "M" : "magenta", "Y" : "yellow" } ,
34                        "RGB" : { "R" : "red", "G" : "green", "B" : "blue" } ,
35                        "BW" : { "B" : "black", "W" : "white" } ,
[3122]36                        "GC" : { "G" : "grayscale", "C" : "colored" } ,
[3034]37                     }
[3413]38    def computeJobSize(self) :
[3034]39        """Do ink accounting for a print job."""
40        if (not self.isPreAccounter) and \
41            (self.filter.accounter.arguments == self.filter.preaccounter.arguments) :
42            # if precomputing has been done and both accounter and preaccounter are
43            # configured the same, no need to launch a second pass since we already
44            # know the result.
45            self.filter.logdebug("Precomputing pass told us that job is %s pages long." % self.filter.softwareJobSize)
[3036]46            self.inkUsage = self.filter.preaccounter.inkUsage   # Optimize : already computed !
47            return self.filter.softwareJobSize                  # Optimize : already computed !
[3413]48
[3036]49        parameters = [p.strip() for p in self.arguments.split(',')]
50        if len(parameters) == 1 :
51            parameters.append("72")
52        (colorspace, resolution) = parameters
[3034]53        colorspace = colorspace.lower()
[3122]54        if colorspace not in ("cmyk", "bw", "cmy", "rgb", "gc") :
[3288]55            raise PyKotaAccounterError, _("Invalid parameters for ink accounter : [%s]") % self.arguments
[3413]56
57        try :
[3034]58            resolution = int(resolution)
[3413]59        except ValueError :
[3034]60            raise PyKotaAccounterError, "Invalid parameters for ink accounter : [%s]" % self.arguments
[3413]61
[3034]62        self.filter.logdebug("Using internal parser to compute job's size and ink usage.")
[3413]63
[3034]64        jobsize = 0
65        if self.filter.JobSizeBytes :
66            try :
67                from pkpgpdls import analyzer, pdlparser
[3413]68            except ImportError :
[3034]69                self.filter.printInfo("pkpgcounter is now distributed separately, please grab it from http://www.pykota.com/software/pkpgcounter", "error")
70                self.filter.printInfo("Precomputed job size will be forced to 0 pages.", "error")
[3413]71            else :
[3034]72                options = analyzer.AnalyzerOptions(colorspace=colorspace, resolution=resolution)
73                try :
[3042]74                    parser = analyzer.PDLAnalyzer(self.filter.DataFile, options)
75                    (cspace, pages) = parser.getInkCoverage()
[3413]76                except pdlparser.PDLParserError, msg :
[3046]77                    # Here we just log the failure, but
78                    # we finally ignore it and return 0 since this
79                    # computation is just an indication of what the
80                    # job's size MAY be.
81                    self.filter.printInfo(_("Unable to precompute the job's size and ink coverage with the generic PDL analyzer : %s") % msg, "warn")
[3413]82                else :
[3042]83                    cspacelabels = self.cspaceExpanded[cspace]
84                    for page in pages :
85                        colordict = {}
86                        for color in page.keys() :
87                            colordict[cspacelabels[color]] = page[color]
[3413]88                        self.inkUsage.append(colordict)
[3042]89                    jobsize = len(pages)
[3245]90                    try :
[3252]91                        if self.filter.Ticket.FileName is not None :
[3413]92                            # when a filename is passed as an argument, the backend
[3245]93                            # must generate the correct number of copies.
[3252]94                            jobsize *= self.filter.Ticket.Copies
95                            self.inkUsage *= self.filter.Ticket.Copies
[3413]96                    except AttributeError : # When not run from the cupspykota backend
[3245]97                        pass
[3046]98                    self.filter.logdebug("Ink usage : %s ===> %s" % (cspace, repr(self.inkUsage)))
[3413]99        return jobsize
Note: See TracBrowser for help on using the browser.