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

Revision 1257, 5.7 kB (checked in by jalet, 20 years ago)

Copyright year changed.

  • 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.10  2004/01/08 14:10:32  jalet
25# Copyright year changed.
26#
27# Revision 1.9  2003/12/27 16:49:25  uid67467
28# Should be ok now.
29#
30# Revision 1.7  2003/11/25 23:46:40  jalet
31# Don't try to verify if module name is valid, Python does this better than us.
32#
33# Revision 1.6  2003/11/12 23:28:55  jalet
34# More work on new backend. This commit may be unstable.
35#
36# Revision 1.5  2003/10/07 09:07:28  jalet
37# Character encoding added to please latest version of Python
38#
39# Revision 1.4  2003/07/14 14:14:59  jalet
40# Old template
41#
42# Revision 1.3  2003/04/30 19:53:58  jalet
43# 1.05
44#
45# Revision 1.2  2003/04/30 13:36:40  jalet
46# Stupid accounting method was added.
47#
48# Revision 1.1  2003/04/29 18:37:54  jalet
49# Pluggable accounting methods (actually doesn't support external scripts)
50#
51#
52#
53
54import sys
55
56class PyKotaAccounterError(Exception):
57    """An exception for Accounter related stuff."""
58    def __init__(self, message = ""):
59        self.message = message
60        Exception.__init__(self, message)
61    def __repr__(self):
62        return self.message
63    __str__ = __repr__
64   
65class AccounterBase :   
66    """A class to account print usage by querying printers."""
67    def __init__(self, kotafilter, arguments) :
68        """Sets instance vars depending on the current printer."""
69        self.filter = kotafilter
70        self.arguments = arguments
71       
72    def getLastPageCounter(self) :   
73        """Returns last internal page counter value (possibly faked)."""
74        try :
75            return self.LastPageCounter
76        except :   
77            return 0
78       
79    def filterInput(self, inputfile) :
80        """Transparent filter."""
81        mustclose = 0   
82        if inputfile is not None :   
83            if hasattr(inputfile, "read") :
84                infile = inputfile
85            else :   
86                infile = open(inputfile, "rb")
87            mustclose = 1
88        else :   
89            infile = sys.stdin
90        data = infile.read(256*1024)   
91        while data :
92            sys.stdout.write(data)
93            data = infile.read(256*1024)
94        if mustclose :   
95            infile.close()
96           
97    def beginJob(self, printer, user) :   
98        """Saves the computed job size."""
99        # computes job's size
100        self.JobSize = self.computeJobSize()
101       
102        # get last job information for this printer
103        if not printer.LastJob.Exists :
104            # The printer hasn't been used yet, from PyKota's point of view
105            self.LastPageCounter = 0
106        else :   
107            # get last job size and page counter from Quota Storage
108            # Last lifetime page counter before actual job is
109            # last page counter + last job size
110            self.LastPageCounter = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0)
111       
112    def endJob(self, printer, user) :   
113        """Do nothing."""
114        pass
115       
116    def getJobSize(self) :   
117        """Returns the actual job size."""
118        try :
119            return self.JobSize
120        except AttributeError :   
121            return 0
122       
123    def doAccounting(self, printer, user) :
124        """Deletgates the computation of the job size to an external command.
125       
126           The command must print the job size on its standard output and exit successfully.
127        """
128        self.beginJob(printer, user)
129       
130        # get the job size, which is real job size * number of copies.
131        jobsize = self.getJobSize() * self.filter.copies
132           
133        # Is the current user allowed to print at all ?
134        userpquota = self.filter.storage.getUserPQuota(user, printer)
135        action = self.filter.warnUserPQuota(userpquota)
136       
137        # update the quota for the current user on this printer, if allowed to print
138        if action == "DENY" :
139            jobsize = 0
140        else :   
141            userpquota.increasePagesUsage(jobsize)
142       
143        # adds the current job to history   
144        jobprice = (float(printer.PricePerPage or 0.0) * jobsize) + float(printer.PricePerJob or 0.0)
145        printer.addJobToHistory(self.filter.jobid, user, self.getLastPageCounter(), action, jobsize, jobprice, self.filter.preserveinputfile, self.filter.title, self.filter.copies, self.filter.options)
146        self.endJob(printer, user)
147        return action
148       
149    def computeJobSize(self) :   
150        """Must be overriden in children classes."""
151        raise RuntimeError, "AccounterBase.computeJobSize() must be overriden !"
152       
153       
154def openAccounter(kotafilter) :
155    """Returns a connection handle to the appropriate accounter."""
156    (backend, args) = kotafilter.config.getAccounterBackend(kotafilter.printername)
157    try :
158        exec "from pykota.accounters import %s as accounterbackend" % backend.lower()
159    except ImportError :
160        raise PyKotaAccounterError, _("Unsupported accounter backend %s") % backend
161    else :   
162        return accounterbackend.Accounter(kotafilter, args)
Note: See TracBrowser for help on using the browser.