Changeset 3261 for pykota/trunk

Show
Ignore:
Timestamp:
11/27/07 18:08:54 (17 years ago)
Author:
jerome
Message:

Cosmetic changes.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/accounters/pjl.py

    r3260 r3261  
    2121# 
    2222 
     23"""This module defines the necessary classes and methods to retrieve  
     24a printer's internal page counter over a TCP connection."""  
     25 
    2326import sys 
    2427import os 
    2528import socket 
    26 import errno 
    2729import time 
    2830import threading 
     
    3335FORMFEEDCHAR = chr(0x0c)     # Form Feed character, ends PJL answers. 
    3436 
    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" 
    3638# Here's a new method, which seems to work fine on my HP2300N, while the  
    3739# previous one didn't. 
    3840# TODO : We could also experiment with USTATUS JOB=ON and we would know for sure  
    3941# 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 = { 
     42PJLMESSAGE = "\033%-12345X@PJL USTATUS DEVICE=ON\r\n@PJL INFO STATUS\r\n@PJL INFO PAGECOUNT\r\n@PJL USTATUS DEVICE=OFF\033%-12345X" 
     43PJLSTATUSVALUES = { 
    4244                    "10000" : "Powersave Mode", 
    4345                    "10001" : "Ready Online", 
     
    109111        """Reading loop thread.""" 
    110112        self.parent.filter.logdebug("Reading thread started.") 
    111         buffer = [] 
     113        readbuffer = [] 
    112114        while not self.quitEvent.isSet() : 
    113115            try : 
     
    115117            except socket.timeout :     
    116118                pass 
    117             except socket.error, (err, msg) : 
     119            except socket.error, (dummy, msg) : 
    118120                self.parent.filter.printInfo(_("Problem while receiving PJL answer from %s:%s : %s") % (self.printerHostname, self.port, str(msg)), "warn") 
    119121            else :     
    120122                if answer : 
    121                     buffer.append(answer) 
     123                    readbuffer.append(answer) 
    122124                    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))             
    127129        self.parent.filter.logdebug("Reading thread ended.") 
    128130             
     
    134136        try : 
    135137            try : 
    136                 nbsent = self.sock.send(pjlMessage) 
    137                 if nbsent != len(pjlMessage) : 
     138                nbsent = self.sock.send(PJLMESSAGE) 
     139                if nbsent != len(PJLMESSAGE) : 
    138140                    raise socket.error, "Short write" 
    139141            except socket.error, msg : 
    140142                self.parent.filter.printInfo(_("Problem while sending PJL query to %s:%s : %s") % (self.printerHostname, self.port, str(msg)), "warn") 
    141143            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))) 
    143145                actualpagecount = self.printerStatus = None 
    144146                while (actualpagecount is None) or (self.printerStatus is None) : 
     
    258260def main(hostname) : 
    259261    """Tries PJL accounting for a printer host.""" 
    260     class fakeFilter : 
     262    class FakeFilter : 
    261263        """Fakes a filter for testing purposes.""" 
    262264        def __init__(self) : 
     
    274276            self.printInfo(msg, "debug") 
    275277             
    276     class fakeAccounter :         
     278    class FakeAccounter :         
    277279        """Fakes an accounter for testing purposes.""" 
    278         def __init__(self) : 
     280        def __init__(self, hostname) : 
    279281            """Initializes fake accounter.""" 
    280282            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) 
    283285             
    284286        def getLastPageCounter(self) :     
     
    286288            return 0 
    287289         
    288     acc = fakeAccounter()             
     290    acc = FakeAccounter(hostname) 
    289291    return acc.protocolHandler.retrieveInternalPageCounter() 
    290292     
     
    294296    else :     
    295297        def _(msg) : 
     298            """Fake gettext method.""" 
    296299            return msg 
    297300