Index: /pykota/trunk/pykota/storages/sql.py
===================================================================
--- /pykota/trunk/pykota/storages/sql.py (revision 713)
+++ /pykota/trunk/pykota/storages/sql.py (revision 719)
@@ -15,4 +15,7 @@
 #
 # $Log$
+# Revision 1.7  2003/02/06 14:28:59  jalet
+# edpykota should be ok, minus some typos
+#
 # Revision 1.6  2003/02/06 09:19:02  jalet
 # More robust behavior (hopefully) when the user or printer is not managed
@@ -40,5 +43,20 @@
 
 class SQLStorage :    
+    def getMatchingPrinters(self, printerpattern) :
+        """Returns the list of all printer names which match a certain pattern."""
+        printerslist = []
+        # We 'could' do a SELECT printername FROM printers WHERE printername LIKE ...
+        # but we don't because other storages semantics may be different, so every
+        # storage should use fnmatch to match patterns and be storage agnostic
+        result = self.doQuery("SELECT printername FROM printers;")
+        result = self.doParseResult()
+        if result is not None :
+            for printer in result :
+                if fnmatch.fnmatchcase(printer["printername"], printerpattern) :
+                    printerslist.append(printer["printername"])
+        return printerslist        
+            
     def getUserId(self, username) :
+        """Returns a userid given a username."""
         result = self.doQuery("SELECT id FROM users WHERE username=%s;" % self.doQuote(username))
         try :
@@ -48,4 +66,5 @@
             
     def getPrinterId(self, printername) :        
+        """Returns a printerid given a printername."""
         result = self.doQuery("SELECT id FROM printers WHERE printername=%s;" % self.doQuote(printername))
         try :
@@ -55,4 +74,5 @@
             
     def getPrinterPageCounter(self, printername) :
+        """Returns the last page counter value for a printer given its name."""
         result = self.doQuery("SELECT pagecounter, lastusername FROM printers WHERE printername=%s;" % self.doQuote(printername))
         try :
@@ -62,4 +82,5 @@
         
     def updatePrinterPageCounter(self, printername, username, pagecount) :
+        """Updates the last page counter information for a printer given its name, last username and pagecount."""
         return self.doQuery("UPDATE printers SET pagecounter=%s, lastusername=%s WHERE printername=%s;" % (self.doQuote(pagecount), self.doQuote(username), self.doQuote(printername)))
         
@@ -75,7 +96,9 @@
         
     def getUPIds(self, username, printername) :    
+        """Returns a tuple (userid, printerid) given a username and a printername."""
         return (self.getUserId(username), self.getPrinterId(printername))
         
     def getUserPQuota(self, username, printername) :
+        """Returns the Print Quota information for a given (username, printername)."""
         (userid, printerid) = self.getUPIds(username, printername)
         if (userid is not None) and (printerid is not None) :
@@ -87,4 +110,5 @@
         
     def setUserPQuota(self, username, printername, softlimit, hardlimit) :
+        """Sets soft and hard limits for a user quota on a specific printer given (username, printername)."""
         (userid, printerid) = self.getUPIds(username, printername)
         if (userid is not None) and (printerid is not None) :
@@ -92,4 +116,5 @@
         
     def updateUserPQuota(self, username, printername, pagecount) :
+        """Updates the used user Quota information given (username, printername) and a job size in pages."""
         (userid, printerid) = self.getUPIds(username, printername)
         if (userid is not None) and (printerid is not None) :
@@ -97,4 +122,5 @@
         
     def buyUserPQuota(self, username, printername, pagebought) :
+        """Buys pages for a given (username, printername)."""
         self.updateUserPQuota(username, printername, -pagebought)
         
Index: /pykota/trunk/bin/edpykota
===================================================================
--- /pykota/trunk/bin/edpykota (revision 717)
+++ /pykota/trunk/bin/edpykota (revision 719)
@@ -17,4 +17,7 @@
 #
 # $Log$
+# Revision 1.4  2003/02/06 14:28:59  jalet
+# edpykota should be ok, minus some typos
+#
 # Revision 1.3  2003/02/06 10:47:21  jalet
 # Documentation string and command line options didn't match.
@@ -52,5 +55,8 @@
   -u | --users         Edit users print quotas, this is the default.
   
-  -P | --printer p     Edit quotas on printer p only.
+  -P | --printer p     Edit quotas on printer p only. Actually p can
+                       use wildcards characters to select only
+                       some printers. The default value is *, meaning
+                       all printers.
   
   -g | --groups        Edit groups print quotas instead of users.
@@ -105,15 +111,73 @@
 Please e-mail bugs to: %s""" % (version.__version__, version.__author__)
         
-def main(arguments, options) :
-    """Edit user or group quotas."""
-    pass
-    
+class EdPyKota(PyKotaTool) :        
+    """A class for edpykota."""
+    def main(self, names, options) :
+        """Edit user or group quotas."""
+        printernames = self.storage.getMatchingPrinters(options["printer"])
+        if not printernames :
+            raise PyKotaToolError, "There's no printer matching %s" % options["printer"]
+        softlimit = hardlimit = None    
+        if options["softlimit"] :
+            try :
+                softlimit = int(options["softlimit"].strip())
+            except ValueError :    
+                raise PyKotaToolError, "Invalid softlimit value %s." % options["softlimit"]
+        if options["hardlimit"] :
+            try :
+                hardlimit = int(options["hardlimit"].strip())
+            except ValueError :    
+                raise PyKotaToolError, "Invalid hardlimit value %s." % options["hardlimit"]
+        if hardlimit < softlimit :        
+            # error, exchange them
+            self.logger.log_message("Hard limit %i is less than soft limit %i, values will be exchanged." % (hardlimit, softlimit), "warn")
+            (softlimit, hardlimit) = (hardlimit, softlimit)
+        for printer in printernames :
+            if options["prototype"] :
+                if options["users"] :
+                    prototype = self.storage.getUserPQuota(options["prototype"], printer)
+                else :     
+                    prototype = self.storage.getGroupPQuota(options["prototype"], printer)
+                if prototype is None :
+                    self.logger.message("Prototype %s not found in Quota Storage for printer %s." % (options["prototype"], printer))
+                    continue    # skip this printer
+                else :    
+                    (softlimit, hardlimit) = (prototype["softlimit"], prototype["hardlimit"])
+            if (hardlimit is None) or (softlimit is None) :        
+                raise PyKotaToolError, "Both hard and soft limits must be set !  Aborting."
+            if hardlimit is None :    
+                hardlimit = softlimit
+                self.logger.message("Undefined hard limit set to %i on printer %s." % (hardlimit, printer))
+            if softlimit is None :    
+                softlimit = hardlimit
+                self.logger.message("Undefined soft limit set to %i on printer %s." % (softlimit, printer))
+            for name in names :
+                if options["users"] :
+                    quota = self.storage.getUserPQuota(name, printer)
+                else :
+                    quota = self.storage.getGroupPQuota(name, printer)
+                if quota is None :
+                    # not found
+                    if options["add"] :
+                        if options["users"] :
+                            self.storage.addUserPQuota(name, printer)
+                            quota = self.storage.getUserPQuota(name, printer)
+                        else :
+                            self.storage.addGroupPQuota(name, printer)
+                            quota = self.storage.getGroupPQuota(name, printer)
+                if quota is None :     
+                    self.logger.log_message("Quota not found for object %s on printer %s." % (name, printer))
+                else :    
+                    if options["users"] :
+                        self.storage.setUserPQuota(name, printer, softlimit, hardlimit)
+                    else :
+                        self.storage.setGroupPQuota(name, printer, softlimit, hardlimit)
+                     
 if __name__ == "__main__" : 
     try :
-        tool = PyKotaTool(doc=__doc__)
-        
         defaults = { \
                      "users"  : 1, \
                      "groups" : 0, \
+                     "printer" : "*", \
                    }
         short_options = "vhaugp:P:S:H:"
@@ -121,5 +185,5 @@
         
         # Initializes the command line tool
-        tool = PyKotaTool(doc=__doc__)
+        tool = EdPyKota(doc=__doc__)
         
         # parse and checks the command line
@@ -143,8 +207,10 @@
         elif options["users"] and options["groups"] :    
             raise PyKotaToolError, "edpykota: options --users and --groups are incompatible.\n"
-        elif options["groups"] : # TODO : add support for group quotas    
-            raise PyKotaToolError, "edpykota: group quotas are currently not implemented, sorry.\n"
+        elif (options["softlimit"] or options["hardlimit"]) and optiions["prototype"] :    
+            raise PyKotaToolError, "edpykota: options --softlimit or --hardlimit and --prototype are incompatible.\n"
+        elif options["groups"] :    
+            raise PyKotaToolError, "edpykota: options --groups is currently not implemented.\n"
         else :
-            apply(main, args, options)
+            sys.exit(apply(tool.main, args, options))
     except PyKotaToolError, msg :            
         sys.stderr.write("%s\n" % msg)
