[1595] | 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 | # |
---|
[2028] | 8 | # (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com> |
---|
[1595] | 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$ |
---|
[2028] | 26 | # Revision 1.12 2005/01/17 08:44:24 jalet |
---|
| 27 | # Modified copyright years |
---|
| 28 | # |
---|
[1832] | 29 | # Revision 1.11 2004/10/17 19:41:54 jalet |
---|
| 30 | # Fixed help message which displayed "pkprinters" instead of "pykosd" |
---|
| 31 | # |
---|
[1803] | 32 | # Revision 1.10 2004/10/11 22:53:05 jalet |
---|
| 33 | # Postponed string interpolation to help message's output method |
---|
| 34 | # |
---|
[1796] | 35 | # Revision 1.9 2004/10/11 12:49:06 jalet |
---|
| 36 | # Renders help translatable |
---|
| 37 | # |
---|
[1785] | 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 | # |
---|
[1608] | 42 | # Revision 1.7 2004/07/20 22:42:26 jalet |
---|
| 43 | # pykosd now supports setting color |
---|
| 44 | # |
---|
[1607] | 45 | # Revision 1.6 2004/07/20 22:29:49 jalet |
---|
| 46 | # pykosd now supports setting the font |
---|
| 47 | # |
---|
[1606] | 48 | # Revision 1.5 2004/07/20 22:19:45 jalet |
---|
| 49 | # Sanitized a bit + use of gettext |
---|
| 50 | # |
---|
[1605] | 51 | # Revision 1.4 2004/07/19 22:37:13 jalet |
---|
| 52 | # pykosd is now a very good tool |
---|
| 53 | # |
---|
[1597] | 54 | # Revision 1.3 2004/07/07 21:44:15 jalet |
---|
| 55 | # Formatting improvements |
---|
| 56 | # |
---|
[1596] | 57 | # Revision 1.2 2004/07/07 14:14:31 jalet |
---|
| 58 | # Now handles limits by quota in addition to limits by balance |
---|
| 59 | # |
---|
[1595] | 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 | |
---|
[1607] | 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 | |
---|
[1796] | 77 | from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_ |
---|
[1595] | 78 | |
---|
[2028] | 79 | __doc__ = N_("""pykosd v%s (c) 2003, 2004, 2005 C@LL - Conseil Internet & Logiciels Libres |
---|
[1605] | 80 | An OSD quota monitor for PyKota. |
---|
[1597] | 81 | |
---|
[1605] | 82 | command line usage : |
---|
| 83 | |
---|
| 84 | pykosd [options] |
---|
| 85 | |
---|
| 86 | options : |
---|
| 87 | |
---|
[1832] | 88 | -v | --version Prints pykosd's version number then exits. |
---|
[1605] | 89 | -h | --help Prints this message then exits. |
---|
| 90 | |
---|
[1608] | 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 | |
---|
[1605] | 95 | -d | --duration d Sets the duration of the display in seconds. |
---|
| 96 | Defaults to 3 seconds. |
---|
[1607] | 97 | |
---|
| 98 | -f | --font f Sets the font to use for display. |
---|
| 99 | Defaults to the Python OSD library's default. |
---|
[1605] | 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 | |
---|
[1803] | 130 | Please e-mail bugs to: %s""") |
---|
[1605] | 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 : |
---|
[1606] | 139 | raise PyKotaToolError, _("Invalid duration option %s") % str(options["duration"]) |
---|
[1605] | 140 | |
---|
| 141 | try : |
---|
| 142 | loop = int(options["loop"]) |
---|
| 143 | except : |
---|
[1606] | 144 | raise PyKotaToolError, _("Invalid loop option %s") % str(options["loop"]) |
---|
[1605] | 145 | |
---|
| 146 | try : |
---|
| 147 | sleep = float(options["sleep"]) |
---|
| 148 | except : |
---|
[1606] | 149 | raise PyKotaToolError, _("Invalid sleep option %s") % str(options["sleep"]) |
---|
[1605] | 150 | |
---|
[1608] | 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 | |
---|
[1785] | 158 | uname = pwd.getpwuid(os.geteuid())[0] |
---|
[1605] | 159 | while 1 : |
---|
[1608] | 160 | color = savecolor |
---|
[1605] | 161 | user = cmd.storage.getUserFromBackend(uname) # don't use cache |
---|
[1595] | 162 | if not user.Exists : |
---|
[1606] | 163 | raise PyKotaToolError, _("User %s doesn't exist in PyKota's database") % uname |
---|
[1596] | 164 | if user.LimitBy == "quota" : |
---|
| 165 | printers = cmd.storage.getMatchingPrinters("*") |
---|
[1605] | 166 | upquotas = [ cmd.storage.getUserPQuotaFromBackend(user, p) for p in printers ] # don't use cache |
---|
[1596] | 167 | nblines = len(upquotas) |
---|
[1608] | 168 | display = pyosd.osd(font=options["font"], colour=color, timeout=duration, shadow=2, lines=nblines) |
---|
[1596] | 169 | for line in range(nblines) : |
---|
| 170 | upq = upquotas[line] |
---|
| 171 | if upq.HardLimit is None : |
---|
| 172 | if upq.SoftLimit is None : |
---|
[1597] | 173 | percent = "%s" % upq.PageCounter |
---|
[1596] | 174 | else : |
---|
[1597] | 175 | percent = "%s%%" % min((upq.PageCounter * 100) / upq.SoftLimit, 100) |
---|
[1596] | 176 | else : |
---|
[1597] | 177 | percent = "%s%%" % min((upq.PageCounter * 100) / upq.HardLimit, 100) |
---|
[1605] | 178 | display.display(_("Pages used on %s : %s") % (upq.Printer.Name, percent), type=pyosd.TYPE_STRING, line=line) |
---|
[1596] | 179 | else : |
---|
[1597] | 180 | if user.AccountBalance <= 0 : |
---|
| 181 | color = "#FF0000" |
---|
[1607] | 182 | display = pyosd.osd(font=options["font"], colour=color, timeout=duration, shadow=2) |
---|
[1605] | 183 | display.display(_("PyKota Units left : %.2f") % user.AccountBalance, type=pyosd.TYPE_STRING) |
---|
[1608] | 184 | |
---|
[1605] | 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 = { \ |
---|
[1608] | 198 | "color" : "#00FF00", \ |
---|
[1605] | 199 | "duration" : "3", \ |
---|
[1607] | 200 | "font" : pyosd.default_font, \ |
---|
[1605] | 201 | "loop" : "0", \ |
---|
| 202 | "sleep" : "180", \ |
---|
| 203 | } |
---|
[1608] | 204 | short_options = "hvc:d:f:l:s:" |
---|
| 205 | long_options = ["help", "version", "color=", "colour=", "duration=", "font=", "loop=", "sleep="] |
---|
[1605] | 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"] |
---|
[1608] | 215 | options["color"] = options["c"] or options["color"] or options["colour"] or defaults["color"] |
---|
[1605] | 216 | options["duration"] = options["d"] or options["duration"] or defaults["duration"] |
---|
[1607] | 217 | options["font"] = options["f"] or options["font"] or defaults["font"] |
---|
[1605] | 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 : |
---|
[1595] | 232 | try : |
---|
[1605] | 233 | cmd.crashed("pykosd failed") |
---|
[1595] | 234 | except : |
---|
[1605] | 235 | crashed("pykosd failed") |
---|
| 236 | |
---|
| 237 | try : |
---|
| 238 | cmd.storage.close() |
---|
| 239 | except : |
---|
| 240 | pass |
---|
| 241 | |
---|
[1595] | 242 | sys.exit(retcode) |
---|