[976] | 1 | # PyKota |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[976] | 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$ |
---|
[1144] | 24 | # Revision 1.5 2003/10/07 09:07:29 jalet |
---|
| 25 | # Character encoding added to please latest version of Python |
---|
| 26 | # |
---|
[1041] | 27 | # Revision 1.4 2003/06/25 14:10:01 jalet |
---|
| 28 | # Hey, it may work (edpykota --reset excepted) ! |
---|
| 29 | # |
---|
[1000] | 30 | # Revision 1.3 2003/05/27 23:00:21 jalet |
---|
| 31 | # Big rewrite of external accounting methods. |
---|
| 32 | # Should work well now. |
---|
| 33 | # |
---|
[977] | 34 | # Revision 1.2 2003/04/30 13:40:47 jalet |
---|
| 35 | # Small fix |
---|
| 36 | # |
---|
[976] | 37 | # Revision 1.1 2003/04/30 13:36:40 jalet |
---|
| 38 | # Stupid accounting method was added. |
---|
| 39 | # |
---|
| 40 | # |
---|
| 41 | # |
---|
| 42 | |
---|
| 43 | import sys |
---|
| 44 | import tempfile |
---|
| 45 | from pykota.accounter import AccounterBase, PyKotaAccounterError |
---|
| 46 | |
---|
| 47 | class Accounter(AccounterBase) : |
---|
[1041] | 48 | def doAccounting(self, printer, user) : |
---|
[976] | 49 | """Does print accounting by stupidly counting the 'showpage' postscript instructions in the document. |
---|
| 50 | |
---|
| 51 | This method is essentially unreliable, but shows how to create a simple accounter. |
---|
| 52 | """ |
---|
| 53 | # first we log a message because using this accounting method is not recommended. |
---|
| 54 | self.filter.logger.log_message(_("Using the 'stupid' accounting method is unreliable."), "warn") |
---|
| 55 | |
---|
| 56 | # get the job size |
---|
[1000] | 57 | jobsize = self.getJobSize() * self.filter.copies |
---|
[976] | 58 | |
---|
| 59 | # get last job information for this printer |
---|
[1041] | 60 | if not printer.LastJob.Exists : |
---|
[976] | 61 | # The printer hasn't been used yet, from PyKota's point of view |
---|
| 62 | counterbeforejob = 0 |
---|
| 63 | else : |
---|
| 64 | # get last job size and page counter from Quota Storage |
---|
| 65 | # Last lifetime page counter before actual job is |
---|
| 66 | # last page counter + last job size |
---|
[1041] | 67 | counterbeforejob = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0) |
---|
[976] | 68 | |
---|
| 69 | # Is the current user allowed to print at all ? |
---|
[1041] | 70 | userpquota = self.filter.storage.getUserPQuota(user, printer) |
---|
| 71 | action = self.filter.warnUserPQuota(userpquota) |
---|
[976] | 72 | |
---|
| 73 | # update the quota for the current user on this printer, if allowed to print |
---|
| 74 | if action == "DENY" : |
---|
| 75 | jobsize = 0 |
---|
| 76 | else : |
---|
[1041] | 77 | userpquota.increasePagesUsage(jobsize) |
---|
[976] | 78 | |
---|
| 79 | # adds the current job to history |
---|
[1041] | 80 | printer.addJobToHistory(self.filter.jobid, user, counterbeforejob, action, jobsize) |
---|
[976] | 81 | |
---|
| 82 | return action |
---|
| 83 | |
---|
| 84 | def getJobSize(self) : |
---|
| 85 | """Computes the job size and return its value. |
---|
| 86 | |
---|
| 87 | THIS METHOD IS COMPLETELY UNRELIABLE BUT SERVES AS AN EXAMPLE. |
---|
| 88 | """ |
---|
| 89 | temporary = None |
---|
| 90 | if self.filter.inputfile is None : |
---|
| 91 | infile = sys.stdin |
---|
| 92 | # we will have to duplicate our standard input |
---|
| 93 | temporary = tempfile.TemporaryFile() |
---|
| 94 | else : |
---|
| 95 | infile = open(self.filter.inputfile, "rb") |
---|
| 96 | |
---|
| 97 | pagecount = 0 |
---|
| 98 | for line in infile.xreadlines() : |
---|
[1000] | 99 | pagecount += line.count("showpage") |
---|
[976] | 100 | if temporary is not None : |
---|
| 101 | temporary.write(line) |
---|
| 102 | |
---|
| 103 | if temporary is not None : |
---|
| 104 | # this is a copy of our previous standard input |
---|
| 105 | # flush, then rewind |
---|
| 106 | temporary.flush() |
---|
| 107 | temporary.seek(0, 0) |
---|
| 108 | # our temporary file will be used later if the |
---|
| 109 | # job is allowed. |
---|
| 110 | self.filter.inputfile = temporary |
---|
| 111 | else : |
---|
| 112 | infile.close() |
---|
| 113 | |
---|
| 114 | return pagecount |
---|
| 115 | |
---|