Show
Ignore:
Timestamp:
05/18/04 16:49:34 (20 years ago)
Author:
jalet
Message:

Big code changes to completely remove the need for "requester" directives,
jsut use "hardware(... your previous requester directive's content ...)"

Files:
1 modified

Legend:

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

    r1475 r1483  
    2222# 
    2323# $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# 
    2428# Revision 1.1  2004/05/13 13:59:30  jalet 
    2529# Code simplifications 
     
    3034import sys 
    3135import os 
     36import popen2 
    3237from pykota.accounter import AccounterBase, PyKotaAccounterError 
    33 from pykota.requester import openRequester, PyKotaRequesterError 
    3438 
    3539class Accounter(AccounterBase) : 
     
    3741        """Initializes querying accounter.""" 
    3842        AccounterBase.__init__(self, kotabackend, arguments) 
    39         self.requester = openRequester(kotabackend, kotabackend.printername) 
    4043        self.isDelayed = 1 # With the pykota filter, accounting is delayed by one job 
    4144         
     
    4447        self.filter.logdebug("Reading printer's internal page counter...") 
    4548        try : 
    46             counter = self.requester.getPrinterPageCounter(self.filter.printerhostname) 
    47         except PyKotaRequesterError, msg : 
     49            counter = self.askPrinterPageCounter(self.filter.printerhostname) 
     50        except PyKotaAccounterError, msg : 
    4851            # can't get actual page counter, assume printer is off or warming up 
    4952            # log the message anyway. 
     
    97100        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) 
    98101        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)  
    99135