Changeset 3368 for pykota/trunk/pykota/dumper.py
- Timestamp:
- 05/03/08 18:58:40 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/dumper.py
r3295 r3368 47 47 class DumPyKota(PyKotaTool) : 48 48 """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"), 60 60 } 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"), 66 66 } 67 67 validfilterkeys = [ "username", … … 75 75 "end", 76 76 ] 77 def main(self, arguments, options, restricted= 1) :77 def main(self, arguments, options, restricted=True) : 78 78 """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 83 82 if datatype not in self.validdatatypes.keys() : 84 raise PyKotaCommandLineError, _("Invalid modifier [%s] for --data command line option, see help.") % datatype83 raise PyKotaCommandLineError, _("Invalid data type '%(datatype)s', see help.") % locals() 85 84 86 orderby = options ["orderby"]85 orderby = options.orderby or [] 87 86 if orderby : 88 87 fields = [f.strip() for f in orderby.split(",")] … … 93 92 orderby.append(field) 94 93 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()) 98 95 99 96 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 : 102 99 self.printInfo(_("Dumping all PyKota's datas forces format to XML, and disables --sum and filters."), "warn") 103 options ["format"] ="xml"104 options ["sum"]= None100 options.format = u"xml" 101 options.sum = None 105 102 else : 106 103 for filterexp in arguments : … … 108 105 try : 109 106 (filterkey, filtervalue) = [part.strip() for part in filterexp.split("=")] 110 filterkey = filterkey. lower()107 filterkey = filterkey.encode("ASCII", "replace").lower() 111 108 if filterkey not in self.validfilterkeys : 112 109 raise ValueError 113 110 except ValueError : 114 raise PyKotaCommandLineError, _("Invalid filter value [%s], see help.") % filterexp111 raise PyKotaCommandLineError, _("Invalid filter value '%(filterexp)s', see help.") % locals() 115 112 else : 116 113 extractonly.update({ filterkey : filtervalue }) 117 114 118 format = options ["format"]115 format = options.format 119 116 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.") % format123 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 : 125 122 raise PyKotaToolError, _("XML output is disabled because the jaxml module is not available.") 126 123 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.") % datatype130 if extractonly.has_key( "start") or extractonly.has_key("end") :131 self.printInfo(_("Invalid filter for the %(datatype)sdata 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") 132 129 try : 133 del extractonly[ "start"]130 del extractonly[u"start"] 134 131 except KeyError : 135 132 pass 136 133 try : 137 del extractonly[ "end"]134 del extractonly[u"end"] 138 135 except KeyError : 139 136 pass … … 141 138 retcode = 0 142 139 nbentries = 0 143 mustclose = 0 144 if options["output"].strip() == "-" : 140 mustclose = False 141 outfname = options.output.strip().encode(sys.getfilesystemencoding()) 142 if outfname == "-" : 145 143 self.outfile = sys.stdout 146 144 else : 147 self.outfile = open(o ptions["output"], "w")148 mustclose = 1149 150 if datatype == "all" :145 self.outfile = open(outfname, "w") 146 mustclose = True 147 148 if datatype == u"all" : 151 149 # NB : order does matter to allow easier or faster restore 152 150 allentries = [] … … 164 162 retcode = self.dumpXml(allentries, neededdatatypes) 165 163 else : 164 datatype = datatype.encode("ASCII") 165 format = format.encode("ASCII") 166 166 entries = getattr(self.storage, "extract%s" % datatype.title())(extractonly, orderby) 167 167 if entries : 168 168 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]) 170 170 171 171 if mustclose : 172 172 self.outfile.close() 173 173 if not nbentries : 174 os.remove(options ["output"])174 os.remove(options.output) 175 175 176 176 return retcode … … 258 258 self.outfile.write("%s\n" % separator.join(line)) 259 259 except IOError, msg : 260 s ys.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") 261 261 return -1 262 262 return 0 … … 291 291 jobdate = DateTime.ISO.ParseDateTime(str(entry[fields["jobdate"]])[:19]) 292 292 gmtoffset = jobdate.gmtoffset() 293 #jobdate = "%s %+03i00" % (jobdate.strftime("%d/%b/%Y:%H:%M:%S"), gmtoffset.hour)294 293 jobdate = "%02i/%s/%04i:%02i:%02i:%02i %+03i%02i" % (jobdate.day, 295 294 months[jobdate.month - 1], … … 305 304 billingcode = entry[fields["billingcode"]] or "-" 306 305 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")) 308 309 return 0 309 310