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

Revision 1483, 6.3 kB (checked in by jalet, 20 years ago)

Big code changes to completely remove the need for "requester" directives,
jsut use "hardware(... your previous requester directive's content ...)"

  • 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.14  2004/05/18 14:49:19  jalet
25# Big code changes to completely remove the need for "requester" directives,
26# jsut use "hardware(... your previous requester directive's content ...)"
27#
28# Revision 1.13  2004/01/12 22:43:40  jalet
29# New formula to compute a job's price
30#
31# Revision 1.12  2004/01/11 23:43:31  jalet
32# Bug wrt number of copies with CUPS should be fixed.
33#
34# Revision 1.11  2004/01/11 23:22:42  jalet
35# Major code refactoring, it's way cleaner, and now allows automated addition
36# of printers on first print.
37#
38# Revision 1.10  2004/01/08 14:10:32  jalet
39# Copyright year changed.
40#
41# Revision 1.9  2003/12/27 16:49:25  uid67467
42# Should be ok now.
43#
44# Revision 1.7  2003/11/25 23:46:40  jalet
45# Don't try to verify if module name is valid, Python does this better than us.
46#
47# Revision 1.6  2003/11/12 23:28:55  jalet
48# More work on new backend. This commit may be unstable.
49#
50# Revision 1.5  2003/10/07 09:07:28  jalet
51# Character encoding added to please latest version of Python
52#
53# Revision 1.4  2003/07/14 14:14:59  jalet
54# Old template
55#
56# Revision 1.3  2003/04/30 19:53:58  jalet
57# 1.05
58#
59# Revision 1.2  2003/04/30 13:36:40  jalet
60# Stupid accounting method was added.
61#
62# Revision 1.1  2003/04/29 18:37:54  jalet
63# Pluggable accounting methods (actually doesn't support external scripts)
64#
65#
66#
67
68import sys
69from pykota import pdlanalyzer
70
71class PyKotaAccounterError(Exception):
72    """An exception for Accounter related stuff."""
73    def __init__(self, message = ""):
74        self.message = message
75        Exception.__init__(self, message)
76    def __repr__(self):
77        return self.message
78    __str__ = __repr__
79   
80class AccounterBase :   
81    """A class to account print usage by querying printers."""
82    def __init__(self, kotafilter, arguments) :
83        """Sets instance vars depending on the current printer."""
84        self.filter = kotafilter
85        self.arguments = arguments
86        self.isDelayed = 0      # Accounting is immediate by default
87        self.firstPassSize = None
88       
89    def getSoftwareJobSize(self) :   
90        """Pre-computes the job's size with a software method."""
91        if self.filter.preserveinputfile is None :
92            raise PyKotaAccounterError, "Only supports raw jobs for now."""
93        else :   
94            fname = self.filter.preserveinputfile
95        parser = pdfanalyzer.PDLAnalyzer(fname)   
96        try : 
97            jobsize = parser.getJobSize()
98        except TypeError, msg :   
99            raise PyKotaAccounterError, msg
100        else :   
101            self.firstPassSize = jobsize
102       
103    def getLastPageCounter(self) :   
104        """Returns last internal page counter value (possibly faked)."""
105        try :
106            return self.LastPageCounter or 0
107        except :   
108            return 0
109           
110    def beginJob(self, userpquota) :   
111        """Saves the computed job size."""
112        # computes job's size
113        self.JobSize = self.computeJobSize()
114        if ((self.filter.printingsystem == "CUPS") \
115            and (self.filter.preserveinputfile is not None)) \
116            or (self.filter.printingsystem != "CUPS") :
117                self.JobSize *= self.filter.copies
118       
119        # get last job information for this printer
120        if not userpquota.Printer.LastJob.Exists :
121            # The printer hasn't been used yet, from PyKota's point of view
122            self.LastPageCounter = 0
123        else :   
124            # get last job size and page counter from Quota Storage
125            # Last lifetime page counter before actual job is
126            # last page counter + last job size
127            self.LastPageCounter = int(userpquota.Printer.LastJob.PrinterPageCounter or 0) + int(userpquota.Printer.LastJob.JobSize or 0)
128       
129    def endJob(self, userpquota) :   
130        """Do nothing."""
131        pass
132       
133    def getJobSize(self) :   
134        """Returns the actual job size."""
135        try :
136            return self.JobSize
137        except AttributeError :   
138            return 0
139       
140    def doAccounting(self, userpquota) :
141        """Does accounting for current job."""
142        self.beginJob(userpquota)
143           
144        # Is the current user allowed to print at all ?
145        action = self.filter.warnUserPQuota(userpquota)
146       
147        # update the quota for the current user on this printer, if allowed to print
148        if action == "DENY" :
149            jobsize = 0
150        else :   
151            # get the job size
152            jobsize = self.getJobSize()
153            userpquota.increasePagesUsage(jobsize)
154       
155        # adds the current job to history   
156        jobprice = userpquota.computeJobPrice(jobsize)
157        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)
158        self.endJob(userpquota)
159        return action
160       
161    def computeJobSize(self) :   
162        """Must be overriden in children classes."""
163        raise RuntimeError, "AccounterBase.computeJobSize() must be overriden !"
164       
165       
166def openAccounter(kotafilter) :
167    """Returns a connection handle to the appropriate accounter."""
168    (backend, args) = kotafilter.config.getAccounterBackend(kotafilter.printername)
169    try :
170        exec "from pykota.accounters import %s as accounterbackend" % backend.lower()
171    except ImportError :
172        raise PyKotaAccounterError, _("Unsupported accounter backend %s") % backend
173    else :   
174        return accounterbackend.Accounter(kotafilter, args)
Note: See TracBrowser for help on using the browser.