[2016] | 1 | # PyKota Print Quota Data Dumper |
---|
| 2 | # |
---|
| 3 | # PyKota - Print Quotas for CUPS and LPRng |
---|
| 4 | # |
---|
[3133] | 5 | # (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com> |
---|
[2016] | 6 | # This program is free software; you can redistribute it and/or modify |
---|
| 7 | # it under the terms of the GNU General Public License as published by |
---|
| 8 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 9 | # (at your option) any later version. |
---|
| 10 | # |
---|
| 11 | # This program is distributed in the hope that it will be useful, |
---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | # GNU General Public License for more details. |
---|
| 15 | # |
---|
| 16 | # You should have received a copy of the GNU General Public License |
---|
| 17 | # along with this program; if not, write to the Free Software |
---|
[2302] | 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[2016] | 19 | # |
---|
| 20 | # $Id$ |
---|
| 21 | # |
---|
[2034] | 22 | # |
---|
[2016] | 23 | |
---|
| 24 | import sys |
---|
| 25 | import os |
---|
| 26 | import pwd |
---|
[2264] | 27 | from xml.sax import saxutils |
---|
| 28 | |
---|
[2016] | 29 | from mx import DateTime |
---|
| 30 | |
---|
| 31 | try : |
---|
| 32 | import jaxml |
---|
| 33 | except ImportError : |
---|
| 34 | sys.stderr.write("The jaxml Python module is not installed. XML output is disabled.\n") |
---|
| 35 | sys.stderr.write("Download jaxml from http://www.librelogiciel.com/software/ or from your Debian archive of choice\n") |
---|
| 36 | hasJAXML = 0 |
---|
| 37 | else : |
---|
| 38 | hasJAXML = 1 |
---|
| 39 | |
---|
| 40 | from pykota import version |
---|
[2512] | 41 | from pykota.tool import PyKotaTool, PyKotaToolError, PyKotaCommandLineError, N_ |
---|
[2016] | 42 | |
---|
| 43 | class DumPyKota(PyKotaTool) : |
---|
| 44 | """A class for dumpykota.""" |
---|
| 45 | validdatatypes = { "history" : N_("History"), |
---|
| 46 | "users" : N_("Users"), |
---|
| 47 | "groups" : N_("Groups"), |
---|
| 48 | "printers" : N_("Printers"), |
---|
| 49 | "upquotas" : N_("Users Print Quotas"), |
---|
| 50 | "gpquotas" : N_("Users Groups Print Quotas"), |
---|
| 51 | "payments" : N_("History of Payments"), |
---|
| 52 | "pmembers" : N_("Printers Groups Membership"), |
---|
| 53 | "umembers" : N_("Users Groups Membership"), |
---|
[2342] | 54 | "billingcodes" : N_("Billing Codes"), |
---|
[2600] | 55 | "all": N_("All"), |
---|
[2016] | 56 | } |
---|
| 57 | validformats = { "csv" : N_("Comma Separated Values"), |
---|
| 58 | "ssv" : N_("Semicolon Separated Values"), |
---|
| 59 | "tsv" : N_("Tabulation Separated Values"), |
---|
| 60 | "xml" : N_("eXtensible Markup Language"), |
---|
| 61 | "cups" : N_("CUPS' page_log"), |
---|
| 62 | } |
---|
| 63 | validfilterkeys = [ "username", |
---|
| 64 | "groupname", |
---|
| 65 | "printername", |
---|
| 66 | "pgroupname", |
---|
[2218] | 67 | "hostname", |
---|
| 68 | "billingcode", |
---|
[3093] | 69 | "jobid", |
---|
[2266] | 70 | "start", |
---|
| 71 | "end", |
---|
[2016] | 72 | ] |
---|
[2032] | 73 | def main(self, arguments, options, restricted=1) : |
---|
[2016] | 74 | """Print Quota Data Dumper.""" |
---|
[2032] | 75 | if restricted and not self.config.isAdmin : |
---|
[2512] | 76 | raise PyKotaCommandLineError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command.")) |
---|
[2016] | 77 | |
---|
| 78 | datatype = options["data"] |
---|
| 79 | if datatype not in self.validdatatypes.keys() : |
---|
[2512] | 80 | raise PyKotaCommandLineError, _("Invalid modifier [%s] for --data command line option, see help.") % datatype |
---|
[2016] | 81 | |
---|
[2600] | 82 | extractonly = {} |
---|
| 83 | if datatype == "all" : |
---|
| 84 | if (options["format"] != "xml") or options["sum"] or arguments : |
---|
| 85 | self.printInfo(_("Dumping all PyKota's datas forces format to XML, and disables --sum and filters."), "warn") |
---|
| 86 | options["format"] = "xml" |
---|
| 87 | options["sum"] = None |
---|
| 88 | else : |
---|
| 89 | for filterexp in arguments : |
---|
| 90 | if filterexp.strip() : |
---|
| 91 | try : |
---|
| 92 | (filterkey, filtervalue) = [part.strip() for part in filterexp.split("=")] |
---|
[3030] | 93 | filterkey = filterkey.lower() |
---|
[2600] | 94 | if filterkey not in self.validfilterkeys : |
---|
| 95 | raise ValueError |
---|
| 96 | except ValueError : |
---|
| 97 | raise PyKotaCommandLineError, _("Invalid filter value [%s], see help.") % filterexp |
---|
| 98 | else : |
---|
| 99 | extractonly.update({ filterkey : filtervalue }) |
---|
| 100 | |
---|
[2016] | 101 | format = options["format"] |
---|
[2567] | 102 | if (format not in self.validformats.keys()) \ |
---|
| 103 | or ((format == "cups") \ |
---|
| 104 | and ((datatype != "history") or options["sum"])) : |
---|
[2512] | 105 | raise PyKotaCommandLineError, _("Invalid modifier [%s] for --format command line option, see help.") % format |
---|
[2016] | 106 | |
---|
| 107 | if (format == "xml") and not hasJAXML : |
---|
| 108 | raise PyKotaToolError, _("XML output is disabled because the jaxml module is not available.") |
---|
| 109 | |
---|
[2285] | 110 | if options["sum"] and datatype not in ("payments", "history") : |
---|
[2512] | 111 | raise PyKotaCommandLineError, _("Invalid data type [%s] for --sum command line option, see help.") % datatype |
---|
[2285] | 112 | |
---|
[2600] | 113 | |
---|
| 114 | retcode = 0 |
---|
| 115 | nbentries = 0 |
---|
| 116 | mustclose = 0 |
---|
| 117 | if options["output"].strip() == "-" : |
---|
| 118 | self.outfile = sys.stdout |
---|
| 119 | else : |
---|
| 120 | self.outfile = open(options["output"], "w") |
---|
| 121 | mustclose = 1 |
---|
| 122 | |
---|
| 123 | if datatype == "all" : |
---|
| 124 | # NB : order does matter to allow easier or faster restore |
---|
| 125 | allentries = [] |
---|
| 126 | datatypes = [ "printers", "pmembers", "users", "groups", \ |
---|
| 127 | "billingcodes", "umembers", "upquotas", \ |
---|
| 128 | "gpquotas", "payments", "history" ] |
---|
[2602] | 129 | neededdatatypes = datatypes[:] |
---|
[2600] | 130 | for datatype in datatypes : |
---|
| 131 | entries = getattr(self.storage, "extract%s" % datatype.title())(extractonly) |
---|
[2601] | 132 | if entries : |
---|
| 133 | nbentries += len(entries) |
---|
| 134 | allentries.append(entries) |
---|
[2602] | 135 | else : |
---|
| 136 | neededdatatypes.remove(datatype) |
---|
| 137 | retcode = self.dumpXml(allentries, neededdatatypes) |
---|
[2600] | 138 | else : |
---|
| 139 | entries = getattr(self.storage, "extract%s" % datatype.title())(extractonly) |
---|
| 140 | if entries : |
---|
| 141 | nbentries = len(entries) |
---|
| 142 | retcode = getattr(self, "dump%s" % format.title())([self.summarizeDatas(entries, datatype, extractonly, options["sum"])], [datatype]) |
---|
[2016] | 143 | |
---|
[2600] | 144 | if mustclose : |
---|
| 145 | self.outfile.close() |
---|
| 146 | if not nbentries : |
---|
| 147 | os.remove(options["output"]) |
---|
[2016] | 148 | |
---|
[2600] | 149 | return retcode |
---|
[2016] | 150 | |
---|
[2295] | 151 | def summarizeDatas(self, entries, datatype, extractonly, sum=0) : |
---|
[2285] | 152 | """Transforms the datas into a summarized view (with totals). |
---|
| 153 | |
---|
| 154 | If sum is false, returns the entries unchanged. |
---|
| 155 | """ |
---|
| 156 | if not sum : |
---|
| 157 | return entries |
---|
| 158 | else : |
---|
[2289] | 159 | headers = entries[0] |
---|
| 160 | nbheaders = len(headers) |
---|
| 161 | fieldnumber = {} |
---|
[2293] | 162 | fieldname = {} |
---|
[2289] | 163 | for i in range(nbheaders) : |
---|
[2295] | 164 | fieldnumber[headers[i]] = i |
---|
| 165 | |
---|
[2289] | 166 | if datatype == "payments" : |
---|
[2293] | 167 | totalize = [ ("amount", float) ] |
---|
[2295] | 168 | keys = [ "username" ] |
---|
| 169 | else : # elif datatype == "history" |
---|
| 170 | totalize = [ ("jobsize", int), |
---|
| 171 | ("jobprice", float), |
---|
| 172 | ("jobsizebytes", int), |
---|
[2664] | 173 | ("precomputedjobsize", int), |
---|
| 174 | ("precomputedjobprice", float), |
---|
[2295] | 175 | ] |
---|
| 176 | keys = [ k for k in ("username", "printername", "hostname", "billingcode") if k in extractonly.keys() ] |
---|
[2293] | 177 | |
---|
[2295] | 178 | newentries = [ headers ] |
---|
| 179 | sortedentries = entries[1:] |
---|
| 180 | if keys : |
---|
| 181 | # If we have several keys, we can sort only on the first one, because they |
---|
| 182 | # will vary the same way. |
---|
| 183 | sortedentries.sort(lambda x, y, fnum=fieldnumber[keys[0]] : cmp(x[fnum], y[fnum])) |
---|
| 184 | totals = {} |
---|
| 185 | for (k, t) in totalize : |
---|
| 186 | totals[k] = { "convert" : t, "value" : 0.0 } |
---|
| 187 | prevkeys = {} |
---|
| 188 | for k in keys : |
---|
| 189 | prevkeys[k] = sortedentries[0][fieldnumber[k]] |
---|
| 190 | for entry in sortedentries : |
---|
| 191 | curval = '-'.join([str(entry[fieldnumber[k]]) for k in keys]) |
---|
| 192 | prevval = '-'.join([str(prevkeys[k]) for k in keys]) |
---|
| 193 | if curval != prevval : |
---|
| 194 | summary = [ "*" ] * nbheaders |
---|
| 195 | for k in keys : |
---|
| 196 | summary[fieldnumber[k]] = prevkeys[k] |
---|
| 197 | for k in totals.keys() : |
---|
| 198 | summary[fieldnumber[k]] = totals[k]["convert"](totals[k]["value"]) |
---|
| 199 | newentries.append(summary) |
---|
| 200 | for k in totals.keys() : |
---|
| 201 | totals[k]["value"] = totals[k]["convert"](entry[fieldnumber[k]]) |
---|
| 202 | else : |
---|
| 203 | for k in totals.keys() : |
---|
[2664] | 204 | totals[k]["value"] += totals[k]["convert"](entry[fieldnumber[k]] or 0.0) |
---|
[2295] | 205 | for k in keys : |
---|
| 206 | prevkeys[k] = entry[fieldnumber[k]] |
---|
| 207 | summary = [ "*" ] * nbheaders |
---|
| 208 | for k in keys : |
---|
| 209 | summary[fieldnumber[k]] = prevkeys[k] |
---|
| 210 | for k in totals.keys() : |
---|
| 211 | summary[fieldnumber[k]] = totals[k]["convert"](totals[k]["value"]) |
---|
| 212 | newentries.append(summary) |
---|
[2289] | 213 | return newentries |
---|
[2285] | 214 | |
---|
[2600] | 215 | def dumpWithSeparator(self, separator, allentries) : |
---|
[2016] | 216 | """Dumps datas with a separator.""" |
---|
[2600] | 217 | for entries in allentries : |
---|
| 218 | for entry in entries : |
---|
| 219 | line = [] |
---|
| 220 | for value in entry : |
---|
| 221 | if type(value).__name__ in ("str", "NoneType") : |
---|
| 222 | line.append('"%s"' % str(value).replace(separator, "\\%s" % separator).replace('"', '\\"')) |
---|
| 223 | else : |
---|
| 224 | line.append(str(value)) |
---|
| 225 | try : |
---|
| 226 | self.outfile.write("%s\n" % separator.join(line)) |
---|
| 227 | except IOError, msg : |
---|
| 228 | sys.stderr.write("%s : %s\n" % (_("PyKota data dumper failed : I/O error"), msg)) |
---|
| 229 | return -1 |
---|
[2016] | 230 | return 0 |
---|
| 231 | |
---|
[2600] | 232 | def dumpCsv(self, allentries, dummy) : |
---|
[2016] | 233 | """Dumps datas with a comma as the separator.""" |
---|
[2600] | 234 | return self.dumpWithSeparator(",", allentries) |
---|
[2016] | 235 | |
---|
[2600] | 236 | def dumpSsv(self, allentries, dummy) : |
---|
[2016] | 237 | """Dumps datas with a comma as the separator.""" |
---|
[2600] | 238 | return self.dumpWithSeparator(";", allentries) |
---|
[2016] | 239 | |
---|
[2600] | 240 | def dumpTsv(self, allentries, dummy) : |
---|
[2016] | 241 | """Dumps datas with a comma as the separator.""" |
---|
[2600] | 242 | return self.dumpWithSeparator("\t", allentries) |
---|
[2016] | 243 | |
---|
[2600] | 244 | def dumpCups(self, allentries, dummy) : |
---|
[2016] | 245 | """Dumps history datas as CUPS' page_log format.""" |
---|
[3121] | 246 | months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ] |
---|
[2600] | 247 | entries = allentries[0] |
---|
[2016] | 248 | fieldnames = entries[0] |
---|
| 249 | fields = {} |
---|
| 250 | for i in range(len(fieldnames)) : |
---|
| 251 | fields[fieldnames[i]] = i |
---|
| 252 | sortindex = fields["jobdate"] |
---|
| 253 | entries = entries[1:] |
---|
[2830] | 254 | entries.sort(lambda m, n, si=sortindex : cmp(m[si], n[si])) |
---|
[2219] | 255 | for entry in entries : |
---|
[2016] | 256 | printername = entry[fields["printername"]] |
---|
| 257 | username = entry[fields["username"]] |
---|
| 258 | jobid = entry[fields["jobid"]] |
---|
[3050] | 259 | jobdate = DateTime.ISO.ParseDateTime(str(entry[fields["jobdate"]])[:19]) |
---|
[2016] | 260 | gmtoffset = jobdate.gmtoffset() |
---|
[3121] | 261 | #jobdate = "%s %+03i00" % (jobdate.strftime("%d/%b/%Y:%H:%M:%S"), gmtoffset.hour) |
---|
| 262 | jobdate = "%02i/%s/%04i:%02i:%02i:%02i %+03i%02i" % (jobdate.day, |
---|
| 263 | months[jobdate.month - 1], |
---|
| 264 | jobdate.year, |
---|
| 265 | jobdate.hour, |
---|
| 266 | jobdate.minute, |
---|
| 267 | jobdate.second, |
---|
| 268 | gmtoffset.hour, |
---|
| 269 | gmtoffset.minute) |
---|
[2016] | 270 | jobsize = entry[fields["jobsize"]] or 0 |
---|
| 271 | copies = entry[fields["copies"]] or 1 |
---|
| 272 | hostname = entry[fields["hostname"]] or "" |
---|
[2217] | 273 | billingcode = entry[fields["billingcode"]] or "-" |
---|
[2163] | 274 | for pagenum in range(1, jobsize+1) : |
---|
[2217] | 275 | self.outfile.write("%s %s %s [%s] %s %s %s %s\n" % (printername, username, jobid, jobdate, pagenum, copies, billingcode, hostname)) |
---|
[2600] | 276 | return 0 |
---|
[2016] | 277 | |
---|
[2600] | 278 | def dumpXml(self, allentries, datatypes) : |
---|
[2016] | 279 | """Dumps datas as XML.""" |
---|
| 280 | x = jaxml.XML_document(encoding="UTF-8") |
---|
| 281 | x.pykota(version=version.__version__, author=version.__author__) |
---|
[2600] | 282 | for (entries, datatype) in zip(allentries, datatypes) : |
---|
[2016] | 283 | x._push() |
---|
[2600] | 284 | x.dump(storage=self.config.getStorageBackend()["storagebackend"], type=datatype) |
---|
| 285 | headers = entries[0] |
---|
| 286 | for entry in entries[1:] : |
---|
| 287 | x._push() |
---|
| 288 | x.entry() |
---|
| 289 | for (header, value) in zip(headers, entry) : |
---|
| 290 | strvalue = str(value) |
---|
| 291 | typval = type(value).__name__ |
---|
| 292 | if header in ("filename", "title", "options", "billingcode") \ |
---|
| 293 | and (typval == "str") : |
---|
| 294 | try : |
---|
[3055] | 295 | strvalue = unicode(strvalue, self.charset).encode("UTF-8") |
---|
[2600] | 296 | except UnicodeError : |
---|
| 297 | pass |
---|
| 298 | strvalue = saxutils.escape(strvalue, { "'" : "'", \ |
---|
| 299 | '"' : """ }) |
---|
| 300 | x.attribute(strvalue, type=typval, name=header) |
---|
| 301 | x._pop() |
---|
[2016] | 302 | x._pop() |
---|
| 303 | x._output(self.outfile) |
---|
[2600] | 304 | return 0 |
---|