Changeset 3511 for pykota

Show
Ignore:
Timestamp:
10/12/09 10:12:34 (15 years ago)
Author:
jerome
Message:

Backported the code that fixed #48.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/branches/1.26_fixes/pykota/accounters/hardware.py

    r3162 r3511  
    1414# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    1515# GNU General Public License for more details. 
    16 #  
     16# 
    1717# You should have received a copy of the GNU General Public License 
    1818# along with this program; if not, write to the Free Software 
     
    3535        AccounterBase.__init__(self, kotabackend, arguments) 
    3636        self.isSoftware = 0 
    37          
    38     def getPrinterInternalPageCounter(self) :     
     37 
     38    def getPrinterInternalPageCounter(self) : 
    3939        """Returns the printer's internal page counter.""" 
    4040        self.filter.logdebug("Reading printer %s's internal page counter..." % self.filter.PrinterName) 
    4141        counter = self.askPrinterPageCounter(self.filter.PrinterHostName) 
    4242        self.filter.logdebug("Printer %s's internal page counter value is : %s" % (self.filter.PrinterName, str(counter))) 
    43         return counter     
    44          
    45     def beginJob(self, printer) :     
     43        return counter 
     44 
     45    def beginJob(self, printer) : 
    4646        """Saves printer internal page counter at start of job.""" 
    4747        # save page counter before job 
    4848        self.LastPageCounter = self.getPrinterInternalPageCounter() 
    4949        self.fakeBeginJob() 
    50          
    51     def fakeBeginJob(self) :     
     50 
     51    def fakeBeginJob(self) : 
    5252        """Fakes a begining of a job.""" 
    5353        self.counterbefore = self.getLastPageCounter() 
    54          
    55     def endJob(self, printer) :     
     54 
     55    def endJob(self, printer) : 
    5656        """Saves printer internal page counter at end of job.""" 
    5757        # save page counter after job 
    5858        self.LastPageCounter = self.counterafter = self.getPrinterInternalPageCounter() 
    59          
    60     def getJobSize(self, printer) :     
     59 
     60    def getJobSize(self, printer) : 
    6161        """Returns the actual job size.""" 
    6262        if (not self.counterbefore) or (not self.counterafter) : 
     
    6666                # if there's a previous job, use the last value from database 
    6767                self.filter.printInfo(_("Retrieving printer %s's page counter from database instead.") % printer.Name, "warn") 
    68                 if not self.counterbefore :  
     68                if not self.counterbefore : 
    6969                    self.counterbefore = printer.LastJob.PrinterPageCounter or 0 
    7070                if not self.counterafter : 
    7171                    self.counterafter = printer.LastJob.PrinterPageCounter or 0 
    72                 before = min(self.counterbefore, self.counterafter)     
    73                 after = max(self.counterbefore, self.counterafter)     
     72                before = min(self.counterbefore, self.counterafter) 
     73                after = max(self.counterbefore, self.counterafter) 
    7474                self.counterbefore = before 
    7575                self.counterafter = after 
     
    8484                self.counterbefore = 0 
    8585                self.counterafter = 1 
    86                  
    87         jobsize = (self.counterafter - self.counterbefore)     
     86 
     87        jobsize = (self.counterafter - self.counterbefore) 
    8888        if jobsize < 0 : 
    89             # Try to take care of HP printers  
     89            # Try to take care of HP printers 
    9090            # Their internal page counter is saved to NVRAM 
    9191            # only every 10 pages. If the printer was switched 
    9292            # off then back on during the job, and that the 
    93             # counters difference is negative, we know  
     93            # counters difference is negative, we know 
    9494            # the formula (we can't know if more than eleven 
    9595            # pages were printed though) : 
    9696            if jobsize > -10 : 
    9797                jobsize += 10 
    98             else :     
     98            else : 
    9999                # here we may have got a printer being replaced 
    100100                # DURING the job. This is HIGHLY improbable (but already happened) ! 
     
    103103                jobsize = 1 
    104104        return jobsize 
    105          
     105 
    106106    def askPrinterPageCounter(self, printer) : 
    107107        """Returns the page counter from the printer via an external command. 
    108          
     108 
    109109           The external command must report the life time page number of the printer on stdout. 
    110110        """ 
    111         skipinitialwait = self.filter.config.getPrinterSkipInitialWait(printer) 
     111        skipinitialwait = self.filter.config.getPrinterSkipInitialWait(self.filter.PrinterName) 
    112112        commandline = self.arguments.strip() % locals() 
    113113        cmdlower = commandline.lower() 
     
    116116        elif (cmdlower == "pjl") or cmdlower.startswith("pjl:") : 
    117117            return pjl.Handler(self, printer, skipinitialwait).retrieveInternalPageCounter() 
    118              
     118 
    119119        if printer is None : 
    120120            raise PyKotaAccounterError, _("Unknown printer address in HARDWARE(%s) for printer %s") % (commandline, self.filter.PrinterName) 
    121         while 1 :     
     121        while 1 : 
    122122            self.filter.printInfo(_("Launching HARDWARE(%s)...") % commandline) 
    123123            pagecounter = None 
    124             child = popen2.Popen4(commandline)     
     124            child = popen2.Popen4(commandline) 
    125125            try : 
    126126                answer = child.fromchild.read() 
    127             except IOError :     
     127            except IOError : 
    128128                # we were interrupted by a signal, certainely a SIGTERM 
    129129                # caused by the user cancelling the current job 
    130130                try : 
    131131                    os.kill(child.pid, signal.SIGTERM) 
    132                 except :     
     132                except : 
    133133                    pass # already killed ? 
    134134                self.filter.printInfo(_("SIGTERM was sent to hardware accounter %s (pid: %s)") % (commandline, child.pid)) 
    135             else :     
     135            else : 
    136136                lines = [l.strip() for l in answer.split("\n")] 
    137                 for i in range(len(lines)) :  
     137                for i in range(len(lines)) : 
    138138                    try : 
    139139                        pagecounter = int(lines[i]) 
    140140                    except (AttributeError, ValueError) : 
    141141                        self.filter.printInfo(_("Line [%s] skipped in accounter's output. Trying again...") % lines[i]) 
    142                     else :     
     142                    else : 
    143143                        break 
    144             child.fromchild.close()     
     144            child.fromchild.close() 
    145145            child.tochild.close() 
    146146            try : 
    147147                status = child.wait() 
    148             except OSError, msg :     
     148            except OSError, msg : 
    149149                self.filter.logdebug("Error while waiting for hardware accounter pid %s : %s" % (child.pid, msg)) 
    150             else :     
     150            else : 
    151151                if os.WIFEXITED(status) : 
    152152                    status = os.WEXITSTATUS(status) 
    153153                self.filter.printInfo(_("Hardware accounter %s exit code is %s") % (self.arguments, str(status))) 
    154                  
     154 
    155155            if pagecounter is None : 
    156156                message = _("Unable to query printer %s via HARDWARE(%s)") % (printer, commandline) 
     
    158158                    self.filter.printInfo(message, "error") 
    159159                else : 
    160                     raise PyKotaAccounterError, message  
    161             else :         
    162                 return pagecounter         
     160                    raise PyKotaAccounterError, message 
     161            else : 
     162                return pagecounter