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

Revision 3036, 4.8 kB (checked in by jerome, 18 years ago)

Charging for ink usage, finally !

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