root / pykota / trunk / pykota / accounter.py @ 1271

Revision 1271, 5.5 kB (checked in by jalet, 20 years ago)

Major code refactoring, it's way cleaner, and now allows automated addition
of printers on first print.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[973]1# PyKota
[1144]2# -*- coding: ISO-8859-15 -*-
[973]3#
4# PyKota : Print Quotas for CUPS and LPRng
5#
[1257]6# (c) 2003-2004 Jerome Alet <alet@librelogiciel.com>
[973]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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
20#
21# $Id$
22#
23# $Log$
[1271]24# Revision 1.11  2004/01/11 23:22:42  jalet
25# Major code refactoring, it's way cleaner, and now allows automated addition
26# of printers on first print.
27#
[1257]28# Revision 1.10  2004/01/08 14:10:32  jalet
29# Copyright year changed.
30#
[1240]31# Revision 1.9  2003/12/27 16:49:25  uid67467
32# Should be ok now.
[1239]33#
[1219]34# Revision 1.7  2003/11/25 23:46:40  jalet
35# Don't try to verify if module name is valid, Python does this better than us.
36#
[1180]37# Revision 1.6  2003/11/12 23:28:55  jalet
38# More work on new backend. This commit may be unstable.
39#
[1144]40# Revision 1.5  2003/10/07 09:07:28  jalet
41# Character encoding added to please latest version of Python
42#
[1083]43# Revision 1.4  2003/07/14 14:14:59  jalet
44# Old template
45#
[980]46# Revision 1.3  2003/04/30 19:53:58  jalet
47# 1.05
48#
[976]49# Revision 1.2  2003/04/30 13:36:40  jalet
50# Stupid accounting method was added.
51#
[973]52# Revision 1.1  2003/04/29 18:37:54  jalet
53# Pluggable accounting methods (actually doesn't support external scripts)
54#
55#
56#
57
[976]58import sys
59
[973]60class PyKotaAccounterError(Exception):
61    """An exception for Accounter related stuff."""
62    def __init__(self, message = ""):
63        self.message = message
64        Exception.__init__(self, message)
65    def __repr__(self):
66        return self.message
67    __str__ = __repr__
68   
69class AccounterBase :   
70    """A class to account print usage by querying printers."""
[980]71    def __init__(self, kotafilter, arguments) :
[973]72        """Sets instance vars depending on the current printer."""
73        self.filter = kotafilter
[980]74        self.arguments = arguments
[1271]75        self.isDelayed = 0      # Accounting is immediate by default
[973]76       
[1180]77    def getLastPageCounter(self) :   
78        """Returns last internal page counter value (possibly faked)."""
79        try :
80            return self.LastPageCounter
81        except :   
82            return 0
[976]83           
[1271]84    def beginJob(self, userpquota) :   
[1239]85        """Saves the computed job size."""
86        # computes job's size
87        self.JobSize = self.computeJobSize()
[973]88       
[1239]89        # get last job information for this printer
[1271]90        if not userpquota.Printer.LastJob.Exists :
[1239]91            # The printer hasn't been used yet, from PyKota's point of view
92            self.LastPageCounter = 0
93        else :   
94            # get last job size and page counter from Quota Storage
95            # Last lifetime page counter before actual job is
96            # last page counter + last job size
[1271]97            self.LastPageCounter = int(userpquota.Printer.LastJob.PrinterPageCounter or 0) + int(userpquota.Printer.LastJob.JobSize or 0)
[1239]98       
[1271]99    def endJob(self, userpquota) :   
[1239]100        """Do nothing."""
101        pass
102       
103    def getJobSize(self) :   
104        """Returns the actual job size."""
105        try :
106            return self.JobSize
107        except AttributeError :   
108            return 0
109       
[1271]110    def doAccounting(self, userpquota) :
111        """Delegates the computation of the job size to an external command.
[1239]112       
113           The command must print the job size on its standard output and exit successfully.
114        """
[1271]115        self.beginJob(userpquota)
[1239]116       
117        # get the job size, which is real job size * number of copies.
[1271]118        # TODO : Double check with CUPS documentation : this is not always correct
[1239]119        jobsize = self.getJobSize() * self.filter.copies
120           
121        # Is the current user allowed to print at all ?
122        action = self.filter.warnUserPQuota(userpquota)
123       
124        # update the quota for the current user on this printer, if allowed to print
125        if action == "DENY" :
126            jobsize = 0
127        else :   
128            userpquota.increasePagesUsage(jobsize)
129       
130        # adds the current job to history   
[1271]131        jobprice = (float(userpquota.Printer.PricePerPage or 0.0) * jobsize) + float(userpquota.Printer.PricePerJob or 0.0)
132        userpquota.Printer.addJobToHistory(self.filter.jobid, userpquota.User, self.getLastPageCounter(), action, jobsize, jobprice, self.filter.preserveinputfile, self.filter.title, self.filter.copies, self.filter.options)
133        self.endJob(userpquota)
[1239]134        return action
135       
[1240]136    def computeJobSize(self) :   
137        """Must be overriden in children classes."""
138        raise RuntimeError, "AccounterBase.computeJobSize() must be overriden !"
139       
140       
[973]141def openAccounter(kotafilter) :
142    """Returns a connection handle to the appropriate accounter."""
[980]143    (backend, args) = kotafilter.config.getAccounterBackend(kotafilter.printername)
[973]144    try :
[1219]145        exec "from pykota.accounters import %s as accounterbackend" % backend.lower()
[973]146    except ImportError :
147        raise PyKotaAccounterError, _("Unsupported accounter backend %s") % backend
148    else :   
[1240]149        return accounterbackend.Accounter(kotafilter, args)
Note: See TracBrowser for help on using the browser.