1 | # PyKota |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | # |
---|
4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
5 | # |
---|
6 | # (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com> |
---|
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | # |
---|
21 | # $Id$ |
---|
22 | # |
---|
23 | # |
---|
24 | |
---|
25 | import sys |
---|
26 | import os |
---|
27 | import imp |
---|
28 | |
---|
29 | class PyKotaAccounterError(Exception): |
---|
30 | """An exception for Accounter related stuff.""" |
---|
31 | def __init__(self, message = ""): |
---|
32 | self.message = message |
---|
33 | Exception.__init__(self, message) |
---|
34 | def __repr__(self): |
---|
35 | return self.message |
---|
36 | __str__ = __repr__ |
---|
37 | |
---|
38 | class AccounterBase : |
---|
39 | """A class to account print usage by querying printers.""" |
---|
40 | def __init__(self, kotafilter, arguments, ispreaccounter=0) : |
---|
41 | """Sets instance vars depending on the current printer.""" |
---|
42 | self.filter = kotafilter |
---|
43 | self.arguments = arguments |
---|
44 | self.onerror = self.filter.config.getPrinterOnAccounterError(self.filter.PrinterName) |
---|
45 | self.isSoftware = 1 # by default software accounting |
---|
46 | self.isPreAccounter = ispreaccounter |
---|
47 | |
---|
48 | def getLastPageCounter(self) : |
---|
49 | """Returns last internal page counter value (possibly faked).""" |
---|
50 | try : |
---|
51 | return self.LastPageCounter or 0 |
---|
52 | except : |
---|
53 | return 0 |
---|
54 | |
---|
55 | def beginJob(self, printer) : |
---|
56 | """Saves the computed job size.""" |
---|
57 | # computes job's size |
---|
58 | self.JobSize = self.computeJobSize() |
---|
59 | |
---|
60 | # get last job information for this printer |
---|
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) |
---|
71 | |
---|
72 | def fakeBeginJob(self) : |
---|
73 | """Do nothing.""" |
---|
74 | pass |
---|
75 | |
---|
76 | def endJob(self, printer) : |
---|
77 | """Do nothing.""" |
---|
78 | pass |
---|
79 | |
---|
80 | def getJobSize(self, printer) : |
---|
81 | """Returns the actual job size.""" |
---|
82 | try : |
---|
83 | return self.JobSize |
---|
84 | except AttributeError : |
---|
85 | return 0 |
---|
86 | |
---|
87 | def computeJobSize(self) : |
---|
88 | """Must be overriden in children classes.""" |
---|
89 | raise RuntimeError, "AccounterBase.computeJobSize() must be overriden !" |
---|
90 | |
---|
91 | def openAccounter(kotafilter, ispreaccounter=0) : |
---|
92 | """Returns a connection handle to the appropriate accounter.""" |
---|
93 | if ispreaccounter : |
---|
94 | (backend, args) = kotafilter.config.getPreAccounterBackend(kotafilter.PrinterName) |
---|
95 | else : |
---|
96 | (backend, args) = kotafilter.config.getAccounterBackend(kotafilter.PrinterName) |
---|
97 | try : |
---|
98 | accounterbackend = imp.load_source("accounterbackend", |
---|
99 | os.path.join(os.path.dirname(__file__), |
---|
100 | "accounters", |
---|
101 | "%s.py" % backend.lower())) |
---|
102 | except ImportError : |
---|
103 | raise PyKotaAccounterError, _("Unsupported accounter backend %s") % backend |
---|
104 | else : |
---|
105 | return accounterbackend.Accounter(kotafilter, args, ispreaccounter) |
---|