[973] | 1 | # PyKota |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[973] | 3 | # |
---|
| 4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
[3133] | 6 | # (c) 2003, 2004, 2005, 2006, 2007 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 | |
---|
[3184] | 25 | """This module defines base classes used by all accounting methods.""" |
---|
| 26 | |
---|
[976] | 27 | import sys |
---|
[2945] | 28 | import os |
---|
| 29 | import imp |
---|
[976] | 30 | |
---|
[973] | 31 | class PyKotaAccounterError(Exception): |
---|
| 32 | """An exception for Accounter related stuff.""" |
---|
| 33 | def __init__(self, message = ""): |
---|
| 34 | self.message = message |
---|
| 35 | Exception.__init__(self, message) |
---|
| 36 | def __repr__(self): |
---|
| 37 | return self.message |
---|
| 38 | __str__ = __repr__ |
---|
| 39 | |
---|
| 40 | class AccounterBase : |
---|
| 41 | """A class to account print usage by querying printers.""" |
---|
[2635] | 42 | def __init__(self, kotafilter, arguments, ispreaccounter=0) : |
---|
[973] | 43 | """Sets instance vars depending on the current printer.""" |
---|
| 44 | self.filter = kotafilter |
---|
[980] | 45 | self.arguments = arguments |
---|
[2409] | 46 | self.onerror = self.filter.config.getPrinterOnAccounterError(self.filter.PrinterName) |
---|
[1600] | 47 | self.isSoftware = 1 # by default software accounting |
---|
[2635] | 48 | self.isPreAccounter = ispreaccounter |
---|
[3036] | 49 | self.inkUsage = [] |
---|
[973] | 50 | |
---|
[1180] | 51 | def getLastPageCounter(self) : |
---|
| 52 | """Returns last internal page counter value (possibly faked).""" |
---|
| 53 | try : |
---|
[1483] | 54 | return self.LastPageCounter or 0 |
---|
[1180] | 55 | except : |
---|
| 56 | return 0 |
---|
[976] | 57 | |
---|
[1624] | 58 | def beginJob(self, printer) : |
---|
[1239] | 59 | """Saves the computed job size.""" |
---|
| 60 | # computes job's size |
---|
| 61 | self.JobSize = self.computeJobSize() |
---|
[973] | 62 | |
---|
[1239] | 63 | # get last job information for this printer |
---|
[2635] | 64 | if not self.isPreAccounter : |
---|
| 65 | # TODO : check if this code is still needed |
---|
| 66 | if not printer.LastJob.Exists : |
---|
| 67 | # The printer hasn't been used yet, from PyKota's point of view |
---|
| 68 | self.LastPageCounter = 0 |
---|
| 69 | else : |
---|
| 70 | # get last job size and page counter from Quota Storage |
---|
| 71 | # Last lifetime page counter before actual job is |
---|
| 72 | # last page counter + last job size |
---|
| 73 | self.LastPageCounter = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0) |
---|
[1239] | 74 | |
---|
[1624] | 75 | def fakeBeginJob(self) : |
---|
[1239] | 76 | """Do nothing.""" |
---|
| 77 | pass |
---|
| 78 | |
---|
[1624] | 79 | def endJob(self, printer) : |
---|
| 80 | """Do nothing.""" |
---|
| 81 | pass |
---|
| 82 | |
---|
[1713] | 83 | def getJobSize(self, printer) : |
---|
[1239] | 84 | """Returns the actual job size.""" |
---|
| 85 | try : |
---|
| 86 | return self.JobSize |
---|
| 87 | except AttributeError : |
---|
| 88 | return 0 |
---|
| 89 | |
---|
[1240] | 90 | def computeJobSize(self) : |
---|
| 91 | """Must be overriden in children classes.""" |
---|
| 92 | raise RuntimeError, "AccounterBase.computeJobSize() must be overriden !" |
---|
| 93 | |
---|
[2635] | 94 | def openAccounter(kotafilter, ispreaccounter=0) : |
---|
[973] | 95 | """Returns a connection handle to the appropriate accounter.""" |
---|
[2635] | 96 | if ispreaccounter : |
---|
| 97 | (backend, args) = kotafilter.config.getPreAccounterBackend(kotafilter.PrinterName) |
---|
| 98 | else : |
---|
| 99 | (backend, args) = kotafilter.config.getAccounterBackend(kotafilter.PrinterName) |
---|
[973] | 100 | try : |
---|
[2945] | 101 | accounterbackend = imp.load_source("accounterbackend", |
---|
| 102 | os.path.join(os.path.dirname(__file__), |
---|
[2947] | 103 | "accounters", |
---|
[2945] | 104 | "%s.py" % backend.lower())) |
---|
[973] | 105 | except ImportError : |
---|
| 106 | raise PyKotaAccounterError, _("Unsupported accounter backend %s") % backend |
---|
| 107 | else : |
---|
[2635] | 108 | return accounterbackend.Accounter(kotafilter, args, ispreaccounter) |
---|