root / pykota / trunk / bin / pykosd @ 1785

Revision 1785, 8.5 kB (checked in by jalet, 20 years ago)

Minor changes to allow any PyKota administrator to launch enhanced versions
of the commands, and not only the root user.

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