1 | # PyKota |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | # |
---|
4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
5 | # |
---|
6 | # (c) 2003 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.9 2003/12/27 16:49:25 uid67467 |
---|
25 | # Should be ok now. |
---|
26 | # |
---|
27 | # Revision 1.7 2003/11/25 23:46:40 jalet |
---|
28 | # Don't try to verify if module name is valid, Python does this better than us. |
---|
29 | # |
---|
30 | # Revision 1.6 2003/11/12 23:28:55 jalet |
---|
31 | # More work on new backend. This commit may be unstable. |
---|
32 | # |
---|
33 | # Revision 1.5 2003/10/07 09:07:28 jalet |
---|
34 | # Character encoding added to please latest version of Python |
---|
35 | # |
---|
36 | # Revision 1.4 2003/07/14 14:14:59 jalet |
---|
37 | # Old template |
---|
38 | # |
---|
39 | # Revision 1.3 2003/04/30 19:53:58 jalet |
---|
40 | # 1.05 |
---|
41 | # |
---|
42 | # Revision 1.2 2003/04/30 13:36:40 jalet |
---|
43 | # Stupid accounting method was added. |
---|
44 | # |
---|
45 | # Revision 1.1 2003/04/29 18:37:54 jalet |
---|
46 | # Pluggable accounting methods (actually doesn't support external scripts) |
---|
47 | # |
---|
48 | # |
---|
49 | # |
---|
50 | |
---|
51 | import sys |
---|
52 | |
---|
53 | class PyKotaAccounterError(Exception): |
---|
54 | """An exception for Accounter related stuff.""" |
---|
55 | def __init__(self, message = ""): |
---|
56 | self.message = message |
---|
57 | Exception.__init__(self, message) |
---|
58 | def __repr__(self): |
---|
59 | return self.message |
---|
60 | __str__ = __repr__ |
---|
61 | |
---|
62 | class AccounterBase : |
---|
63 | """A class to account print usage by querying printers.""" |
---|
64 | def __init__(self, kotafilter, arguments) : |
---|
65 | """Sets instance vars depending on the current printer.""" |
---|
66 | self.filter = kotafilter |
---|
67 | self.arguments = arguments |
---|
68 | |
---|
69 | def getLastPageCounter(self) : |
---|
70 | """Returns last internal page counter value (possibly faked).""" |
---|
71 | try : |
---|
72 | return self.LastPageCounter |
---|
73 | except : |
---|
74 | return 0 |
---|
75 | |
---|
76 | def filterInput(self, inputfile) : |
---|
77 | """Transparent filter.""" |
---|
78 | mustclose = 0 |
---|
79 | if inputfile is not None : |
---|
80 | if hasattr(inputfile, "read") : |
---|
81 | infile = inputfile |
---|
82 | else : |
---|
83 | infile = open(inputfile, "rb") |
---|
84 | mustclose = 1 |
---|
85 | else : |
---|
86 | infile = sys.stdin |
---|
87 | data = infile.read(256*1024) |
---|
88 | while data : |
---|
89 | sys.stdout.write(data) |
---|
90 | data = infile.read(256*1024) |
---|
91 | if mustclose : |
---|
92 | infile.close() |
---|
93 | |
---|
94 | def beginJob(self, printer, user) : |
---|
95 | """Saves the computed job size.""" |
---|
96 | # computes job's size |
---|
97 | self.JobSize = self.computeJobSize() |
---|
98 | |
---|
99 | # get last job information for this printer |
---|
100 | if not printer.LastJob.Exists : |
---|
101 | # The printer hasn't been used yet, from PyKota's point of view |
---|
102 | self.LastPageCounter = 0 |
---|
103 | else : |
---|
104 | # get last job size and page counter from Quota Storage |
---|
105 | # Last lifetime page counter before actual job is |
---|
106 | # last page counter + last job size |
---|
107 | self.LastPageCounter = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0) |
---|
108 | |
---|
109 | def endJob(self, printer, user) : |
---|
110 | """Do nothing.""" |
---|
111 | pass |
---|
112 | |
---|
113 | def getJobSize(self) : |
---|
114 | """Returns the actual job size.""" |
---|
115 | try : |
---|
116 | return self.JobSize |
---|
117 | except AttributeError : |
---|
118 | return 0 |
---|
119 | |
---|
120 | def doAccounting(self, printer, user) : |
---|
121 | """Deletgates the computation of the job size to an external command. |
---|
122 | |
---|
123 | The command must print the job size on its standard output and exit successfully. |
---|
124 | """ |
---|
125 | self.beginJob(printer, user) |
---|
126 | |
---|
127 | # get the job size, which is real job size * number of copies. |
---|
128 | jobsize = self.getJobSize() * self.filter.copies |
---|
129 | |
---|
130 | # Is the current user allowed to print at all ? |
---|
131 | userpquota = self.filter.storage.getUserPQuota(user, printer) |
---|
132 | action = self.filter.warnUserPQuota(userpquota) |
---|
133 | |
---|
134 | # update the quota for the current user on this printer, if allowed to print |
---|
135 | if action == "DENY" : |
---|
136 | jobsize = 0 |
---|
137 | else : |
---|
138 | userpquota.increasePagesUsage(jobsize) |
---|
139 | |
---|
140 | # adds the current job to history |
---|
141 | jobprice = (float(printer.PricePerPage or 0.0) * jobsize) + float(printer.PricePerJob or 0.0) |
---|
142 | printer.addJobToHistory(self.filter.jobid, user, self.getLastPageCounter(), action, jobsize, jobprice, self.filter.preserveinputfile, self.filter.title, self.filter.copies, self.filter.options) |
---|
143 | self.endJob(printer, user) |
---|
144 | return action |
---|
145 | |
---|
146 | def computeJobSize(self) : |
---|
147 | """Must be overriden in children classes.""" |
---|
148 | raise RuntimeError, "AccounterBase.computeJobSize() must be overriden !" |
---|
149 | |
---|
150 | |
---|
151 | def openAccounter(kotafilter) : |
---|
152 | """Returns a connection handle to the appropriate accounter.""" |
---|
153 | (backend, args) = kotafilter.config.getAccounterBackend(kotafilter.printername) |
---|
154 | try : |
---|
155 | exec "from pykota.accounters import %s as accounterbackend" % backend.lower() |
---|
156 | except ImportError : |
---|
157 | raise PyKotaAccounterError, _("Unsupported accounter backend %s") % backend |
---|
158 | else : |
---|
159 | return accounterbackend.Accounter(kotafilter, args) |
---|