1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | # PyKota Print Quota Quote sender |
---|
4 | # |
---|
5 | # PyKota - Print Quotas for CUPS and LPRng |
---|
6 | # |
---|
7 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
8 | # This program is free software; you can redistribute it and/or modify |
---|
9 | # it under the terms of the GNU General Public License as published by |
---|
10 | # the Free Software Foundation; either version 2 of the License, or |
---|
11 | # (at your option) any later version. |
---|
12 | # |
---|
13 | # This program is distributed in the hope that it will be useful, |
---|
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | # GNU General Public License for more details. |
---|
17 | # |
---|
18 | # You should have received a copy of the GNU General Public License |
---|
19 | # along with this program; if not, write to the Free Software |
---|
20 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
21 | # |
---|
22 | # $Id$ |
---|
23 | # |
---|
24 | # $Log$ |
---|
25 | # Revision 1.3 2003/07/29 20:55:17 jalet |
---|
26 | # 1.14 is out ! |
---|
27 | # |
---|
28 | # Revision 1.2 2003/07/25 10:41:29 jalet |
---|
29 | # Better documentation. |
---|
30 | # pykotme now displays the current user's account balance. |
---|
31 | # Some test changed in ldap module. |
---|
32 | # |
---|
33 | # Revision 1.1 2003/07/03 09:44:01 jalet |
---|
34 | # Now includes the pykotme utility |
---|
35 | # |
---|
36 | # |
---|
37 | # |
---|
38 | |
---|
39 | import sys |
---|
40 | import os |
---|
41 | import pwd |
---|
42 | |
---|
43 | from pykota import version |
---|
44 | from pykota.tool import PyKotaTool, PyKotaToolError |
---|
45 | from pykota.config import PyKotaConfigError |
---|
46 | from pykota.storage import PyKotaStorageError |
---|
47 | |
---|
48 | __doc__ = """pykotme v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres |
---|
49 | |
---|
50 | Gives print quotes to users. |
---|
51 | |
---|
52 | command line usage : |
---|
53 | |
---|
54 | pykotme [options] [files] |
---|
55 | |
---|
56 | options : |
---|
57 | |
---|
58 | -v | --version Prints pykotme's version number then exits. |
---|
59 | -h | --help Prints this message then exits. |
---|
60 | |
---|
61 | -P | --printer p Gives a quote for this printer only. Actually p can |
---|
62 | use wildcards characters to select only |
---|
63 | some printers. The default value is *, meaning |
---|
64 | all printers. |
---|
65 | |
---|
66 | examples : |
---|
67 | |
---|
68 | $ pykotme --printer apple file1.ps file2.ps |
---|
69 | |
---|
70 | This will give a print quote to the current user. The quote will show |
---|
71 | the price and size of a job consisting in file1.ps and file2.ps |
---|
72 | which would be sent to the apple printer. |
---|
73 | |
---|
74 | $ pykotme |
---|
75 | |
---|
76 | This will give a quote for a job consisting of what is on standard |
---|
77 | input. The quote will list the job size, and the price the job |
---|
78 | would cost on each printer. |
---|
79 | |
---|
80 | |
---|
81 | This program is free software; you can redistribute it and/or modify |
---|
82 | it under the terms of the GNU General Public License as published by |
---|
83 | the Free Software Foundation; either version 2 of the License, or |
---|
84 | (at your option) any later version. |
---|
85 | |
---|
86 | This program is distributed in the hope that it will be useful, |
---|
87 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
88 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
89 | GNU General Public License for more details. |
---|
90 | |
---|
91 | You should have received a copy of the GNU General Public License |
---|
92 | along with this program; if not, write to the Free Software |
---|
93 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
94 | |
---|
95 | Please e-mail bugs to: %s""" % (version.__version__, version.__author__) |
---|
96 | |
---|
97 | class PyKotMe(PyKotaTool) : |
---|
98 | """A class for pykotme.""" |
---|
99 | def main(self, files, options) : |
---|
100 | """Gives print quotes.""" |
---|
101 | if not files : |
---|
102 | files = [ sys.stdin ] |
---|
103 | |
---|
104 | nbpages = 0 |
---|
105 | for filename in files : |
---|
106 | if filename is sys.stdin : |
---|
107 | stream = filename |
---|
108 | else : |
---|
109 | if filename == "-" : |
---|
110 | stream = sys.stdin |
---|
111 | else : |
---|
112 | try : |
---|
113 | stream = open(filename, "rb") |
---|
114 | except IOError, msg : |
---|
115 | sys.stderr.write("%s\n" % msg) |
---|
116 | continue |
---|
117 | nb = self.getNbPages(stream) |
---|
118 | if nb is None : |
---|
119 | sys.stderr.write(_("Unable to compute size of %s in number of pages.\n") % filename) |
---|
120 | else : |
---|
121 | nbpages += nb |
---|
122 | if not (filename is sys.stdin) : |
---|
123 | stream.close() |
---|
124 | |
---|
125 | # get current user |
---|
126 | uid = os.geteuid() |
---|
127 | username = pwd.getpwuid(uid)[0] |
---|
128 | user = self.storage.getUser(username) |
---|
129 | if user.Exists and user.LimitBy and (user.LimitBy.lower() == "balance"): |
---|
130 | print _("Your account balance : %.2f") % (user.AccountBalance or 0.0) |
---|
131 | |
---|
132 | printers = self.storage.getMatchingPrinters(options["printer"]) |
---|
133 | if not printers : |
---|
134 | raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] |
---|
135 | |
---|
136 | print _("Job size : %i pages") % nbpages |
---|
137 | for printer in printers : |
---|
138 | cost = (nbpages * float(printer.PricePerPage or 0)) + float(printer.PricePerJob or 0) |
---|
139 | print _("Cost on printer %s : %.2f") % (printer.Name, cost) |
---|
140 | |
---|
141 | def getNbPages(self, stream) : |
---|
142 | """Tries to compute the file's size in number of pages, the best we can.""" |
---|
143 | nbpages = [] # in case some user wants to put several %%Pages: entries in the file |
---|
144 | for line in stream.xreadlines() : |
---|
145 | line = line.strip() |
---|
146 | if line.startswith("%%Pages: ") : |
---|
147 | try : |
---|
148 | nbpages.append(int(line[9:])) |
---|
149 | except ValueError : |
---|
150 | pass |
---|
151 | if nbpages : |
---|
152 | return max(nbpages) |
---|
153 | |
---|
154 | if __name__ == "__main__" : |
---|
155 | retcode = 0 |
---|
156 | try : |
---|
157 | defaults = { \ |
---|
158 | "printer" : "*", \ |
---|
159 | } |
---|
160 | short_options = "vhP:" |
---|
161 | long_options = ["help", "version", "printer="] |
---|
162 | |
---|
163 | # Initializes the command line tool |
---|
164 | sender = PyKotMe(doc=__doc__) |
---|
165 | |
---|
166 | # parse and checks the command line |
---|
167 | (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
168 | |
---|
169 | # sets long options |
---|
170 | options["help"] = options["h"] or options["help"] |
---|
171 | options["version"] = options["v"] or options["version"] |
---|
172 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
173 | |
---|
174 | if options["help"] : |
---|
175 | sender.display_usage_and_quit() |
---|
176 | elif options["version"] : |
---|
177 | sender.display_version_and_quit() |
---|
178 | else : |
---|
179 | retcode = sender.main(args, options) |
---|
180 | except (PyKotaToolError, PyKotaConfigError, PyKotaStorageError), msg : |
---|
181 | sys.stderr.write("%s\n" % msg) |
---|
182 | sys.stderr.flush() |
---|
183 | retcode = -1 |
---|
184 | |
---|
185 | try : |
---|
186 | sender.storage.close() |
---|
187 | except (TypeError, NameError, AttributeError) : |
---|
188 | pass |
---|
189 | |
---|
190 | sys.exit(retcode) |
---|