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

Revision 3245, 4.2 kB (checked in by jerome, 17 years ago)

Now the pykotme command line tool computes size and price based on the preaccounter
define for each printer instead of always using the internal page counter.
IMPORTANT : pykotme.cgi still uses the old behavior.

  • 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, 2005, 2006, 2007 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
25"""This module defines base classes used by all accounting methods."""
26
27import sys
28import os
29import imp
30
31class PyKotaAccounterError(Exception):
32    """An exception for Accounter related stuff."""
33    def __init__(self, message = ""):
34        self.message = message
35        Exception.__init__(self, message)
36    def __repr__(self):
37        return self.message
38    __str__ = __repr__
39   
40class AccounterBase :   
41    """A class to account print usage by querying printers."""
42    def __init__(self, kotafilter, arguments, ispreaccounter=0, name="Unknown") :
43        """Sets instance vars depending on the current printer."""
44        self.name = name
45        self.filter = kotafilter
46        self.arguments = arguments
47        self.onerror = self.filter.config.getPrinterOnAccounterError(self.filter.PrinterName)
48        self.isSoftware = 1 # by default software accounting
49        self.isPreAccounter = ispreaccounter 
50        self.inkUsage = []
51       
52    def getLastPageCounter(self) :   
53        """Returns last internal page counter value (possibly faked)."""
54        try :
55            return self.LastPageCounter or 0
56        except :   
57            return 0
58           
59    def beginJob(self, printer) :   
60        """Saves the computed job size."""
61        # computes job's size
62        self.JobSize = self.computeJobSize()
63       
64        # get last job information for this printer
65        if not self.isPreAccounter :
66            # TODO : check if this code is still needed
67            if not printer.LastJob.Exists :
68                # The printer hasn't been used yet, from PyKota's point of view
69                self.LastPageCounter = 0
70            else :   
71                # get last job size and page counter from Quota Storage
72                # Last lifetime page counter before actual job is
73                # last page counter + last job size
74                self.LastPageCounter = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0)
75       
76    def fakeBeginJob(self) :   
77        """Do nothing."""
78        pass
79       
80    def endJob(self, printer) :   
81        """Do nothing."""
82        pass
83       
84    def getJobSize(self, printer) :   
85        """Returns the actual job size."""
86        try :
87            return self.JobSize
88        except AttributeError :   
89            return 0
90       
91    def computeJobSize(self) :   
92        """Must be overriden in children classes."""
93        raise RuntimeError, "AccounterBase.computeJobSize() must be overriden !"
94       
95def openAccounter(kotafilter, ispreaccounter=0) :
96    """Returns a connection handle to the appropriate accounter."""
97    if ispreaccounter :
98        (backend, args) = kotafilter.config.getPreAccounterBackend(kotafilter.PrinterName)
99    else :
100        (backend, args) = kotafilter.config.getAccounterBackend(kotafilter.PrinterName)
101    try :
102        accounterbackend = imp.load_source("accounterbackend", 
103                                            os.path.join(os.path.dirname(__file__),
104                                                         "accounters",
105                                                         "%s.py" % backend.lower()))
106    except ImportError :
107        raise PyKotaAccounterError, _("Unsupported accounter backend %s") % backend
108    else :   
109        return accounterbackend.Accounter(kotafilter, args, ispreaccounter, backend.lower())
Note: See TracBrowser for help on using the browser.