Changeset 1934 for pykota

Show
Ignore:
Timestamp:
11/17/04 14:12:04 (20 years ago)
Author:
jalet
Message:

Implemented the --savetoner command line option

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/pkbanner

    r1926 r1934  
    2424# 
    2525# $Log$ 
     26# Revision 1.8  2004/11/17 13:12:04  jalet 
     27# Implemented the --savetoner command line option 
     28# 
    2629# Revision 1.7  2004/11/15 22:29:01  jalet 
    2730# Moved title and filename to the left to free some space. 
     
    8689  -h | --help          Prints this message then exits. 
    8790   
    88   -d | --darkness d    Sets the darkness to d%%. This can be used to 
    89                        save toner. The default value is 100. 
    90                        NOT IMPLEMENTED YET. 
    91    
    92   -p | --pagesize sz   Sets sz as the page size. Most well known 
    93                        page sizes are recognized, like 'A4' or 'Letter' 
    94                        to name a few. 
    95    
    9691  -l | --logo img      Use the image as the banner's logo. The logo will 
    9792                       be drawn at the top center of the page. The default 
    9893                       logo is /usr/share/pykota/logos/pykota.jpeg 
    9994                        
     95  -p | --pagesize sz   Sets sz as the page size. Most well known 
     96                       page sizes are recognized, like 'A4' or 'Letter' 
     97                       to name a few. The default size is A4. 
     98   
     99  -s | --savetoner s   Sets the text luminosity factor to d%%. This can be  
     100                       used to save toner. The default value is 0, which 
     101                       means that no toner saving will be done. 
     102   
    100103  -u | --url u         Uses u as an url to be written at the bottom of  
    101104                       the banner page. The default url is : 
     
    142145        return os.environ.get(varname) or _("Unknown") 
    143146         
    144     def printVar(self, canvas, x, y, label, value, size, darkness) : 
     147    def printVar(self, canvas, x, y, label, value, size, savetoner) : 
    145148        """Outputs a variable onto the PDF canvas. 
    146149         
     
    149152        canvas.saveState() 
    150153        canvas.setFont("Helvetica-Bold", size) 
    151         (r, g, b) =  (0, 0, 0)  # Black : TODO : darkness 
     154        (r, g, b) =  [ color + (savetoner * (1.0 - color)) for color in (0, 0, 0) ] # Black * savetoner 
    152155        canvas.setFillColorRGB(r, g, b) 
    153156        message = "%s :" % _(label) 
    154157        canvas.drawRightString(x, y, message) 
    155158        canvas.setFont("Courier-Bold", size) 
    156         (r, g, b) = (1, 0, 0)   # Red : TODO : darkness 
     159        (r, g, b) =  [ color + (savetoner * (1.0 - color)) for color in (1, 0, 0) ] # Red * savetoner 
    157160        canvas.setFillColorRGB(r, g, b) 
    158161        canvas.drawString(x + 0.5*cm, y, value) 
     
    160163        return (size + 4) 
    161164     
    162     def genPDF(self, pagesize, logo, url, darkness) : 
     165    def genPDF(self, pagesize, logo, url, savetoner) : 
    163166        """Generates the banner in PDF format, return the PDF document as a string.""" 
    164167         
     
    198201         
    199202        # Outputs the username 
    200         ypos -= self.printVar(c, xcenter, ypos, _("Username"), username, 20, darkness)  
     203        ypos -= self.printVar(c, xcenter, ypos, _("Username"), username, 20, savetoner)  
    201204         
    202205        # Printer and Job Id 
    203206        job = "%s - %s" % (printername, self.getVar("PYKOTAJOBID")) 
    204         ypos -= self.printVar(c, xcenter, ypos, _("Job"), job, 14, darkness)  
     207        ypos -= self.printVar(c, xcenter, ypos, _("Job"), job, 14, savetoner)  
    205208         
    206209        # Current date (TODO : at the time the banner was printed ! Change this to job's submission date) 
    207210        datetime = time.strftime("%c", time.localtime()) 
    208         ypos -= self.printVar(c, xcenter, ypos, _("Date"), datetime, 14, darkness)  
     211        ypos -= self.printVar(c, xcenter, ypos, _("Date"), datetime, 14, savetoner)  
    209212         
    210213        # Result of the print job 
     
    216219        elif action == "WARN" :     
    217220            action = _("Allowed with Warning") 
    218         ypos -= self.printVar(c, xcenter, ypos, _("Result"), action, 14, darkness)  
     221        ypos -= self.printVar(c, xcenter, ypos, _("Result"), action, 14, savetoner)  
    219222         
    220223        # skip some space 
     
    224227        # We put them at x=0.25*pagewidth so that the line is long enough to hold them 
    225228        title = self.getVar("PYKOTATITLE") 
    226         ypos -= self.printVar(c, xcenter / 2.0, ypos, _("Title"), title, 10, darkness)  
     229        ypos -= self.printVar(c, xcenter / 2.0, ypos, _("Title"), title, 10, savetoner)  
    227230         
    228231        filename = self.getVar("PYKOTAFILENAME") 
    229         ypos -= self.printVar(c, xcenter / 2.0, ypos, _("Filename"), filename, 10, darkness)  
     232        ypos -= self.printVar(c, xcenter / 2.0, ypos, _("Filename"), filename, 10, savetoner)  
    230233         
    231234        # skip some space 
     
    233236         
    234237        # Now outputs the user's account balance or page counter 
    235         ypos -= self.printVar(c, xcenter, ypos, _("Pages printed so far on %s") % printername, self.getVar("PYKOTAPAGECOUNTER"), 14, darkness)  
     238        ypos -= self.printVar(c, xcenter, ypos, _("Pages printed so far on %s") % printername, self.getVar("PYKOTAPAGECOUNTER"), 14, savetoner)  
    236239        limitby = self.getVar("PYKOTALIMITBY") 
    237240        if limitby == "balance" :   
    238             ypos -= self.printVar(c, xcenter, ypos, _("Account balance"), self.getVar("PYKOTABALANCE"), 14, darkness)  
     241            ypos -= self.printVar(c, xcenter, ypos, _("Account balance"), self.getVar("PYKOTABALANCE"), 14, savetoner)  
    239242        else : 
    240             ypos -= self.printVar(c, xcenter, ypos, _("Soft Limit"), self.getVar("PYKOTASOFTLIMIT"), 14, darkness)  
    241             ypos -= self.printVar(c, xcenter, ypos, _("Hard Limit"), self.getVar("PYKOTAHARDLIMIT"), 14, darkness)  
    242             ypos -= self.printVar(c, xcenter, ypos, _("Date Limit"), self.getVar("PYKOTADATELIMIT"), 14, darkness)  
     243            ypos -= self.printVar(c, xcenter, ypos, _("Soft Limit"), self.getVar("PYKOTASOFTLIMIT"), 14, savetoner)  
     244            ypos -= self.printVar(c, xcenter, ypos, _("Hard Limit"), self.getVar("PYKOTAHARDLIMIT"), 14, savetoner)  
     245            ypos -= self.printVar(c, xcenter, ypos, _("Date Limit"), self.getVar("PYKOTADATELIMIT"), 14, savetoner)  
    243246             
    244247        # URL 
     
    246249            c.saveState() 
    247250            c.setFont("Courier-Bold", 16) 
    248             (r, g, b) = (0, 0, 1)   # Blue : TODO : darkness 
     251            (r, g, b) =  [ color + (savetoner * (1.0 - color)) for color in (0, 0, 1) ] # Blue * savetoner 
    249252            c.setFillColorRGB(r, g, b) 
    250253            c.drawCentredString(xcenter, 2 * cm, url) 
     
    263266             
    264267        try : 
    265             darkness = int(options["darkness"]) 
    266             if (darkness <= 0) or (darkness > 100) : 
    267                 raise ValueError, "Allowed range is (1..100)" 
    268             darkness /= 100.0     
     268            savetoner = int(options["savetoner"]) 
     269            if (savetoner < 0) or (savetoner > 99) : 
     270                raise ValueError, _("Allowed range is (0..99)") 
     271            savetoner /= 100.0     
    269272        except (TypeError, ValueError), msg : 
    270             self.printInfo("Invalid darkness value %s : %s" % (options["darkness"], msg), "warn") 
    271             darkness = 1.0 
     273            self.printInfo(_("Invalid 'savetoner' option %s : %s") % (options["savetoner"], msg), "warn") 
     274            savetoner = 0.0 
    272275             
    273276        pagesize = self.getPageSize(options["pagesize"]) 
    274277        if pagesize is None : 
    275278            pagesize = self.getPageSize("a4") 
    276             self.printInfo("Unknown page size %s, defaulting to A4." % options["pagesize"], "warn") 
     279            self.printInfo(_("Invalid 'pagesize' option %s, defaulting to A4.") % options["pagesize"], "warn") 
    277280             
    278281        self.logdebug("Generating the banner in PDF format...")     
    279         doc = self.genPDF(pagesize, options["logo"].strip(), options["url"].strip(), darkness) 
     282        doc = self.genPDF(pagesize, options["logo"].strip(), options["url"].strip(), savetoner) 
    280283         
    281284        self.logdebug("Converting the banner to PostScript...")     
     
    294297        return status 
    295298 
    296 def getInfo(name) : 
    297     """Extracts some information from the environment.""" 
    298     return os.environ.get(name, _("Unknown")) 
    299  
    300299if __name__ == "__main__" : 
    301300    # TODO : --papertray : to print banners on a different paper (colored for example) 
     
    303302    try : 
    304303        defaults = { \ 
    305                      "darkness" : "100", \ 
     304                     "savetoner" : "100", \ 
    306305                     "pagesize" : "a4", \ 
    307306                     "logo" : "/usr/share/pykota/logos/pykota.jpeg", 
    308307                     "url" : "http://www.librelogiciel.com/software/", 
    309308                   } 
    310         short_options = "vhd:l:p:u:" 
    311         long_options = ["help", "version", "darkness=", "pagesize=", "logo=", "url="] 
     309        short_options = "vhs:l:p:u:" 
     310        long_options = ["help", "version", "savetoner=", "pagesize=", "logo=", "url="] 
    312311         
    313312        # Initializes the command line tool 
     
    320319        options["help"] = options["h"] or options["help"] 
    321320        options["version"] = options["v"] or options["version"] 
    322         options["darkness"] = options["d"] or options["darkness"] or defaults["darkness"] 
     321        options["savetoner"] = options["s"] or options["savetoner"] or defaults["savetoner"] 
    323322        options["pagesize"] = options["p"] or options["pagesize"] or defaults["pagesize"] 
    324         options["logo"] = options["l"] or options["logo"] or defaults["logo"] 
    325323        options["url"] = options["u"] or options["url"] or defaults["url"] 
     324         
     325        options["logo"] = options["l"] or options["logo"] 
     326        if options["logo"] is None : # Allows --logo="" to disable the logo entirely 
     327            options["logo"] = defaults["logo"]   
    326328         
    327329        if options["help"] :