root / pykota / trunk / bin / pykosd @ 1607

Revision 1607, 7.7 kB (checked in by jalet, 20 years ago)

pykosd now supports setting the font

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