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