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

Revision 3042, 4.6 kB (checked in by jerome, 18 years ago)

Simplified code : pkpgcounter's code already opens the data file.

  • 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                try :
72                    parser = analyzer.PDLAnalyzer(self.filter.DataFile, options)
73                    (cspace, pages) = parser.getInkCoverage()
74                    cspacelabels = self.cspaceExpanded[cspace]
75                    for page in pages :
76                        colordict = {}
77                        for color in page.keys() :
78                            colordict[cspacelabels[color]] = page[color]
79                        self.inkUsage.append(colordict)   
80                    jobsize = len(pages)
81                    self.filter.logdebug("Ink usage : %s ===> %s" % (cspace, repr(self.inkUsage)))
82                except pdlparser.PDLParserError, msg :   
83                    # Here we just log the failure, but
84                    # we finally ignore it and return 0 since this
85                    # computation is just an indication of what the
86                    # job's size MAY be.
87                    self.filter.printInfo(_("Unable to precompute the job's size with the generic PDL analyzer : %s") % msg, "warn")
88                else :   
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        return jobsize       
Note: See TracBrowser for help on using the browser.