root / pykota / trunk / pykota / accounters / querying.py @ 1271

Revision 1271, 5.6 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
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:22:42  jalet
25# Major code refactoring, it's way cleaner, and now allows automated addition
26# of printers on first print.
27#
28# Revision 1.11  2004/01/08 14:10:32  jalet
29# Copyright year changed.
30#
31# Revision 1.10  2003/12/27 16:49:25  uid67467
32# Should be ok now.
33#
34# Revision 1.8  2003/11/21 14:28:46  jalet
35# More complete job history.
36#
37# Revision 1.7  2003/11/12 23:29:24  jalet
38# More work on new backend. This commit may be unstable.
39#
40# Revision 1.6  2003/10/07 09:07:29  jalet
41# Character encoding added to please latest version of Python
42#
43# Revision 1.5  2003/07/07 11:49:24  jalet
44# Lots of small fixes with the help of PyChecker
45#
46# Revision 1.4  2003/06/25 14:10:01  jalet
47# Hey, it may work (edpykota --reset excepted) !
48#
49# Revision 1.3  2003/05/06 14:55:47  jalet
50# Missing import !
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
62import os
63import time
64from pykota.accounter import AccounterBase, PyKotaAccounterError
65from pykota.requester import openRequester, PyKotaRequesterError
66
67MAXTRIES = 12    # maximum number of tries to get the printer's internal page counter
68TIMETOSLEEP = 10 # number of seconds to sleep between two tries to get the printer's internal page counter
69
70class Accounter(AccounterBase) :
71    def __init__(self, kotabackend, arguments) :
72        """Initializes querying accounter."""
73        AccounterBase.__init__(self, kotabackend, arguments)
74        self.requester = openRequester(kotabackend.config, kotabackend.printername)
75        self.isDelayed = 1 # With the pykota filter, accounting is delayed by one job
76       
77    def getPrinterInternalPageCounter(self) :   
78        """Returns the printer's internal page counter."""
79        global MAXTRIES, TIMETOSLEEP
80        self.filter.logdebug("Reading printer's internal page counter...")
81        for dummy in range(MAXTRIES) :
82            try :
83                counter = self.requester.getPrinterPageCounter(self.filter.printerhostname)
84            except PyKotaRequesterError, msg :
85                # can't get actual page counter, assume printer is off or warming up
86                # log the message anyway.
87                self.filter.logger.log_message("%s" % msg, "warn")
88                counter = None
89            else :   
90                # printer answered, it is on so we can exit the loop
91                break
92            time.sleep(TIMETOSLEEP)   
93        self.filter.logdebug("Printer's internal page counter value is : %s" % str(counter))
94        return counter   
95       
96    def beginJob(self, userpquota) :   
97        """Saves printer internal page counter at start of job."""
98        # save page counter before job
99        self.LastPageCounter = self.counterbefore = self.getPrinterInternalPageCounter()
100       
101    def endJob(self, userpquota) :   
102        """Saves printer internal page counter at end of job."""
103        # save page counter after job
104        self.LastPageCounter = self.counterafter = self.getPrinterInternalPageCounter()
105       
106    def getJobSize(self) :   
107        """Returns the actual job size."""
108        try :
109            jobsize = (self.counterafter - self.counterbefore)   
110            if jobsize < 0 :
111                # Try to take care of HP printers
112                # Their internal page counter is saved to NVRAM
113                # only every 10 pages. If the printer was switched
114                # off then back on during the job, and that the
115                # counters difference is negative, we know
116                # the formula (we can't know if more than eleven
117                # pages were printed though) :
118                if jobsize > -10 :
119                    jobsize += 10
120                else :   
121                    # here we may have got a printer being replaced
122                    # DURING the job. This is HIGHLY improbable !
123                    jobsize = 0
124        except :   
125            # takes care of the case where one counter (or both) was never set.
126            jobsize = 0
127        return jobsize
128       
129    def doAccounting(self, userpquota) :
130        """Does print accounting and returns if the job status is ALLOW or DENY."""
131        # Get the page counter directly from the printer itself
132        counterbeforejob = self.getPrinterInternalPageCounter() or 0
133       
134        # Is the current user allowed to print at all ?
135        action = self.filter.warnUserPQuota(userpquota)
136       
137        # adds the current job to history   
138        userpquota.Printer.addJobToHistory(self.filter.jobid, userpquota.User, counterbeforejob, action, filename=self.filter.preserveinputfile, title=self.filter.title, copies=self.filter.copies, options=self.filter.options)
139        return action
140           
Note: See TracBrowser for help on using the browser.