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

Revision 3561, 4.8 kB (checked in by jerome, 11 years ago)

Changed copyright years.

  • 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-2013 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
23"""This module handles ink accounting in PyKota."""
24
25import os
26
27from pykota.errors import PyKotaAccounterError
28from pykota.accounter import AccounterBase
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" } ,
36                        "GC" : { "G" : "grayscale", "C" : "colored" } ,
37                     }
38    def computeJobSize(self) :
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)
46            self.inkUsage = self.filter.preaccounter.inkUsage   # Optimize : already computed !
47            return self.filter.softwareJobSize                  # Optimize : already computed !
48
49        parameters = [p.strip() for p in self.arguments.split(',')]
50        if len(parameters) == 1 :
51            parameters.append("72")
52        (colorspace, resolution) = parameters
53        colorspace = colorspace.lower()
54        if colorspace not in ("cmyk", "bw", "cmy", "rgb", "gc") :
55            raise PyKotaAccounterError, _("Invalid parameters for ink accounter : [%s]") % self.arguments
56
57        try :
58            resolution = int(resolution)
59        except ValueError :
60            raise PyKotaAccounterError, "Invalid parameters for ink accounter : [%s]" % self.arguments
61
62        self.filter.logdebug("Using internal parser to compute job's size and ink usage.")
63
64        jobsize = 0
65        if self.filter.JobSizeBytes :
66            try :
67                from pkpgpdls import analyzer, pdlparser
68            except ImportError :
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")
71            else :
72                options = analyzer.AnalyzerOptions(colorspace=colorspace, resolution=resolution)
73                try :
74                    parser = analyzer.PDLAnalyzer(self.filter.DataFile, options)
75                    (cspace, pages) = parser.getInkCoverage()
76                except pdlparser.PDLParserError, msg :
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")
82                else :
83                    cspacelabels = self.cspaceExpanded[cspace]
84                    for page in pages :
85                        colordict = {}
86                        for color in page.keys() :
87                            colordict[cspacelabels[color]] = page[color]
88                        self.inkUsage.append(colordict)
89                    jobsize = len(pages)
90                    try :
91                        if self.filter.Ticket.FileName 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.Ticket.Copies
95                            self.inkUsage *= self.filter.Ticket.Copies
96                    except AttributeError : # When not run from the cupspykota backend
97                        pass
98                    self.filter.logdebug("Ink usage : %s ===> %s" % (cspace, repr(self.inkUsage)))
99        return jobsize
Note: See TracBrowser for help on using the browser.