[973] | 1 | # PyKota |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[973] | 3 | # |
---|
| 4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
[1257] | 6 | # (c) 2003-2004 Jerome Alet <alet@librelogiciel.com> |
---|
[973] | 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$ |
---|
[1873] | 24 | # Revision 1.21 2004/10/24 12:49:02 jalet |
---|
| 25 | # Fixed bad indentation |
---|
| 26 | # |
---|
[1713] | 27 | # Revision 1.20 2004/09/13 16:02:45 jalet |
---|
| 28 | # Added fix for incorrect job's size when hardware accounting fails |
---|
| 29 | # |
---|
[1687] | 30 | # Revision 1.19 2004/08/31 23:29:53 jalet |
---|
| 31 | # Introduction of the new 'onaccountererror' configuration directive. |
---|
| 32 | # Small fix for software accounter's return code which can't be None anymore. |
---|
| 33 | # Make software and hardware accounting code look similar : will be factorized |
---|
| 34 | # later. |
---|
| 35 | # |
---|
[1624] | 36 | # Revision 1.18 2004/07/22 22:41:48 jalet |
---|
| 37 | # Hardware accounting for LPRng should be OK now. UNTESTED. |
---|
| 38 | # |
---|
[1600] | 39 | # Revision 1.17 2004/07/16 12:22:47 jalet |
---|
| 40 | # LPRng support early version |
---|
| 41 | # |
---|
[1499] | 42 | # Revision 1.16 2004/05/25 09:15:15 jalet |
---|
| 43 | # accounter.py : old code deleted |
---|
| 44 | # the rest : now exports PYKOTAPRECOMPUTEDJOBSIZE and PYKOTAPRECOMPUTEDJOBPRICE |
---|
| 45 | # |
---|
[1495] | 46 | # Revision 1.15 2004/05/24 22:45:49 jalet |
---|
| 47 | # New 'enforcement' directive added |
---|
| 48 | # Polling loop improvements |
---|
| 49 | # |
---|
[1483] | 50 | # Revision 1.14 2004/05/18 14:49:19 jalet |
---|
| 51 | # Big code changes to completely remove the need for "requester" directives, |
---|
| 52 | # jsut use "hardware(... your previous requester directive's content ...)" |
---|
| 53 | # |
---|
[1285] | 54 | # Revision 1.13 2004/01/12 22:43:40 jalet |
---|
| 55 | # New formula to compute a job's price |
---|
| 56 | # |
---|
[1272] | 57 | # Revision 1.12 2004/01/11 23:43:31 jalet |
---|
| 58 | # Bug wrt number of copies with CUPS should be fixed. |
---|
| 59 | # |
---|
[1271] | 60 | # Revision 1.11 2004/01/11 23:22:42 jalet |
---|
| 61 | # Major code refactoring, it's way cleaner, and now allows automated addition |
---|
| 62 | # of printers on first print. |
---|
| 63 | # |
---|
[1257] | 64 | # Revision 1.10 2004/01/08 14:10:32 jalet |
---|
| 65 | # Copyright year changed. |
---|
| 66 | # |
---|
[1240] | 67 | # Revision 1.9 2003/12/27 16:49:25 uid67467 |
---|
| 68 | # Should be ok now. |
---|
[1239] | 69 | # |
---|
[1219] | 70 | # Revision 1.7 2003/11/25 23:46:40 jalet |
---|
| 71 | # Don't try to verify if module name is valid, Python does this better than us. |
---|
| 72 | # |
---|
[1180] | 73 | # Revision 1.6 2003/11/12 23:28:55 jalet |
---|
| 74 | # More work on new backend. This commit may be unstable. |
---|
| 75 | # |
---|
[1144] | 76 | # Revision 1.5 2003/10/07 09:07:28 jalet |
---|
| 77 | # Character encoding added to please latest version of Python |
---|
| 78 | # |
---|
[1083] | 79 | # Revision 1.4 2003/07/14 14:14:59 jalet |
---|
| 80 | # Old template |
---|
| 81 | # |
---|
[980] | 82 | # Revision 1.3 2003/04/30 19:53:58 jalet |
---|
| 83 | # 1.05 |
---|
| 84 | # |
---|
[976] | 85 | # Revision 1.2 2003/04/30 13:36:40 jalet |
---|
| 86 | # Stupid accounting method was added. |
---|
| 87 | # |
---|
[973] | 88 | # Revision 1.1 2003/04/29 18:37:54 jalet |
---|
| 89 | # Pluggable accounting methods (actually doesn't support external scripts) |
---|
| 90 | # |
---|
| 91 | # |
---|
| 92 | # |
---|
| 93 | |
---|
[976] | 94 | import sys |
---|
| 95 | |
---|
[973] | 96 | class PyKotaAccounterError(Exception): |
---|
| 97 | """An exception for Accounter related stuff.""" |
---|
| 98 | def __init__(self, message = ""): |
---|
| 99 | self.message = message |
---|
| 100 | Exception.__init__(self, message) |
---|
| 101 | def __repr__(self): |
---|
| 102 | return self.message |
---|
| 103 | __str__ = __repr__ |
---|
| 104 | |
---|
| 105 | class AccounterBase : |
---|
| 106 | """A class to account print usage by querying printers.""" |
---|
[980] | 107 | def __init__(self, kotafilter, arguments) : |
---|
[973] | 108 | """Sets instance vars depending on the current printer.""" |
---|
| 109 | self.filter = kotafilter |
---|
[980] | 110 | self.arguments = arguments |
---|
[1687] | 111 | self.onerror = self.filter.config.getPrinterOnAccounterError(self.filter.printername) |
---|
[1600] | 112 | self.isSoftware = 1 # by default software accounting |
---|
[973] | 113 | |
---|
[1180] | 114 | def getLastPageCounter(self) : |
---|
| 115 | """Returns last internal page counter value (possibly faked).""" |
---|
| 116 | try : |
---|
[1483] | 117 | return self.LastPageCounter or 0 |
---|
[1180] | 118 | except : |
---|
| 119 | return 0 |
---|
[976] | 120 | |
---|
[1624] | 121 | def beginJob(self, printer) : |
---|
[1239] | 122 | """Saves the computed job size.""" |
---|
| 123 | # computes job's size |
---|
| 124 | self.JobSize = self.computeJobSize() |
---|
[1272] | 125 | if ((self.filter.printingsystem == "CUPS") \ |
---|
| 126 | and (self.filter.preserveinputfile is not None)) \ |
---|
| 127 | or (self.filter.printingsystem != "CUPS") : |
---|
[1873] | 128 | self.JobSize *= self.filter.copies |
---|
[973] | 129 | |
---|
[1239] | 130 | # get last job information for this printer |
---|
[1624] | 131 | if not printer.LastJob.Exists : |
---|
[1239] | 132 | # The printer hasn't been used yet, from PyKota's point of view |
---|
| 133 | self.LastPageCounter = 0 |
---|
| 134 | else : |
---|
| 135 | # get last job size and page counter from Quota Storage |
---|
| 136 | # Last lifetime page counter before actual job is |
---|
| 137 | # last page counter + last job size |
---|
[1624] | 138 | self.LastPageCounter = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0) |
---|
[1239] | 139 | |
---|
[1624] | 140 | def fakeBeginJob(self) : |
---|
[1239] | 141 | """Do nothing.""" |
---|
| 142 | pass |
---|
| 143 | |
---|
[1624] | 144 | def endJob(self, printer) : |
---|
| 145 | """Do nothing.""" |
---|
| 146 | pass |
---|
| 147 | |
---|
[1713] | 148 | def getJobSize(self, printer) : |
---|
[1239] | 149 | """Returns the actual job size.""" |
---|
| 150 | try : |
---|
| 151 | return self.JobSize |
---|
| 152 | except AttributeError : |
---|
| 153 | return 0 |
---|
| 154 | |
---|
[1240] | 155 | def computeJobSize(self) : |
---|
| 156 | """Must be overriden in children classes.""" |
---|
| 157 | raise RuntimeError, "AccounterBase.computeJobSize() must be overriden !" |
---|
| 158 | |
---|
[973] | 159 | def openAccounter(kotafilter) : |
---|
| 160 | """Returns a connection handle to the appropriate accounter.""" |
---|
[980] | 161 | (backend, args) = kotafilter.config.getAccounterBackend(kotafilter.printername) |
---|
[973] | 162 | try : |
---|
[1219] | 163 | exec "from pykota.accounters import %s as accounterbackend" % backend.lower() |
---|
[973] | 164 | except ImportError : |
---|
| 165 | raise PyKotaAccounterError, _("Unsupported accounter backend %s") % backend |
---|
| 166 | else : |
---|
[1240] | 167 | return accounterbackend.Accounter(kotafilter, args) |
---|