[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 | # |
---|
[1873] | 23 | # |
---|
[973] | 24 | |
---|
[976] | 25 | import sys |
---|
| 26 | |
---|
[973] | 27 | class PyKotaAccounterError(Exception): |
---|
| 28 | """An exception for Accounter related stuff.""" |
---|
| 29 | def __init__(self, message = ""): |
---|
| 30 | self.message = message |
---|
| 31 | Exception.__init__(self, message) |
---|
| 32 | def __repr__(self): |
---|
| 33 | return self.message |
---|
| 34 | __str__ = __repr__ |
---|
| 35 | |
---|
| 36 | class AccounterBase : |
---|
| 37 | """A class to account print usage by querying printers.""" |
---|
[980] | 38 | def __init__(self, kotafilter, arguments) : |
---|
[973] | 39 | """Sets instance vars depending on the current printer.""" |
---|
| 40 | self.filter = kotafilter |
---|
[980] | 41 | self.arguments = arguments |
---|
[1687] | 42 | self.onerror = self.filter.config.getPrinterOnAccounterError(self.filter.printername) |
---|
[1600] | 43 | self.isSoftware = 1 # by default software accounting |
---|
[973] | 44 | |
---|
[1180] | 45 | def getLastPageCounter(self) : |
---|
| 46 | """Returns last internal page counter value (possibly faked).""" |
---|
| 47 | try : |
---|
[1483] | 48 | return self.LastPageCounter or 0 |
---|
[1180] | 49 | except : |
---|
| 50 | return 0 |
---|
[976] | 51 | |
---|
[1624] | 52 | def beginJob(self, printer) : |
---|
[1239] | 53 | """Saves the computed job size.""" |
---|
| 54 | # computes job's size |
---|
| 55 | self.JobSize = self.computeJobSize() |
---|
[1272] | 56 | if ((self.filter.printingsystem == "CUPS") \ |
---|
| 57 | and (self.filter.preserveinputfile is not None)) \ |
---|
| 58 | or (self.filter.printingsystem != "CUPS") : |
---|
[1873] | 59 | self.JobSize *= self.filter.copies |
---|
[973] | 60 | |
---|
[1239] | 61 | # get last job information for this printer |
---|
[1624] | 62 | if not printer.LastJob.Exists : |
---|
[1239] | 63 | # The printer hasn't been used yet, from PyKota's point of view |
---|
| 64 | self.LastPageCounter = 0 |
---|
| 65 | else : |
---|
| 66 | # get last job size and page counter from Quota Storage |
---|
| 67 | # Last lifetime page counter before actual job is |
---|
| 68 | # last page counter + last job size |
---|
[1624] | 69 | self.LastPageCounter = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0) |
---|
[1239] | 70 | |
---|
[1624] | 71 | def fakeBeginJob(self) : |
---|
[1239] | 72 | """Do nothing.""" |
---|
| 73 | pass |
---|
| 74 | |
---|
[1624] | 75 | def endJob(self, printer) : |
---|
| 76 | """Do nothing.""" |
---|
| 77 | pass |
---|
| 78 | |
---|
[1713] | 79 | def getJobSize(self, printer) : |
---|
[1239] | 80 | """Returns the actual job size.""" |
---|
| 81 | try : |
---|
| 82 | return self.JobSize |
---|
| 83 | except AttributeError : |
---|
| 84 | return 0 |
---|
| 85 | |
---|
[1240] | 86 | def computeJobSize(self) : |
---|
| 87 | """Must be overriden in children classes.""" |
---|
| 88 | raise RuntimeError, "AccounterBase.computeJobSize() must be overriden !" |
---|
| 89 | |
---|
[973] | 90 | def openAccounter(kotafilter) : |
---|
| 91 | """Returns a connection handle to the appropriate accounter.""" |
---|
[980] | 92 | (backend, args) = kotafilter.config.getAccounterBackend(kotafilter.printername) |
---|
[973] | 93 | try : |
---|
[1219] | 94 | exec "from pykota.accounters import %s as accounterbackend" % backend.lower() |
---|
[973] | 95 | except ImportError : |
---|
| 96 | raise PyKotaAccounterError, _("Unsupported accounter backend %s") % backend |
---|
| 97 | else : |
---|
[1240] | 98 | return accounterbackend.Accounter(kotafilter, args) |
---|