Index: pykota/trunk/pykota/tool.py
===================================================================
--- pykota/trunk/pykota/tool.py (revision 3411)
+++ pykota/trunk/pykota/tool.py (revision 3413)
@@ -8,10 +8,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/>.
@@ -53,6 +53,6 @@
         self.previous = None
         self.before = time.time()
-        
-    def setSize(self, size) :     
+
+    def setSize(self, size) :
         """Sets the total size."""
         self.number = 0
@@ -60,10 +60,10 @@
         if size :
             self.factor = 100.0 / float(size)
-        
-    def display(self, msg) :    
+
+    def display(self, msg) :
         """Displays the value."""
         self.app.display(msg)
-        
-    def oneMore(self) :    
+
+    def oneMore(self) :
         """Increments internal counter."""
         if self.size :
@@ -73,6 +73,6 @@
                 self.display("\r%s%%" % percent)
                 self.previous = percent
-            
-    def done(self) :         
+
+    def done(self) :
         """Displays the 'done' message."""
         after = time.time()
@@ -80,11 +80,11 @@
             try :
                 speed = self.size / ((after - self.before) + 0.00000000001) # adds an epsilon to avoid an user's problem I can't reproduce...
-            except ZeroDivisionError :    
+            except ZeroDivisionError :
                 speed = 1 # Fake value in case of division by zero, shouldn't happen anyway with the epsilon above...
             self.display("\r100.00%%\r        \r%s. %s : %.2f %s.\n" \
                      % (_("Done"), _("Average speed"), speed, _("entries per second")))
-        else :             
+        else :
             self.display("\r100.00%%\r        \r%s.\n" % _("Done"))
-        
+
 class Tool :
     """Base class for tools with no database access."""
@@ -93,5 +93,5 @@
         self.debug = True # in case of early failure
         self.logger = logger.openLogger("stderr")
-        
+
         # Saves a copy of the locale settings
         (self.language, self.charset) = locale.getlocale()
@@ -100,17 +100,17 @@
         if not self.charset :
             self.charset = "UTF-8"
-        
+
         # pykota specific stuff
         self.documentation = doc
-        
+
         # Extract the effective username
         uid = os.geteuid()
         try :
             self.effectiveUserName = pwd.getpwuid(uid)[0]
-        except (KeyError, IndexError), msg :    
+        except (KeyError, IndexError), msg :
             self.printInfo(_("Strange problem with uid(%s) : %s") % (uid, msg), "warn")
             self.effectiveUserName = os.getlogin()
-        
-    def deferredInit(self) :        
+
+    def deferredInit(self) :
         """Deferred initialization."""
         confdir = os.environ.get("PYKOTA_HOME")
@@ -123,9 +123,9 @@
                 self.pykotauser = pwd.getpwnam("pykota")
                 confdir = self.pykotauser[5]
-            except KeyError :    
+            except KeyError :
                 self.pykotauser = None
                 confdir = "/etc/pykota"
                 missingUser = True
-            
+
         self.config = config.PyKotaConfig(confdir)
         self.debug = self.config.getDebug()
@@ -133,21 +133,21 @@
         self.maildomain = self.config.getMailDomain()
         self.logger = logger.openLogger(self.config.getLoggingBackend())
-            
-        # TODO : We NEED this here, even when not in an accounting filter/backend    
+
+        # TODO : We NEED this here, even when not in an accounting filter/backend
         self.softwareJobSize = 0
         self.softwareJobPrice = 0.0
-        
+
         if environHome :
             self.printInfo("PYKOTA_HOME environment variable is set. Configuration files were searched in %s" % confdir, "info")
         else :
-            if missingUser :     
+            if missingUser :
                 self.printInfo("The 'pykota' system account is missing. Configuration files were searched in %s instead." % confdir, "warn")
-        
+
         self.logdebug("Language in use : %s" % self.language)
         self.logdebug("Charset in use : %s" % self.charset)
-        
+
         arguments = " ".join(['"%s"' % arg for arg in sys.argv])
         self.logdebug("Command line arguments : %s" % arguments)
-        
+
     def display(self, message) :
         """Display a message but only if stdout is a tty."""
@@ -156,6 +156,6 @@
                                             "replace"))
             sys.stdout.flush()
-            
-    def logdebug(self, message) :    
+
+    def logdebug(self, message) :
         """Logs something to debug output if debug is enabled."""
         if self.debug :
@@ -163,6 +163,6 @@
                                                    "replace"), \
                                     "debug")
-            
-    def printInfo(self, message, level="info") :        
+
+    def printInfo(self, message, level="info") :
         """Sends a message to standard error."""
         sys.stderr.write("%s: %s\n" % (level.upper(), \
@@ -170,20 +170,20 @@
                                                       "replace")))
         sys.stderr.flush()
-        
+
     def adminOnly(self, restricted=True) :
         """Raises an exception if the user is not a PyKota administrator."""
         if restricted and not self.config.isAdmin :
             raise PyKotaCommandLineError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command."))
-            
+
     def matchString(self, s, patterns) :
         """Returns True if the string s matches one of the patterns, else False."""
         if not patterns :
             return True # No pattern, always matches.
-        else :    
+        else :
             for pattern in patterns :
                 if fnmatch.fnmatchcase(s, pattern) :
                     return True
             return False
-        
+
     def sanitizeNames(self, options, names) :
         """Ensures that an user can only see the datas he is allowed to see, by modifying the list of names."""
@@ -198,23 +198,23 @@
                         return [ g.Name for g in self.storage.getUserGroups(user) ]
                 return [ username ]
-        elif not names :        
+        elif not names :
             return ["*"]
-        else :    
+        else :
             return names
-        
+
     def display_version_and_quit(self) :
         """Displays version number, then exists successfully."""
         try :
             self.clean()
-        except AttributeError :    
+        except AttributeError :
             pass
         print __version__
         sys.exit(0)
-    
+
     def display_usage_and_quit(self) :
         """Displays command line usage, then exists successfully."""
         try :
             self.clean()
-        except AttributeError :    
+        except AttributeError :
             pass
         print _(self.documentation) % globals()
@@ -223,6 +223,6 @@
         print _("Please report bugs to :"), __author__
         sys.exit(0)
-        
-    def crashed(self, message="Bug in PyKota") :    
+
+    def crashed(self, message="Bug in PyKota") :
         """Outputs a crash message, and optionally sends it to software author."""
         msg = utils.crashed(message)
@@ -248,6 +248,6 @@
             self.printInfo("PyKota double crash !", "error")
             raise
-        return fullmessage    
-        
+        return fullmessage
+
     def parseCommandline(self, argv, short, long, allownothing=0) :
         """Parses the command line, controlling options."""
@@ -269,5 +269,5 @@
                 withoutarg.append(short[i])
             i = ii
-                
+
         for option in long :
             if option[-1] == '=' :
@@ -277,5 +277,5 @@
                 # doesn't need an argument
                 withoutarg.append(option)
-        
+
         # then we parse the command line
         done = 0
@@ -309,5 +309,5 @@
             except getopt.error, msg :
                 raise PyKotaCommandLineError, str(msg)
-            else :    
+            else :
                 if parsed["arguments"] or parsed["A"] :
                     # arguments are in a file, we ignore all other arguments
@@ -321,11 +321,11 @@
                         if argi.startswith('"') and argi.endswith('"') :
                             argv[i] = argi[1:-1]
-                else :    
+                else :
                     done = 1
         return (parsed, args)
-    
-class PyKotaTool(Tool) :    
+
+class PyKotaTool(Tool) :
     """Base class for all PyKota command line tools."""
-    def deferredInit(self) :    
+    def deferredInit(self) :
         """Deferred initialization."""
         Tool.deferredInit(self)
@@ -333,14 +333,14 @@
         if self.config.isAdmin : # TODO : We don't know this before, fix this !
             self.logdebug("Beware : running as a PyKota administrator !")
-        else :    
+        else :
             self.logdebug("Don't Panic : running as a mere mortal !")
-        
-    def clean(self) :    
+
+    def clean(self) :
         """Ensures that the database is closed."""
         try :
             self.storage.close()
-        except (TypeError, NameError, AttributeError) :    
+        except (TypeError, NameError, AttributeError) :
             pass
-            
+
     def isValidName(self, name) :
         """Checks if a user or printer name is valid."""
@@ -349,7 +349,7 @@
             if c in name :
                 return 0
-        return 1        
-        
-    def _checkUserPQuota(self, userpquota) :            
+        return 1
+
+    def _checkUserPQuota(self, userpquota) :
         """Checks the user quota on a printer and deny or accept the job."""
         # then we check the user's own quota
@@ -361,11 +361,11 @@
         (policy, dummy) = self.config.getPrinterPolicy(userpquota.Printer.Name)
         if not userpquota.Exists :
-            # Unknown userquota 
+            # Unknown userquota
             if policy == "ALLOW" :
                 action = "POLICY_ALLOW"
-            else :    
+            else :
                 action = "POLICY_DENY"
             self.printInfo(_("Unable to match user %s on printer %s, applying default policy (%s)") % (user.Name, printer.Name, action))
-        else :    
+        else :
             pagecounter = int(userpquota.PageCounter or 0)
             if enforcement == "STRICT" :
@@ -375,11 +375,11 @@
                 if pagecounter < softlimit :
                     action = "ALLOW"
-                else :    
+                else :
                     if userpquota.HardLimit is None :
                         # only a soft limit, this is equivalent to having only a hard limit
                         action = "DENY"
-                    else :    
+                    else :
                         hardlimit = int(userpquota.HardLimit)
-                        if softlimit <= pagecounter < hardlimit :    
+                        if softlimit <= pagecounter < hardlimit :
                             now = DateTime.now()
                             if userpquota.DateLimit is not None :
@@ -390,9 +390,9 @@
                             if now < datelimit :
                                 action = "WARN"
-                            else :    
+                            else :
                                 action = "DENY"
-                        else :         
+                        else :
                             action = "DENY"
-            else :        
+            else :
                 if userpquota.HardLimit is not None :
                     # no soft limit, only a hard one.
@@ -400,5 +400,5 @@
                     if pagecounter < hardlimit :
                         action = "ALLOW"
-                    else :      
+                    else :
                         action = "DENY"
                 else :
@@ -406,6 +406,6 @@
                     action = "ALLOW"
         return action
-    
-    def checkGroupPQuota(self, grouppquota) :    
+
+    def checkGroupPQuota(self, grouppquota) :
         """Checks the group quota on a printer and deny or accept the job."""
         group = grouppquota.Group
@@ -413,14 +413,14 @@
         enforcement = self.config.getPrinterEnforcement(printer.Name)
         self.logdebug("Checking group %s's quota on printer %s" % (group.Name, printer.Name))
-        if group.LimitBy and (group.LimitBy.lower() == "balance") : 
+        if group.LimitBy and (group.LimitBy.lower() == "balance") :
             val = group.AccountBalance or 0.0
-            if enforcement == "STRICT" : 
+            if enforcement == "STRICT" :
                 val -= self.softwareJobPrice # use precomputed size.
             balancezero = self.config.getBalanceZero()
             if val <= balancezero :
                 action = "DENY"
-            elif val <= self.config.getPoorMan() :    
+            elif val <= self.config.getPoorMan() :
                 action = "WARN"
-            else :    
+            else :
                 action = "ALLOW"
             if (enforcement == "STRICT") and (val == balancezero) :
@@ -434,11 +434,11 @@
                 if val < softlimit :
                     action = "ALLOW"
-                else :    
+                else :
                     if grouppquota.HardLimit is None :
                         # only a soft limit, this is equivalent to having only a hard limit
                         action = "DENY"
-                    else :    
+                    else :
                         hardlimit = int(grouppquota.HardLimit)
-                        if softlimit <= val < hardlimit :    
+                        if softlimit <= val < hardlimit :
                             now = DateTime.now()
                             if grouppquota.DateLimit is not None :
@@ -449,9 +449,9 @@
                             if now < datelimit :
                                 action = "WARN"
-                            else :    
+                            else :
                                 action = "DENY"
-                        else :         
+                        else :
                             action = "DENY"
-            else :        
+            else :
                 if grouppquota.HardLimit is not None :
                     # no soft limit, only a hard one.
@@ -459,5 +459,5 @@
                     if val < hardlimit :
                         action = "ALLOW"
-                    else :      
+                    else :
                         action = "DENY"
                 else :
@@ -465,13 +465,13 @@
                     action = "ALLOW"
         return action
-    
+
     def checkUserPQuota(self, userpquota) :
         """Checks the user quota on a printer and all its parents and deny or accept the job."""
         user = userpquota.User
         printer = userpquota.Printer
-        
+
         # indicates that a warning needs to be sent
-        warned = 0                
-        
+        warned = 0
+
         # first we check any group the user is a member of
         for group in self.storage.getUserGroups(user) :
@@ -485,20 +485,20 @@
                         if action == "DENY" :
                             return action
-                        elif action == "WARN" :    
+                        elif action == "WARN" :
                             warned = 1
-                        
+
         # Then we check the user's account balance
         # if we get there we are sure that policy is not EXTERNAL
         (policy, dummy) = self.config.getPrinterPolicy(printer.Name)
-        if user.LimitBy and (user.LimitBy.lower() == "balance") : 
+        if user.LimitBy and (user.LimitBy.lower() == "balance") :
             self.logdebug("Checking account balance for user %s" % user.Name)
             if user.AccountBalance is None :
                 if policy == "ALLOW" :
                     action = "POLICY_ALLOW"
-                else :    
+                else :
                     action = "POLICY_DENY"
                 self.printInfo(_("Unable to find user %s's account balance, applying default policy (%s) for printer %s") % (user.Name, action, printer.Name))
-                return action        
-            else :    
+                return action
+            else :
                 if user.OverCharge == 0.0 :
                     self.printInfo(_("User %s will not be charged for printing.") % user.Name)
@@ -507,10 +507,10 @@
                     val = float(user.AccountBalance or 0.0)
                     enforcement = self.config.getPrinterEnforcement(printer.Name)
-                    if enforcement == "STRICT" : 
+                    if enforcement == "STRICT" :
                         val -= self.softwareJobPrice # use precomputed size.
-                    balancezero = self.config.getBalanceZero()    
+                    balancezero = self.config.getBalanceZero()
                     if val <= balancezero :
                         action = "DENY"
-                    elif val <= self.config.getPoorMan() :    
+                    elif val <= self.config.getPoorMan() :
                         action = "WARN"
                     else :
@@ -518,23 +518,23 @@
                     if (enforcement == "STRICT") and (val == balancezero) :
                         action = "WARN" # we can still print until account is 0
-                return action    
-        else :
-            # Then check the user quota on current printer and all its parents.                
+                return action
+        else :
+            # Then check the user quota on current printer and all its parents.
             policyallowed = 0
-            for upquota in [ userpquota ] + userpquota.ParentPrintersUserPQuota :               
+            for upquota in [ userpquota ] + userpquota.ParentPrintersUserPQuota :
                 action = self._checkUserPQuota(upquota)
                 if action in ("DENY", "POLICY_DENY") :
                     return action
-                elif action == "WARN" :    
+                elif action == "WARN" :
                     warned = 1
-                elif action == "POLICY_ALLOW" :    
+                elif action == "POLICY_ALLOW" :
                     policyallowed = 1
-            if warned :        
+            if warned :
                 return "WARN"
-            elif policyallowed :    
-                return "POLICY_ALLOW" 
-            else :    
+            elif policyallowed :
+                return "POLICY_ALLOW"
+            else :
                 return "ALLOW"
-                
+
     def externalMailTo(self, cmd, action, user, printer, message) :
         """Warns the user with an external command."""
@@ -545,5 +545,5 @@
             email = "%s@%s" % (email, self.maildomain or self.smtpserver)
         os.system(cmd % locals())
-    
+
     def formatCommandLine(self, cmd, user, printer) :
         """Executes an external command."""
@@ -551,3 +551,3 @@
         printername = printer.Name
         return cmd % locals()
-        
+
