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

Revision 1144, 3.1 kB (checked in by jalet, 21 years ago)

Character encoding added to please latest version of Python

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