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