1 | # PyKota |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | # |
---|
4 | # PyKota - Print Quotas for CUPS and LPRng |
---|
5 | # |
---|
6 | # (c) 2003-2004 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.11 2004/01/08 14:10:32 jalet |
---|
25 | # Copyright year changed. |
---|
26 | # |
---|
27 | # Revision 1.10 2003/12/27 16:49:25 uid67467 |
---|
28 | # Should be ok now. |
---|
29 | # |
---|
30 | # Revision 1.8 2003/11/23 19:01:37 jalet |
---|
31 | # Job price added to history |
---|
32 | # |
---|
33 | # Revision 1.7 2003/11/21 14:28:46 jalet |
---|
34 | # More complete job history. |
---|
35 | # |
---|
36 | # Revision 1.6 2003/11/12 23:29:24 jalet |
---|
37 | # More work on new backend. This commit may be unstable. |
---|
38 | # |
---|
39 | # Revision 1.5 2003/10/07 09:07:29 jalet |
---|
40 | # Character encoding added to please latest version of Python |
---|
41 | # |
---|
42 | # Revision 1.4 2003/06/25 14:10:01 jalet |
---|
43 | # Hey, it may work (edpykota --reset excepted) ! |
---|
44 | # |
---|
45 | # Revision 1.3 2003/05/27 23:00:21 jalet |
---|
46 | # Big rewrite of external accounting methods. |
---|
47 | # Should work well now. |
---|
48 | # |
---|
49 | # Revision 1.2 2003/04/30 13:40:47 jalet |
---|
50 | # Small fix |
---|
51 | # |
---|
52 | # Revision 1.1 2003/04/30 13:36:40 jalet |
---|
53 | # Stupid accounting method was added. |
---|
54 | # |
---|
55 | # |
---|
56 | # |
---|
57 | |
---|
58 | import sys |
---|
59 | import tempfile |
---|
60 | from pykota.accounter import AccounterBase, PyKotaAccounterError |
---|
61 | |
---|
62 | class Accounter(AccounterBase) : |
---|
63 | def computeJobSize(self) : |
---|
64 | """Computes the job size and return its value. |
---|
65 | |
---|
66 | THIS METHOD IS COMPLETELY UNRELIABLE BUT SERVES AS AN EXAMPLE. |
---|
67 | """ |
---|
68 | # first we log a message because using this accounting method is not recommended. |
---|
69 | self.filter.logger.log_message(_("Using the 'stupid' accounting method is unreliable."), "warn") |
---|
70 | |
---|
71 | temporary = None |
---|
72 | if self.filter.inputfile is None : |
---|
73 | infile = sys.stdin |
---|
74 | # we will have to duplicate our standard input |
---|
75 | temporary = tempfile.TemporaryFile() |
---|
76 | else : |
---|
77 | infile = open(self.filter.inputfile, "rb") |
---|
78 | |
---|
79 | pagecount = 0 |
---|
80 | for line in infile.xreadlines() : |
---|
81 | pagecount += line.count("showpage") |
---|
82 | if temporary is not None : |
---|
83 | temporary.write(line) |
---|
84 | |
---|
85 | if temporary is not None : |
---|
86 | # this is a copy of our previous standard input |
---|
87 | # flush, then rewind |
---|
88 | temporary.flush() |
---|
89 | temporary.seek(0, 0) |
---|
90 | # our temporary file will be used later if the |
---|
91 | # job is allowed. |
---|
92 | self.filter.inputfile = temporary |
---|
93 | else : |
---|
94 | infile.close() |
---|
95 | return pagecount |
---|