root / pykota / trunk / bin / pykosd @ 1608

Revision 1608, 8.4 kB (checked in by jalet, 20 years ago)

pykosd now supports setting color

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