1 | # PyKota |
---|
2 | # |
---|
3 | # PyKota - Print Quotas for CUPS and LPRng |
---|
4 | # |
---|
5 | # (c) 2003 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 2 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, write to the Free Software |
---|
18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | # |
---|
20 | # $Id$ |
---|
21 | # |
---|
22 | # $Log$ |
---|
23 | # Revision 1.1 2003/04/30 13:36:40 jalet |
---|
24 | # Stupid accounting method was added. |
---|
25 | # |
---|
26 | # |
---|
27 | # |
---|
28 | |
---|
29 | import sys |
---|
30 | import tempfile |
---|
31 | from pykota.accounter import AccounterBase, PyKotaAccounterError |
---|
32 | |
---|
33 | class Accounter(AccounterBase) : |
---|
34 | def doAccounting(self, printerid, userid) : |
---|
35 | """Does print accounting by stupidly counting the 'showpage' postscript instructions in the document. |
---|
36 | |
---|
37 | This method is essentially unreliable, but shows how to create a simple accounter. |
---|
38 | """ |
---|
39 | # first we log a message because using this accounting method is not recommended. |
---|
40 | self.filter.logger.log_message(_("Using the 'stupid' accounting method is unreliable."), "warn") |
---|
41 | |
---|
42 | # get the job size |
---|
43 | jobsize = self.getJobSize() |
---|
44 | |
---|
45 | # get last job information for this printer |
---|
46 | pgc = self.filter.storage.getPrinterPageCounter(printerid) |
---|
47 | if pgc is None : |
---|
48 | # The printer hasn't been used yet, from PyKota's point of view |
---|
49 | counterbeforejob = 0 |
---|
50 | else : |
---|
51 | # get last job size and page counter from Quota Storage |
---|
52 | # Last lifetime page counter before actual job is |
---|
53 | # last page counter + last job size |
---|
54 | counterbeforejob = pgc["pagecounter"] + pgc["jobsize"] |
---|
55 | |
---|
56 | # Is the current user allowed to print at all ? |
---|
57 | action = self.filter.warnUserPQuota(self.filter.username, self.filter.printername) |
---|
58 | |
---|
59 | # update the quota for the current user on this printer, if allowed to print |
---|
60 | if action == "DENY" : |
---|
61 | jobsize = 0 |
---|
62 | else : |
---|
63 | self.filter.storage.updateUserPQuota(userid, printerid, jobsize) |
---|
64 | |
---|
65 | # adds the current job to history |
---|
66 | self.filter.storage.addJobToHistory(self.filter.jobid, self.filter.storage.getUserId(self.filter.username), printerid, counterbeforejob, action, jobsize) |
---|
67 | |
---|
68 | return action |
---|
69 | |
---|
70 | def getJobSize(self) : |
---|
71 | """Computes the job size and return its value. |
---|
72 | |
---|
73 | THIS METHOD IS COMPLETELY UNRELIABLE BUT SERVES AS AN EXAMPLE. |
---|
74 | """ |
---|
75 | temporary = None |
---|
76 | if self.filter.inputfile is None : |
---|
77 | infile = sys.stdin |
---|
78 | # we will have to duplicate our standard input |
---|
79 | temporary = tempfile.TemporaryFile() |
---|
80 | else : |
---|
81 | infile = open(self.filter.inputfile, "rb") |
---|
82 | |
---|
83 | pagecount = 0 |
---|
84 | for line in infile.xreadlines() : |
---|
85 | if line.startswith("showpage") : |
---|
86 | pagecount += 1 |
---|
87 | if temporary is not None : |
---|
88 | temporary.write(line) |
---|
89 | |
---|
90 | if temporary is not None : |
---|
91 | # this is a copy of our previous standard input |
---|
92 | # flush, then rewind |
---|
93 | temporary.flush() |
---|
94 | temporary.seek(0, 0) |
---|
95 | # our temporary file will be used later if the |
---|
96 | # job is allowed. |
---|
97 | self.filter.inputfile = temporary |
---|
98 | else : |
---|
99 | infile.close() |
---|
100 | |
---|
101 | return pagecount |
---|
102 | |
---|