1 | #! /usr/bin/env python |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | |
---|
4 | # PyKota Print Quota Editor |
---|
5 | # |
---|
6 | # PyKota - Print Quotas for CUPS and LPRng |
---|
7 | # |
---|
8 | # (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com> |
---|
9 | # This program is free software; you can redistribute it and/or modify |
---|
10 | # it under the terms of the GNU General Public License as published by |
---|
11 | # the Free Software Foundation; either version 2 of the License, or |
---|
12 | # (at your option) any later version. |
---|
13 | # |
---|
14 | # This program is distributed in the hope that it will be useful, |
---|
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | # GNU General Public License for more details. |
---|
18 | # |
---|
19 | # You should have received a copy of the GNU General Public License |
---|
20 | # along with this program; if not, write to the Free Software |
---|
21 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
22 | # |
---|
23 | # $Id$ |
---|
24 | # |
---|
25 | # $Log$ |
---|
26 | # Revision 1.12 2005/01/17 08:44:24 jalet |
---|
27 | # Modified copyright years |
---|
28 | # |
---|
29 | # Revision 1.11 2004/10/17 19:41:54 jalet |
---|
30 | # Fixed help message which displayed "pkprinters" instead of "pykosd" |
---|
31 | # |
---|
32 | # Revision 1.10 2004/10/11 22:53:05 jalet |
---|
33 | # Postponed string interpolation to help message's output method |
---|
34 | # |
---|
35 | # Revision 1.9 2004/10/11 12:49:06 jalet |
---|
36 | # Renders help translatable |
---|
37 | # |
---|
38 | # Revision 1.8 2004/10/06 10:05:47 jalet |
---|
39 | # Minor changes to allow any PyKota administrator to launch enhanced versions |
---|
40 | # of the commands, and not only the root user. |
---|
41 | # |
---|
42 | # Revision 1.7 2004/07/20 22:42:26 jalet |
---|
43 | # pykosd now supports setting color |
---|
44 | # |
---|
45 | # Revision 1.6 2004/07/20 22:29:49 jalet |
---|
46 | # pykosd now supports setting the font |
---|
47 | # |
---|
48 | # Revision 1.5 2004/07/20 22:19:45 jalet |
---|
49 | # Sanitized a bit + use of gettext |
---|
50 | # |
---|
51 | # Revision 1.4 2004/07/19 22:37:13 jalet |
---|
52 | # pykosd is now a very good tool |
---|
53 | # |
---|
54 | # Revision 1.3 2004/07/07 21:44:15 jalet |
---|
55 | # Formatting improvements |
---|
56 | # |
---|
57 | # Revision 1.2 2004/07/07 14:14:31 jalet |
---|
58 | # Now handles limits by quota in addition to limits by balance |
---|
59 | # |
---|
60 | # Revision 1.1 2004/07/07 13:21:27 jalet |
---|
61 | # Introduction of the pykosd command |
---|
62 | # |
---|
63 | # |
---|
64 | # |
---|
65 | |
---|
66 | import sys |
---|
67 | import os |
---|
68 | import pwd |
---|
69 | import time |
---|
70 | |
---|
71 | try : |
---|
72 | import pyosd |
---|
73 | except ImportError : |
---|
74 | sys.stderr.write("Sorry ! You need both xosd and the Python OSD library (pyosd) for this software to work.\n") |
---|
75 | sys.exit(-1) |
---|
76 | |
---|
77 | from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_ |
---|
78 | |
---|
79 | __doc__ = N_("""pykosd v%s (c) 2003, 2004, 2005 C@LL - Conseil Internet & Logiciels Libres |
---|
80 | An OSD quota monitor for PyKota. |
---|
81 | |
---|
82 | command line usage : |
---|
83 | |
---|
84 | pykosd [options] |
---|
85 | |
---|
86 | options : |
---|
87 | |
---|
88 | -v | --version Prints pykosd's version number then exits. |
---|
89 | -h | --help Prints this message then exits. |
---|
90 | |
---|
91 | -c | --color #rrggbb Sets the color to use for display as an hexadecimal |
---|
92 | triplet, for example #FF0000 is 100%% red. |
---|
93 | Defaults to 100%% green (#00FF00). |
---|
94 | |
---|
95 | -d | --duration d Sets the duration of the display in seconds. |
---|
96 | Defaults to 3 seconds. |
---|
97 | |
---|
98 | -f | --font f Sets the font to use for display. |
---|
99 | Defaults to the Python OSD library's default. |
---|
100 | |
---|
101 | -l | --loop n Sets the number of times the info will be displayed. |
---|
102 | Defaults to 0, which means loop forever. |
---|
103 | |
---|
104 | -s | --sleep s Sets the sleeping duration between two displays |
---|
105 | in seconds. Defaults to 180 seconds (3 minutes). |
---|
106 | |
---|
107 | |
---|
108 | examples : |
---|
109 | |
---|
110 | $ pykosd -s 60 --loop 5 |
---|
111 | |
---|
112 | Will launch pykosd. Display will be refreshed every 60 seconds, |
---|
113 | and will last for 3 seconds (the default) each time. After five |
---|
114 | iterations, the program will exit. |
---|
115 | |
---|
116 | This program is free software; you can redistribute it and/or modify |
---|
117 | it under the terms of the GNU General Public License as published by |
---|
118 | the Free Software Foundation; either version 2 of the License, or |
---|
119 | (at your option) any later version. |
---|
120 | |
---|
121 | This program is distributed in the hope that it will be useful, |
---|
122 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
123 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
124 | GNU General Public License for more details. |
---|
125 | |
---|
126 | You should have received a copy of the GNU General Public License |
---|
127 | along with this program; if not, write to the Free Software |
---|
128 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
129 | |
---|
130 | Please e-mail bugs to: %s""") |
---|
131 | |
---|
132 | |
---|
133 | class PyKOSD(PyKotaTool) : |
---|
134 | def main(self, args, options) : |
---|
135 | """Main function starts here.""" |
---|
136 | try : |
---|
137 | duration = int(options["duration"]) |
---|
138 | except : |
---|
139 | raise PyKotaToolError, _("Invalid duration option %s") % str(options["duration"]) |
---|
140 | |
---|
141 | try : |
---|
142 | loop = int(options["loop"]) |
---|
143 | except : |
---|
144 | raise PyKotaToolError, _("Invalid loop option %s") % str(options["loop"]) |
---|
145 | |
---|
146 | try : |
---|
147 | sleep = float(options["sleep"]) |
---|
148 | except : |
---|
149 | raise PyKotaToolError, _("Invalid sleep option %s") % str(options["sleep"]) |
---|
150 | |
---|
151 | color = options["color"] |
---|
152 | if not color.startswith("#") : |
---|
153 | color = "#%s" % color |
---|
154 | if len(color) != 7 : |
---|
155 | raise PyKotaToolError, _("Invalid color option %s") % str(color) |
---|
156 | savecolor = color |
---|
157 | |
---|
158 | uname = pwd.getpwuid(os.geteuid())[0] |
---|
159 | while 1 : |
---|
160 | color = savecolor |
---|
161 | user = cmd.storage.getUserFromBackend(uname) # don't use cache |
---|
162 | if not user.Exists : |
---|
163 | raise PyKotaToolError, _("User %s doesn't exist in PyKota's database") % uname |
---|
164 | if user.LimitBy == "quota" : |
---|
165 | printers = cmd.storage.getMatchingPrinters("*") |
---|
166 | upquotas = [ cmd.storage.getUserPQuotaFromBackend(user, p) for p in printers ] # don't use cache |
---|
167 | nblines = len(upquotas) |
---|
168 | display = pyosd.osd(font=options["font"], colour=color, timeout=duration, shadow=2, lines=nblines) |
---|
169 | for line in range(nblines) : |
---|
170 | upq = upquotas[line] |
---|
171 | if upq.HardLimit is None : |
---|
172 | if upq.SoftLimit is None : |
---|
173 | percent = "%s" % upq.PageCounter |
---|
174 | else : |
---|
175 | percent = "%s%%" % min((upq.PageCounter * 100) / upq.SoftLimit, 100) |
---|
176 | else : |
---|
177 | percent = "%s%%" % min((upq.PageCounter * 100) / upq.HardLimit, 100) |
---|
178 | display.display(_("Pages used on %s : %s") % (upq.Printer.Name, percent), type=pyosd.TYPE_STRING, line=line) |
---|
179 | else : |
---|
180 | if user.AccountBalance <= 0 : |
---|
181 | color = "#FF0000" |
---|
182 | display = pyosd.osd(font=options["font"], colour=color, timeout=duration, shadow=2) |
---|
183 | display.display(_("PyKota Units left : %.2f") % user.AccountBalance, type=pyosd.TYPE_STRING) |
---|
184 | |
---|
185 | time.sleep(duration + 1) |
---|
186 | if loop : |
---|
187 | loop -= 1 |
---|
188 | if not loop : |
---|
189 | break |
---|
190 | time.sleep(sleep) |
---|
191 | |
---|
192 | return 0 |
---|
193 | |
---|
194 | if __name__ == "__main__" : |
---|
195 | retcode = -1 |
---|
196 | try : |
---|
197 | defaults = { \ |
---|
198 | "color" : "#00FF00", \ |
---|
199 | "duration" : "3", \ |
---|
200 | "font" : pyosd.default_font, \ |
---|
201 | "loop" : "0", \ |
---|
202 | "sleep" : "180", \ |
---|
203 | } |
---|
204 | short_options = "hvc:d:f:l:s:" |
---|
205 | long_options = ["help", "version", "color=", "colour=", "duration=", "font=", "loop=", "sleep="] |
---|
206 | |
---|
207 | cmd = PyKOSD(doc=__doc__) |
---|
208 | |
---|
209 | # parse and checks the command line |
---|
210 | (options, args) = cmd.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
211 | |
---|
212 | # sets long options |
---|
213 | options["help"] = options["h"] or options["help"] |
---|
214 | options["version"] = options["v"] or options["version"] |
---|
215 | options["color"] = options["c"] or options["color"] or options["colour"] or defaults["color"] |
---|
216 | options["duration"] = options["d"] or options["duration"] or defaults["duration"] |
---|
217 | options["font"] = options["f"] or options["font"] or defaults["font"] |
---|
218 | options["loop"] = options["l"] or options["loop"] or defaults["loop"] |
---|
219 | options["sleep"] = options["s"] or options["sleep"] or defaults["sleep"] |
---|
220 | |
---|
221 | if options["help"] : |
---|
222 | cmd.display_usage_and_quit() |
---|
223 | elif options["version"] : |
---|
224 | cmd.display_version_and_quit() |
---|
225 | else : |
---|
226 | retcode = cmd.main(args, options) |
---|
227 | except KeyboardInterrupt : |
---|
228 | retcode = 0 |
---|
229 | except SystemExit : |
---|
230 | pass |
---|
231 | except : |
---|
232 | try : |
---|
233 | cmd.crashed("pykosd failed") |
---|
234 | except : |
---|
235 | crashed("pykosd failed") |
---|
236 | |
---|
237 | try : |
---|
238 | cmd.storage.close() |
---|
239 | except : |
---|
240 | pass |
---|
241 | |
---|
242 | sys.exit(retcode) |
---|