Changeset 3261
- Timestamp:
- 11/27/07 18:08:54 (17 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/accounters/pjl.py
r3260 r3261 21 21 # 22 22 23 """This module defines the necessary classes and methods to retrieve 24 a printer's internal page counter over a TCP connection.""" 25 23 26 import sys 24 27 import os 25 28 import socket 26 import errno27 29 import time 28 30 import threading … … 33 35 FORMFEEDCHAR = chr(0x0c) # Form Feed character, ends PJL answers. 34 36 35 # Old method : pjlMessage= "\033%-12345X@PJL USTATUSOFF\r\n@PJL INFO STATUS\r\n@PJL INFO PAGECOUNT\r\n\033%-12345X"37 # Old method : PJLMESSAGE = "\033%-12345X@PJL USTATUSOFF\r\n@PJL INFO STATUS\r\n@PJL INFO PAGECOUNT\r\n\033%-12345X" 36 38 # Here's a new method, which seems to work fine on my HP2300N, while the 37 39 # previous one didn't. 38 40 # TODO : We could also experiment with USTATUS JOB=ON and we would know for sure 39 41 # when the job is finished, without having to poll the printer repeatedly. 40 pjlMessage= "\033%-12345X@PJL USTATUS DEVICE=ON\r\n@PJL INFO STATUS\r\n@PJL INFO PAGECOUNT\r\n@PJL USTATUS DEVICE=OFF\033%-12345X"41 pjlStatusValues= {42 PJLMESSAGE = "\033%-12345X@PJL USTATUS DEVICE=ON\r\n@PJL INFO STATUS\r\n@PJL INFO PAGECOUNT\r\n@PJL USTATUS DEVICE=OFF\033%-12345X" 43 PJLSTATUSVALUES = { 42 44 "10000" : "Powersave Mode", 43 45 "10001" : "Ready Online", … … 109 111 """Reading loop thread.""" 110 112 self.parent.filter.logdebug("Reading thread started.") 111 buffer = []113 readbuffer = [] 112 114 while not self.quitEvent.isSet() : 113 115 try : … … 115 117 except socket.timeout : 116 118 pass 117 except socket.error, ( err, msg) :119 except socket.error, (dummy, msg) : 118 120 self.parent.filter.printInfo(_("Problem while receiving PJL answer from %s:%s : %s") % (self.printerHostname, self.port, str(msg)), "warn") 119 121 else : 120 122 if answer : 121 buffer.append(answer)123 readbuffer.append(answer) 122 124 if answer.endswith(FORMFEEDCHAR) : 123 self.queue.put("".join( buffer))124 buffer = []125 if buffer :126 self.queue.put("".join( buffer))125 self.queue.put("".join(readbuffer)) 126 readbuffer = [] 127 if readbuffer : 128 self.queue.put("".join(readbuffer)) 127 129 self.parent.filter.logdebug("Reading thread ended.") 128 130 … … 134 136 try : 135 137 try : 136 nbsent = self.sock.send( pjlMessage)137 if nbsent != len( pjlMessage) :138 nbsent = self.sock.send(PJLMESSAGE) 139 if nbsent != len(PJLMESSAGE) : 138 140 raise socket.error, "Short write" 139 141 except socket.error, msg : 140 142 self.parent.filter.printInfo(_("Problem while sending PJL query to %s:%s : %s") % (self.printerHostname, self.port, str(msg)), "warn") 141 143 else : 142 self.parent.filter.logdebug("Query sent to %s : %s" % (self.printerHostname, repr( pjlMessage)))144 self.parent.filter.logdebug("Query sent to %s : %s" % (self.printerHostname, repr(PJLMESSAGE))) 143 145 actualpagecount = self.printerStatus = None 144 146 while (actualpagecount is None) or (self.printerStatus is None) : … … 258 260 def main(hostname) : 259 261 """Tries PJL accounting for a printer host.""" 260 class fakeFilter :262 class FakeFilter : 261 263 """Fakes a filter for testing purposes.""" 262 264 def __init__(self) : … … 274 276 self.printInfo(msg, "debug") 275 277 276 class fakeAccounter :278 class FakeAccounter : 277 279 """Fakes an accounter for testing purposes.""" 278 def __init__(self ) :280 def __init__(self, hostname) : 279 281 """Initializes fake accounter.""" 280 282 self.arguments = "pjl:9100" 281 self.filter = fakeFilter()282 self.protocolHandler = Handler(self, sys.argv[1])283 self.filter = FakeFilter() 284 self.protocolHandler = Handler(self, hostname) 283 285 284 286 def getLastPageCounter(self) : … … 286 288 return 0 287 289 288 acc = fakeAccounter()290 acc = FakeAccounter(hostname) 289 291 return acc.protocolHandler.retrieveInternalPageCounter() 290 292 … … 294 296 else : 295 297 def _(msg) : 298 """Fake gettext method.""" 296 299 return msg 297 300