Index: pykota/trunk/bin/cupspykota
===================================================================
--- pykota/trunk/bin/cupspykota (revision 1178)
+++ pykota/trunk/bin/cupspykota (revision 1180)
@@ -24,4 +24,7 @@
 #
 # $Log$
+# Revision 1.3  2003/11/12 23:27:44  jalet
+# More work on new backend. This commit may be unstable.
+#
 # Revision 1.2  2003/11/12 09:33:34  jalet
 # New CUPS backend supports device enumeration
@@ -120,4 +123,5 @@
         # we let the job pass through, but log a warning message
         thebackend.logger.log_message(_("Printer %s not registered in the PyKota system") % thebackend.printername, "warn")
+        action = "ALLOW"
     else :    
         for dummy in range(2) :
@@ -157,5 +161,4 @@
                 break    
                     
-        # if user exists, do accounting
         if user.Exists :
             # Is the current user allowed to print at all ?
@@ -168,22 +171,4 @@
             return 0
         
-    MAXTRIES = 12    # maximum number of tries to get the printer's internal page counter
-    TIMETOSLEEP = 10 # number of seconds to sleep between two tries to get the printer's internal page counter
-    requester = openRequester(thebackend.config, thebackend.printername)
-    def getPPC(requester, backend) :
-        for i in range(MAXTRIES) :
-            try :
-                counterbeforejob = requester.getPrinterPageCounter(backend.printerhostname)
-            except PyKotaRequesterError, msg :
-                # can't get actual page counter, assume printer is off or warming up
-                # log the message anyway.
-                backend.logger.log_message("%s" % msg, "warn")
-                counterbeforejob = None
-            else :    
-                # printer answered, it is on so we can exit the loop
-                break
-            time.sleep(TIMETOSLEEP)    
-        return counterbeforejob    
-        
     if action not in ["ALLOW", "WARN"] :    
         # if not allowed to print then die, else proceed.
@@ -193,26 +178,27 @@
     else :
         # pass the job untouched to the underlying layer
-        # but get printer page counter before and after
-        # print job is submitted to the hardware.
-        
-        # get page counter before job
-        before = getPPC(requester, thebackend)
-        
-        # executes backend
-        # TODO : use correct original backend.
+        # and starts accounting at the same time
+        thebackend.accounter.beginJob(printer, user)
+        
+        # executes original backend
+        #mustclose = 0    
+        #if thebackend.inputfile is not None :    
+        #    if hasattr(thebackend.inputfile, "read") :
+        #        infile = thebackend.inputfile
+        #    else :    
+        #        infile = open(thebackend.inputfile, "rb")
+        #    mustclose = 1
+        #else :    
+        #    infile = sys.stdin
+            
         realbackend = os.path.join(os.path.split(sys.argv[0])[0], thebackend.originalbackend)
         retcode = os.spawnve(os.P_WAIT, realbackend, [os.environ["DEVICE_URI"]] + sys.argv[1:], os.environ)
     
-    # get page counter after job
-    after = getPPC(requester, thebackend)
-    
-    # Computes the last job size as the difference between internal page
-    # counter in the printer and last page counter taken from the Quota
-    # Storage database for this particular printer
-    try :
-        jobsize = (after - before)    
-    except :    
-        jobsize = 0
-        
+    # stops accounting. 
+    thebackend.accounter.endJob(printer, user)
+        
+    # retrieve the job size    
+    jobsize = thebackend.accounter.getJobSize()
+    
     # update the quota for the current user on this printer 
     if printer.Exists :
@@ -223,5 +209,5 @@
         
         # adds the current job to history    
-        printer.addJobToHistory(thebackend.jobid, user, after, action, jobsize)
+        printer.addJobToHistory(thebackend.jobid, user, thebackend.accounter.getLastPageCounter(), action, jobsize)
     
     return retcode
@@ -242,4 +228,6 @@
             if status is None :
                 for d in devices :
+                    # each line is of the form : 'xxxx xxxx "xxxx xxx" "xxxx xxx"'
+                    # so we have to decompose it carefully
                     fdevice = cStringIO.StringIO("%s" % d)
                     tokenizer = shlex.shlex(fdevice)
@@ -253,10 +241,14 @@
                             break
                     fdevice.close()
-                    (devicetype, device, name, fullname) = arguments
-                    if name.startswith('"') and name.endswith('"') :
-                        name = name[1:-1]
-                    if fullname.startswith('"') and fullname.endswith('"') :
-                        fullname = fullname[1:-1]
-                    print '%s cupspykota:%s "PyKota+%s" "PyKota managed %s"' % (devicetype, device, name, fullname)    
+                    try :
+                        (devicetype, device, name, fullname) = arguments
+                    except ValueError :    
+                        pass    # ignore this 'bizarre' device
+                    else :    
+                        if name.startswith('"') and name.endswith('"') :
+                            name = name[1:-1]
+                        if fullname.startswith('"') and fullname.endswith('"') :
+                            fullname = fullname[1:-1]
+                        print '%s cupspykota:%s "PyKota+%s" "PyKota managed %s"' % (devicetype, device, name, fullname)    
         retcode = 0
     elif len(sys.argv) not in (6, 7) :    
