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

Revision 3489, 3.7 kB (checked in by jerome, 16 years ago)

Removed bad copy and paste artifact.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1# -*- coding: utf-8 -*-
2#
3# PyKota : Print Quotas for CUPS
4#
5# (c) 2003-2009 Jerome Alet <alet@librelogiciel.com>
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
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
17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18#
19# $Id$
20#
21#
22
23"""This module defines base classes used by all accounting methods."""
24
25import sys
26import os
27import imp
28
29from pykota.errors import PyKotaAccounterError
30
31class AccounterBase :
32    """A class to account print usage by querying printers."""
33    def __init__(self, kotafilter, arguments, ispreaccounter=0, name="Unknown") :
34        """Sets instance vars depending on the current printer."""
35        self.name = name
36        self.filter = kotafilter
37        self.arguments = arguments
38        self.onerror = self.filter.config.getPrinterOnAccounterError(self.filter.PrinterName)
39        self.isSoftware = 1 # by default software accounting
40        self.isPreAccounter = ispreaccounter
41        self.inkUsage = []
42
43    def getLastPageCounter(self) :
44        """Returns last internal page counter value (possibly faked)."""
45        try :
46            return self.LastPageCounter or 0
47        except :
48            return 0
49
50    def beginJob(self, printer) :
51        """Saves the computed job size."""
52        # computes job's size
53        self.JobSize = self.computeJobSize()
54
55        # get last job information for this printer
56        if not self.isPreAccounter :
57            # TODO : check if this code is still needed
58            if not printer.LastJob.Exists :
59                # The printer hasn't been used yet, from PyKota's point of view
60                self.LastPageCounter = 0
61            else :
62                # get last job size and page counter from Quota Storage
63                # Last lifetime page counter before actual job is
64                # last page counter + last job size
65                self.LastPageCounter = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0)
66
67    def fakeBeginJob(self) :
68        """Do nothing."""
69        pass
70
71    def endJob(self, printer) :
72        """Do nothing."""
73        pass
74
75    def getJobSize(self, printer) :
76        """Returns the actual job size."""
77        try :
78            return self.JobSize
79        except AttributeError :
80            return 0
81
82    def computeJobSize(self) :
83        """Must be overriden in children classes."""
84        raise RuntimeError, "AccounterBase.computeJobSize() must be overriden !"
85
86def openAccounter(kotafilter, ispreaccounter=0) :
87    """Returns a connection handle to the appropriate accounter."""
88    if ispreaccounter :
89        (backend, args) = kotafilter.config.getPreAccounterBackend(kotafilter.PrinterName)
90    else :
91        (backend, args) = kotafilter.config.getAccounterBackend(kotafilter.PrinterName)
92    try :
93        accounterbackend = imp.load_source("accounterbackend",
94                                            os.path.join(os.path.dirname(__file__),
95                                                         "accounters",
96                                                         "%s.py" % backend.lower()))
97    except ImportError :
98        raise PyKotaAccounterError, _("Unsupported accounter backend %s") % backend
99    else :
100        return accounterbackend.Accounter(kotafilter, args, ispreaccounter, backend.lower())
Note: See TracBrowser for help on using the browser.