Changeset 728

Show
Ignore:
Timestamp:
02/06/03 23:54:33 (21 years ago)
Author:
jalet
Message:

warnpykota should be ok

Location:
pykota/trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/pykota

    r704 r728  
    1717# 
    1818# $Log$ 
     19# Revision 1.6  2003/02/06 22:54:33  jalet 
     20# warnpykota should be ok 
     21# 
    1922# Revision 1.5  2003/02/05 22:45:25  jalet 
    2023# Forgotten import 
     
    115118         
    116119        # Is the current user allowed to print at all ? 
    117         action = tool.warnQuotaPrinter(username) 
     120        action = tool.warnUserPQuota(username) 
    118121        if action == "DENY" : 
    119122            # No, just die cleanly 
     
    127130        if jobsize >= 0: 
    128131            tool.storage.updateUserPQuota(lastusername, tool.printername, jobsize) 
    129             tool.warnQuotaPrinter(lastusername) 
     132            tool.warnUserPQuota(lastusername) 
    130133        else :     
    131134            tool.logger.log_message("Error in page count value %i for user %s on printer %s" % (jobsize, tool.printername, lastusername), "error") 
  • pykota/trunk/bin/warnpykota

    r696 r728  
    11#! /usr/bin/env python 
    22 
    3 # PyKota Print Quota Warner  
     3# PyKota Print Quota Warning sender 
    44# 
    55# PyKota - Print Quotas for CUPS 
     
    1717# 
    1818# $Log$ 
    19 # Revision 1.1  2003/02/05 21:41:09  jalet 
    20 # Skeletons added for all command line tools 
     19# Revision 1.2  2003/02/06 22:54:33  jalet 
     20# warnpykota should be ok 
    2121# 
    2222# 
    2323# 
    2424 
     25import sys 
     26 
     27from pykota import version 
     28from pykota.tool import PyKotaTool, PyKotaToolError 
     29 
     30__doc__ = """warnpykota v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres 
     31send mail to users over print quota 
     32 
     33command line usage : 
     34 
     35  warnpykota [options] user1 user2 ... userN 
     36  warnpykota [options] group1 group2 ... groupN 
     37 
     38options : 
     39 
     40  -v | --version       Prints warnpykota's version number then exits. 
     41  -h | --help          Prints this message then exits. 
     42   
     43  -u | --users         Send mail to users over print quota, this is  
     44                       the default. 
     45   
     46  -g | --groups        Send mail to group administrators instead of users. 
     47   
     48  -P | --printer p     Verify quotas on this printer only. Actually p can 
     49                       use wildcards characters to select only 
     50                       some printers. The default value is *, meaning 
     51                       all printers. 
     52   
     53examples :                               
     54 
     55  $ warnpykota --printer lp 
     56   
     57  This will warn all users of the lp printer who have exceeded their 
     58  print quota. 
     59 
     60  $ warnpykota  
     61   
     62  This will warn all users  who have exceeded their print quota on 
     63  any printer. 
     64 
     65  $ warnpykota --groups --printer "laserjet*" 
     66   
     67  This will warn all group administrators of groups which have exceeded  
     68  their print quota on any printer which name begins with "laserjet" 
     69 
     70This program is free software; you can redistribute it and/or modify 
     71it under the terms of the GNU General Public License as published by 
     72the Free Software Foundation; either version 2 of the License, or 
     73(at your option) any later version. 
     74 
     75This program is distributed in the hope that it will be useful, 
     76but WITHOUT ANY WARRANTY; without even the implied warranty of 
     77MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     78GNU General Public License for more details. 
     79 
     80You should have received a copy of the GNU General Public License 
     81along with this program; if not, write to the Free Software 
     82Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 
     83 
     84Please e-mail bugs to: %s""" % (version.__version__, version.__author__) 
     85         
     86class WarnPyKota(PyKotaTool) :         
     87    """A class for warnpykota.""" 
     88    def main(self, names, options) : 
     89        """Warn users or groups over print quota.""" 
     90        printernames = self.storage.getMatchingPrinters(options["printer"]) 
     91        if not printernames : 
     92            raise PyKotaToolError, "There's no printer matching %s" % options["printer"] 
     93        for printer in printernames : 
     94            for name in names : 
     95                if options["users"] : 
     96                    self.warnUserPQuota(name, printer) 
     97                else : 
     98                    self.warnGroupPQuota(name, printer) 
     99                      
     100if __name__ == "__main__" :  
     101    try : 
     102        defaults = { \ 
     103                     "users"  : 1, \ 
     104                     "groups" : 0, \ 
     105                     "printer" : "*", \ 
     106                   } 
     107        short_options = "vhugP:" 
     108        long_options = ["help", "version", "users", "groups", "printer="] 
     109         
     110        # Initializes the command line tool 
     111        sender = WarnPyKota(doc=__doc__) 
     112         
     113        # parse and checks the command line 
     114        (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options) 
     115         
     116        # sets long options 
     117        options["help"] = options["h"] or options["help"] 
     118        options["version"] = options["v"] or options["version"] 
     119        options["users"] = options["u"] or options["users"] or defaults["users"] 
     120        options["groups"] = options["g"] or options["groups"] or defaults["groups"] 
     121        options["printer"] = options["P"] or options["printer"] or defaults["printer"] 
     122         
     123        if options["help"] : 
     124            sender.display_usage_and_quit() 
     125        elif options["version"] : 
     126            sender.display_version_and_quit() 
     127        elif options["users"] and options["groups"] :     
     128            raise PyKotaToolError, "warnpykota: options --users and --groups are incompatible." 
     129        elif options["groups"] :     
     130            raise PyKotaToolError, "warnpykota: options --groups is currently not implemented." 
     131        else : 
     132            sys.exit(sender.main(args, options)) 
     133    except PyKotaToolError, msg :             
     134        sys.stderr.write("%s\n" % msg) 
     135        sys.stderr.flush() 
     136        sys.exit(-1) 
     137 
  • pykota/trunk/pykota/tool.py

    r722 r728  
    1515# 
    1616# $Log$ 
     17# Revision 1.11  2003/02/06 22:54:33  jalet 
     18# warnpykota should be ok 
     19# 
    1720# Revision 1.10  2003/02/06 15:03:11  jalet 
    1821# added a method to set the limit date 
     
    209212            return (action, (hardlimit - pagecounter), datelimit) 
    210213     
    211     def warnQuotaPrinter(self, username) : 
     214    def warnGroupPQuota(self, username, printername=None) : 
    212215        """Checks a user quota and send him a message if quota is exceeded on current printer.""" 
    213         (action, grace, gracedate) = self.checkUserPQuota(username, self.printername) 
     216        pname = printername or self.printername 
     217        raise PyKotaToolError, "Group quotas are currently not implemented." 
     218         
     219    def warnUserPQuota(self, username, printername=None) : 
     220        """Checks a user quota and send him a message if quota is exceeded on current printer.""" 
     221        pname = printername or self.printername 
     222        (action, grace, gracedate) = self.checkUserPQuota(username, pname) 
    214223        if action == "DENY" : 
    215224            if (grace is not None) and (gracedate is not None) : 
    216225                # only when both user and printer are known 
    217                 adminmessage = "Print Quota exceeded for user %s on printer %s" % (username, self.printername) 
     226                adminmessage = "Print Quota exceeded for user %s on printer %s" % (username, pname) 
    218227                self.logger.log_message(adminmessage) 
    219                 self.sendMessageToUser(username, "Print Quota Exceeded", "You are not allowed to print anymore because\nyour Print Quota is exceeded on printer %s." % self.printername) 
     228                self.sendMessageToUser(username, "Print Quota Exceeded", "You are not allowed to print anymore because\nyour Print Quota is exceeded on printer %s." % pname) 
    220229                self.sendMessageToAdmin("Print Quota", adminmessage) 
    221230        elif action == "WARN" :     
    222             adminmessage = "Print Quota soft limit exceeded for user %s on printer %s" % (username, self.printername) 
     231            adminmessage = "Print Quota soft limit exceeded for user %s on printer %s" % (username, pname) 
    223232            self.logger.log_message(adminmessage) 
    224             self.sendMessageToUser(username, "Print Quota Exceeded", "You will soon be forbidden to print anymore because\nyour Print Quota is almost reached on printer %s." % self.printername) 
     233            self.sendMessageToUser(username, "Print Quota Exceeded", "You will soon be forbidden to print anymore because\nyour Print Quota is almost reached on printer %s." % pname) 
    225234            self.sendMessageToAdmin("Print Quota", adminmessage) 
    226235        return action