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