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