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

Revision 3133, 4.8 kB (checked in by jerome, 17 years ago)

Changed copyright years.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1# PyKota
2# -*- coding: ISO-8859-15 -*-
3#
4# PyKota - Print Quotas for CUPS and LPRng
5#
6# (c) 2003, 2004, 2005, 2006, 2007 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
25import os
26from pykota.accounter import AccounterBase, PyKotaAccounterError
27
28
29class 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                        "GC" : { "G" : "grayscale", "C" : "colored" } ,
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)
45            self.inkUsage = self.filter.preaccounter.inkUsage   # Optimize : already computed !
46            return self.filter.softwareJobSize                  # Optimize : already computed !
47           
48        parameters = [p.strip() for p in self.arguments.split(',')]
49        if len(parameters) == 1 :
50            parameters.append("72")
51        (colorspace, resolution) = parameters
52        colorspace = colorspace.lower()
53        if colorspace not in ("cmyk", "bw", "cmy", "rgb", "gc") :
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 :
73                    parser = analyzer.PDLAnalyzer(self.filter.DataFile, options)
74                    (cspace, pages) = parser.getInkCoverage()
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 :   
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
93                        self.inkUsage *= self.filter.Copies
94                    self.filter.logdebug("Ink usage : %s ===> %s" % (cspace, repr(self.inkUsage)))
95        return jobsize       
Note: See TracBrowser for help on using the browser.