Changeset 3361

Show
Ignore:
Timestamp:
03/29/08 16:46:01 (16 years ago)
Author:
jerome
Message:

pkbcodes converted to new style.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/pkbcodes

    r3295 r3361  
    2222# 
    2323 
     24"""A billing codes manager for PyKota.""" 
     25 
    2426import os 
    2527import sys 
     
    2729 
    2830import pykota.appinit 
    29 from pykota.utils import * 
    30  
     31from pykota.utils import run 
     32from pykota.commandline import PyKotaOptionParser 
    3133from pykota.errors import PyKotaCommandLineError 
    3234from pykota.tool import Percent, PyKotaTool 
    3335from pykota.storage import StorageBillingCode 
    3436 
    35 __doc__ = N_("""pkbcodes v%(__version__)s (c) %(__years__)s %(__author__)s 
    36  
    37 A billing codes Manager for PyKota. 
    38  
    39 command line usage : 
    40  
    41   pkbcodes [options] code1 code2 code3 ... codeN 
    42  
    43 options : 
    44  
    45   -v | --version       Prints pkbcodes version number then exits. 
    46   -h | --help          Prints this message then exits. 
    47    
    48   -a | --add           Adds billing codes if they don't exist in PyKota's 
    49                        database. If they exist, they are modified 
    50                        unless -s|--skipexisting is also used. 
    51  
    52   -d | --delete        Deletes billing codes from PyKota's database. 
    53                        NB : the history entries with this billing code 
    54                        are not deleted, voluntarily. 
    55  
    56   -D | --description d Adds a textual description to billing codes. 
    57  
    58   -l | --list          List informations about the billing codes. 
    59  
    60   -r | --reset         Resets the billing codes' balance and page counters 
    61                        to 0. 
    62  
    63   -s | --skipexisting  In combination with the --add option above, tells 
    64                        pkbcodes to not modify existing billing codes. 
    65  
    66   code1 through codeN can contain wildcards if the --add option 
    67   is not set. 
    68  
    69 examples :                               
    70  
    71   $ pkbcodes --add -D "My project" myproj 
    72  
    73   Will create the myproj billing code with "My project" 
    74   as the description. 
    75  
    76   $ pkbcodes --delete "*" 
    77  
    78   This will completely delete all the billing codes, but without 
    79   removing any matching job from the history. USE WITH CARE ANYWAY ! 
    80    
    81   $ pkbcodes --list "my*" 
    82    
    83   This will list all billing codes which name begins with 'my'. 
    84 """) 
    85          
    8637class PKBcodes(PyKotaTool) :         
    8738    """A class for a billing codes manager.""" 
     
    9546    def main(self, names, options) : 
    9647        """Manage billing codes.""" 
    97         if (not self.config.isAdmin) and (not options["list"]) : 
     48        if (not self.config.isAdmin) and (options.action != "list") : 
    9849            raise PyKotaCommandLineError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command.")) 
     50            
     51        islist = (options.action == "list") 
     52        isadd = (options.action == "add") 
     53        isdelete = (options.action == "delete") 
     54         
     55        if not names : 
     56            if isdelete or isadd : 
     57                raise PyKotaCommandLineError, _("You must specify billing codes on the command line.") 
     58            names = [u"*"]     
    9959             
    100         if not options["list"] :     
     60        if not islist : 
    10161            percent = Percent(self) 
    10262             
    103         if not options["add"] : 
    104             if not options["list"] : 
     63        if not isadd : 
     64            if not islist : 
    10565                percent.display("%s..." % _("Extracting datas")) 
    106             if not names :      # NB : can't happen for --delete because it's catched earlier 
     66            if not names : 
    10767                names = ["*"] 
    10868            billingcodes = self.storage.getMatchingBillingCodes(",".join(names)) 
    10969            if not billingcodes : 
    110                 if not options["list"] : 
     70                if not islist : 
    11171                    percent.display("\n") 
    11272                raise PyKotaCommandLineError, _("There's no billingcode matching %s") % " ".join(names) 
    113             if not options["list"] :     
     73            if not islist : 
    11474                percent.setSize(len(billingcodes)) 
    11575                         
    116         if options["list"] : 
     76        if islist : 
    11777            for billingcode in billingcodes : 
    118                 print "%s [%s] %s %s %s %.2f %s" % \ 
     78                self.display("%s [%s] %s %s %s %.2f %s\n" % \ 
    11979                      (billingcode.BillingCode, billingcode.Description, \ 
    12080                       billingcode.PageCounter, \ 
     
    12282                       _("and"), \ 
    12383                       billingcode.Balance, \ 
    124                        _("credits")) 
    125         elif options["delete"] :     
     84                       _("credits"))) 
     85        elif isdelete :     
    12686            percent.display("\n%s..." % _("Deletion")) 
    12787            self.storage.deleteManyBillingCodes(billingcodes) 
    12888            percent.display("\n") 
    12989        else : 
    130             reset = options["reset"] 
    131             description = options["description"] 
     90            description = options.description 
    13291            if description : 
    133                 description = options["description"].strip() 
    134             skipexisting = options["skipexisting"]         
     92                description = description.strip() 
    13593             
    13694            self.storage.beginTransaction() 
    13795            try : 
    138                 if options["add"] :     
     96                if isadd :     
    13997                    percent.display("%s...\n" % _("Creation")) 
    14098                    percent.setSize(len(names)) 
    14199                    for bname in names : 
    142100                        billingcode = StorageBillingCode(self.storage, bname) 
    143                         self.modifyBillingCode(billingcode, reset, description) 
     101                        self.modifyBillingCode(billingcode,  
     102                                               options.reset,  
     103                                               description) 
    144104                        oldbillingcode = self.storage.addBillingCode(billingcode) 
    145105                        if oldbillingcode is not None : 
    146                             if skipexisting : 
    147                                 self.logdebug(_("Billing code [%s] already exists, skipping.") % bname) 
     106                            if options.skipexisting : 
     107                                self.logdebug(_("Billing code '%(bname)s' already exists, skipping.") % locals()) 
    148108                            else :     
    149                                 self.logdebug(_("Billing code [%s] already exists, will be modified.") % bname) 
    150                                 self.modifyBillingCode(oldbillingcode, reset, description) 
     109                                self.logdebug(_("Billing code '%(bname)s' already exists, will be modified.") % locals()) 
     110                                self.modifyBillingCode(oldbillingcode,  
     111                                                       options.reset,  
     112                                                       description) 
    151113                                oldbillingcode.save() 
    152114                        percent.oneMore() 
     
    154116                    percent.display("\n%s...\n" % _("Modification")) 
    155117                    for billingcode in billingcodes : 
    156                         self.modifyBillingCode(billingcode, reset, description) 
     118                        self.modifyBillingCode(billingcode,  
     119                                               options.reset,  
     120                                               description) 
    157121                        billingcode.save()     
    158122                        percent.oneMore() 
     
    163127                self.storage.commitTransaction() 
    164128                         
    165         if not options["list"] : 
     129        if not islist : 
    166130            percent.done() 
    167131                      
    168132if __name__ == "__main__" :  
    169     retcode = 0 
    170     try : 
    171         short_options = "hvaD:dlrs" 
    172         long_options = ["help", "version", "add", "description=", "delete", "list", "reset", "skipexisting"] 
    173          
    174         # Initializes the command line tool 
    175         manager = PKBcodes(doc=__doc__) 
    176         manager.deferredInit() 
    177          
    178         # parse and checks the command line 
    179         (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options) 
    180          
    181         # sets long options 
    182         options["help"] = options["h"] or options["help"] 
    183         options["version"] = options["v"] or options["version"] 
    184         options["add"] = options["a"] or options["add"] 
    185         options["description"] = options["D"] or options["description"] 
    186         options["delete"] = options["d"] or options["delete"]  
    187         options["list"] = options["l"] or options["list"] 
    188         options["reset"] = options["r"] or options["reset"] 
    189         options["skipexisting"] = options["s"] or options["skipexisting"] 
    190          
    191         if options["help"] : 
    192             manager.display_usage_and_quit() 
    193         elif options["version"] : 
    194             manager.display_version_and_quit() 
    195         elif (options["delete"] and (options["add"] or options["reset"] or options["description"])) \ 
    196            or (options["skipexisting"] and not options["add"]) \ 
    197            or (options["list"] and (options["add"] or options["delete"] or options["reset"] or options["description"])) : 
    198             raise PyKotaCommandLineError, _("incompatible options, see help.") 
    199         elif (not args) and (options["add"] or options["delete"]) :     
    200             raise PyKotaCommandLineError, _("You have to pass billing codes on the command line") 
    201         else : 
    202             retcode = manager.main(args, options) 
    203     except KeyboardInterrupt :         
    204         logerr("\nInterrupted with Ctrl+C !\n") 
    205         retcode = -3 
    206     except PyKotaCommandLineError, msg :     
    207         logerr("%s : %s\n" % (sys.argv[0], msg)) 
    208         retcode = -2 
    209     except SystemExit :         
    210         pass 
    211     except : 
    212         try : 
    213             manager.crashed("pkbcodes failed") 
    214         except :     
    215             crashed("pkbcodes failed") 
    216         retcode = -1 
    217  
    218     try : 
    219         manager.storage.close() 
    220     except (TypeError, NameError, AttributeError) :     
    221         pass 
    222          
    223     sys.exit(retcode)     
     133    parser = PyKotaOptionParser(description=_("A billing codes manager for PyKota."), 
     134                                usage="pkbcodes [options] code1 code2 ... codeN") 
     135    parser.add_option("-a", "--add", 
     136                            action="store_const", 
     137                            const="add", 
     138                            dest="action", 
     139                            help=_("Add new, or modify existing, billing codes.")) 
     140    parser.add_option("-d", "--delete", 
     141                            action="store_const", 
     142                            const="delete", 
     143                            dest="action", 
     144                            help=_("Deletes billing codes. Matching entries in the printing history are not deleted, on purpose.")) 
     145    parser.add_option("-D", "--description", 
     146                            dest="description", 
     147                            help=_("Set a textual description for the specified billing codes.")) 
     148    parser.add_option("-l", "--list", 
     149                            action="store_const", 
     150                            const="list", 
     151                            dest="action", 
     152                            help=_("Display information about the specified billing codes.")) 
     153    parser.add_option("-r", "--reset", 
     154                            action="store_true", 
     155                            dest="reset", 
     156                            help=_("Reset the page count and amount spent for the specified billing codes.")) 
     157    parser.add_option("-s", "--skipexisting", 
     158                            action="store_true", 
     159                            dest="skipexisting", 
     160                            help=_("If --add is used, ensure that existing billing codes won't be modified.")) 
     161                             
     162    parser.add_example('-D "Financial Department" financial', 
     163                       _("Would create a billing code labelled 'financial' with the specified textual description.")) 
     164    parser.add_example('--delete "fin*"',  
     165                       _("Would delete all billing codes which label begins with 'fin'. Matching jobs in the printing history wouldn't be deleted though.")) 
     166    parser.add_example("--list", 
     167                       _("Would display details about all existing billing codes.")) 
     168                        
     169    (options, arguments) = parser.parse_args() 
     170    run(parser, PKBcodes)