root / pykota / trunk / bin / pykosd @ 1803

Revision 1803, 8.6 kB (checked in by jalet, 20 years ago)

Postponed string interpolation to help message's output method

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