Show
Ignore:
Timestamp:
05/03/08 18:58:40 (16 years ago)
Author:
jerome
Message:

dumpykota now support new style command line options.
TODO : dumpykota.cgi

Files:
1 modified

Legend:

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

    r3295 r3368  
    4747class DumPyKota(PyKotaTool) :         
    4848    """A class for dumpykota.""" 
    49     validdatatypes = { "history" : N_("History"), 
    50                        "users" : N_("Users"), 
    51                        "groups" : N_("Groups"), 
    52                        "printers" : N_("Printers"), 
    53                        "upquotas" : N_("Users Print Quotas"), 
    54                        "gpquotas" : N_("Users Groups Print Quotas"), 
    55                        "payments" : N_("History of Payments"), 
    56                        "pmembers" : N_("Printers Groups Membership"),  
    57                        "umembers" : N_("Users Groups Membership"), 
    58                        "billingcodes" : N_("Billing Codes"), 
    59                        "all": N_("All"), 
     49    validdatatypes = { u"history" : N_("History"), 
     50                       u"users" : N_("Users"), 
     51                       u"groups" : N_("Groups"), 
     52                       u"printers" : N_("Printers"), 
     53                       u"upquotas" : N_("Users Print Quotas"), 
     54                       u"gpquotas" : N_("Users Groups Print Quotas"), 
     55                       u"payments" : N_("History of Payments"), 
     56                       u"pmembers" : N_("Printers Groups Membership"),  
     57                       u"umembers" : N_("Users Groups Membership"), 
     58                       u"billingcodes" : N_("Billing Codes"), 
     59                       u"all": N_("All"), 
    6060                     } 
    61     validformats = { "csv" : N_("Comma Separated Values"), 
    62                      "ssv" : N_("Semicolon Separated Values"), 
    63                      "tsv" : N_("Tabulation Separated Values"), 
    64                      "xml" : N_("eXtensible Markup Language"), 
    65                      "cups" : N_("CUPS' page_log"), 
     61    validformats = { u"csv" : N_("Comma Separated Values"), 
     62                     u"ssv" : N_("Semicolon Separated Values"), 
     63                     u"tsv" : N_("Tabulation Separated Values"), 
     64                     u"xml" : N_("eXtensible Markup Language"), 
     65                     u"cups" : N_("CUPS' page_log"), 
    6666                   }   
    6767    validfilterkeys = [ "username", 
     
    7575                        "end", 
    7676                      ] 
    77     def main(self, arguments, options, restricted=1) : 
     77    def main(self, arguments, options, restricted=True) : 
    7878        """Print Quota Data Dumper.""" 
    79         if restricted and not self.config.isAdmin : 
    80             raise PyKotaCommandLineError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command.")) 
    81              
    82         datatype = options["data"] 
     79        self.adminOnly(restricted) 
     80             
     81        datatype = options.data 
    8382        if datatype not in self.validdatatypes.keys() : 
    84             raise PyKotaCommandLineError, _("Invalid modifier [%s] for --data command line option, see help.") % datatype 
     83            raise PyKotaCommandLineError, _("Invalid data type '%(datatype)s', see help.") % locals() 
    8584                     
    86         orderby = options["orderby"]              
     85        orderby = options.orderby or [] 
    8786        if orderby : 
    8887            fields = [f.strip() for f in orderby.split(",")] 
     
    9392                    orderby.append(field) 
    9493                else :     
    95                     self.printInfo("Skipping invalid ordering statement '%(field)s'" % locals(), "error")  
    96         else : 
    97             orderby = [] 
     94                    logerr(_("Skipping invalid ordering statement '%(field)s'") % locals()) 
    9895             
    9996        extractonly = {} 
    100         if datatype == "all" :             
    101             if (options["format"] != "xml") or options["sum"] or arguments : 
     97        if datatype == u"all" :             
     98            if (options.format != u"xml") or options.sum or arguments : 
    10299                self.printInfo(_("Dumping all PyKota's datas forces format to XML, and disables --sum and filters."), "warn") 
    103             options["format"] = "xml" 
    104             options["sum"] = None 
     100            options.format = u"xml" 
     101            options.sum = None 
    105102        else :     
    106103            for filterexp in arguments : 
     
    108105                    try : 
    109106                        (filterkey, filtervalue) = [part.strip() for part in filterexp.split("=")] 
    110                         filterkey = filterkey.lower() 
     107                        filterkey = filterkey.encode("ASCII", "replace").lower() 
    111108                        if filterkey not in self.validfilterkeys : 
    112109                            raise ValueError                 
    113110                    except ValueError :     
    114                         raise PyKotaCommandLineError, _("Invalid filter value [%s], see help.") % filterexp 
     111                        raise PyKotaCommandLineError, _("Invalid filter value '%(filterexp)s', see help.") % locals() 
    115112                    else :     
    116113                        extractonly.update({ filterkey : filtervalue }) 
    117114             
    118         format = options["format"] 
     115        format = options.format 
    119116        if (format not in self.validformats.keys()) \ 
    120            or ((format == "cups") \ 
    121               and ((datatype != "history") or options["sum"])) : 
    122             raise PyKotaCommandLineError, _("Invalid modifier [%s] for --format command line option, see help.") % format 
    123              
    124         if (format == "xml") and not hasJAXML : 
     117           or ((format == u"cups") \ 
     118              and ((datatype != u"history") or options.sum)) : 
     119            raise PyKotaCommandLineError, _("Invalid format '%(format)s', see help.") % locals() 
     120             
     121        if (format == u"xml") and not hasJAXML : 
    125122            raise PyKotaToolError, _("XML output is disabled because the jaxml module is not available.") 
    126123             
    127         if datatype not in ("payments", "history") :  
    128             if options["sum"] :  
    129                 raise PyKotaCommandLineError, _("Invalid data type [%s] for --sum command line option, see help.") % datatype 
    130             if extractonly.has_key("start") or extractonly.has_key("end") :     
    131                 self.printInfo(_("Invalid filter for the %(datatype)s data type.") % locals(), "warn") 
     124        if datatype not in (u"payments", u"history") :  
     125            if options.sum :  
     126                raise PyKotaCommandLineError, _("Invalid data type '%(datatype)s' for --sum command line option, see help.") % locals() 
     127            if extractonly.has_key(u"start") or extractonly.has_key(u"end") :     
     128                self.printInfo(_("Invalid filter for the '%(datatype)s' data type.") % locals(), "warn") 
    132129                try : 
    133                     del extractonly["start"] 
     130                    del extractonly[u"start"] 
    134131                except KeyError :     
    135132                    pass 
    136133                try : 
    137                     del extractonly["end"] 
     134                    del extractonly[u"end"] 
    138135                except KeyError :     
    139136                    pass 
     
    141138        retcode = 0 
    142139        nbentries = 0     
    143         mustclose = 0     
    144         if options["output"].strip() == "-" :     
     140        mustclose = False 
     141        outfname = options.output.strip().encode(sys.getfilesystemencoding()) 
     142        if outfname == "-" :     
    145143            self.outfile = sys.stdout 
    146144        else :     
    147             self.outfile = open(options["output"], "w") 
    148             mustclose = 1 
    149              
    150         if datatype == "all" :     
     145            self.outfile = open(outfname, "w") 
     146            mustclose = True 
     147             
     148        if datatype == u"all" :     
    151149            # NB : order does matter to allow easier or faster restore 
    152150            allentries = [] 
     
    164162            retcode = self.dumpXml(allentries, neededdatatypes) 
    165163        else :     
     164            datatype = datatype.encode("ASCII") 
     165            format = format.encode("ASCII") 
    166166            entries = getattr(self.storage, "extract%s" % datatype.title())(extractonly, orderby) 
    167167            if entries : 
    168168                nbentries = len(entries) 
    169                 retcode = getattr(self, "dump%s" % format.title())([self.summarizeDatas(entries, datatype, extractonly, options["sum"])], [datatype]) 
     169                retcode = getattr(self, "dump%s" % format.title())([self.summarizeDatas(entries, datatype, extractonly, options.sum)], [datatype]) 
    170170                 
    171171        if mustclose : 
    172172            self.outfile.close() 
    173173            if not nbentries :  
    174                 os.remove(options["output"]) 
     174                os.remove(options.output) 
    175175             
    176176        return retcode 
     
    258258                    self.outfile.write("%s\n" % separator.join(line)) 
    259259                except IOError, msg :     
    260                     sys.stderr.write("%s : %s\n" % (_("PyKota data dumper failed : I/O error"), msg)) 
     260                    self.printInfo("%s : %s" % (_("PyKota data dumper failed : I/O error"), msg), "error") 
    261261                    return -1 
    262262        return 0         
     
    291291            jobdate = DateTime.ISO.ParseDateTime(str(entry[fields["jobdate"]])[:19]) 
    292292            gmtoffset = jobdate.gmtoffset() 
    293             #jobdate = "%s %+03i00" % (jobdate.strftime("%d/%b/%Y:%H:%M:%S"), gmtoffset.hour) 
    294293            jobdate = "%02i/%s/%04i:%02i:%02i:%02i %+03i%02i" % (jobdate.day, 
    295294                                                                 months[jobdate.month - 1], 
     
    305304            billingcode = entry[fields["billingcode"]] or "-" 
    306305            for pagenum in range(1, jobsize+1) : 
    307                 self.outfile.write("%s %s %s [%s] %s %s %s %s\n" % (printername, username, jobid, jobdate, pagenum, copies, billingcode, hostname)) 
     306                line = "%s %s %s [%s] %s %s %s %s" % (printername, username, jobid, jobdate, pagenum, copies, billingcode, hostname) 
     307                self.outfile.write("%s\n" % line.encode(self.charset,  
     308                                                        "replace")) 
    308309        return 0         
    309310