root / pykota / trunk / pykota / accounter.py @ 1180

Revision 1180, 3.4 kB (checked in by jalet, 20 years ago)

More work on new backend. This commit may be unstable.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1# PyKota
2# -*- coding: ISO-8859-15 -*-
3#
4# PyKota : Print Quotas for CUPS and LPRng
5#
6# (c) 2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
20#
21# $Id$
22#
23# $Log$
24# Revision 1.6  2003/11/12 23:28:55  jalet
25# More work on new backend. This commit may be unstable.
26#
27# Revision 1.5  2003/10/07 09:07:28  jalet
28# Character encoding added to please latest version of Python
29#
30# Revision 1.4  2003/07/14 14:14:59  jalet
31# Old template
32#
33# Revision 1.3  2003/04/30 19:53:58  jalet
34# 1.05
35#
36# Revision 1.2  2003/04/30 13:36:40  jalet
37# Stupid accounting method was added.
38#
39# Revision 1.1  2003/04/29 18:37:54  jalet
40# Pluggable accounting methods (actually doesn't support external scripts)
41#
42#
43#
44
45import sys
46
47class PyKotaAccounterError(Exception):
48    """An exception for Accounter related stuff."""
49    def __init__(self, message = ""):
50        self.message = message
51        Exception.__init__(self, message)
52    def __repr__(self):
53        return self.message
54    __str__ = __repr__
55   
56class AccounterBase :   
57    """A class to account print usage by querying printers."""
58    def __init__(self, kotafilter, arguments) :
59        """Sets instance vars depending on the current printer."""
60        self.filter = kotafilter
61        self.arguments = arguments
62       
63    def getLastPageCounter(self) :   
64        """Returns last internal page counter value (possibly faked)."""
65        try :
66            return self.LastPageCounter
67        except :   
68            return 0
69       
70    def filterInput(self, inputfile) :
71        """Transparent filter."""
72        mustclose = 0   
73        if inputfile is not None :   
74            if hasattr(inputfile, "read") :
75                infile = inputfile
76            else :   
77                infile = open(inputfile, "rb")
78            mustclose = 1
79        else :   
80            infile = sys.stdin
81        data = infile.read(256*1024)   
82        while data :
83            sys.stdout.write(data)
84            data = infile.read(256*1024)
85        if mustclose :   
86            infile.close()
87           
88    def doAccounting(self, printer, user) :   
89        """Does the real accounting."""
90        raise PyKotaAccounterError, "Accounter not implemented !"
91       
92def openAccounter(kotafilter) :
93    """Returns a connection handle to the appropriate accounter."""
94    (backend, args) = kotafilter.config.getAccounterBackend(kotafilter.printername)
95    try :
96        if not backend.isalpha() :
97            # don't trust user input
98            raise ImportError
99        exec "from pykota.accounters import %s as accounterbackend" % backend.lower()   
100    except ImportError :
101        raise PyKotaAccounterError, _("Unsupported accounter backend %s") % backend
102    else :   
103        return getattr(accounterbackend, "Accounter")(kotafilter, args)
Note: See TracBrowser for help on using the browser.