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

Revision 3260, 4.1 kB (checked in by jerome, 16 years ago)

Changed license to GNU GPL v3 or later.
Changed Python source encoding from ISO-8859-15 to UTF-8 (only ASCII
was used anyway).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[3260]1# -*- coding: UTF-8 -*-
[973]2#
[3260]3# PyKota : Print Quotas for CUPS
[973]4#
[3133]5# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
[3260]6# This program is free software: you can redistribute it and/or modify
[973]7# it under the terms of the GNU General Public License as published by
[3260]8# the Free Software Foundation, either version 3 of the License, or
[973]9# (at your option) any later version.
[3260]10#
[973]11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
[3260]17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[973]18#
19# $Id$
20#
[1873]21#
[973]22
[3184]23"""This module defines base classes used by all accounting methods."""
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."""
[3245]40    def __init__(self, kotafilter, arguments, ispreaccounter=0, name="Unknown") :
[973]41        """Sets instance vars depending on the current printer."""
[3245]42        self.name = name
[973]43        self.filter = kotafilter
[980]44        self.arguments = arguments
[2409]45        self.onerror = self.filter.config.getPrinterOnAccounterError(self.filter.PrinterName)
[1600]46        self.isSoftware = 1 # by default software accounting
[2635]47        self.isPreAccounter = ispreaccounter 
[3036]48        self.inkUsage = []
[973]49       
[1180]50    def getLastPageCounter(self) :   
51        """Returns last internal page counter value (possibly faked)."""
52        try :
[1483]53            return self.LastPageCounter or 0
[1180]54        except :   
55            return 0
[976]56           
[1624]57    def beginJob(self, printer) :   
[1239]58        """Saves the computed job size."""
59        # computes job's size
60        self.JobSize = self.computeJobSize()
[973]61       
[1239]62        # get last job information for this printer
[2635]63        if not self.isPreAccounter :
64            # TODO : check if this code is still needed
65            if not printer.LastJob.Exists :
66                # The printer hasn't been used yet, from PyKota's point of view
67                self.LastPageCounter = 0
68            else :   
69                # get last job size and page counter from Quota Storage
70                # Last lifetime page counter before actual job is
71                # last page counter + last job size
72                self.LastPageCounter = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0)
[1239]73       
[1624]74    def fakeBeginJob(self) :   
[1239]75        """Do nothing."""
76        pass
77       
[1624]78    def endJob(self, printer) :   
79        """Do nothing."""
80        pass
81       
[1713]82    def getJobSize(self, printer) :   
[1239]83        """Returns the actual job size."""
84        try :
85            return self.JobSize
86        except AttributeError :   
87            return 0
88       
[1240]89    def computeJobSize(self) :   
90        """Must be overriden in children classes."""
91        raise RuntimeError, "AccounterBase.computeJobSize() must be overriden !"
92       
[2635]93def openAccounter(kotafilter, ispreaccounter=0) :
[973]94    """Returns a connection handle to the appropriate accounter."""
[2635]95    if ispreaccounter :
96        (backend, args) = kotafilter.config.getPreAccounterBackend(kotafilter.PrinterName)
97    else :
98        (backend, args) = kotafilter.config.getAccounterBackend(kotafilter.PrinterName)
[973]99    try :
[2945]100        accounterbackend = imp.load_source("accounterbackend", 
101                                            os.path.join(os.path.dirname(__file__),
[2947]102                                                         "accounters",
[2945]103                                                         "%s.py" % backend.lower()))
[973]104    except ImportError :
105        raise PyKotaAccounterError, _("Unsupported accounter backend %s") % backend
106    else :   
[3245]107        return accounterbackend.Accounter(kotafilter, args, ispreaccounter, backend.lower())
Note: See TracBrowser for help on using the browser.