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