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

Revision 1624, 5.3 kB (checked in by jalet, 20 years ago)

Hardware accounting for LPRng should be OK now. UNTESTED.

  • 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#
[1257]6# (c) 2003-2004 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
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
20#
21# $Id$
22#
23# $Log$
[1624]24# Revision 1.18  2004/07/22 22:41:48  jalet
25# Hardware accounting for LPRng should be OK now. UNTESTED.
26#
[1600]27# Revision 1.17  2004/07/16 12:22:47  jalet
28# LPRng support early version
29#
[1499]30# Revision 1.16  2004/05/25 09:15:15  jalet
31# accounter.py : old code deleted
32# the rest : now exports PYKOTAPRECOMPUTEDJOBSIZE and PYKOTAPRECOMPUTEDJOBPRICE
33#
[1495]34# Revision 1.15  2004/05/24 22:45:49  jalet
35# New 'enforcement' directive added
36# Polling loop improvements
37#
[1483]38# Revision 1.14  2004/05/18 14:49:19  jalet
39# Big code changes to completely remove the need for "requester" directives,
40# jsut use "hardware(... your previous requester directive's content ...)"
41#
[1285]42# Revision 1.13  2004/01/12 22:43:40  jalet
43# New formula to compute a job's price
44#
[1272]45# Revision 1.12  2004/01/11 23:43:31  jalet
46# Bug wrt number of copies with CUPS should be fixed.
47#
[1271]48# Revision 1.11  2004/01/11 23:22:42  jalet
49# Major code refactoring, it's way cleaner, and now allows automated addition
50# of printers on first print.
51#
[1257]52# Revision 1.10  2004/01/08 14:10:32  jalet
53# Copyright year changed.
54#
[1240]55# Revision 1.9  2003/12/27 16:49:25  uid67467
56# Should be ok now.
[1239]57#
[1219]58# Revision 1.7  2003/11/25 23:46:40  jalet
59# Don't try to verify if module name is valid, Python does this better than us.
60#
[1180]61# Revision 1.6  2003/11/12 23:28:55  jalet
62# More work on new backend. This commit may be unstable.
63#
[1144]64# Revision 1.5  2003/10/07 09:07:28  jalet
65# Character encoding added to please latest version of Python
66#
[1083]67# Revision 1.4  2003/07/14 14:14:59  jalet
68# Old template
69#
[980]70# Revision 1.3  2003/04/30 19:53:58  jalet
71# 1.05
72#
[976]73# Revision 1.2  2003/04/30 13:36:40  jalet
74# Stupid accounting method was added.
75#
[973]76# Revision 1.1  2003/04/29 18:37:54  jalet
77# Pluggable accounting methods (actually doesn't support external scripts)
78#
79#
80#
81
[976]82import sys
83
[973]84class PyKotaAccounterError(Exception):
85    """An exception for Accounter related stuff."""
86    def __init__(self, message = ""):
87        self.message = message
88        Exception.__init__(self, message)
89    def __repr__(self):
90        return self.message
91    __str__ = __repr__
92   
93class AccounterBase :   
94    """A class to account print usage by querying printers."""
[980]95    def __init__(self, kotafilter, arguments) :
[973]96        """Sets instance vars depending on the current printer."""
97        self.filter = kotafilter
[980]98        self.arguments = arguments
[1600]99        self.isSoftware = 1 # by default software accounting
[973]100       
[1180]101    def getLastPageCounter(self) :   
102        """Returns last internal page counter value (possibly faked)."""
103        try :
[1483]104            return self.LastPageCounter or 0
[1180]105        except :   
106            return 0
[976]107           
[1624]108    def beginJob(self, printer) :   
[1239]109        """Saves the computed job size."""
110        # computes job's size
111        self.JobSize = self.computeJobSize()
[1272]112        if ((self.filter.printingsystem == "CUPS") \
113            and (self.filter.preserveinputfile is not None)) \
114            or (self.filter.printingsystem != "CUPS") :
115                self.JobSize *= self.filter.copies
[973]116       
[1239]117        # get last job information for this printer
[1624]118        if not printer.LastJob.Exists :
[1239]119            # The printer hasn't been used yet, from PyKota's point of view
120            self.LastPageCounter = 0
121        else :   
122            # get last job size and page counter from Quota Storage
123            # Last lifetime page counter before actual job is
124            # last page counter + last job size
[1624]125            self.LastPageCounter = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0)
[1239]126       
[1624]127    def fakeBeginJob(self) :   
[1239]128        """Do nothing."""
129        pass
130       
[1624]131    def endJob(self, printer) :   
132        """Do nothing."""
133        pass
134       
[1239]135    def getJobSize(self) :   
136        """Returns the actual job size."""
137        try :
138            return self.JobSize
139        except AttributeError :   
140            return 0
141       
[1240]142    def computeJobSize(self) :   
143        """Must be overriden in children classes."""
144        raise RuntimeError, "AccounterBase.computeJobSize() must be overriden !"
145       
[973]146def openAccounter(kotafilter) :
147    """Returns a connection handle to the appropriate accounter."""
[980]148    (backend, args) = kotafilter.config.getAccounterBackend(kotafilter.printername)
[973]149    try :
[1219]150        exec "from pykota.accounters import %s as accounterbackend" % backend.lower()
[973]151    except ImportError :
152        raise PyKotaAccounterError, _("Unsupported accounter backend %s") % backend
153    else :   
[1240]154        return accounterbackend.Accounter(kotafilter, args)
Note: See TracBrowser for help on using the browser.