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

Revision 1495, 5.5 kB (checked in by jalet, 20 years ago)

New 'enforcement' directive added
Polling loop improvements

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