Changeset 719

Show
Ignore:
Timestamp:
02/06/03 15:28:59 (21 years ago)
Author:
jalet
Message:

edpykota should be ok, minus some typos

Location:
pykota/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/edpykota

    r717 r719  
    1717# 
    1818# $Log$ 
     19# Revision 1.4  2003/02/06 14:28:59  jalet 
     20# edpykota should be ok, minus some typos 
     21# 
    1922# Revision 1.3  2003/02/06 10:47:21  jalet 
    2023# Documentation string and command line options didn't match. 
     
    5255  -u | --users         Edit users print quotas, this is the default. 
    5356   
    54   -P | --printer p     Edit quotas on printer p only. 
     57  -P | --printer p     Edit quotas on printer p only. Actually p can 
     58                       use wildcards characters to select only 
     59                       some printers. The default value is *, meaning 
     60                       all printers. 
    5561   
    5662  -g | --groups        Edit groups print quotas instead of users. 
     
    105111Please e-mail bugs to: %s""" % (version.__version__, version.__author__) 
    106112         
    107 def main(arguments, options) : 
    108     """Edit user or group quotas.""" 
    109     pass 
    110      
     113class EdPyKota(PyKotaTool) :         
     114    """A class for edpykota.""" 
     115    def main(self, names, options) : 
     116        """Edit user or group quotas.""" 
     117        printernames = self.storage.getMatchingPrinters(options["printer"]) 
     118        if not printernames : 
     119            raise PyKotaToolError, "There's no printer matching %s" % options["printer"] 
     120        softlimit = hardlimit = None     
     121        if options["softlimit"] : 
     122            try : 
     123                softlimit = int(options["softlimit"].strip()) 
     124            except ValueError :     
     125                raise PyKotaToolError, "Invalid softlimit value %s." % options["softlimit"] 
     126        if options["hardlimit"] : 
     127            try : 
     128                hardlimit = int(options["hardlimit"].strip()) 
     129            except ValueError :     
     130                raise PyKotaToolError, "Invalid hardlimit value %s." % options["hardlimit"] 
     131        if hardlimit < softlimit :         
     132            # error, exchange them 
     133            self.logger.log_message("Hard limit %i is less than soft limit %i, values will be exchanged." % (hardlimit, softlimit), "warn") 
     134            (softlimit, hardlimit) = (hardlimit, softlimit) 
     135        for printer in printernames : 
     136            if options["prototype"] : 
     137                if options["users"] : 
     138                    prototype = self.storage.getUserPQuota(options["prototype"], printer) 
     139                else :      
     140                    prototype = self.storage.getGroupPQuota(options["prototype"], printer) 
     141                if prototype is None : 
     142                    self.logger.message("Prototype %s not found in Quota Storage for printer %s." % (options["prototype"], printer)) 
     143                    continue    # skip this printer 
     144                else :     
     145                    (softlimit, hardlimit) = (prototype["softlimit"], prototype["hardlimit"]) 
     146            if (hardlimit is None) or (softlimit is None) :         
     147                raise PyKotaToolError, "Both hard and soft limits must be set !  Aborting." 
     148            if hardlimit is None :     
     149                hardlimit = softlimit 
     150                self.logger.message("Undefined hard limit set to %i on printer %s." % (hardlimit, printer)) 
     151            if softlimit is None :     
     152                softlimit = hardlimit 
     153                self.logger.message("Undefined soft limit set to %i on printer %s." % (softlimit, printer)) 
     154            for name in names : 
     155                if options["users"] : 
     156                    quota = self.storage.getUserPQuota(name, printer) 
     157                else : 
     158                    quota = self.storage.getGroupPQuota(name, printer) 
     159                if quota is None : 
     160                    # not found 
     161                    if options["add"] : 
     162                        if options["users"] : 
     163                            self.storage.addUserPQuota(name, printer) 
     164                            quota = self.storage.getUserPQuota(name, printer) 
     165                        else : 
     166                            self.storage.addGroupPQuota(name, printer) 
     167                            quota = self.storage.getGroupPQuota(name, printer) 
     168                if quota is None :      
     169                    self.logger.log_message("Quota not found for object %s on printer %s." % (name, printer)) 
     170                else :     
     171                    if options["users"] : 
     172                        self.storage.setUserPQuota(name, printer, softlimit, hardlimit) 
     173                    else : 
     174                        self.storage.setGroupPQuota(name, printer, softlimit, hardlimit) 
     175                      
    111176if __name__ == "__main__" :  
    112177    try : 
    113         tool = PyKotaTool(doc=__doc__) 
    114          
    115178        defaults = { \ 
    116179                     "users"  : 1, \ 
    117180                     "groups" : 0, \ 
     181                     "printer" : "*", \ 
    118182                   } 
    119183        short_options = "vhaugp:P:S:H:" 
     
    121185         
    122186        # Initializes the command line tool 
    123         tool = PyKotaTool(doc=__doc__) 
     187        tool = EdPyKota(doc=__doc__) 
    124188         
    125189        # parse and checks the command line 
     
    143207        elif options["users"] and options["groups"] :     
    144208            raise PyKotaToolError, "edpykota: options --users and --groups are incompatible.\n" 
    145         elif options["groups"] : # TODO : add support for group quotas     
    146             raise PyKotaToolError, "edpykota: group quotas are currently not implemented, sorry.\n" 
     209        elif (options["softlimit"] or options["hardlimit"]) and optiions["prototype"] :     
     210            raise PyKotaToolError, "edpykota: options --softlimit or --hardlimit and --prototype are incompatible.\n" 
     211        elif options["groups"] :     
     212            raise PyKotaToolError, "edpykota: options --groups is currently not implemented.\n" 
    147213        else : 
    148             apply(main, args, options) 
     214            sys.exit(apply(tool.main, args, options)) 
    149215    except PyKotaToolError, msg :             
    150216        sys.stderr.write("%s\n" % msg) 
  • pykota/trunk/pykota/storages/sql.py

    r713 r719  
    1515# 
    1616# $Log$ 
     17# Revision 1.7  2003/02/06 14:28:59  jalet 
     18# edpykota should be ok, minus some typos 
     19# 
    1720# Revision 1.6  2003/02/06 09:19:02  jalet 
    1821# More robust behavior (hopefully) when the user or printer is not managed 
     
    4043 
    4144class SQLStorage :     
     45    def getMatchingPrinters(self, printerpattern) : 
     46        """Returns the list of all printer names which match a certain pattern.""" 
     47        printerslist = [] 
     48        # We 'could' do a SELECT printername FROM printers WHERE printername LIKE ... 
     49        # but we don't because other storages semantics may be different, so every 
     50        # storage should use fnmatch to match patterns and be storage agnostic 
     51        result = self.doQuery("SELECT printername FROM printers;") 
     52        result = self.doParseResult() 
     53        if result is not None : 
     54            for printer in result : 
     55                if fnmatch.fnmatchcase(printer["printername"], printerpattern) : 
     56                    printerslist.append(printer["printername"]) 
     57        return printerslist         
     58             
    4259    def getUserId(self, username) : 
     60        """Returns a userid given a username.""" 
    4361        result = self.doQuery("SELECT id FROM users WHERE username=%s;" % self.doQuote(username)) 
    4462        try : 
     
    4866             
    4967    def getPrinterId(self, printername) :         
     68        """Returns a printerid given a printername.""" 
    5069        result = self.doQuery("SELECT id FROM printers WHERE printername=%s;" % self.doQuote(printername)) 
    5170        try : 
     
    5574             
    5675    def getPrinterPageCounter(self, printername) : 
     76        """Returns the last page counter value for a printer given its name.""" 
    5777        result = self.doQuery("SELECT pagecounter, lastusername FROM printers WHERE printername=%s;" % self.doQuote(printername)) 
    5878        try : 
     
    6282         
    6383    def updatePrinterPageCounter(self, printername, username, pagecount) : 
     84        """Updates the last page counter information for a printer given its name, last username and pagecount.""" 
    6485        return self.doQuery("UPDATE printers SET pagecounter=%s, lastusername=%s WHERE printername=%s;" % (self.doQuote(pagecount), self.doQuote(username), self.doQuote(printername))) 
    6586         
     
    7596         
    7697    def getUPIds(self, username, printername) :     
     98        """Returns a tuple (userid, printerid) given a username and a printername.""" 
    7799        return (self.getUserId(username), self.getPrinterId(printername)) 
    78100         
    79101    def getUserPQuota(self, username, printername) : 
     102        """Returns the Print Quota information for a given (username, printername).""" 
    80103        (userid, printerid) = self.getUPIds(username, printername) 
    81104        if (userid is not None) and (printerid is not None) : 
     
    87110         
    88111    def setUserPQuota(self, username, printername, softlimit, hardlimit) : 
     112        """Sets soft and hard limits for a user quota on a specific printer given (username, printername).""" 
    89113        (userid, printerid) = self.getUPIds(username, printername) 
    90114        if (userid is not None) and (printerid is not None) : 
     
    92116         
    93117    def updateUserPQuota(self, username, printername, pagecount) : 
     118        """Updates the used user Quota information given (username, printername) and a job size in pages.""" 
    94119        (userid, printerid) = self.getUPIds(username, printername) 
    95120        if (userid is not None) and (printerid is not None) : 
     
    97122         
    98123    def buyUserPQuota(self, username, printername, pagebought) : 
     124        """Buys pages for a given (username, printername).""" 
    99125        self.updateUserPQuota(username, printername, -pagebought) 
    100126