root / pykota / trunk / bin / pykosd @ 1597

Revision 1597, 3.3 kB (checked in by jalet, 20 years ago)

Formatting improvements

  • 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.3  2004/07/07 21:44:15  jalet
27# Formatting improvements
28#
29# Revision 1.2  2004/07/07 14:14:31  jalet
30# Now handles limits by quota in addition to limits by balance
31#
32# Revision 1.1  2004/07/07 13:21:27  jalet
33# Introduction of the pykosd command
34#
35#
36#
37
38import sys
39import os
40import pwd
41import time
42import pyosd
43
44from pykota.tool import PyKotaTool, PyKotaToolError, crashed
45
46DURATION = 5
47
48if __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)
59            if not user.Exists :
60                raise PyKotaToolError, "User %s doesn't exist in PyKota's database." % uname
61            if user.LimitBy == "quota" :   
62                printers = cmd.storage.getMatchingPrinters("*")
63                upquotas = [ cmd.storage.getUserPQuota(user, p) for p in printers ]
64                nblines = len(upquotas)
65                display = pyosd.osd(colour="#00FF00", timeout=DURATION, shadow=2, lines=nblines)
66                for line in range(nblines) :
67                    upq = upquotas[line]
68                    if upq.HardLimit is None :
69                        if upq.SoftLimit is None :
70                            percent = "%s" % upq.PageCounter
71                        else :       
72                            percent = "%s%%" % min((upq.PageCounter * 100) / upq.SoftLimit, 100)
73                    else :       
74                        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)
76            else :
77                if user.AccountBalance <= 0 :
78                    color = "#FF0000"
79                else :   
80                    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
88        try :
89            cmd.storage.close()
90        except :   
91            pass
92    sys.exit(retcode)
Note: See TracBrowser for help on using the browser.