root / pykota / trunk / bin / pykosd @ 1605

Revision 1605, 7.0 kB (checked in by jalet, 20 years ago)

pykosd is now a very good tool

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