root / pykota / trunk / pykota / accounters / stupid.py @ 1180

Revision 1180, 5.2 kB (checked in by jalet, 21 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:29:24  jalet
25# More work on new backend. This commit may be unstable.
26#
27# Revision 1.5  2003/10/07 09:07:29  jalet
28# Character encoding added to please latest version of Python
29#
30# Revision 1.4  2003/06/25 14:10:01  jalet
31# Hey, it may work (edpykota --reset excepted) !
32#
33# Revision 1.3  2003/05/27 23:00:21  jalet
34# Big rewrite of external accounting methods.
35# Should work well now.
36#
37# Revision 1.2  2003/04/30 13:40:47  jalet
38# Small fix
39#
40# Revision 1.1  2003/04/30 13:36:40  jalet
41# Stupid accounting method was added.
42#
43#
44#
45
46import sys
47import tempfile
48from pykota.accounter import AccounterBase, PyKotaAccounterError
49
50class Accounter(AccounterBase) :
51    def beginJob(self) :   
52        """Saves the computed job size."""
53        self.JobSize = self.computeJobSize()
54       
55        # get last job information for this printer
56        if not printer.LastJob.Exists :
57            # The printer hasn't been used yet, from PyKota's point of view
58            self.LastPageCounter = 0
59        else :   
60            # get last job size and page counter from Quota Storage
61            # Last lifetime page counter before actual job is
62            # last page counter + last job size
63            self.LastPageCounter = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0)
64       
65    def endJob(self) :   
66        """Do nothing."""
67        pass
68       
69    def getJobSize(self) :   
70        """Returns the actual job size."""
71        try :
72            return self.JobSize
73        except AttributeError :   
74            return 0
75       
76    def doAccounting(self, printer, user) :
77        """Does print accounting by stupidly counting the 'showpage' postscript instructions in the document.
78       
79           This method is essentially unreliable, but shows how to create a simple accounter.
80        """
81        # first we log a message because using this accounting method is not recommended.
82        self.filter.logger.log_message(_("Using the 'stupid' accounting method is unreliable."), "warn")
83       
84        # get the job size   
85        jobsize = self.computeJobSize() * self.filter.copies
86           
87        # get last job information for this printer
88        if not printer.LastJob.Exists :
89            # The printer hasn't been used yet, from PyKota's point of view
90            counterbeforejob = 0
91        else :   
92            # get last job size and page counter from Quota Storage
93            # Last lifetime page counter before actual job is
94            # last page counter + last job size
95            counterbeforejob = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0)
96           
97        # Is the current user allowed to print at all ?
98        userpquota = self.filter.storage.getUserPQuota(user, printer)
99        action = self.filter.warnUserPQuota(userpquota)
100       
101        # update the quota for the current user on this printer, if allowed to print
102        if action == "DENY" :
103            jobsize = 0
104        else :   
105            userpquota.increasePagesUsage(jobsize)
106       
107        # adds the current job to history   
108        printer.addJobToHistory(self.filter.jobid, user, counterbeforejob, action, jobsize)
109           
110        return action
111       
112    def computeJobSize(self) :   
113        """Computes the job size and return its value.
114       
115           THIS METHOD IS COMPLETELY UNRELIABLE BUT SERVES AS AN EXAMPLE.
116        """
117        temporary = None   
118        if self.filter.inputfile is None :   
119            infile = sys.stdin
120            # we will have to duplicate our standard input
121            temporary = tempfile.TemporaryFile()
122        else :   
123            infile = open(self.filter.inputfile, "rb")
124           
125        pagecount = 0
126        for line in infile.xreadlines() :
127            pagecount += line.count("showpage")
128            if temporary is not None :   
129                temporary.write(line)   
130               
131        if temporary is not None :   
132            # this is a copy of our previous standard input
133            # flush, then rewind
134            temporary.flush()
135            temporary.seek(0, 0)
136            # our temporary file will be used later if the
137            # job is allowed.
138            self.filter.inputfile = temporary
139        else :
140            infile.close()
141           
142        return pagecount   
143           
Note: See TracBrowser for help on using the browser.