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