[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 | # |
---|
[2028] | 25 | # |
---|
[1595] | 26 | |
---|
| 27 | import sys |
---|
| 28 | import os |
---|
| 29 | import pwd |
---|
| 30 | import time |
---|
| 31 | |
---|
[1607] | 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 | |
---|
[1796] | 38 | from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_ |
---|
[1595] | 39 | |
---|
[2028] | 40 | __doc__ = N_("""pykosd v%s (c) 2003, 2004, 2005 C@LL - Conseil Internet & Logiciels Libres |
---|
[1605] | 41 | An OSD quota monitor for PyKota. |
---|
[1597] | 42 | |
---|
[1605] | 43 | command line usage : |
---|
| 44 | |
---|
| 45 | pykosd [options] |
---|
| 46 | |
---|
| 47 | options : |
---|
| 48 | |
---|
[1832] | 49 | -v | --version Prints pykosd's version number then exits. |
---|
[1605] | 50 | -h | --help Prints this message then exits. |
---|
| 51 | |
---|
[1608] | 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 | |
---|
[1605] | 56 | -d | --duration d Sets the duration of the display in seconds. |
---|
| 57 | Defaults to 3 seconds. |
---|
[1607] | 58 | |
---|
| 59 | -f | --font f Sets the font to use for display. |
---|
| 60 | Defaults to the Python OSD library's default. |
---|
[1605] | 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 | |
---|
[1803] | 91 | Please e-mail bugs to: %s""") |
---|
[1605] | 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 : |
---|
[1606] | 100 | raise PyKotaToolError, _("Invalid duration option %s") % str(options["duration"]) |
---|
[1605] | 101 | |
---|
| 102 | try : |
---|
| 103 | loop = int(options["loop"]) |
---|
| 104 | except : |
---|
[1606] | 105 | raise PyKotaToolError, _("Invalid loop option %s") % str(options["loop"]) |
---|
[1605] | 106 | |
---|
| 107 | try : |
---|
| 108 | sleep = float(options["sleep"]) |
---|
| 109 | except : |
---|
[1606] | 110 | raise PyKotaToolError, _("Invalid sleep option %s") % str(options["sleep"]) |
---|
[1605] | 111 | |
---|
[1608] | 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 | |
---|
[2232] | 119 | uname = pwd.getpwuid(os.getuid())[0] |
---|
[1605] | 120 | while 1 : |
---|
[1608] | 121 | color = savecolor |
---|
[1605] | 122 | user = cmd.storage.getUserFromBackend(uname) # don't use cache |
---|
[1595] | 123 | if not user.Exists : |
---|
[1606] | 124 | raise PyKotaToolError, _("User %s doesn't exist in PyKota's database") % uname |
---|
[1596] | 125 | if user.LimitBy == "quota" : |
---|
| 126 | printers = cmd.storage.getMatchingPrinters("*") |
---|
[1605] | 127 | upquotas = [ cmd.storage.getUserPQuotaFromBackend(user, p) for p in printers ] # don't use cache |
---|
[1596] | 128 | nblines = len(upquotas) |
---|
[1608] | 129 | display = pyosd.osd(font=options["font"], colour=color, timeout=duration, shadow=2, lines=nblines) |
---|
[1596] | 130 | for line in range(nblines) : |
---|
| 131 | upq = upquotas[line] |
---|
| 132 | if upq.HardLimit is None : |
---|
| 133 | if upq.SoftLimit is None : |
---|
[1597] | 134 | percent = "%s" % upq.PageCounter |
---|
[1596] | 135 | else : |
---|
[1597] | 136 | percent = "%s%%" % min((upq.PageCounter * 100) / upq.SoftLimit, 100) |
---|
[1596] | 137 | else : |
---|
[1597] | 138 | percent = "%s%%" % min((upq.PageCounter * 100) / upq.HardLimit, 100) |
---|
[1605] | 139 | display.display(_("Pages used on %s : %s") % (upq.Printer.Name, percent), type=pyosd.TYPE_STRING, line=line) |
---|
[1596] | 140 | else : |
---|
[1597] | 141 | if user.AccountBalance <= 0 : |
---|
| 142 | color = "#FF0000" |
---|
[1607] | 143 | display = pyosd.osd(font=options["font"], colour=color, timeout=duration, shadow=2) |
---|
[1605] | 144 | display.display(_("PyKota Units left : %.2f") % user.AccountBalance, type=pyosd.TYPE_STRING) |
---|
[1608] | 145 | |
---|
[1605] | 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 = { \ |
---|
[1608] | 159 | "color" : "#00FF00", \ |
---|
[1605] | 160 | "duration" : "3", \ |
---|
[1607] | 161 | "font" : pyosd.default_font, \ |
---|
[1605] | 162 | "loop" : "0", \ |
---|
| 163 | "sleep" : "180", \ |
---|
| 164 | } |
---|
[1608] | 165 | short_options = "hvc:d:f:l:s:" |
---|
| 166 | long_options = ["help", "version", "color=", "colour=", "duration=", "font=", "loop=", "sleep="] |
---|
[1605] | 167 | |
---|
| 168 | cmd = PyKOSD(doc=__doc__) |
---|
[2210] | 169 | cmd.deferredInit() |
---|
[1605] | 170 | |
---|
| 171 | # parse and checks the command line |
---|
| 172 | (options, args) = cmd.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
| 173 | |
---|
| 174 | # sets long options |
---|
| 175 | options["help"] = options["h"] or options["help"] |
---|
| 176 | options["version"] = options["v"] or options["version"] |
---|
[1608] | 177 | options["color"] = options["c"] or options["color"] or options["colour"] or defaults["color"] |
---|
[1605] | 178 | options["duration"] = options["d"] or options["duration"] or defaults["duration"] |
---|
[1607] | 179 | options["font"] = options["f"] or options["font"] or defaults["font"] |
---|
[1605] | 180 | options["loop"] = options["l"] or options["loop"] or defaults["loop"] |
---|
| 181 | options["sleep"] = options["s"] or options["sleep"] or defaults["sleep"] |
---|
| 182 | |
---|
| 183 | if options["help"] : |
---|
| 184 | cmd.display_usage_and_quit() |
---|
| 185 | elif options["version"] : |
---|
| 186 | cmd.display_version_and_quit() |
---|
| 187 | else : |
---|
| 188 | retcode = cmd.main(args, options) |
---|
| 189 | except KeyboardInterrupt : |
---|
| 190 | retcode = 0 |
---|
[2216] | 191 | except KeyboardInterrupt : |
---|
| 192 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
[1605] | 193 | except SystemExit : |
---|
| 194 | pass |
---|
| 195 | except : |
---|
[1595] | 196 | try : |
---|
[1605] | 197 | cmd.crashed("pykosd failed") |
---|
[1595] | 198 | except : |
---|
[1605] | 199 | crashed("pykosd failed") |
---|
| 200 | |
---|
| 201 | try : |
---|
| 202 | cmd.storage.close() |
---|
| 203 | except : |
---|
| 204 | pass |
---|
| 205 | |
---|
[1595] | 206 | sys.exit(retcode) |
---|