[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 |
---|
[2303] | 21 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[1595] | 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 | |
---|
[2512] | 38 | from pykota.tool import PyKotaTool, PyKotaToolError, PyKotaCommandLineError, crashed, N_ |
---|
[1595] | 39 | |
---|
[2344] | 40 | __doc__ = N_("""pykosd v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
[2267] | 41 | |
---|
[1605] | 42 | An OSD quota monitor for PyKota. |
---|
[1597] | 43 | |
---|
[1605] | 44 | command line usage : |
---|
| 45 | |
---|
| 46 | pykosd [options] |
---|
| 47 | |
---|
| 48 | options : |
---|
| 49 | |
---|
[1832] | 50 | -v | --version Prints pykosd's version number then exits. |
---|
[1605] | 51 | -h | --help Prints this message then exits. |
---|
| 52 | |
---|
[1608] | 53 | -c | --color #rrggbb Sets the color to use for display as an hexadecimal |
---|
| 54 | triplet, for example #FF0000 is 100%% red. |
---|
| 55 | Defaults to 100%% green (#00FF00). |
---|
| 56 | |
---|
[1605] | 57 | -d | --duration d Sets the duration of the display in seconds. |
---|
| 58 | Defaults to 3 seconds. |
---|
[1607] | 59 | |
---|
| 60 | -f | --font f Sets the font to use for display. |
---|
| 61 | Defaults to the Python OSD library's default. |
---|
[1605] | 62 | |
---|
| 63 | -l | --loop n Sets the number of times the info will be displayed. |
---|
| 64 | Defaults to 0, which means loop forever. |
---|
| 65 | |
---|
| 66 | -s | --sleep s Sets the sleeping duration between two displays |
---|
| 67 | in seconds. Defaults to 180 seconds (3 minutes). |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | examples : |
---|
| 71 | |
---|
| 72 | $ pykosd -s 60 --loop 5 |
---|
| 73 | |
---|
| 74 | Will launch pykosd. Display will be refreshed every 60 seconds, |
---|
| 75 | and will last for 3 seconds (the default) each time. After five |
---|
| 76 | iterations, the program will exit. |
---|
[2344] | 77 | """) |
---|
[1605] | 78 | |
---|
| 79 | |
---|
| 80 | class PyKOSD(PyKotaTool) : |
---|
| 81 | def main(self, args, options) : |
---|
| 82 | """Main function starts here.""" |
---|
| 83 | try : |
---|
| 84 | duration = int(options["duration"]) |
---|
[2512] | 85 | if duration <= 0 : |
---|
| 86 | raise ValueError |
---|
[1605] | 87 | except : |
---|
[2512] | 88 | raise PyKotaCommandLineError, _("Invalid duration option %s") % str(options["duration"]) |
---|
[1605] | 89 | |
---|
| 90 | try : |
---|
| 91 | loop = int(options["loop"]) |
---|
[2512] | 92 | if loop < 0 : |
---|
| 93 | raise ValueError |
---|
[1605] | 94 | except : |
---|
[2512] | 95 | raise PyKotaCommandLineError, _("Invalid loop option %s") % str(options["loop"]) |
---|
[1605] | 96 | |
---|
| 97 | try : |
---|
| 98 | sleep = float(options["sleep"]) |
---|
[2512] | 99 | if sleep <= 0 : |
---|
| 100 | raise ValueError |
---|
[1605] | 101 | except : |
---|
[2512] | 102 | raise PyKotaCommandLineError, _("Invalid sleep option %s") % str(options["sleep"]) |
---|
[1605] | 103 | |
---|
[1608] | 104 | color = options["color"] |
---|
| 105 | if not color.startswith("#") : |
---|
| 106 | color = "#%s" % color |
---|
| 107 | if len(color) != 7 : |
---|
[2512] | 108 | raise PyKotaCommandLineError, _("Invalid color option %s") % str(color) |
---|
[1608] | 109 | savecolor = color |
---|
| 110 | |
---|
[2232] | 111 | uname = pwd.getpwuid(os.getuid())[0] |
---|
[1605] | 112 | while 1 : |
---|
[1608] | 113 | color = savecolor |
---|
[1605] | 114 | user = cmd.storage.getUserFromBackend(uname) # don't use cache |
---|
[1595] | 115 | if not user.Exists : |
---|
[2512] | 116 | raise PyKotaCommandLineError, _("User %s doesn't exist in PyKota's database") % uname |
---|
[1596] | 117 | if user.LimitBy == "quota" : |
---|
| 118 | printers = cmd.storage.getMatchingPrinters("*") |
---|
[1605] | 119 | upquotas = [ cmd.storage.getUserPQuotaFromBackend(user, p) for p in printers ] # don't use cache |
---|
[1596] | 120 | nblines = len(upquotas) |
---|
[1608] | 121 | display = pyosd.osd(font=options["font"], colour=color, timeout=duration, shadow=2, lines=nblines) |
---|
[1596] | 122 | for line in range(nblines) : |
---|
| 123 | upq = upquotas[line] |
---|
| 124 | if upq.HardLimit is None : |
---|
| 125 | if upq.SoftLimit is None : |
---|
[1597] | 126 | percent = "%s" % upq.PageCounter |
---|
[1596] | 127 | else : |
---|
[1597] | 128 | percent = "%s%%" % min((upq.PageCounter * 100) / upq.SoftLimit, 100) |
---|
[1596] | 129 | else : |
---|
[1597] | 130 | percent = "%s%%" % min((upq.PageCounter * 100) / upq.HardLimit, 100) |
---|
[1605] | 131 | display.display(_("Pages used on %s : %s") % (upq.Printer.Name, percent), type=pyosd.TYPE_STRING, line=line) |
---|
[2544] | 132 | elif user.LimitBy == "balance" : |
---|
[1597] | 133 | if user.AccountBalance <= 0 : |
---|
| 134 | color = "#FF0000" |
---|
[1607] | 135 | display = pyosd.osd(font=options["font"], colour=color, timeout=duration, shadow=2) |
---|
[1605] | 136 | display.display(_("PyKota Units left : %.2f") % user.AccountBalance, type=pyosd.TYPE_STRING) |
---|
[2544] | 137 | elif user.LimitBy == "noprint" : |
---|
| 138 | display = pyosd.osd(font=options["font"], colour="#FF0000", timeout=duration, shadow=2) |
---|
| 139 | display.display(_("Printing denied."), type=pyosd.TYPE_STRING) |
---|
| 140 | elif user.LimitBy == "noquota" : |
---|
| 141 | display = pyosd.osd(font=options["font"], colour=savecolor, timeout=duration, shadow=2) |
---|
| 142 | display.display(_("Printing not limited."), type=pyosd.TYPE_STRING) |
---|
| 143 | elif user.LimitBy == "nochange" : |
---|
| 144 | display = pyosd.osd(font=options["font"], colour=savecolor, timeout=duration, shadow=2) |
---|
| 145 | display.display(_("Printing not limited, no accounting."), type=pyosd.TYPE_STRING) |
---|
| 146 | else : |
---|
| 147 | raise PyKotaToolError, "Incorrect limitation factor %s for user %s" % (repr(user.LimitBy), user.Name) |
---|
[1608] | 148 | |
---|
[1605] | 149 | time.sleep(duration + 1) |
---|
| 150 | if loop : |
---|
| 151 | loop -= 1 |
---|
| 152 | if not loop : |
---|
| 153 | break |
---|
| 154 | time.sleep(sleep) |
---|
| 155 | |
---|
| 156 | return 0 |
---|
| 157 | |
---|
| 158 | if __name__ == "__main__" : |
---|
| 159 | retcode = -1 |
---|
| 160 | try : |
---|
| 161 | defaults = { \ |
---|
[1608] | 162 | "color" : "#00FF00", \ |
---|
[1605] | 163 | "duration" : "3", \ |
---|
[1607] | 164 | "font" : pyosd.default_font, \ |
---|
[1605] | 165 | "loop" : "0", \ |
---|
| 166 | "sleep" : "180", \ |
---|
| 167 | } |
---|
[1608] | 168 | short_options = "hvc:d:f:l:s:" |
---|
| 169 | long_options = ["help", "version", "color=", "colour=", "duration=", "font=", "loop=", "sleep="] |
---|
[1605] | 170 | |
---|
| 171 | cmd = PyKOSD(doc=__doc__) |
---|
[2210] | 172 | cmd.deferredInit() |
---|
[1605] | 173 | |
---|
| 174 | # parse and checks the command line |
---|
| 175 | (options, args) = cmd.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
| 176 | |
---|
| 177 | # sets long options |
---|
| 178 | options["help"] = options["h"] or options["help"] |
---|
| 179 | options["version"] = options["v"] or options["version"] |
---|
[1608] | 180 | options["color"] = options["c"] or options["color"] or options["colour"] or defaults["color"] |
---|
[1605] | 181 | options["duration"] = options["d"] or options["duration"] or defaults["duration"] |
---|
[1607] | 182 | options["font"] = options["f"] or options["font"] or defaults["font"] |
---|
[1605] | 183 | options["loop"] = options["l"] or options["loop"] or defaults["loop"] |
---|
| 184 | options["sleep"] = options["s"] or options["sleep"] or defaults["sleep"] |
---|
| 185 | |
---|
| 186 | if options["help"] : |
---|
| 187 | cmd.display_usage_and_quit() |
---|
| 188 | elif options["version"] : |
---|
| 189 | cmd.display_version_and_quit() |
---|
| 190 | else : |
---|
| 191 | retcode = cmd.main(args, options) |
---|
| 192 | except KeyboardInterrupt : |
---|
| 193 | retcode = 0 |
---|
[2216] | 194 | except KeyboardInterrupt : |
---|
| 195 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
[2512] | 196 | except PyKotaCommandLineError, msg : |
---|
| 197 | sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) |
---|
[1605] | 198 | except SystemExit : |
---|
| 199 | pass |
---|
| 200 | except : |
---|
[1595] | 201 | try : |
---|
[1605] | 202 | cmd.crashed("pykosd failed") |
---|
[1595] | 203 | except : |
---|
[1605] | 204 | crashed("pykosd failed") |
---|
| 205 | |
---|
| 206 | try : |
---|
| 207 | cmd.storage.close() |
---|
| 208 | except : |
---|
| 209 | pass |
---|
| 210 | |
---|
[1595] | 211 | sys.exit(retcode) |
---|