[1595] | 1 | #! /usr/bin/env python |
---|
[3260] | 2 | # -*- coding: UTF-8 -*- |
---|
[1595] | 3 | # |
---|
[3260] | 4 | # PyKota : Print Quotas for CUPS |
---|
[1595] | 5 | # |
---|
[3275] | 6 | # (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com> |
---|
[3260] | 7 | # This program is free software: you can redistribute it and/or modify |
---|
[1595] | 8 | # it under the terms of the GNU General Public License as published by |
---|
[3260] | 9 | # the Free Software Foundation, either version 3 of the License, or |
---|
[1595] | 10 | # (at your option) any later version. |
---|
[3260] | 11 | # |
---|
[1595] | 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
[3260] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[1595] | 19 | # |
---|
| 20 | # $Id$ |
---|
| 21 | # |
---|
[2028] | 22 | # |
---|
[1595] | 23 | |
---|
| 24 | import sys |
---|
| 25 | import os |
---|
| 26 | import pwd |
---|
| 27 | import time |
---|
| 28 | |
---|
[1607] | 29 | try : |
---|
| 30 | import pyosd |
---|
| 31 | except ImportError : |
---|
[3294] | 32 | logerr("Sorry ! You need both xosd and the Python OSD library (pyosd) for this software to work.\n") |
---|
[1607] | 33 | sys.exit(-1) |
---|
| 34 | |
---|
[3294] | 35 | import pykota.appinit |
---|
| 36 | from pykota.utils import * |
---|
| 37 | |
---|
[3288] | 38 | from pykota.errors import PyKotaToolError, PyKotaCommandLineError |
---|
[3295] | 39 | from pykota.tool import PyKotaTool |
---|
[1595] | 40 | |
---|
[2344] | 41 | __doc__ = N_("""pykosd v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
[2267] | 42 | |
---|
[1605] | 43 | An OSD quota monitor for PyKota. |
---|
[1597] | 44 | |
---|
[1605] | 45 | command line usage : |
---|
| 46 | |
---|
| 47 | pykosd [options] |
---|
| 48 | |
---|
| 49 | options : |
---|
| 50 | |
---|
[1832] | 51 | -v | --version Prints pykosd's version number then exits. |
---|
[1605] | 52 | -h | --help Prints this message then exits. |
---|
| 53 | |
---|
[1608] | 54 | -c | --color #rrggbb Sets the color to use for display as an hexadecimal |
---|
| 55 | triplet, for example #FF0000 is 100%% red. |
---|
| 56 | Defaults to 100%% green (#00FF00). |
---|
| 57 | |
---|
[1605] | 58 | -d | --duration d Sets the duration of the display in seconds. |
---|
| 59 | Defaults to 3 seconds. |
---|
[1607] | 60 | |
---|
| 61 | -f | --font f Sets the font to use for display. |
---|
| 62 | Defaults to the Python OSD library's default. |
---|
[1605] | 63 | |
---|
| 64 | -l | --loop n Sets the number of times the info will be displayed. |
---|
| 65 | Defaults to 0, which means loop forever. |
---|
| 66 | |
---|
| 67 | -s | --sleep s Sets the sleeping duration between two displays |
---|
| 68 | in seconds. Defaults to 180 seconds (3 minutes). |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | examples : |
---|
| 72 | |
---|
| 73 | $ pykosd -s 60 --loop 5 |
---|
| 74 | |
---|
| 75 | Will launch pykosd. Display will be refreshed every 60 seconds, |
---|
| 76 | and will last for 3 seconds (the default) each time. After five |
---|
| 77 | iterations, the program will exit. |
---|
[2344] | 78 | """) |
---|
[1605] | 79 | |
---|
| 80 | |
---|
| 81 | class PyKOSD(PyKotaTool) : |
---|
[2829] | 82 | """A class for an On Screen Display print quota monitor.""" |
---|
[1605] | 83 | def main(self, args, options) : |
---|
| 84 | """Main function starts here.""" |
---|
| 85 | try : |
---|
| 86 | duration = int(options["duration"]) |
---|
[2512] | 87 | if duration <= 0 : |
---|
| 88 | raise ValueError |
---|
[1605] | 89 | except : |
---|
[2512] | 90 | raise PyKotaCommandLineError, _("Invalid duration option %s") % str(options["duration"]) |
---|
[1605] | 91 | |
---|
| 92 | try : |
---|
| 93 | loop = int(options["loop"]) |
---|
[2512] | 94 | if loop < 0 : |
---|
| 95 | raise ValueError |
---|
[1605] | 96 | except : |
---|
[2512] | 97 | raise PyKotaCommandLineError, _("Invalid loop option %s") % str(options["loop"]) |
---|
[1605] | 98 | |
---|
| 99 | try : |
---|
| 100 | sleep = float(options["sleep"]) |
---|
[2512] | 101 | if sleep <= 0 : |
---|
| 102 | raise ValueError |
---|
[1605] | 103 | except : |
---|
[2512] | 104 | raise PyKotaCommandLineError, _("Invalid sleep option %s") % str(options["sleep"]) |
---|
[1605] | 105 | |
---|
[1608] | 106 | color = options["color"] |
---|
| 107 | if not color.startswith("#") : |
---|
| 108 | color = "#%s" % color |
---|
| 109 | if len(color) != 7 : |
---|
[2512] | 110 | raise PyKotaCommandLineError, _("Invalid color option %s") % str(color) |
---|
[1608] | 111 | savecolor = color |
---|
| 112 | |
---|
[2232] | 113 | uname = pwd.getpwuid(os.getuid())[0] |
---|
[1605] | 114 | while 1 : |
---|
[1608] | 115 | color = savecolor |
---|
[2692] | 116 | user = self.storage.getUserFromBackend(uname) # don't use cache |
---|
[1595] | 117 | if not user.Exists : |
---|
[2512] | 118 | raise PyKotaCommandLineError, _("User %s doesn't exist in PyKota's database") % uname |
---|
[1596] | 119 | if user.LimitBy == "quota" : |
---|
[2692] | 120 | printers = self.storage.getMatchingPrinters("*") |
---|
| 121 | upquotas = [ self.storage.getUserPQuotaFromBackend(user, p) for p in printers ] # don't use cache |
---|
[1596] | 122 | nblines = len(upquotas) |
---|
[1608] | 123 | display = pyosd.osd(font=options["font"], colour=color, timeout=duration, shadow=2, lines=nblines) |
---|
[1596] | 124 | for line in range(nblines) : |
---|
| 125 | upq = upquotas[line] |
---|
| 126 | if upq.HardLimit is None : |
---|
| 127 | if upq.SoftLimit is None : |
---|
[1597] | 128 | percent = "%s" % upq.PageCounter |
---|
[1596] | 129 | else : |
---|
[1597] | 130 | percent = "%s%%" % min((upq.PageCounter * 100) / upq.SoftLimit, 100) |
---|
[1596] | 131 | else : |
---|
[1597] | 132 | percent = "%s%%" % min((upq.PageCounter * 100) / upq.HardLimit, 100) |
---|
[1605] | 133 | display.display(_("Pages used on %s : %s") % (upq.Printer.Name, percent), type=pyosd.TYPE_STRING, line=line) |
---|
[2544] | 134 | elif user.LimitBy == "balance" : |
---|
[2692] | 135 | if user.AccountBalance <= self.config.getBalanceZero() : |
---|
[1597] | 136 | color = "#FF0000" |
---|
[1607] | 137 | display = pyosd.osd(font=options["font"], colour=color, timeout=duration, shadow=2) |
---|
[1605] | 138 | display.display(_("PyKota Units left : %.2f") % user.AccountBalance, type=pyosd.TYPE_STRING) |
---|
[2544] | 139 | elif user.LimitBy == "noprint" : |
---|
| 140 | display = pyosd.osd(font=options["font"], colour="#FF0000", timeout=duration, shadow=2) |
---|
| 141 | display.display(_("Printing denied."), type=pyosd.TYPE_STRING) |
---|
| 142 | elif user.LimitBy == "noquota" : |
---|
| 143 | display = pyosd.osd(font=options["font"], colour=savecolor, timeout=duration, shadow=2) |
---|
| 144 | display.display(_("Printing not limited."), type=pyosd.TYPE_STRING) |
---|
| 145 | elif user.LimitBy == "nochange" : |
---|
| 146 | display = pyosd.osd(font=options["font"], colour=savecolor, timeout=duration, shadow=2) |
---|
| 147 | display.display(_("Printing not limited, no accounting."), type=pyosd.TYPE_STRING) |
---|
| 148 | else : |
---|
| 149 | raise PyKotaToolError, "Incorrect limitation factor %s for user %s" % (repr(user.LimitBy), user.Name) |
---|
[1608] | 150 | |
---|
[1605] | 151 | time.sleep(duration + 1) |
---|
| 152 | if loop : |
---|
| 153 | loop -= 1 |
---|
| 154 | if not loop : |
---|
| 155 | break |
---|
| 156 | time.sleep(sleep) |
---|
| 157 | |
---|
| 158 | return 0 |
---|
| 159 | |
---|
| 160 | if __name__ == "__main__" : |
---|
| 161 | retcode = -1 |
---|
| 162 | try : |
---|
| 163 | defaults = { \ |
---|
[1608] | 164 | "color" : "#00FF00", \ |
---|
[1605] | 165 | "duration" : "3", \ |
---|
[1607] | 166 | "font" : pyosd.default_font, \ |
---|
[1605] | 167 | "loop" : "0", \ |
---|
| 168 | "sleep" : "180", \ |
---|
| 169 | } |
---|
[1608] | 170 | short_options = "hvc:d:f:l:s:" |
---|
| 171 | long_options = ["help", "version", "color=", "colour=", "duration=", "font=", "loop=", "sleep="] |
---|
[1605] | 172 | |
---|
| 173 | cmd = PyKOSD(doc=__doc__) |
---|
[2210] | 174 | cmd.deferredInit() |
---|
[1605] | 175 | |
---|
| 176 | # parse and checks the command line |
---|
| 177 | (options, args) = cmd.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
| 178 | |
---|
| 179 | # sets long options |
---|
| 180 | options["help"] = options["h"] or options["help"] |
---|
| 181 | options["version"] = options["v"] or options["version"] |
---|
[1608] | 182 | options["color"] = options["c"] or options["color"] or options["colour"] or defaults["color"] |
---|
[1605] | 183 | options["duration"] = options["d"] or options["duration"] or defaults["duration"] |
---|
[1607] | 184 | options["font"] = options["f"] or options["font"] or defaults["font"] |
---|
[1605] | 185 | options["loop"] = options["l"] or options["loop"] or defaults["loop"] |
---|
| 186 | options["sleep"] = options["s"] or options["sleep"] or defaults["sleep"] |
---|
| 187 | |
---|
| 188 | if options["help"] : |
---|
| 189 | cmd.display_usage_and_quit() |
---|
| 190 | elif options["version"] : |
---|
| 191 | cmd.display_version_and_quit() |
---|
| 192 | else : |
---|
| 193 | retcode = cmd.main(args, options) |
---|
| 194 | except KeyboardInterrupt : |
---|
| 195 | retcode = 0 |
---|
[2216] | 196 | except KeyboardInterrupt : |
---|
[3294] | 197 | logerr("\nInterrupted with Ctrl+C !\n") |
---|
[2609] | 198 | retcode = -3 |
---|
[2512] | 199 | except PyKotaCommandLineError, msg : |
---|
[3294] | 200 | logerr("%s : %s\n" % (sys.argv[0], msg)) |
---|
[2609] | 201 | retcode = -2 |
---|
[1605] | 202 | except SystemExit : |
---|
| 203 | pass |
---|
| 204 | except : |
---|
[1595] | 205 | try : |
---|
[1605] | 206 | cmd.crashed("pykosd failed") |
---|
[1595] | 207 | except : |
---|
[1605] | 208 | crashed("pykosd failed") |
---|
| 209 | |
---|
| 210 | try : |
---|
| 211 | cmd.storage.close() |
---|
| 212 | except : |
---|
| 213 | pass |
---|
| 214 | |
---|
[1595] | 215 | sys.exit(retcode) |
---|