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