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