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

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

Changed license to GNU GPL v3 or later.
Changed Python source encoding from ISO-8859-15 to UTF-8 (only ASCII
was used anyway).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1# -*- coding: UTF-8 -*-
2#
3# PyKota : Print Quotas for CUPS
4#
5# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
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
17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18#
19# $Id$
20#
21#
22
23import os
24from pykota.accounter import AccounterBase, PyKotaAccounterError
25
26
27class 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" } ,
33                        "GC" : { "G" : "grayscale", "C" : "colored" } ,
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)
43            self.inkUsage = self.filter.preaccounter.inkUsage   # Optimize : already computed !
44            return self.filter.softwareJobSize                  # Optimize : already computed !
45           
46        parameters = [p.strip() for p in self.arguments.split(',')]
47        if len(parameters) == 1 :
48            parameters.append("72")
49        (colorspace, resolution) = parameters
50        colorspace = colorspace.lower()
51        if colorspace not in ("cmyk", "bw", "cmy", "rgb", "gc") :
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 :
71                    parser = analyzer.PDLAnalyzer(self.filter.DataFile, options)
72                    (cspace, pages) = parser.getInkCoverage()
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 :   
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)
87                    try :
88                        if self.filter.Ticket.FileName 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.Ticket.Copies
92                            self.inkUsage *= self.filter.Ticket.Copies
93                    except AttributeError : # When not run from the cupspykota backend
94                        pass
95                    self.filter.logdebug("Ink usage : %s ===> %s" % (cspace, repr(self.inkUsage)))
96        return jobsize       
Note: See TracBrowser for help on using the browser.