Changeset 2600
- Timestamp:
- 12/01/05 23:55:11 (19 years ago)
- Location:
- pykota/trunk
- Files:
-
- 4 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/bin/dumpykota
r2512 r2600 57 57 - umembers : dumps user groups members. 58 58 - billingcodes : dumps billing codes. 59 - all : dumps all PyKota datas. The output format 60 is always XML in this case. 59 61 60 62 NB : the -d | --data command line option … … 69 71 - ssv : separate datas with semicolons 70 72 - tsv : separate datas with tabs 71 - xml : dump data as XML 73 - xml : dump data as XML 72 74 - cups : dump datas in CUPS' page_log format : 73 75 ONLY AVAILABLE WITH --data history -
pykota/trunk/NEWS
r2593 r2600 22 22 PyKota NEWS : 23 23 24 - 1.24alpha5 : 25 26 - dumpykota can now dump the complete database in XML 27 format. 28 24 29 - 1.24alpha4 : 25 30 -
pykota/trunk/pykota/dumper.py
r2567 r2600 53 53 "umembers" : N_("Users Groups Membership"), 54 54 "billingcodes" : N_("Billing Codes"), 55 "all": N_("All"), 55 56 } 56 57 validformats = { "csv" : N_("Comma Separated Values"), … … 74 75 raise PyKotaCommandLineError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command.")) 75 76 76 extractonly = {}77 for filterexp in arguments :78 if filterexp.strip() :79 try :80 (filterkey, filtervalue) = [part.strip() for part in filterexp.split("=")]81 if filterkey not in self.validfilterkeys :82 raise ValueError83 except ValueError :84 raise PyKotaCommandLineError, _("Invalid filter value [%s], see help.") % filterexp85 else :86 extractonly.update({ filterkey : filtervalue })87 88 77 datatype = options["data"] 89 78 if datatype not in self.validdatatypes.keys() : 90 79 raise PyKotaCommandLineError, _("Invalid modifier [%s] for --data command line option, see help.") % datatype 91 80 81 extractonly = {} 82 if datatype == "all" : 83 if (options["format"] != "xml") or options["sum"] or arguments : 84 self.printInfo(_("Dumping all PyKota's datas forces format to XML, and disables --sum and filters."), "warn") 85 options["format"] = "xml" 86 options["sum"] = None 87 else : 88 for filterexp in arguments : 89 if filterexp.strip() : 90 try : 91 (filterkey, filtervalue) = [part.strip() for part in filterexp.split("=")] 92 if filterkey not in self.validfilterkeys : 93 raise ValueError 94 except ValueError : 95 raise PyKotaCommandLineError, _("Invalid filter value [%s], see help.") % filterexp 96 else : 97 extractonly.update({ filterkey : filtervalue }) 98 92 99 format = options["format"] 93 100 if (format not in self.validformats.keys()) \ … … 102 109 raise PyKotaCommandLineError, _("Invalid data type [%s] for --sum command line option, see help.") % datatype 103 110 104 entries = getattr(self.storage, "extract%s" % datatype.title())(extractonly) 105 if entries : 106 mustclose = 0 107 if options["output"].strip() == "-" : 108 self.outfile = sys.stdout 109 else : 110 self.outfile = open(options["output"], "w") 111 mustclose = 1 111 112 retcode = 0 113 nbentries = 0 114 mustclose = 0 115 if options["output"].strip() == "-" : 116 self.outfile = sys.stdout 117 else : 118 self.outfile = open(options["output"], "w") 119 mustclose = 1 120 121 if datatype == "all" : 122 # NB : order does matter to allow easier or faster restore 123 allentries = [] 124 datatypes = [ "printers", "pmembers", "users", "groups", \ 125 "billingcodes", "umembers", "upquotas", \ 126 "gpquotas", "payments", "history" ] 127 for datatype in datatypes : 128 entries = getattr(self.storage, "extract%s" % datatype.title())(extractonly) 129 nbentries += len(entries) 130 allentries.append(entries) 131 retcode = self.dumpXml(allentries, datatypes) 132 133 else : 134 entries = getattr(self.storage, "extract%s" % datatype.title())(extractonly) 135 if entries : 136 nbentries = len(entries) 137 retcode = getattr(self, "dump%s" % format.title())([self.summarizeDatas(entries, datatype, extractonly, options["sum"])], [datatype]) 112 138 113 retcode = getattr(self, "dump%s" % format.title())(self.summarizeDatas(entries, datatype, extractonly, options["sum"]), datatype) 114 115 if mustclose : 116 self.outfile.close() 117 118 return retcode 119 return 0 139 if mustclose : 140 self.outfile.close() 141 if not nbentries : 142 os.remove(options["output"]) 143 144 return retcode 120 145 121 146 def summarizeDatas(self, entries, datatype, extractonly, sum=0) : … … 181 206 return newentries 182 207 183 def dumpWithSeparator(self, separator, entries) :208 def dumpWithSeparator(self, separator, allentries) : 184 209 """Dumps datas with a separator.""" 185 for entry in entries : 186 line = [] 187 for value in entry : 188 if type(value).__name__ in ("str", "NoneType") : 189 line.append('"%s"' % str(value).replace(separator, "\\%s" % separator).replace('"', '\\"')) 190 else : 191 line.append(str(value)) 192 try : 193 self.outfile.write("%s\n" % separator.join(line)) 194 except IOError, msg : 195 sys.stderr.write("%s : %s\n" % (_("PyKota data dumper failed : I/O error"), msg)) 196 return -1 210 for entries in allentries : 211 for entry in entries : 212 line = [] 213 for value in entry : 214 if type(value).__name__ in ("str", "NoneType") : 215 line.append('"%s"' % str(value).replace(separator, "\\%s" % separator).replace('"', '\\"')) 216 else : 217 line.append(str(value)) 218 try : 219 self.outfile.write("%s\n" % separator.join(line)) 220 except IOError, msg : 221 sys.stderr.write("%s : %s\n" % (_("PyKota data dumper failed : I/O error"), msg)) 222 return -1 197 223 return 0 198 224 199 def dumpCsv(self, entries, dummy) :225 def dumpCsv(self, allentries, dummy) : 200 226 """Dumps datas with a comma as the separator.""" 201 return self.dumpWithSeparator(",", entries)227 return self.dumpWithSeparator(",", allentries) 202 228 203 def dumpSsv(self, entries, dummy) :229 def dumpSsv(self, allentries, dummy) : 204 230 """Dumps datas with a comma as the separator.""" 205 return self.dumpWithSeparator(";", entries)231 return self.dumpWithSeparator(";", allentries) 206 232 207 def dumpTsv(self, entries, dummy) :233 def dumpTsv(self, allentries, dummy) : 208 234 """Dumps datas with a comma as the separator.""" 209 return self.dumpWithSeparator("\t", entries)210 211 def dumpCups(self, entries, dummy) :235 return self.dumpWithSeparator("\t", allentries) 236 237 def dumpCups(self, allentries, dummy) : 212 238 """Dumps history datas as CUPS' page_log format.""" 239 entries = allentries[0] 213 240 fieldnames = entries[0] 214 241 fields = {} … … 231 258 for pagenum in range(1, jobsize+1) : 232 259 self.outfile.write("%s %s %s [%s] %s %s %s %s\n" % (printername, username, jobid, jobdate, pagenum, copies, billingcode, hostname)) 233 234 def dumpXml(self, entries, datatype) : 260 return 0 261 262 def dumpXml(self, allentries, datatypes) : 235 263 """Dumps datas as XML.""" 236 264 x = jaxml.XML_document(encoding="UTF-8") 237 265 x.pykota(version=version.__version__, author=version.__author__) 238 x.dump(storage=self.config.getStorageBackend()["storagebackend"], type=datatype) 239 headers = entries[0] 240 for entry in entries[1:] : 266 for (entries, datatype) in zip(allentries, datatypes) : 241 267 x._push() 242 x.entry() 243 for (header, value) in zip(headers, entry) : 244 strvalue = str(value) 245 typval = type(value).__name__ 246 if header in ("filename", "title", "options", "billingcode") \ 247 and (typval == "str") : 248 try : 249 strvalue = unicode(strvalue, self.getCharset()).encode("UTF-8") 250 except UnicodeError : 251 pass 252 strvalue = saxutils.escape(strvalue, { "'" : "'", \ 253 '"' : """ }) 254 x.attribute(strvalue, type=typval, name=header) 268 x.dump(storage=self.config.getStorageBackend()["storagebackend"], type=datatype) 269 headers = entries[0] 270 for entry in entries[1:] : 271 x._push() 272 x.entry() 273 for (header, value) in zip(headers, entry) : 274 strvalue = str(value) 275 typval = type(value).__name__ 276 if header in ("filename", "title", "options", "billingcode") \ 277 and (typval == "str") : 278 try : 279 strvalue = unicode(strvalue, self.getCharset()).encode("UTF-8") 280 except UnicodeError : 281 pass 282 strvalue = saxutils.escape(strvalue, { "'" : "'", \ 283 '"' : """ }) 284 x.attribute(strvalue, type=typval, name=header) 285 x._pop() 255 286 x._pop() 256 287 x._output(self.outfile) 288 return 0 -
pykota/trunk/pykota/version.py
r2593 r2600 22 22 # 23 23 24 __version__ = "1.24alpha 4_unofficial"24 __version__ = "1.24alpha5_unofficial" 25 25 26 26 __doc__ = "PyKota : a complete Printing Quota Solution for CUPS."