Changeset 3511 for pykota/branches
- Timestamp:
- 10/12/09 10:12:34 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/branches/1.26_fixes/pykota/accounters/hardware.py
r3162 r3511 14 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 15 # GNU General Public License for more details. 16 # 16 # 17 17 # You should have received a copy of the GNU General Public License 18 18 # along with this program; if not, write to the Free Software … … 35 35 AccounterBase.__init__(self, kotabackend, arguments) 36 36 self.isSoftware = 0 37 38 def getPrinterInternalPageCounter(self) : 37 38 def getPrinterInternalPageCounter(self) : 39 39 """Returns the printer's internal page counter.""" 40 40 self.filter.logdebug("Reading printer %s's internal page counter..." % self.filter.PrinterName) 41 41 counter = self.askPrinterPageCounter(self.filter.PrinterHostName) 42 42 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) : 46 46 """Saves printer internal page counter at start of job.""" 47 47 # save page counter before job 48 48 self.LastPageCounter = self.getPrinterInternalPageCounter() 49 49 self.fakeBeginJob() 50 51 def fakeBeginJob(self) : 50 51 def fakeBeginJob(self) : 52 52 """Fakes a begining of a job.""" 53 53 self.counterbefore = self.getLastPageCounter() 54 55 def endJob(self, printer) : 54 55 def endJob(self, printer) : 56 56 """Saves printer internal page counter at end of job.""" 57 57 # save page counter after job 58 58 self.LastPageCounter = self.counterafter = self.getPrinterInternalPageCounter() 59 60 def getJobSize(self, printer) : 59 60 def getJobSize(self, printer) : 61 61 """Returns the actual job size.""" 62 62 if (not self.counterbefore) or (not self.counterafter) : … … 66 66 # if there's a previous job, use the last value from database 67 67 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 : 69 69 self.counterbefore = printer.LastJob.PrinterPageCounter or 0 70 70 if not self.counterafter : 71 71 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) 74 74 self.counterbefore = before 75 75 self.counterafter = after … … 84 84 self.counterbefore = 0 85 85 self.counterafter = 1 86 87 jobsize = (self.counterafter - self.counterbefore) 86 87 jobsize = (self.counterafter - self.counterbefore) 88 88 if jobsize < 0 : 89 # Try to take care of HP printers 89 # Try to take care of HP printers 90 90 # Their internal page counter is saved to NVRAM 91 91 # only every 10 pages. If the printer was switched 92 92 # off then back on during the job, and that the 93 # counters difference is negative, we know 93 # counters difference is negative, we know 94 94 # the formula (we can't know if more than eleven 95 95 # pages were printed though) : 96 96 if jobsize > -10 : 97 97 jobsize += 10 98 else : 98 else : 99 99 # here we may have got a printer being replaced 100 100 # DURING the job. This is HIGHLY improbable (but already happened) ! … … 103 103 jobsize = 1 104 104 return jobsize 105 105 106 106 def askPrinterPageCounter(self, printer) : 107 107 """Returns the page counter from the printer via an external command. 108 108 109 109 The external command must report the life time page number of the printer on stdout. 110 110 """ 111 skipinitialwait = self.filter.config.getPrinterSkipInitialWait( printer)111 skipinitialwait = self.filter.config.getPrinterSkipInitialWait(self.filter.PrinterName) 112 112 commandline = self.arguments.strip() % locals() 113 113 cmdlower = commandline.lower() … … 116 116 elif (cmdlower == "pjl") or cmdlower.startswith("pjl:") : 117 117 return pjl.Handler(self, printer, skipinitialwait).retrieveInternalPageCounter() 118 118 119 119 if printer is None : 120 120 raise PyKotaAccounterError, _("Unknown printer address in HARDWARE(%s) for printer %s") % (commandline, self.filter.PrinterName) 121 while 1 : 121 while 1 : 122 122 self.filter.printInfo(_("Launching HARDWARE(%s)...") % commandline) 123 123 pagecounter = None 124 child = popen2.Popen4(commandline) 124 child = popen2.Popen4(commandline) 125 125 try : 126 126 answer = child.fromchild.read() 127 except IOError : 127 except IOError : 128 128 # we were interrupted by a signal, certainely a SIGTERM 129 129 # caused by the user cancelling the current job 130 130 try : 131 131 os.kill(child.pid, signal.SIGTERM) 132 except : 132 except : 133 133 pass # already killed ? 134 134 self.filter.printInfo(_("SIGTERM was sent to hardware accounter %s (pid: %s)") % (commandline, child.pid)) 135 else : 135 else : 136 136 lines = [l.strip() for l in answer.split("\n")] 137 for i in range(len(lines)) : 137 for i in range(len(lines)) : 138 138 try : 139 139 pagecounter = int(lines[i]) 140 140 except (AttributeError, ValueError) : 141 141 self.filter.printInfo(_("Line [%s] skipped in accounter's output. Trying again...") % lines[i]) 142 else : 142 else : 143 143 break 144 child.fromchild.close() 144 child.fromchild.close() 145 145 child.tochild.close() 146 146 try : 147 147 status = child.wait() 148 except OSError, msg : 148 except OSError, msg : 149 149 self.filter.logdebug("Error while waiting for hardware accounter pid %s : %s" % (child.pid, msg)) 150 else : 150 else : 151 151 if os.WIFEXITED(status) : 152 152 status = os.WEXITSTATUS(status) 153 153 self.filter.printInfo(_("Hardware accounter %s exit code is %s") % (self.arguments, str(status))) 154 154 155 155 if pagecounter is None : 156 156 message = _("Unable to query printer %s via HARDWARE(%s)") % (printer, commandline) … … 158 158 self.filter.printInfo(message, "error") 159 159 else : 160 raise PyKotaAccounterError, message 161 else : 162 return pagecounter 160 raise PyKotaAccounterError, message 161 else : 162 return pagecounter