[973] | 1 | # PyKota |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[973] | 3 | # |
---|
| 4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
[2622] | 6 | # (c) 2003, 2004, 2005, 2006 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 |
---|
[2302] | 19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[973] | 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.""" |
---|
[2635] | 38 | def __init__(self, kotafilter, arguments, ispreaccounter=0) : |
---|
[973] | 39 | """Sets instance vars depending on the current printer.""" |
---|
| 40 | self.filter = kotafilter |
---|
[980] | 41 | self.arguments = arguments |
---|
[2409] | 42 | self.onerror = self.filter.config.getPrinterOnAccounterError(self.filter.PrinterName) |
---|
[1600] | 43 | self.isSoftware = 1 # by default software accounting |
---|
[2635] | 44 | self.isPreAccounter = ispreaccounter |
---|
[973] | 45 | |
---|
[1180] | 46 | def getLastPageCounter(self) : |
---|
| 47 | """Returns last internal page counter value (possibly faked).""" |
---|
| 48 | try : |
---|
[1483] | 49 | return self.LastPageCounter or 0 |
---|
[1180] | 50 | except : |
---|
| 51 | return 0 |
---|
[976] | 52 | |
---|
[1624] | 53 | def beginJob(self, printer) : |
---|
[1239] | 54 | """Saves the computed job size.""" |
---|
| 55 | # computes job's size |
---|
| 56 | self.JobSize = self.computeJobSize() |
---|
[2409] | 57 | if self.filter.InputFile is not None : |
---|
| 58 | self.JobSize *= self.filter.Copies |
---|
[973] | 59 | |
---|
[1239] | 60 | # get last job information for this printer |
---|
[2635] | 61 | if not self.isPreAccounter : |
---|
| 62 | # TODO : check if this code is still needed |
---|
| 63 | if not printer.LastJob.Exists : |
---|
| 64 | # The printer hasn't been used yet, from PyKota's point of view |
---|
| 65 | self.LastPageCounter = 0 |
---|
| 66 | else : |
---|
| 67 | # get last job size and page counter from Quota Storage |
---|
| 68 | # Last lifetime page counter before actual job is |
---|
| 69 | # last page counter + last job size |
---|
| 70 | self.LastPageCounter = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0) |
---|
[1239] | 71 | |
---|
[1624] | 72 | def fakeBeginJob(self) : |
---|
[1239] | 73 | """Do nothing.""" |
---|
| 74 | pass |
---|
| 75 | |
---|
[1624] | 76 | def endJob(self, printer) : |
---|
| 77 | """Do nothing.""" |
---|
| 78 | pass |
---|
| 79 | |
---|
[1713] | 80 | def getJobSize(self, printer) : |
---|
[1239] | 81 | """Returns the actual job size.""" |
---|
| 82 | try : |
---|
| 83 | return self.JobSize |
---|
| 84 | except AttributeError : |
---|
| 85 | return 0 |
---|
| 86 | |
---|
[1240] | 87 | def computeJobSize(self) : |
---|
| 88 | """Must be overriden in children classes.""" |
---|
| 89 | raise RuntimeError, "AccounterBase.computeJobSize() must be overriden !" |
---|
| 90 | |
---|
[2635] | 91 | def openAccounter(kotafilter, ispreaccounter=0) : |
---|
[973] | 92 | """Returns a connection handle to the appropriate accounter.""" |
---|
[2635] | 93 | if ispreaccounter : |
---|
| 94 | (backend, args) = kotafilter.config.getPreAccounterBackend(kotafilter.PrinterName) |
---|
| 95 | else : |
---|
| 96 | (backend, args) = kotafilter.config.getAccounterBackend(kotafilter.PrinterName) |
---|
[973] | 97 | try : |
---|
[1219] | 98 | exec "from pykota.accounters import %s as accounterbackend" % backend.lower() |
---|
[973] | 99 | except ImportError : |
---|
| 100 | raise PyKotaAccounterError, _("Unsupported accounter backend %s") % backend |
---|
| 101 | else : |
---|
[2635] | 102 | return accounterbackend.Accounter(kotafilter, args, ispreaccounter) |
---|