Changeset 1483 for pykota/trunk/pykota/accounters/hardware.py
- Timestamp:
- 05/18/04 16:49:34 (20 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/accounters/hardware.py
r1475 r1483 22 22 # 23 23 # $Log$ 24 # Revision 1.2 2004/05/18 14:49:22 jalet 25 # Big code changes to completely remove the need for "requester" directives, 26 # jsut use "hardware(... your previous requester directive's content ...)" 27 # 24 28 # Revision 1.1 2004/05/13 13:59:30 jalet 25 29 # Code simplifications … … 30 34 import sys 31 35 import os 36 import popen2 32 37 from pykota.accounter import AccounterBase, PyKotaAccounterError 33 from pykota.requester import openRequester, PyKotaRequesterError34 38 35 39 class Accounter(AccounterBase) : … … 37 41 """Initializes querying accounter.""" 38 42 AccounterBase.__init__(self, kotabackend, arguments) 39 self.requester = openRequester(kotabackend, kotabackend.printername)40 43 self.isDelayed = 1 # With the pykota filter, accounting is delayed by one job 41 44 … … 44 47 self.filter.logdebug("Reading printer's internal page counter...") 45 48 try : 46 counter = self. requester.getPrinterPageCounter(self.filter.printerhostname)47 except PyKota RequesterError, msg :49 counter = self.askPrinterPageCounter(self.filter.printerhostname) 50 except PyKotaAccounterError, msg : 48 51 # can't get actual page counter, assume printer is off or warming up 49 52 # log the message anyway. … … 97 100 userpquota.Printer.addJobToHistory(self.filter.jobid, userpquota.User, counterbeforejob, action, filename=self.filter.preserveinputfile, title=self.filter.title, copies=self.filter.copies, options=self.filter.options) 98 101 return action 102 103 def askPrinterPageCounter(self, printer) : 104 """Returns the page counter from the printer via an external command. 105 106 The external command must report the life time page number of the printer on stdout. 107 """ 108 commandline = self.arguments.strip() % locals() 109 if printer is None : 110 raise PyKotaAccounterError, _("Unknown printer address in HARDWARE(%s) for printer %s") % (commandline, self.filter.printername) 111 error = 1 112 pagecounter = None 113 child = popen2.Popen4(commandline) 114 try : 115 pagecounter = int(child.fromchild.readline().strip()) 116 except ValueError : 117 pass 118 except IOError : 119 # we were interrupted by a signal, certainely a SIGTERM 120 # caused by the user cancelling the current job 121 try : 122 os.kill(child.pid, signal.SIGTERM) 123 except : 124 pass # already killed ? 125 self.filter.logger.log_message(_("SIGTERM was sent to hardware accounter %s (pid: %s)") % (commandline, child.pid), "info") 126 else : 127 error = 0 128 child.fromchild.close() 129 child.tochild.close() 130 status = child.wait() 131 if (not error) and os.WIFEXITED(status) and (not os.WEXITSTATUS(status)) : 132 return pagecounter 133 else : 134 raise PyKotaAccounterError, _("Unable to query printer %s via HARDWARE(%s)") % (printer, commandline) 99 135