[973] | 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$ |
---|
[1083] | 23 | # Revision 1.4 2003/07/14 14:14:59 jalet |
---|
| 24 | # Old template |
---|
| 25 | # |
---|
[980] | 26 | # Revision 1.3 2003/04/30 19:53:58 jalet |
---|
| 27 | # 1.05 |
---|
| 28 | # |
---|
[976] | 29 | # Revision 1.2 2003/04/30 13:36:40 jalet |
---|
| 30 | # Stupid accounting method was added. |
---|
| 31 | # |
---|
[973] | 32 | # Revision 1.1 2003/04/29 18:37:54 jalet |
---|
| 33 | # Pluggable accounting methods (actually doesn't support external scripts) |
---|
| 34 | # |
---|
| 35 | # |
---|
| 36 | # |
---|
| 37 | |
---|
[976] | 38 | import sys |
---|
| 39 | |
---|
[973] | 40 | class PyKotaAccounterError(Exception): |
---|
| 41 | """An exception for Accounter related stuff.""" |
---|
| 42 | def __init__(self, message = ""): |
---|
| 43 | self.message = message |
---|
| 44 | Exception.__init__(self, message) |
---|
| 45 | def __repr__(self): |
---|
| 46 | return self.message |
---|
| 47 | __str__ = __repr__ |
---|
| 48 | |
---|
| 49 | class AccounterBase : |
---|
| 50 | """A class to account print usage by querying printers.""" |
---|
[980] | 51 | def __init__(self, kotafilter, arguments) : |
---|
[973] | 52 | """Sets instance vars depending on the current printer.""" |
---|
| 53 | self.filter = kotafilter |
---|
[980] | 54 | self.arguments = arguments |
---|
[973] | 55 | |
---|
[976] | 56 | def filterInput(self, inputfile) : |
---|
| 57 | """Transparent filter.""" |
---|
| 58 | mustclose = 0 |
---|
| 59 | if inputfile is not None : |
---|
| 60 | if hasattr(inputfile, "read") : |
---|
| 61 | infile = inputfile |
---|
| 62 | else : |
---|
| 63 | infile = open(inputfile, "rb") |
---|
| 64 | mustclose = 1 |
---|
| 65 | else : |
---|
| 66 | infile = sys.stdin |
---|
| 67 | data = infile.read(256*1024) |
---|
| 68 | while data : |
---|
| 69 | sys.stdout.write(data) |
---|
| 70 | data = infile.read(256*1024) |
---|
| 71 | if mustclose : |
---|
| 72 | infile.close() |
---|
| 73 | |
---|
[1083] | 74 | def doAccounting(self, printer, user) : |
---|
[973] | 75 | """Does the real accounting.""" |
---|
| 76 | raise PyKotaAccounterError, "Accounter not implemented !" |
---|
| 77 | |
---|
| 78 | def openAccounter(kotafilter) : |
---|
| 79 | """Returns a connection handle to the appropriate accounter.""" |
---|
[980] | 80 | (backend, args) = kotafilter.config.getAccounterBackend(kotafilter.printername) |
---|
[973] | 81 | try : |
---|
| 82 | if not backend.isalpha() : |
---|
| 83 | # don't trust user input |
---|
| 84 | raise ImportError |
---|
| 85 | exec "from pykota.accounters import %s as accounterbackend" % backend.lower() |
---|
| 86 | except ImportError : |
---|
| 87 | raise PyKotaAccounterError, _("Unsupported accounter backend %s") % backend |
---|
| 88 | else : |
---|
[980] | 89 | return getattr(accounterbackend, "Accounter")(kotafilter, args) |
---|