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

Revision 3133, 4.1 kB (checked in by jerome, 17 years ago)

Changed copyright years.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[973]1# PyKota
[1144]2# -*- coding: ISO-8859-15 -*-
[973]3#
4# PyKota : Print Quotas for CUPS and LPRng
5#
[3133]6# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
[973]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
[2302]19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
[973]20#
21# $Id$
22#
[1873]23#
[973]24
[976]25import sys
[2945]26import os
27import imp
[976]28
[973]29class PyKotaAccounterError(Exception):
30    """An exception for Accounter related stuff."""
31    def __init__(self, message = ""):
32        self.message = message
33        Exception.__init__(self, message)
34    def __repr__(self):
35        return self.message
36    __str__ = __repr__
37   
38class AccounterBase :   
39    """A class to account print usage by querying printers."""
[2635]40    def __init__(self, kotafilter, arguments, ispreaccounter=0) :
[973]41        """Sets instance vars depending on the current printer."""
42        self.filter = kotafilter
[980]43        self.arguments = arguments
[2409]44        self.onerror = self.filter.config.getPrinterOnAccounterError(self.filter.PrinterName)
[1600]45        self.isSoftware = 1 # by default software accounting
[2635]46        self.isPreAccounter = ispreaccounter 
[3036]47        self.inkUsage = []
[973]48       
[1180]49    def getLastPageCounter(self) :   
50        """Returns last internal page counter value (possibly faked)."""
51        try :
[1483]52            return self.LastPageCounter or 0
[1180]53        except :   
54            return 0
[976]55           
[1624]56    def beginJob(self, printer) :   
[1239]57        """Saves the computed job size."""
58        # computes job's size
59        self.JobSize = self.computeJobSize()
[973]60       
[1239]61        # get last job information for this printer
[2635]62        if not self.isPreAccounter :
63            # TODO : check if this code is still needed
64            if not printer.LastJob.Exists :
65                # The printer hasn't been used yet, from PyKota's point of view
66                self.LastPageCounter = 0
67            else :   
68                # get last job size and page counter from Quota Storage
69                # Last lifetime page counter before actual job is
70                # last page counter + last job size
71                self.LastPageCounter = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0)
[1239]72       
[1624]73    def fakeBeginJob(self) :   
[1239]74        """Do nothing."""
75        pass
76       
[1624]77    def endJob(self, printer) :   
78        """Do nothing."""
79        pass
80       
[1713]81    def getJobSize(self, printer) :   
[1239]82        """Returns the actual job size."""
83        try :
84            return self.JobSize
85        except AttributeError :   
86            return 0
87       
[1240]88    def computeJobSize(self) :   
89        """Must be overriden in children classes."""
90        raise RuntimeError, "AccounterBase.computeJobSize() must be overriden !"
91       
[2635]92def openAccounter(kotafilter, ispreaccounter=0) :
[973]93    """Returns a connection handle to the appropriate accounter."""
[2635]94    if ispreaccounter :
95        (backend, args) = kotafilter.config.getPreAccounterBackend(kotafilter.PrinterName)
96    else :
97        (backend, args) = kotafilter.config.getAccounterBackend(kotafilter.PrinterName)
[973]98    try :
[2945]99        accounterbackend = imp.load_source("accounterbackend", 
100                                            os.path.join(os.path.dirname(__file__),
[2947]101                                                         "accounters",
[2945]102                                                         "%s.py" % backend.lower()))
[973]103    except ImportError :
104        raise PyKotaAccounterError, _("Unsupported accounter backend %s") % backend
105    else :   
[2635]106        return accounterbackend.Accounter(kotafilter, args, ispreaccounter)
Note: See TracBrowser for help on using the browser.