root / pykota / trunk / pykota / dumper.py @ 3121

Revision 3121, 14.0 kB (checked in by jerome, 17 years ago)

Fixed localized month problem in data dumper with CUPS format.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1# PyKota Print Quota Data Dumper
2#
3# PyKota - Print Quotas for CUPS and LPRng
4#
5# (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com>
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
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19#
20# $Id$
21#
22#
23
24import sys
25import os
26import pwd
27from xml.sax import saxutils
28
29from mx import DateTime
30
31try :
32    import jaxml
33except 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
37else :   
38    hasJAXML = 1
39
40from pykota import version
41from pykota.tool import PyKotaTool, PyKotaToolError, PyKotaCommandLineError, N_
42
43class 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"),
54                       "billingcodes" : N_("Billing Codes"),
55                       "all": N_("All"),
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",
67                        "hostname",
68                        "billingcode",
69                        "jobid", 
70                        "start",
71                        "end",
72                      ]
73    def main(self, arguments, options, restricted=1) :
74        """Print Quota Data Dumper."""
75        if restricted and not self.config.isAdmin :
76            raise PyKotaCommandLineError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command."))
77           
78        datatype = options["data"]
79        if datatype not in self.validdatatypes.keys() :
80            raise PyKotaCommandLineError, _("Invalid modifier [%s] for --data command line option, see help.") % datatype
81                   
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("=")]
93                        filterkey = filterkey.lower()
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           
101        format = options["format"]
102        if (format not in self.validformats.keys()) \
103           or ((format == "cups") \
104              and ((datatype != "history") or options["sum"])) :
105            raise PyKotaCommandLineError, _("Invalid modifier [%s] for --format command line option, see help.") % format
106           
107        if (format == "xml") and not hasJAXML :
108            raise PyKotaToolError, _("XML output is disabled because the jaxml module is not available.")
109           
110        if options["sum"] and datatype not in ("payments", "history") : 
111            raise PyKotaCommandLineError, _("Invalid data type [%s] for --sum command line option, see help.") % datatype
112           
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" ]
129            neededdatatypes = datatypes[:]             
130            for datatype in datatypes :
131                entries = getattr(self.storage, "extract%s" % datatype.title())(extractonly)
132                if entries :
133                    nbentries += len(entries)
134                    allentries.append(entries)
135                else :   
136                    neededdatatypes.remove(datatype)
137            retcode = self.dumpXml(allentries, neededdatatypes)
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])
143               
144        if mustclose :
145            self.outfile.close()
146            if not nbentries : 
147                os.remove(options["output"])
148           
149        return retcode
150       
151    def summarizeDatas(self, entries, datatype, extractonly, sum=0) :   
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 :   
159            headers = entries[0]
160            nbheaders = len(headers)
161            fieldnumber = {}
162            fieldname = {}
163            for i in range(nbheaders) :
164                fieldnumber[headers[i]] = i
165           
166            if datatype == "payments" :
167                totalize = [ ("amount", float) ]
168                keys = [ "username" ]
169            else : # elif datatype == "history"
170                totalize = [ ("jobsize", int), 
171                             ("jobprice", float),
172                             ("jobsizebytes", int),
173                             ("precomputedjobsize", int),
174                             ("precomputedjobprice", float),
175                           ]
176                keys = [ k for k in ("username", "printername", "hostname", "billingcode") if k in extractonly.keys() ]
177               
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() :   
204                        totals[k]["value"] += totals[k]["convert"](entry[fieldnumber[k]] or 0.0)
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)
213            return newentries
214           
215    def dumpWithSeparator(self, separator, allentries) :   
216        """Dumps datas with a separator."""
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
230        return 0       
231       
232    def dumpCsv(self, allentries, dummy) :   
233        """Dumps datas with a comma as the separator."""
234        return self.dumpWithSeparator(",", allentries)
235                           
236    def dumpSsv(self, allentries, dummy) :   
237        """Dumps datas with a comma as the separator."""
238        return self.dumpWithSeparator(";", allentries)
239                           
240    def dumpTsv(self, allentries, dummy) :   
241        """Dumps datas with a comma as the separator."""
242        return self.dumpWithSeparator("\t", allentries)
243       
244    def dumpCups(self, allentries, dummy) :   
245        """Dumps history datas as CUPS' page_log format."""
246        months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]
247        entries = allentries[0]
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:]
254        entries.sort(lambda m, n, si=sortindex : cmp(m[si], n[si]))
255        for entry in entries :   
256            printername = entry[fields["printername"]]
257            username = entry[fields["username"]]
258            jobid = entry[fields["jobid"]]
259            jobdate = DateTime.ISO.ParseDateTime(str(entry[fields["jobdate"]])[:19])
260            gmtoffset = jobdate.gmtoffset()
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)
270            jobsize = entry[fields["jobsize"]] or 0
271            copies = entry[fields["copies"]] or 1
272            hostname = entry[fields["hostname"]] or ""
273            billingcode = entry[fields["billingcode"]] or "-"
274            for pagenum in range(1, jobsize+1) :
275                self.outfile.write("%s %s %s [%s] %s %s %s %s\n" % (printername, username, jobid, jobdate, pagenum, copies, billingcode, hostname))
276        return 0       
277       
278    def dumpXml(self, allentries, datatypes) :
279        """Dumps datas as XML."""
280        x = jaxml.XML_document(encoding="UTF-8")
281        x.pykota(version=version.__version__, author=version.__author__)
282        for (entries, datatype) in zip(allentries, datatypes) :
283            x._push()
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 :
295                            strvalue = unicode(strvalue, self.charset).encode("UTF-8")
296                        except UnicodeError :   
297                            pass
298                        strvalue = saxutils.escape(strvalue, { "'" : "&apos;", \
299                                                               '"' : "&quot;" })
300                    x.attribute(strvalue, type=typval, name=header)
301                x._pop()   
302            x._pop()   
303        x._output(self.outfile)
304        return 0       
Note: See TracBrowser for help on using the browser.