Changeset 3245

Show
Ignore:
Timestamp:
08/31/07 09:27:44 (17 years ago)
Author:
jerome
Message:

Now the pykotme command line tool computes size and price based on the preaccounter
define for each printer instead of always using the internal page counter.
IMPORTANT : pykotme.cgi still uses the old behavior.

Location:
pykota/trunk
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/pykotme

    r3133 r3245  
    3030 
    3131from pykota.tool import PyKotaTool, PyKotaCommandLineError, crashed, N_ 
    32  
    33 from pkpgpdls import analyzer, pdlparser 
     32from pykota.accounter import openAccounter 
    3433     
    3534 
     
    8382        if (not sys.stdin.isatty()) and ("-" not in files) : 
    8483            files.append("-") 
    85         totalsize = 0     
    86         for filename in files :     
    87             try : 
    88                 parser = analyzer.PDLAnalyzer(filename) 
    89                 totalsize += parser.getJobSize() 
    90             except (pdlparser.PDLParserError, IOError), msg :     
    91                 self.printInfo(msg) 
    9284             
    9385        printers = self.storage.getMatchingPrinters(options["printer"]) 
     
    10092            print _("Your account balance : %.2f") % (user.AccountBalance or 0.0) 
    10193             
    102         print _("Job size : %i pages") % totalsize     
    10394        if user.Exists : 
     95            sizeprinted = False 
     96            done = {} 
     97            for printer in printers : 
     98                # Now fake some values. TODO : improve API to not need this anymore     
     99                self.PrinterName = printer.Name 
     100                self.JobSizeBytes = 1 
     101                self.preaccounter = openAccounter(self, ispreaccounter=1) 
     102                key = self.preaccounter.name + self.preaccounter.arguments 
     103                if not done.has_key(key) : 
     104                    totalsize = 0     
     105                    inkusage = [] 
     106                    for filename in files :     
     107                        self.DataFile = filename 
     108                        self.preaccounter.beginJob(None) 
     109                        self.preaccounter.endJob(None) 
     110                        totalsize += self.preaccounter.getJobSize(None) 
     111                        inkusage.extend(self.preaccounter.inkUsage) 
     112                    done[key] = (totalsize, inkusage)     
     113                (totalsize, inkusage) = done[key]     
     114                if not sizeprinted :     
     115                    print _("Job size : %i pages") % totalsize     
     116                    sizeprinted = True 
     117                userpquota = self.storage.getUserPQuota(user, printer) 
     118                if userpquota.Exists : 
     119                    if printer.MaxJobSize and (totalsize > printer.MaxJobSize) : 
     120                        print _("You are not allowed to print so many pages on printer %s at this time.") % printer.Name 
     121                    else :     
     122                        cost = userpquota.computeJobPrice(totalsize, inkusage) 
     123                        msg = _("Cost on printer %s : %.2f") % (printer.Name, cost) 
     124                        if printer.PassThrough : 
     125                            msg = "%s (%s)" % (msg, _("won't be charged, printer is in passthrough mode")) 
     126                        elif user.LimitBy == "nochange" :     
     127                            msg = "%s (%s)" % (msg, _("won't be charged, your account is immutable")) 
     128                        print msg     
    104129            if user.LimitBy == "noprint" : 
    105130                print _("Your account settings forbid you to print at this time.") 
    106             else :     
    107                 for printer in printers : 
    108                     userpquota = self.storage.getUserPQuota(user, printer) 
    109                     if userpquota.Exists : 
    110                         if printer.MaxJobSize and (totalsize > printer.MaxJobSize) : 
    111                             print _("You are not allowed to print so many pages on printer %s at this time.") % printer.Name 
    112                         else :     
    113                             cost = userpquota.computeJobPrice(totalsize) 
    114                             msg = _("Cost on printer %s : %.2f") % (printer.Name, cost) 
    115                             if printer.PassThrough : 
    116                                 msg = "%s (%s)" % (msg, _("won't be charged, printer is in passthrough mode")) 
    117                             elif user.LimitBy == "nochange" :     
    118                                 msg = "%s (%s)" % (msg, _("won't be charged, your account is immutable")) 
    119                             print msg     
    120131             
    121132if __name__ == "__main__" :  
  • pykota/trunk/pykota/accounter.py

    r3184 r3245  
    4040class AccounterBase :     
    4141    """A class to account print usage by querying printers.""" 
    42     def __init__(self, kotafilter, arguments, ispreaccounter=0) : 
     42    def __init__(self, kotafilter, arguments, ispreaccounter=0, name="Unknown") : 
    4343        """Sets instance vars depending on the current printer.""" 
     44        self.name = name 
    4445        self.filter = kotafilter 
    4546        self.arguments = arguments 
     
    106107        raise PyKotaAccounterError, _("Unsupported accounter backend %s") % backend 
    107108    else :     
    108         return accounterbackend.Accounter(kotafilter, args, ispreaccounter) 
     109        return accounterbackend.Accounter(kotafilter, args, ispreaccounter, backend.lower()) 
  • pykota/trunk/pykota/accounters/hardware.py

    r3162 r3245  
    3131 
    3232class Accounter(AccounterBase) : 
    33     def __init__(self, kotabackend, arguments, ispreaccounter=0) : 
     33    def __init__(self, kotabackend, arguments, ispreaccounter=0, name="hardware") : 
    3434        """Initializes querying accounter.""" 
    35         AccounterBase.__init__(self, kotabackend, arguments) 
     35        AccounterBase.__init__(self, kotabackend, arguments, ispreaccounter, name) 
    3636        self.isSoftware = 0 
    3737         
  • pykota/trunk/pykota/accounters/ink.py

    r3133 r3245  
    8787                        self.inkUsage.append(colordict)     
    8888                    jobsize = len(pages) 
    89                     if self.filter.InputFile is not None : 
    90                         # when a filename is passed as an argument, the backend  
    91                         # must generate the correct number of copies. 
    92                         jobsize *= self.filter.Copies 
    93                         self.inkUsage *= self.filter.Copies 
     89                    try : 
     90                        if self.filter.InputFile is not None : 
     91                            # when a filename is passed as an argument, the backend  
     92                            # must generate the correct number of copies. 
     93                            jobsize *= self.filter.Copies 
     94                            self.inkUsage *= self.filter.Copies 
     95                    except AttributeError : # When not run from the cupspykota backend  
     96                        pass 
    9497                    self.filter.logdebug("Ink usage : %s ===> %s" % (cspace, repr(self.inkUsage))) 
    9598        return jobsize         
  • pykota/trunk/pykota/accounters/software.py

    r3133 r3245  
    6666                    self.filter.printInfo(_("Unable to precompute the job's size with the generic PDL analyzer : %s") % msg, "warn") 
    6767                else :     
    68                     if self.filter.InputFile is not None : 
    69                         # when a filename is passed as an argument, the backend  
    70                         # must generate the correct number of copies. 
    71                         jobsize *= self.filter.Copies 
     68                    try : 
     69                        if self.filter.InputFile is not None : 
     70                            # when a filename is passed as an argument, the backend  
     71                            # must generate the correct number of copies. 
     72                            jobsize *= self.filter.Copies 
     73                    except AttributeError : # When not run from the cupspykota backend         
     74                        pass 
    7275                infile.close()         
    7376        return jobsize         
  • pykota/trunk/pykota/storage.py

    r3133 r3245  
    338338                                totalprice += pageprice 
    339339                            else :     
    340                                 coefficients = self.Printer.Coefficients 
     340                                coefficients = upq.Printer.Coefficients 
    341341                                for (ink, value) in usage.items() : 
    342342                                    coefvalue = coefficients.get(ink, 1.0) 
    343343                                    coefprice = (coefvalue * pageprice) / 100.0 
    344344                                    inkprice = coefprice * value 
    345                                     self.parent.tool.logdebug("Applying coefficient %f for color %s (used at %f%% on page %i) to base cost %f gives %f" % (coefvalue, ink, value, pageindex+1, pageprice, inkprice)) 
     345                                    self.parent.tool.logdebug("Applying coefficient %f for color %s (used at %f%% on page %i) to base cost %f on printer %s gives %f" % (coefvalue, ink, value, pageindex+1, pageprice, upq.Printer.Name, inkprice)) 
    346346                                    totalprice += inkprice 
    347347        if self.User.OverCharge != 1.0 : # TODO : beware of rounding errors