| 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 | |
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]) |
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 |
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() |