root / pykota / trunk / bin / pykosd @ 1832

Revision 1832, 8.7 kB (checked in by jalet, 20 years ago)

Fixed help message which displayed "pkprinters" instead of "pykosd"

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