Index: pykota/trunk/bin/pkprinters
===================================================================
--- pykota/trunk/bin/pkprinters (revision 3411)
+++ pykota/trunk/bin/pkprinters (revision 3413)
@@ -9,10 +9,10 @@
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
-# 
+#
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
@@ -47,11 +47,11 @@
   -v | --version       Prints pkprinters's version number then exits.
   -h | --help          Prints this message then exits.
-  
-  -a | --add           Adds printers if they don't exist on the Quota 
+
+  -a | --add           Adds printers if they don't exist on the Quota
                        Storage Server. If they exist, they are modified
                        unless -s|--skipexisting is also used.
-                       
+
   -d | --delete        Deletes printers from the quota storage.
-  
+
   -D | --description d Adds a textual description to printers.
 
@@ -62,11 +62,11 @@
                        If both are to be set, separate them with a comma.
                        Floating point and negative values are allowed.
-  
-  -g | --groups pg1[,pg2...] Adds or Remove the printer(s) to the printer 
+
+  -g | --groups pg1[,pg2...] Adds or Remove the printer(s) to the printer
                        groups pg1, pg2, etc... which must already exist.
                        A printer group is just like a normal printer,
                        only that it is usually unknown from the printing
                        system. Create printer groups exactly the same
-                       way that you create printers, then add other 
+                       way that you create printers, then add other
                        printers to them with this option.
                        Accounting is done on a printer and on all
@@ -74,35 +74,35 @@
                        is done on a printer and on all the printer groups
                        it belongs to.
-                       If the --remove option below is not used, the 
+                       If the --remove option below is not used, the
                        default action is to add printers to the specified
                        printer groups.
-                       
+
   -l | --list          List informations about the printer(s) and the
                        printers groups it is a member of.
-                       
-  -r | --remove        In combination with the --groups option above,                       
+
+  -r | --remove        In combination with the --groups option above,
                        remove printers from the specified printers groups.
-                       
+
   -s | --skipexisting  In combination with the --add option above, tells
                        pkprinters to not modify existing printers.
-                       
+
   -m | --maxjobsize s  Sets the maximum job size allowed on the printer
                        to s pages.
-                       
+
   -p | --passthrough   Activate passthrough mode for the printer. In this
                        mode, users are allowed to print without any impact
                        on their quota or account balance.
-                       
+
   -n | --nopassthrough Deactivate passthrough mode for the printer.
-                       Without -p or -n, printers are created in 
+                       Without -p or -n, printers are created in
                        normal mode, i.e. no passthrough.
-  
-  printer1 through printerN can contain wildcards if the --add option 
+
+  printer1 through printerN can contain wildcards if the --add option
   is not set.
-  
-examples :                              
+
+examples :
 
   $ pkprinters --add -D "HP Printer" --charge 0.05,0.1 hp2100 hp2200 hp8000
-  
+
   Will create three printers named hp2100, hp2200 and hp8000.
   Their price per page will be set at 0.05 unit, and their price
@@ -110,37 +110,37 @@
   or whatever you want them to mean.
   All of their descriptions will be set to the string "HP Printer".
-  If any of these printers already exists, it will also be modified 
+  If any of these printers already exists, it will also be modified
   unless the -s|--skipexisting command line option is also used.
-            
+
   $ pkprinters --delete "*"
-  
+
   This will completely delete all printers and associated quota information,
   as well as their job history. USE WITH CARE !
-  
+
   $ pkprinters --groups Laser,HP "hp*"
-  
-  This will put all printers which name matches "hp*" into printers groups 
+
+  This will put all printers which name matches "hp*" into printers groups
   Laser and HP, which MUST already exist.
-  
+
   $ pkprinters --groups LexMark --remove hp2200
-  
+
   This will remove the hp2200 printer from the LexMark printer group.
 """)
-        
-class PKPrinters(PyKotaTool) :        
+
+class PKPrinters(PyKotaTool) :
     """A class for a printers manager."""
     def modifyPrinter(self, printer, charges, perpage, perjob, description, passthrough, nopassthrough, maxjobsize) :
         if charges :
-            printer.setPrices(perpage, perjob)    
+            printer.setPrices(perpage, perjob)
         if description is not None :        # NB : "" is allowed !
             printer.setDescription(description)
-        if nopassthrough :    
+        if nopassthrough :
             printer.setPassThrough(False)
-        if passthrough :    
+        if passthrough :
             printer.setPassThrough(True)
         if maxjobsize is not None :
             printer.setMaxJobSize(maxjobsize)
-            
-    def managePrintersGroups(self, pgroups, printer, remove) :        
+
+    def managePrintersGroups(self, pgroups, printer, remove) :
         """Manage printer group membership."""
         for pgroup in pgroups :
@@ -148,7 +148,7 @@
                 pgroup.delPrinterFromGroup(printer)
             else :
-                pgroup.addPrinterToGroup(printer)    
-                
-    def getPrinterDeviceURI(self, printername) :            
+                pgroup.addPrinterToGroup(printer)
+
+    def getPrinterDeviceURI(self, printername) :
         """Returns the Device URI attribute for a particular printer."""
         if not printername :
@@ -159,16 +159,16 @@
         try :
             return cups.doRequest(req).printer["device-uri"][0][1]
-        except :    
+        except :
             self.printInfo(_("Impossible to retrieve %(printername)s's DeviceURI") % locals(), "warn")
             return ""
-        
+
     def isPrinterCaptured(self, printername=None, deviceuri=None) :
         """Returns True if the printer is already redirected through PyKota's backend, else False."""
         if (deviceuri or self.getPrinterDeviceURI(printername)).find("cupspykota:") != -1 :
             return True
-        else :    
+        else :
             return False
-        
-    def reroutePrinterThroughPyKota(self, printer) :    
+
+    def reroutePrinterThroughPyKota(self, printer) :
         """Reroutes a CUPS printer through PyKota."""
         uri = self.getPrinterDeviceURI(printer.Name)
@@ -177,6 +177,6 @@
              os.system('lpadmin -p "%s" -v "%s"' % (printer.Name, newuri))
              self.logdebug("Printer %s rerouted to %s" % (printer.Name, newuri))
-             
-    def deroutePrinterFromPyKota(self, printer) :    
+
+    def deroutePrinterFromPyKota(self, printer) :
         """Deroutes a PyKota printer through CUPS only."""
         uri = self.getPrinterDeviceURI(printer.Name)
@@ -187,15 +187,15 @@
              os.system('lpadmin -p "%s" -v "%s"' % (printer.Name, newuri))
              self.logdebug("Printer %s rerouted to %s" % (printer.Name, newuri))
-                                     
+
     def main(self, names, options) :
         """Manage printers."""
         if not options["list"] :
             self.adminOnly()
-            
+
         docups = options["cups"]
-        
-        if not options["list"] :    
+
+        if not options["list"] :
             percent = Percent(self)
-            
+
         if not options["add"] :
             if not options["list"] :
@@ -208,7 +208,7 @@
                     percent.display("\n")
                 raise PyKotaCommandLineError, _("There's no printer matching %s") % " ".join(names)
-            if not options["list"] :    
+            if not options["list"] :
                 percent.setSize(len(printers))
-                
+
         if options["list"] :
             for printer in printers :
@@ -220,8 +220,8 @@
                 print "    %s" % (_("Maximum job size : %s") % ((printer.MaxJobSize and (_("%s pages") % printer.MaxJobSize)) or _("Unlimited")))
                 print "    %s" % (_("Routed through PyKota : %s") % ((self.isPrinterCaptured(printer.Name) and _("YES")) or _("NO")))
-                if parents : 
+                if parents :
                     print "    %s %s" % (_("in"), parents)
-                print    
-        elif options["delete"] :    
+                print
+        elif options["delete"] :
             percent.display("\n%s..." % _("Deletion"))
             self.storage.deleteManyPrinters(printers)
@@ -233,17 +233,17 @@
                     percent.oneMore()
         else :
-            if options["groups"] :        
+            if options["groups"] :
                 printersgroups = self.storage.getMatchingPrinters(options["groups"])
                 if not printersgroups :
                     raise PyKotaCommandLineError, _("There's no printer matching %s") % " ".join(options["groups"].split(','))
-            else :         
+            else :
                 printersgroups = []
-                    
+
             if options["charge"] :
                 try :
                     charges = [float(part) for part in options["charge"].split(',', 1)]
-                except ValueError :    
+                except ValueError :
                     raise PyKotaCommandLineError, _("Invalid charge amount value %s") % options["charge"]
-                else :    
+                else :
                     if len(charges) > 2 :
                         charges = charges[:2]
@@ -251,22 +251,22 @@
                         charges = [charges[0], None]
                     (perpage, perjob) = charges
-            else :        
+            else :
                 charges = perpage = perjob = None
-                    
-            if options["maxjobsize"] :        
+
+            if options["maxjobsize"] :
                 try :
                     maxjobsize = int(options["maxjobsize"])
                     if maxjobsize < 0 :
                         raise ValueError
-                except ValueError :    
+                except ValueError :
                     raise PyKotaCommandLineError, _("Invalid maximum job size value %s") % options["maxjobsize"]
-            else :        
+            else :
                 maxjobsize = None
-                    
+
             description = options["description"]
             if description :
                 description = description.strip()
-                
-            nopassthrough = options["nopassthrough"]    
+
+            nopassthrough = options["nopassthrough"]
             passthrough = options["passthrough"]
             remove = options["remove"]
@@ -274,5 +274,5 @@
             self.storage.beginTransaction()
             try :
-                if options["add"] :    
+                if options["add"] :
                     percent.display("%s...\n" % _("Creation"))
                     percent.setSize(len(names))
@@ -283,13 +283,13 @@
                                            description, passthrough, \
                                            nopassthrough, maxjobsize)
-                            oldprinter = self.storage.addPrinter(printer)               
-                            
+                            oldprinter = self.storage.addPrinter(printer)
+
                             if docups :
                                  self.reroutePrinterThroughPyKota(printer)
-                                     
+
                             if oldprinter is not None :
                                 if skipexisting :
                                     self.logdebug(_("Printer %s already exists, skipping.") % pname)
-                                else :    
+                                else :
                                     self.logdebug(_("Printer %s already exists, will be modified.") % pname)
                                     self.modifyPrinter(oldprinter, charges, \
@@ -297,34 +297,34 @@
                                                passthrough, nopassthrough, \
                                                maxjobsize)
-                                    oldprinter.save()           
+                                    oldprinter.save()
                                     self.managePrintersGroups(printersgroups, oldprinter, remove)
-                            elif printersgroups :        
+                            elif printersgroups :
                                 self.managePrintersGroups(printersgroups, \
                                                           self.storage.getPrinter(pname), \
                                                           remove)
-                        else :    
+                        else :
                             raise PyKotaCommandLineError, _("Invalid printer name %s") % pname
                         percent.oneMore()
-                else :        
+                else :
                     percent.display("\n%s...\n" % _("Modification"))
-                    for printer in printers :        
+                    for printer in printers :
                         self.modifyPrinter(printer, charges, perpage, perjob, \
                                            description, passthrough, \
                                            nopassthrough, maxjobsize)
-                        printer.save()    
+                        printer.save()
                         self.managePrintersGroups(printersgroups, printer, remove)
                         if docups :
                             self.reroutePrinterThroughPyKota(printer)
                         percent.oneMore()
-            except :                    
+            except :
                 self.storage.rollbackTransaction()
                 raise
-            else :    
+            else :
                 self.storage.commitTransaction()
-                
+
         if not options["list"] :
             percent.done()
-                     
-if __name__ == "__main__" : 
+
+if __name__ == "__main__" :
     retcode = 0
     try :
@@ -334,12 +334,12 @@
                         "skipexisting", "passthrough", "nopassthrough", \
                         "maxjobsize="]
-        
+
         # Initializes the command line tool
         manager = PKPrinters(doc=__doc__)
         manager.deferredInit()
-        
+
         # parse and checks the command line
         (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options)
-        
+
         # sets long options
         options["help"] = options["h"] or options["help"]
@@ -349,5 +349,5 @@
         options["charge"] = options["c"] or options["charge"]
         options["description"] = options["D"] or options["description"]
-        options["delete"] = options["d"] or options["delete"] 
+        options["delete"] = options["d"] or options["delete"]
         options["groups"] = options["g"] or options["groups"]
         options["list"] = options["l"] or options["list"]
@@ -357,5 +357,5 @@
         options["passthrough"] = options["p"] or options["passthrough"]
         options["nopassthrough"] = options["n"] or options["nopassthrough"]
-        
+
         if options["help"] :
             manager.display_usage_and_quit()
@@ -374,16 +374,16 @@
         else :
             retcode = manager.main(args, options)
-    except KeyboardInterrupt :        
+    except KeyboardInterrupt :
         logerr("\nInterrupted with Ctrl+C !\n")
         retcode = -3
-    except PyKotaCommandLineError, msg :    
+    except PyKotaCommandLineError, msg :
         logerr("%s : %s\n" % (sys.argv[0], msg))
         retcode = -2
-    except SystemExit :        
+    except SystemExit :
         pass
     except :
         try :
             manager.crashed("pkprinters failed")
-        except :    
+        except :
             crashed("pkprinters failed")
         retcode = -1
@@ -391,6 +391,6 @@
     try :
         manager.storage.close()
-    except (TypeError, NameError, AttributeError) :    
+    except (TypeError, NameError, AttributeError) :
         pass
-        
-    sys.exit(retcode)    
+
+    sys.exit(retcode)
