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

Revision 2409, 3.5 kB (checked in by jerome, 19 years ago)

The cupspykota backend was rewritten from scratch. MacOSX servers should
work just fine now.
Severity : High. Testers MORE than welcome :-)

  • 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20#
21# $Id$
22#
23#
24
25import sys
26
27class PyKotaAccounterError(Exception):
28    """An exception for Accounter related stuff."""
29    def __init__(self, message = ""):
30        self.message = message
31        Exception.__init__(self, message)
32    def __repr__(self):
33        return self.message
34    __str__ = __repr__
35   
36class AccounterBase :   
37    """A class to account print usage by querying printers."""
38    def __init__(self, kotafilter, arguments) :
39        """Sets instance vars depending on the current printer."""
40        self.filter = kotafilter
41        self.arguments = arguments
42        self.onerror = self.filter.config.getPrinterOnAccounterError(self.filter.PrinterName)
43        self.isSoftware = 1 # by default software accounting
44       
45    def getLastPageCounter(self) :   
46        """Returns last internal page counter value (possibly faked)."""
47        try :
48            return self.LastPageCounter or 0
49        except :   
50            return 0
51           
52    def beginJob(self, printer) :   
53        """Saves the computed job size."""
54        # computes job's size
55        self.JobSize = self.computeJobSize()
56        if self.filter.InputFile is not None :
57            self.JobSize *= self.filter.Copies
58       
59        # get last job information for this printer
60        if not printer.LastJob.Exists :
61            # The printer hasn't been used yet, from PyKota's point of view
62            self.LastPageCounter = 0
63        else :   
64            # get last job size and page counter from Quota Storage
65            # Last lifetime page counter before actual job is
66            # last page counter + last job size
67            self.LastPageCounter = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0)
68       
69    def fakeBeginJob(self) :   
70        """Do nothing."""
71        pass
72       
73    def endJob(self, printer) :   
74        """Do nothing."""
75        pass
76       
77    def getJobSize(self, printer) :   
78        """Returns the actual job size."""
79        try :
80            return self.JobSize
81        except AttributeError :   
82            return 0
83       
84    def computeJobSize(self) :   
85        """Must be overriden in children classes."""
86        raise RuntimeError, "AccounterBase.computeJobSize() must be overriden !"
87       
88def openAccounter(kotafilter) :
89    """Returns a connection handle to the appropriate accounter."""
90    (backend, args) = kotafilter.config.getAccounterBackend(kotafilter.PrinterName)
91    try :
92        exec "from pykota.accounters import %s as accounterbackend" % backend.lower()
93    except ImportError :
94        raise PyKotaAccounterError, _("Unsupported accounter backend %s") % backend
95    else :   
96        return accounterbackend.Accounter(kotafilter, args)
Note: See TracBrowser for help on using the browser.