root / pykota / trunk / bin / pykosd @ 1796

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

Renders help translatable

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