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

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

Bug wrt number of copies with CUPS should be fixed.

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