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

Revision 976, 2.8 kB (checked in by jalet, 21 years ago)

Stupid accounting method was added.

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