root / pykota / trunk / pykota / accounters / hardware.py @ 1475

Revision 1475, 4.2 kB (checked in by jalet, 20 years ago)

Code simplifications

  • 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.1  2004/05/13 13:59:30  jalet
25# Code simplifications
26#
27#
28#
29
30import sys
31import os
32from pykota.accounter import AccounterBase, PyKotaAccounterError
33from pykota.requester import openRequester, PyKotaRequesterError
34
35class Accounter(AccounterBase) :
36    def __init__(self, kotabackend, arguments) :
37        """Initializes querying accounter."""
38        AccounterBase.__init__(self, kotabackend, arguments)
39        self.requester = openRequester(kotabackend, kotabackend.printername)
40        self.isDelayed = 1 # With the pykota filter, accounting is delayed by one job
41       
42    def getPrinterInternalPageCounter(self) :   
43        """Returns the printer's internal page counter."""
44        self.filter.logdebug("Reading printer's internal page counter...")
45        try :
46            counter = self.requester.getPrinterPageCounter(self.filter.printerhostname)
47        except PyKotaRequesterError, msg :
48            # can't get actual page counter, assume printer is off or warming up
49            # log the message anyway.
50            self.filter.logger.log_message("%s" % msg, "warn")
51            counter = None
52        self.filter.logdebug("Printer's internal page counter value is : %s" % str(counter))
53        return counter   
54       
55    def beginJob(self, userpquota) :   
56        """Saves printer internal page counter at start of job."""
57        # save page counter before job
58        self.LastPageCounter = self.counterbefore = self.getPrinterInternalPageCounter()
59       
60    def endJob(self, userpquota) :   
61        """Saves printer internal page counter at end of job."""
62        # save page counter after job
63        self.LastPageCounter = self.counterafter = self.getPrinterInternalPageCounter()
64       
65    def getJobSize(self) :   
66        """Returns the actual job size."""
67        try :
68            jobsize = (self.counterafter - self.counterbefore)   
69            if jobsize < 0 :
70                # Try to take care of HP printers
71                # Their internal page counter is saved to NVRAM
72                # only every 10 pages. If the printer was switched
73                # off then back on during the job, and that the
74                # counters difference is negative, we know
75                # the formula (we can't know if more than eleven
76                # pages were printed though) :
77                if jobsize > -10 :
78                    jobsize += 10
79                else :   
80                    # here we may have got a printer being replaced
81                    # DURING the job. This is HIGHLY improbable !
82                    jobsize = 0
83        except :   
84            # takes care of the case where one counter (or both) was never set.
85            jobsize = 0
86        return jobsize
87       
88    def doAccounting(self, userpquota) :
89        """Does print accounting and returns if the job status is ALLOW or DENY."""
90        # Get the page counter directly from the printer itself
91        counterbeforejob = self.getPrinterInternalPageCounter() or 0
92       
93        # Is the current user allowed to print at all ?
94        action = self.filter.warnUserPQuota(userpquota)
95       
96        # adds the current job to history   
97        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)
98        return action
99           
Note: See TracBrowser for help on using the browser.