Index: tea4cups/trunk/tea4cups
===================================================================
--- tea4cups/trunk/tea4cups (revision 674)
+++ tea4cups/trunk/tea4cups (revision 676)
@@ -26,4 +26,5 @@
 import sys
 import os
+import time
 import pwd
 import errno
@@ -123,5 +124,5 @@
         self.tags[0x05] = "unsupported-attributes-tag"
         self.tags[0x06] = "subscription-attributes-tag"
-        self.tags[0x07] = "event-notification-attributes-tag"
+        self.tags[0x07] = "event_notification-attributes-tag"
         
         # out of band values
@@ -531,5 +532,5 @@
         self.DeviceURI = device_uri
         self.PrinterName = os.environ.get("PRINTER", "")
-        self.Directory = self.getPrintQueueOption(self.PrinterName, "directory")
+        self.Directory = self.getPrintQueueOption(self.PrinterName, "directory", ignore=1) or tempfile.gettempdir()
         self.DataFile = os.path.join(self.Directory, "%s-%s-%s-%s" % (self.myname, self.PrinterName, self.UserName, self.JobId))
         (ippfilename, ippmessage) = self.parseIPPRequestFile()
@@ -675,6 +676,10 @@
     def cleanUp(self) :
         """Cleans up the place."""
-        if not self.isTrue(self.getPrintQueueOption(self.PrinterName, "keepfiles", ignore=1)) :
-            os.remove(self.DataFile)
+        if (not self.isTrue(self.getPrintQueueOption(self.PrinterName, "keepfiles", ignore=1))) \
+            and os.path.exists(self.DataFile) :
+            try :
+                os.remove(self.DataFile)
+            except OSError, msg :    
+                self.logInfo("Problem when removing %s : %s" % (self.DataFile, msg), "error")
 
     def sigtermHandler(self, signum, frame) :
@@ -698,5 +703,5 @@
         if not self.isCancelled and not self.gotSigTerm :
             if self.RealBackend :
-                retcode = self.runOriginalBackend()
+                retcode = self.launchOriginalBackend()
                 if retcode :
                     onfail = self.getPrintQueueOption(self.PrinterName, \
@@ -802,4 +807,31 @@
         return exitcode
 
+    def launchOriginalBackend(self) :
+        """Launches the original backend, optionally retrying if needed."""
+        number = 1
+        delay = 0
+        retry = self.getPrintQueueOption(self.PrinterName, "retry", ignore=1)
+        if retry is not None :
+            try :
+                (number, delay) = [int(p) for p in retry.strip().split(",")]
+            except (ValueError, AttributeError, TypeError) :    
+                self.logInfo("Invalid value '%s' for the 'retry' directive for printer %s in %s." % (retry, self.PrinterName, self.conffile), "error")
+                number = 1
+                delay = 0
+                
+        loopcount = 1 
+        while 1 :            
+            retcode = self.runOriginalBackend()
+            if not retcode :
+                break
+            else :
+                if (not number) or (loopcount < number) :
+                    self.logInfo(_("The real backend produced an error, we will try again in %s seconds.") % delay, "warn")
+                    time.sleep(delay)
+                    loopcount += 1
+                else :    
+                    break
+        return retcode            
+        
     def runOriginalBackend(self) :
         """Launches the original backend."""
@@ -814,4 +846,6 @@
                 os.dup2(f.fileno(), 0)
                 f.close()
+            else :    
+                arguments[6] = self.DataFile # in case a tea4cups filter was applied
             try :
                 os.execve(originalbackend, arguments, os.environ)
@@ -831,5 +865,5 @@
             status = os.WEXITSTATUS(status)
             if status :
-              self.logInfo("CUPS backend %s returned %d." % (originalbackend,\
+                self.logInfo("CUPS backend %s returned %d." % (originalbackend,\
                                                              status), "error")
             return status
@@ -852,23 +886,26 @@
         sys.exit(1)
     else :
+        retcode = 1
         try :
-            wrapper.readConfig()
-            wrapper.initBackend()
-            wrapper.saveDatasAndCheckSum()
-            wrapper.exportAttributes()
-            retcode = wrapper.runBranches()
+            try :
+                wrapper.readConfig()
+                wrapper.initBackend()
+                wrapper.saveDatasAndCheckSum()
+                wrapper.exportAttributes()
+                retcode = wrapper.runBranches()
+            except SystemExit, e :
+                retcode = e.code
+            except :
+                import traceback
+                lines = []
+                for line in traceback.format_exception(*sys.exc_info()) :
+                    lines.extend([l for l in line.split("\n") if l])
+                msg = "ERROR: ".join(["%s (PID %s) : %s\n" % (wrapper.MyName, \
+                                                              wrapper.pid, l) \
+                            for l in (["ERROR: Tea4CUPS v%s" % __version__] + lines)])
+                sys.stderr.write(msg)
+                sys.stderr.flush()
+                retcode = 1
+        finally :        
             wrapper.cleanUp()
-        except SystemExit, e :
-            retcode = e.code
-        except :
-            import traceback
-            lines = []
-            for line in traceback.format_exception(*sys.exc_info()) :
-                lines.extend([l for l in line.split("\n") if l])
-            msg = "ERROR: ".join(["%s (PID %s) : %s\n" % (wrapper.MyName, \
-                                                          wrapper.pid, l) \
-                        for l in (["ERROR: Tea4CUPS v%s" % __version__] + lines)])
-            sys.stderr.write(msg)
-            sys.stderr.flush()
-            retcode = 1
         sys.exit(retcode)
