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