[3489] | 1 | # -*- coding: utf-8 -*- |
---|
[973] | 2 | # |
---|
[3260] | 3 | # PyKota : Print Quotas for CUPS |
---|
[973] | 4 | # |
---|
[3561] | 5 | # (c) 2003-2013 Jerome Alet <alet@librelogiciel.com> |
---|
[3260] | 6 | # This program is free software: you can redistribute it and/or modify |
---|
[973] | 7 | # it under the terms of the GNU General Public License as published by |
---|
[3260] | 8 | # the Free Software Foundation, either version 3 of the License, or |
---|
[973] | 9 | # (at your option) any later version. |
---|
[3413] | 10 | # |
---|
[973] | 11 | # This program is distributed in the hope that it will be useful, |
---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | # GNU General Public License for more details. |
---|
[3413] | 15 | # |
---|
[973] | 16 | # You should have received a copy of the GNU General Public License |
---|
[3260] | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[973] | 18 | # |
---|
| 19 | # $Id$ |
---|
| 20 | # |
---|
[1873] | 21 | # |
---|
[973] | 22 | |
---|
[3184] | 23 | """This module defines base classes used by all accounting methods.""" |
---|
| 24 | |
---|
[976] | 25 | import sys |
---|
[2945] | 26 | import os |
---|
| 27 | import imp |
---|
[976] | 28 | |
---|
[3288] | 29 | from pykota.errors import PyKotaAccounterError |
---|
| 30 | |
---|
[3413] | 31 | class AccounterBase : |
---|
[973] | 32 | """A class to account print usage by querying printers.""" |
---|
[3245] | 33 | def __init__(self, kotafilter, arguments, ispreaccounter=0, name="Unknown") : |
---|
[973] | 34 | """Sets instance vars depending on the current printer.""" |
---|
[3245] | 35 | self.name = name |
---|
[973] | 36 | self.filter = kotafilter |
---|
[980] | 37 | self.arguments = arguments |
---|
[2409] | 38 | self.onerror = self.filter.config.getPrinterOnAccounterError(self.filter.PrinterName) |
---|
[1600] | 39 | self.isSoftware = 1 # by default software accounting |
---|
[3413] | 40 | self.isPreAccounter = ispreaccounter |
---|
[3036] | 41 | self.inkUsage = [] |
---|
[3413] | 42 | |
---|
| 43 | def getLastPageCounter(self) : |
---|
[1180] | 44 | """Returns last internal page counter value (possibly faked).""" |
---|
| 45 | try : |
---|
[1483] | 46 | return self.LastPageCounter or 0 |
---|
[3413] | 47 | except : |
---|
[1180] | 48 | return 0 |
---|
[3413] | 49 | |
---|
| 50 | def beginJob(self, printer) : |
---|
[1239] | 51 | """Saves the computed job size.""" |
---|
| 52 | # computes job's size |
---|
| 53 | self.JobSize = self.computeJobSize() |
---|
[3413] | 54 | |
---|
[1239] | 55 | # get last job information for this printer |
---|
[2635] | 56 | if not self.isPreAccounter : |
---|
| 57 | # TODO : check if this code is still needed |
---|
| 58 | if not printer.LastJob.Exists : |
---|
| 59 | # The printer hasn't been used yet, from PyKota's point of view |
---|
| 60 | self.LastPageCounter = 0 |
---|
[3413] | 61 | else : |
---|
[2635] | 62 | # get last job size and page counter from Quota Storage |
---|
[3413] | 63 | # Last lifetime page counter before actual job is |
---|
[2635] | 64 | # last page counter + last job size |
---|
| 65 | self.LastPageCounter = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0) |
---|
[3413] | 66 | |
---|
| 67 | def fakeBeginJob(self) : |
---|
[1239] | 68 | """Do nothing.""" |
---|
| 69 | pass |
---|
[3413] | 70 | |
---|
| 71 | def endJob(self, printer) : |
---|
[1624] | 72 | """Do nothing.""" |
---|
| 73 | pass |
---|
[3413] | 74 | |
---|
| 75 | def getJobSize(self, printer) : |
---|
[1239] | 76 | """Returns the actual job size.""" |
---|
| 77 | try : |
---|
| 78 | return self.JobSize |
---|
[3413] | 79 | except AttributeError : |
---|
[1239] | 80 | return 0 |
---|
[3413] | 81 | |
---|
| 82 | def computeJobSize(self) : |
---|
[1240] | 83 | """Must be overriden in children classes.""" |
---|
| 84 | raise RuntimeError, "AccounterBase.computeJobSize() must be overriden !" |
---|
[3413] | 85 | |
---|
[2635] | 86 | def openAccounter(kotafilter, ispreaccounter=0) : |
---|
[973] | 87 | """Returns a connection handle to the appropriate accounter.""" |
---|
[2635] | 88 | if ispreaccounter : |
---|
| 89 | (backend, args) = kotafilter.config.getPreAccounterBackend(kotafilter.PrinterName) |
---|
| 90 | else : |
---|
| 91 | (backend, args) = kotafilter.config.getAccounterBackend(kotafilter.PrinterName) |
---|
[973] | 92 | try : |
---|
[3413] | 93 | accounterbackend = imp.load_source("accounterbackend", |
---|
[2945] | 94 | os.path.join(os.path.dirname(__file__), |
---|
[2947] | 95 | "accounters", |
---|
[2945] | 96 | "%s.py" % backend.lower())) |
---|
[973] | 97 | except ImportError : |
---|
| 98 | raise PyKotaAccounterError, _("Unsupported accounter backend %s") % backend |
---|
[3413] | 99 | else : |
---|
[3245] | 100 | return accounterbackend.Accounter(kotafilter, args, ispreaccounter, backend.lower()) |
---|