Changeset 3305

Show
Ignore:
Timestamp:
01/30/08 00:41:23 (16 years ago)
Author:
jerome
Message:

Now pkbanner uses the new command line parser. A bit rough around
the edges, and requires some refactoring, but it seems to work fine.

Location:
pykota/trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/pkbanner

    r3303 r3305  
    2222# 
    2323 
     24"""A banner generator for PyKota"""         
     25 
    2426import sys 
    2527import os 
     
    4648import pykota.appinit 
    4749from pykota.utils import * 
     50from pykota.commandline import parser 
    4851 
    4952from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
     
    5154from pykota import version 
    5255 
    53 __doc__ = N_("""pkbanner v%(__version__)s (c) %(__years__)s %(__author__)s 
    54  
    55 Generates banners. 
    56  
    57 command line usage : 
    58  
    59   pkbanner  [options]  [more info] 
    60  
    61 options : 
    62  
    63   -v | --version       Prints pkbanner's version number then exits. 
    64   -h | --help          Prints this message then exits. 
    65    
    66   -l | --logo img      Use the image as the banner's logo. The logo will 
    67                        be drawn at the center top of the page. The default 
    68                        logo is /usr/share/pykota/logos/pykota.jpeg 
    69                         
    70   -p | --pagesize sz   Sets sz as the page size. Most well known 
    71                        page sizes are recognized, like 'A4' or 'Letter' 
    72                        to name a few. The default size is A4. 
    73    
    74   -s | --savetoner s   Sets the text luminosity factor to s%%. This can be  
    75                        used to save toner. The default value is 0, which 
    76                        means that no toner saving will be done. 
    77    
    78   -u | --url u         Uses u as an url to be written at the bottom of  
    79                        the banner page. The default url is : 
    80                        http://www.pykota.com/ 
    81    
    82 examples :                               
    83  
    84   Using pkbanner directly from the command line is not recommended, 
    85   excepted for testing purposes. You should use pkbanner in the 
    86   'startingbanner' or 'endingbanner' directives in pykota.conf 
    87    
    88     startingbanner: /usr/bin/pkbanner --logo="" --savetoner=75 
    89    
    90       With such a setting in pykota.conf, all print jobs will be  
    91       prefixed with an A4 banner with no logo, and text luminosity will 
    92       be increased by 75%%. The PostScript output will be directly sent 
    93       to your printer. 
    94        
    95   You'll find more examples in the sample configuration file included     
    96   in PyKota. 
    97 """) 
    98          
    9956class PyKotaBanner(Tool) :         
    10057    """A class for pkbanner.""" 
     
    253210             
    254211        try : 
    255             savetoner = int(options["savetoner"]) 
    256             if (savetoner < 0) or (savetoner > 99) : 
     212            if (options.savetoner < 0) or (options.savetoner > 99) : 
    257213                raise ValueError, _("Allowed range is (0..99)") 
    258             savetoner /= 100.0     
    259         except (TypeError, ValueError), msg : 
    260             self.printInfo(_("Invalid 'savetoner' option %s : %s") % (options["savetoner"], msg), "warn") 
     214        except ValueError, msg : 
     215            self.printInfo(_("Invalid 'savetoner' option %s : %s") % (options.savetoner, msg), "warn") 
    261216            savetoner = 0.0 
    262              
    263         pagesize = self.getPageSize(options["pagesize"]) 
     217        else :     
     218            savetoner = options.savetoner / 100.0     
     219             
     220        pagesize = self.getPageSize(options.pagesize) 
    264221        if pagesize is None : 
    265222            pagesize = self.getPageSize("a4") 
    266             self.printInfo(_("Invalid 'pagesize' option %s, defaulting to A4.") % options["pagesize"], "warn") 
     223            self.printInfo(_("Invalid 'pagesize' option %s, defaulting to A4.") % options.pagesize, "warn") 
    267224             
    268225        self.logdebug("Generating the banner in PDF format...")     
    269226        doc = self.genPDF(pagesize,  
    270                           options["logo"].strip(),  
    271                           options["url"].strip(),  
     227                          options.logo.strip().encode(sys.getfilesystemencoding(), "replace"),  
     228                          options.url.strip(),  
    272229                          " ".join(arguments).strip(),  
    273230                          savetoner) 
     
    293250if __name__ == "__main__" : 
    294251    # TODO : --papertray : to print banners on a different paper (colored for example) 
     252    parser = parser.PyKotaOptionParser(description=_("Banner generator for PyKota")) 
     253    parser.add_option("-l", "--logo", 
     254                            dest="logo", 
     255                            default=u"/usr/share/pykota/logos/pykota.jpeg", 
     256                            help=_("The image to use as the banner's logo. The logo will be drawn at the center top of the page. The default logo is %default")) 
     257    parser.add_option("-p", "--pagesize", 
     258                            dest="pagesize", 
     259                            default=u"A4", 
     260                            help=_("Sets the size of the page. Most well known page sizes are recognized, like 'A4' or 'Letter' to name a few. The default page size is %default")) 
     261    parser.add_option("-s", "--savetoner", 
     262                            dest="savetoner", 
     263                            type="int", 
     264                            default=0, 
     265                            help=_("Sets the text luminosity to this percent. This can be used to save toner. The default value is %default, which means that no toner saving will be done.")) 
     266    parser.add_option("-u", "--url", 
     267                            dest="url", 
     268                            default=u"http://www.pykota.com", 
     269                            help=_("Sets the url to write at the bottom of the banner page. The default url is %default")) 
     270    parser.add_example('--logo="" --savetoner=75', 
     271                       _("This would generate an A4 banner with no logo, and text luminosity would be increased by 75%%.")) 
     272    (options, arguments) = parser.parse_args()                    
     273    if options.version : 
     274        from pykota import version 
     275        sys.stdout.write("%s\n" % version.__version__) 
     276        sys.exit(0) 
     277     
    295278    retcode = 0 
    296279    try : 
    297         defaults = { \ 
    298                      "savetoner" : "0", \ 
    299                      "pagesize" : "a4", \ 
    300                      "logo" : "/usr/share/pykota/logos/pykota.jpeg", 
    301                      "url" : "http://www.pykota.com/", 
    302                    } 
    303         short_options = "vhs:l:p:u:" 
    304         long_options = ["help", "version", "savetoner=", "pagesize=", "logo=", "url="] 
    305          
    306280        # Initializes the command line tool 
    307         banner = PyKotaBanner(doc=__doc__) 
     281        banner = PyKotaBanner() 
    308282        banner.deferredInit() 
    309          
    310         # parse and checks the command line 
    311         (options, args) = banner.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) 
    312          
    313         # sets long options 
    314         options["help"] = options["h"] or options["help"] 
    315         options["version"] = options["v"] or options["version"] 
    316         options["savetoner"] = options["s"] or options["savetoner"] or defaults["savetoner"] 
    317         options["pagesize"] = options["p"] or options["pagesize"] or defaults["pagesize"] 
    318         options["url"] = options["u"] or options["url"] or defaults["url"] 
    319          
    320         options["logo"] = options["l"] or options["logo"] 
    321         if options["logo"] is None : # Allows --logo="" to disable the logo entirely 
    322             options["logo"] = defaults["logo"]   
    323          
    324         if options["help"] : 
    325             banner.display_usage_and_quit() 
    326         elif options["version"] : 
    327             banner.display_version_and_quit() 
    328         else : 
    329             retcode = banner.main(args, options) 
     283        retcode = banner.main(arguments, options) 
    330284    except KeyboardInterrupt :         
    331285        logerr("\nInterrupted with Ctrl+C !\n") 
  • pykota/trunk/pykota/commandline/parser.py

    r3304 r3305  
    3939        optparse.OptionParser.__init__(self, *args, **kwargs) 
    4040        self.disable_interspersed_args() 
    41         self.add_option("-", "--version", 
    42                              action="store_true", 
    43                              dest="version", 
    44                              help=_("show %prog's version number and exit.")) 
     41        self.add_option("-v", "--version", 
     42                              action="store_true", 
     43                              dest="version", 
     44                              help=_("show %prog's version number and exit.")) 
    4545         
    4646    def format_help(self, formatter=None) : 
  • pykota/trunk/setup.py

    r3275 r3305  
    127127      author_email = "alet@librelogiciel.com", 
    128128      url = "http://www.pykota.com", 
    129       packages = [ "pykota", "pykota.storages", "pykota.loggers", "pykota.accounters", "pykota.reporters" ], 
     129      packages = [ "pykota",  
     130                   "pykota.storages",  
     131                   "pykota.loggers",  
     132                   "pykota.accounters",  
     133                   "pykota.reporters", 
     134                   "pykota.commandline", 
     135                 ], 
    130136      scripts = [ "bin/pknotify", "bin/pkusers", "bin/pkinvoice", "bin/pksetup", \ 
    131137                  "bin/pkrefund", "bin/pkturnkey", "bin/pkbcodes", "bin/pkmail", \