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

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

Don't try to verify if module name is valid, Python does this better than us.

  • 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.7  2003/11/25 23:46:40  jalet
25# Don't try to verify if module name is valid, Python does this better than us.
26#
27# Revision 1.6  2003/11/12 23:28:55  jalet
28# More work on new backend. This commit may be unstable.
29#
30# Revision 1.5  2003/10/07 09:07:28  jalet
31# Character encoding added to please latest version of Python
32#
33# Revision 1.4  2003/07/14 14:14:59  jalet
34# Old template
35#
36# Revision 1.3  2003/04/30 19:53:58  jalet
37# 1.05
38#
39# Revision 1.2  2003/04/30 13:36:40  jalet
40# Stupid accounting method was added.
41#
42# Revision 1.1  2003/04/29 18:37:54  jalet
43# Pluggable accounting methods (actually doesn't support external scripts)
44#
45#
46#
47
48import sys
49
50class PyKotaAccounterError(Exception):
51    """An exception for Accounter related stuff."""
52    def __init__(self, message = ""):
53        self.message = message
54        Exception.__init__(self, message)
55    def __repr__(self):
56        return self.message
57    __str__ = __repr__
58   
59class AccounterBase :   
60    """A class to account print usage by querying printers."""
61    def __init__(self, kotafilter, arguments) :
62        """Sets instance vars depending on the current printer."""
63        self.filter = kotafilter
64        self.arguments = arguments
65       
66    def getLastPageCounter(self) :   
67        """Returns last internal page counter value (possibly faked)."""
68        try :
69            return self.LastPageCounter
70        except :   
71            return 0
72       
73    def filterInput(self, inputfile) :
74        """Transparent filter."""
75        mustclose = 0   
76        if inputfile is not None :   
77            if hasattr(inputfile, "read") :
78                infile = inputfile
79            else :   
80                infile = open(inputfile, "rb")
81            mustclose = 1
82        else :   
83            infile = sys.stdin
84        data = infile.read(256*1024)   
85        while data :
86            sys.stdout.write(data)
87            data = infile.read(256*1024)
88        if mustclose :   
89            infile.close()
90           
91    def doAccounting(self, printer, user) :   
92        """Does the real accounting."""
93        raise PyKotaAccounterError, "Accounter not implemented !"
94       
95def openAccounter(kotafilter) :
96    """Returns a connection handle to the appropriate accounter."""
97    (backend, args) = kotafilter.config.getAccounterBackend(kotafilter.printername)
98    try :
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.