1 | # PyKota |
---|
2 | # |
---|
3 | # PyKota : Print Quotas for CUPS and LPRng |
---|
4 | # |
---|
5 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
6 | # This program is free software; you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation; either version 2 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
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. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with this program; if not, write to the Free Software |
---|
18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | # |
---|
20 | # $Id$ |
---|
21 | # |
---|
22 | # $Log$ |
---|
23 | # Revision 1.3 2003/04/30 19:53:58 jalet |
---|
24 | # 1.05 |
---|
25 | # |
---|
26 | # Revision 1.2 2003/04/30 13:36:40 jalet |
---|
27 | # Stupid accounting method was added. |
---|
28 | # |
---|
29 | # Revision 1.1 2003/04/29 18:37:54 jalet |
---|
30 | # Pluggable accounting methods (actually doesn't support external scripts) |
---|
31 | # |
---|
32 | # |
---|
33 | # |
---|
34 | |
---|
35 | import sys |
---|
36 | |
---|
37 | class PyKotaAccounterError(Exception): |
---|
38 | """An exception for Accounter related stuff.""" |
---|
39 | def __init__(self, message = ""): |
---|
40 | self.message = message |
---|
41 | Exception.__init__(self, message) |
---|
42 | def __repr__(self): |
---|
43 | return self.message |
---|
44 | __str__ = __repr__ |
---|
45 | |
---|
46 | class AccounterBase : |
---|
47 | """A class to account print usage by querying printers.""" |
---|
48 | def __init__(self, kotafilter, arguments) : |
---|
49 | """Sets instance vars depending on the current printer.""" |
---|
50 | self.filter = kotafilter |
---|
51 | self.arguments = arguments |
---|
52 | |
---|
53 | def filterInput(self, inputfile) : |
---|
54 | """Transparent filter.""" |
---|
55 | mustclose = 0 |
---|
56 | if inputfile is not None : |
---|
57 | if hasattr(inputfile, "read") : |
---|
58 | infile = inputfile |
---|
59 | else : |
---|
60 | infile = open(inputfile, "rb") |
---|
61 | mustclose = 1 |
---|
62 | else : |
---|
63 | infile = sys.stdin |
---|
64 | data = infile.read(256*1024) |
---|
65 | while data : |
---|
66 | sys.stdout.write(data) |
---|
67 | data = infile.read(256*1024) |
---|
68 | if mustclose : |
---|
69 | infile.close() |
---|
70 | |
---|
71 | def doAccounting(self, printerid, userid) : |
---|
72 | """Does the real accounting.""" |
---|
73 | raise PyKotaAccounterError, "Accounter not implemented !" |
---|
74 | |
---|
75 | def openAccounter(kotafilter) : |
---|
76 | """Returns a connection handle to the appropriate accounter.""" |
---|
77 | (backend, args) = kotafilter.config.getAccounterBackend(kotafilter.printername) |
---|
78 | try : |
---|
79 | if not backend.isalpha() : |
---|
80 | # don't trust user input |
---|
81 | raise ImportError |
---|
82 | exec "from pykota.accounters import %s as accounterbackend" % backend.lower() |
---|
83 | except ImportError : |
---|
84 | raise PyKotaAccounterError, _("Unsupported accounter backend %s") % backend |
---|
85 | else : |
---|
86 | return getattr(accounterbackend, "Accounter")(kotafilter, args) |
---|