root / pykota / trunk / bin / pykosd @ 2303

Revision 2303, 7.8 kB (checked in by jerome, 19 years ago)

Updated the FSF's address

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