Show
Ignore:
Timestamp:
07/20/04 00:37:25 (20 years ago)
Author:
jalet
Message:

pykosd is now a very good tool

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/pykosd

    r1597 r1605  
    2424# 
    2525# $Log$ 
     26# Revision 1.4  2004/07/19 22:37:13  jalet 
     27# pykosd is now a very good tool 
     28# 
    2629# Revision 1.3  2004/07/07 21:44:15  jalet 
    2730# Formatting improvements 
     
    4245import pyosd 
    4346 
     47from pykota import version 
    4448from pykota.tool import PyKotaTool, PyKotaToolError, crashed 
    4549 
    46 DURATION = 5 
     50__doc__ = """pykosd v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres 
     51An OSD quota monitor for PyKota. 
    4752 
    48 if __name__ == "__main__" : 
    49     retcode = -1 
    50     try : 
    51         cmd = PyKotaTool(doc="A tool to display remaining print units") 
    52     except :     
    53         crashed("Initialization problem.") 
    54     else :     
    55         try : 
    56             uid = os.geteuid() 
    57             uname = pwd.getpwuid(uid)[0] 
    58             user = cmd.storage.getUser(uname) 
     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 
    59119            if not user.Exists : 
    60120                raise PyKotaToolError, "User %s doesn't exist in PyKota's database." % uname 
    61121            if user.LimitBy == "quota" :     
    62122                printers = cmd.storage.getMatchingPrinters("*") 
    63                 upquotas = [ cmd.storage.getUserPQuota(user, p) for p in printers ] 
     123                upquotas = [ cmd.storage.getUserPQuotaFromBackend(user, p) for p in printers ] # don't use cache 
    64124                nblines = len(upquotas) 
    65                 display = pyosd.osd(colour="#00FF00", timeout=DURATION, shadow=2, lines=nblines) 
     125                display = pyosd.osd(colour="#00FF00", timeout=duration, shadow=2, lines=nblines) 
    66126                for line in range(nblines) : 
    67127                    upq = upquotas[line] 
     
    73133                    else :         
    74134                        percent = "%s%%" % min((upq.PageCounter * 100) / upq.HardLimit, 100) 
    75                     display.display("Pages used on %s : %s" % (upq.Printer.Name, percent), type=pyosd.TYPE_STRING, line=line) 
     135                    display.display(_("Pages used on %s : %s") % (upq.Printer.Name, percent), type=pyosd.TYPE_STRING, line=line) 
    76136            else : 
    77137                if user.AccountBalance <= 0 : 
     
    79139                else :     
    80140                    color = "#00FF00" 
    81                 display = pyosd.osd(colour=color, timeout=DURATION, shadow=2) 
    82                 display.display("PyKota Units left : %.2f" % user.AccountBalance, type=pyosd.TYPE_STRING)     
    83             time.sleep(DURATION + 1) 
    84         except : 
    85             cmd.crashed("Strange problem : please report it ASAP to alet@librelogiciel.com") 
    86         else : 
    87             retcode = 0 
     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 :     
    88186        try : 
    89             cmd.storage.close() 
     187            cmd.crashed("pykosd failed") 
    90188        except :     
    91             pass 
     189            crashed("pykosd failed") 
     190         
     191    try : 
     192        cmd.storage.close() 
     193    except :     
     194        pass 
     195         
    92196    sys.exit(retcode)