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

Revision 1600, 5.1 kB (checked in by jalet, 20 years ago)

LPRng support early version

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