Changeset 1483 for pykota/trunk/pykota

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 ...)"

Location:
pykota/trunk/pykota
Files:
2 removed
7 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/accounter.py

    r1285 r1483  
    2222# 
    2323# $Log$ 
     24# Revision 1.14  2004/05/18 14:49:19  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.13  2004/01/12 22:43:40  jalet 
    2529# New formula to compute a job's price 
     
    6367 
    6468import sys 
     69from pykota import pdlanalyzer 
    6570 
    6671class PyKotaAccounterError(Exception): 
     
    8085        self.arguments = arguments 
    8186        self.isDelayed = 0      # Accounting is immediate by default 
     87        self.firstPassSize = None 
     88         
     89    def getSoftwareJobSize(self) :     
     90        """Pre-computes the job's size with a software method.""" 
     91        if self.filter.preserveinputfile is None : 
     92            raise PyKotaAccounterError, "Only supports raw jobs for now.""" 
     93        else :     
     94            fname = self.filter.preserveinputfile 
     95        parser = pdfanalyzer.PDLAnalyzer(fname)     
     96        try :  
     97            jobsize = parser.getJobSize() 
     98        except TypeError, msg :     
     99            raise PyKotaAccounterError, msg 
     100        else :     
     101            self.firstPassSize = jobsize 
    82102         
    83103    def getLastPageCounter(self) :     
    84104        """Returns last internal page counter value (possibly faked).""" 
    85105        try : 
    86             return self.LastPageCounter 
     106            return self.LastPageCounter or 0 
    87107        except :     
    88108            return 0 
  • 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             
  • pykota/trunk/pykota/accounters/software.py

    r1475 r1483  
    2222# 
    2323# $Log$ 
     24# Revision 1.2  2004/05/18 14:49:23  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 
     
    4549            infile = open(self.filter.inputfile, "rb") 
    4650             
    47         # launches external accounter 
     51        # launches software accounter 
    4852        # TODO : USE tempfile.mkstemp() instead ! Needs some work ! 
    4953        infilename = tempfile.mktemp() 
     
    6872            # check exit status 
    6973            if (os.WIFEXITED(retcode) and not os.WEXITSTATUS(retcode)) or os.stat(errfilename) : 
    70                 # tries to extract the job size from the external accounter's 
     74                # tries to extract the job size from the software accounter's 
    7175                # standard output 
    7276                childoutput = open(outfilename, "r") 
     
    7478                    pagecount = int(childoutput.readline().strip()) 
    7579                except (AttributeError, ValueError) : 
    76                     self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % self.arguments) 
     80                    self.filter.logger.log_message(_("Unable to compute job size with accounter %s") % self.arguments) 
    7781                    pagecount = 0 
    7882                childoutput.close()     
    7983            else : 
    80                 self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % self.arguments) 
     84                self.filter.logger.log_message(_("Unable to compute job size with accounter %s") % self.arguments) 
    8185                pagecount = 0 
    8286            os.remove(infilename) 
     
    8690            # TODO : temporary files may remain on the filesystem... 
    8791            msg = "%s : %s" % (self.arguments, msg)  
    88             self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % msg) 
     92            self.filter.logger.log_message(_("Unable to compute job size with accounter %s") % msg) 
    8993            pagecount = 0 
    9094             
  • pykota/trunk/pykota/config.py

    r1475 r1483  
    2222# 
    2323# $Log$ 
     24# Revision 1.47  2004/05/18 14:49:20  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.46  2004/05/13 13:59:28  jalet 
    2529# Code simplifications 
     
    311315        validaccounters = [ "hardware", "software" ]      
    312316        fullaccounter = self.getPrinterOption(printername, "accounter").strip() 
    313         if fullaccounter.lower().startswith("software") :     
     317        flower = fullaccounter.lower() 
     318        if flower.startswith("software") or flower.startswith("hardware") :     
    314319            try : 
    315320                (accounter, args) = [x.strip() for x in fullaccounter.split('(', 1)] 
    316321            except ValueError :     
    317                 raise PyKotaConfigError, _("Invalid external accounter %s for printer %s") % (fullaccounter, printername) 
     322                raise PyKotaConfigError, _("Invalid accounter %s for printer %s") % (fullaccounter, printername) 
    318323            if args.endswith(')') : 
    319324                args = args[:-1] 
    320325            if not args : 
    321                 raise PyKotaConfigError, _("Invalid external accounter %s for printer %s") % (fullaccounter, printername) 
     326                raise PyKotaConfigError, _("Invalid accounter %s for printer %s") % (fullaccounter, printername) 
    322327            return (accounter.lower(), args)     
    323         elif fullaccounter.lower() not in validaccounters : 
     328        else : 
    324329            raise PyKotaConfigError, _("Option accounter in section %s only supports values in %s") % (printername, str(validaccounters)) 
    325         else :     
    326             return (fullaccounter.lower(), None) 
    327330         
    328331    def getPreHook(self, printername) :     
     
    340343            return      # No command to launch in the post-hook 
    341344             
    342     def getRequesterBackend(self, printername) :     
    343         """Returns the requester backend to use for a given printer, with its arguments.""" 
    344         try : 
    345             fullrequester = self.getPrinterOption(printername, "requester") 
    346         except PyKotaConfigError :     
    347             # No requester defined, maybe it is not needed if accounting method 
    348             # is not set to 'hardware', but if we are called, then the accounting 
    349             # method really IS 'hardware', and so there's a big problem. 
    350             raise PyKotaConfigError, _("Option requester for printer %s was not set") % printername 
    351         else :     
    352             try : 
    353                 (requester, args) = [x.strip() for x in fullrequester.split('(', 1)] 
    354             except ValueError :     
    355                 raise PyKotaConfigError, _("Invalid requester %s for printer %s") % (fullrequester, printername) 
    356             if args.endswith(')') : 
    357                 args = args[:-1] 
    358             if not args : 
    359                 raise PyKotaConfigError, _("Invalid requester %s for printer %s") % (fullrequester, printername) 
    360             validrequesters = [ "external" ]  
    361             requester = requester.lower() 
    362             if requester not in validrequesters : 
    363                 raise PyKotaConfigError, _("Option requester for printer %s only supports values in %s") % (printername, str(validrequesters)) 
    364             return (requester, args) 
    365          
    366345    def getPrinterPolicy(self, printername) :     
    367346        """Returns the default policy for the current printer.""" 
  • pykota/trunk/pykota/Makefile.am

    r1482 r1483  
    77        logger.py               \ 
    88        reporter.py             \ 
    9         requester.py    \ 
    109        storage.py              \ 
    1110        tool.py                 \ 
     
    1312        version.py 
    1413 
    15 SUBDIRS = accounters loggers reporters requesters storages 
     14SUBDIRS = accounters loggers reporters storages 
    1615 
  • pykota/trunk/pykota/tool.py

    r1481 r1483  
    2222# 
    2323# $Log$ 
     24# Revision 1.88  2004/05/18 14:49:20  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.87  2004/05/17 19:14:59  jalet 
    2529# Now catches SIGPIPE and SIGCHLD 
     
    764768         self.options, \ 
    765769         self.originalbackend) = self.extractInfoFromCupsOrLprng() 
    766         self.username = self.username or 'root'  
     770        self.username = self.username or 'root' # when printing test page from CUPS web interface, username is empty 
    767771        if self.config.getUserNameToLower() : 
    768772            self.username = self.username.lower() 
  • pykota/trunk/pykota/version.py

    r1478 r1483  
    2222# 
    2323 
    24 __version__ = "1.19alpha9_unofficial" 
     24__version__ = "1.19alpha10_unofficial" 
    2525 
    2626__doc__ = """PyKota : a complete Printing Quota Solution for CUPS and LPRng."""