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

Revision 2289, 10.3 kB (checked in by jerome, 19 years ago)

The --sum command line option now works for payments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[2016]1# PyKota Print Quota Data Dumper
2#
3# PyKota - Print Quotas for CUPS and LPRng
4#
5# (c) 2003-2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19#
20# $Id$
21#
[2034]22#
[2016]23
24import sys
25import os
26import pwd
[2264]27from xml.sax import saxutils
28
[2016]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, 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                     }
55    validformats = { "csv" : N_("Comma Separated Values"),
56                     "ssv" : N_("Semicolon Separated Values"),
57                     "tsv" : N_("Tabulation Separated Values"),
58                     "xml" : N_("eXtensible Markup Language"),
59                     "cups" : N_("CUPS' page_log"),
60                   } 
61    validfilterkeys = [ "username",
62                        "groupname",
63                        "printername",
64                        "pgroupname",
[2218]65                        "hostname",
66                        "billingcode",
[2266]67                        "start",
68                        "end",
[2016]69                      ]
[2032]70    def main(self, arguments, options, restricted=1) :
[2016]71        """Print Quota Data Dumper."""
[2032]72        if restricted and not self.config.isAdmin :
[2016]73            raise PyKotaToolError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command."))
74           
75        extractonly = {}
76        for filterexp in arguments :
77            if filterexp.strip() :
78                try :
79                    (filterkey, filtervalue) = [part.strip() for part in filterexp.split("=")]
80                    if filterkey not in self.validfilterkeys :
81                        raise ValueError               
82                except ValueError :   
[2218]83                    raise PyKotaToolError, _("Invalid filter value [%s], see help.") % filterexp
[2016]84                else :   
85                    extractonly.update({ filterkey : filtervalue })
86           
87        datatype = options["data"]
88        if datatype not in self.validdatatypes.keys() :
89            raise PyKotaToolError, _("Invalid modifier [%s] for --data command line option, see help.") % datatype
90                   
91        format = options["format"]
92        if (format not in self.validformats.keys()) \
93              or ((format == "cups") and (datatype != "history")) :
94            raise PyKotaToolError, _("Invalid modifier [%s] for --format command line option, see help.") % format
95           
96        if (format == "xml") and not hasJAXML :
97            raise PyKotaToolError, _("XML output is disabled because the jaxml module is not available.")
98           
[2285]99        if options["sum"] and datatype not in ("payments", "history") : 
[2286]100            raise PyKotaToolError, _("Invalid data type [%s] for --sum command line option, see help.") % datatype
[2285]101           
[2218]102        entries = getattr(self.storage, "extract%s" % datatype.title())(extractonly)
[2016]103        if entries :
104            mustclose = 0   
105            if options["output"].strip() == "-" :   
106                self.outfile = sys.stdout
107            else :   
108                self.outfile = open(options["output"], "w")
109                mustclose = 1
110               
[2285]111            retcode = getattr(self, "dump%s" % format.title())(self.summarizeDatas(entries, datatype, options["sum"]), datatype)
[2016]112           
113            if mustclose :
114                self.outfile.close()
115               
116            return retcode   
117        return 0
118       
[2285]119    def summarizeDatas(self, entries, datatype, sum=0) :   
120        """Transforms the datas into a summarized view (with totals).
121       
122           If sum is false, returns the entries unchanged.
123        """   
124        if not sum :
125            return entries
126        else :   
127            sys.stderr.write("WARNING : --sum command line option is not implemented yet !\n")
[2289]128            headers = entries[0]
129            nbheaders = len(headers)
130            fieldnumber = {}
131            for i in range(nbheaders) :
132                fieldnumber[headers[i]] = i
133            if datatype == "payments" :
134                newentries = [ headers ]
135                fnusername = fieldnumber["username"]
136                fnamount = fieldnumber["amount"]
137                fndate = fieldnumber["date"]
138                sortedentries = entries[1:]
139                sortedentries.sort(lambda x, y, fnum=fnusername : cmp(x[fnum], y[fnum]))
140                amount = 0.0
141                prevusername = sortedentries[0][fnusername]
142                for entry in sortedentries :
143                    if entry[fnusername] != prevusername :
144                        summary = [None] * nbheaders
145                        summary[fnusername] = prevusername
146                        summary[fnamount] = amount
147                        summary[fndate] = "*"
148                        newentries.append(summary)
149                        amount = entry[fnamount]
150                    else :   
151                        amount += entry[fnamount]
152                    prevusername = entry[fnusername]   
153                summary = [None] * nbheaders
154                summary[fnusername] = prevusername
155                summary[fnamount] = amount
156                summary[fndate] = "*"
157                newentries.append(summary)
158            elif datatype == "history" :
159                newentries = entries # Fake this for now
160            else :
161                raise PyKotaToolError, _("Summarizing is not implemented for the [%s] data type, sorry.") % datatype
162            return newentries
[2285]163           
[2016]164    def dumpWithSeparator(self, separator, entries) :   
165        """Dumps datas with a separator."""
166        for entry in entries :
[2264]167            line = []
168            for value in entry :
169                if type(value).__name__ in ("str", "NoneType") :
170                    line.append('"%s"' % str(value).replace(separator, "\\%s" % separator).replace('"', '\\"'))
171                else :   
172                    line.append(str(value))
[2016]173            try :
[2264]174                self.outfile.write("%s\n" % separator.join(line))
[2016]175            except IOError, msg :   
176                sys.stderr.write("%s : %s\n" % (_("PyKota data dumper failed : I/O error"), msg))
177                return -1
178        return 0       
179       
180    def dumpCsv(self, entries, dummy) :   
181        """Dumps datas with a comma as the separator."""
182        return self.dumpWithSeparator(",", entries)
183                           
184    def dumpSsv(self, entries, dummy) :   
185        """Dumps datas with a comma as the separator."""
186        return self.dumpWithSeparator(";", entries)
187                           
188    def dumpTsv(self, entries, dummy) :   
189        """Dumps datas with a comma as the separator."""
190        return self.dumpWithSeparator("\t", entries)
191       
192    def dumpCups(self, entries, dummy) :   
193        """Dumps history datas as CUPS' page_log format."""
194        fieldnames = entries[0]
195        fields = {}
196        for i in range(len(fieldnames)) :
197            fields[fieldnames[i]] = i
198        sortindex = fields["jobdate"]   
199        entries = entries[1:]
[2034]200        entries.sort(lambda m,n,si=sortindex : cmp(m[si], n[si]))
[2219]201        for entry in entries :   
[2016]202            printername = entry[fields["printername"]]
203            username = entry[fields["username"]]
204            jobid = entry[fields["jobid"]]
205            jobdate = DateTime.ISO.ParseDateTime(entry[fields["jobdate"]])
206            gmtoffset = jobdate.gmtoffset()
207            jobdate = "%s %+03i00" % (jobdate.strftime("%d/%b/%Y:%H:%M:%S"), gmtoffset.hour)
208            jobsize = entry[fields["jobsize"]] or 0
209            copies = entry[fields["copies"]] or 1
210            hostname = entry[fields["hostname"]] or ""
[2217]211            billingcode = entry[fields["billingcode"]] or "-"
[2163]212            for pagenum in range(1, jobsize+1) :
[2217]213                self.outfile.write("%s %s %s [%s] %s %s %s %s\n" % (printername, username, jobid, jobdate, pagenum, copies, billingcode, hostname))
[2016]214       
215    def dumpXml(self, entries, datatype) :   
216        """Dumps datas as XML."""
217        x = jaxml.XML_document(encoding="UTF-8")
218        x.pykota(version=version.__version__, author=version.__author__)
219        x.dump(storage=self.config.getStorageBackend()["storagebackend"], type=datatype)
220        headers = entries[0]
221        for entry in entries[1:] :
222            x._push()
223            x.entry()
[2264]224            for (header, value) in zip(headers, entry) :
225                strvalue = str(value)
226                typval = type(value).__name__
227                if header in ("filename", "title", "options", "billingcode") \
228                          and (typval == "str") :
229                    try :
230                        strvalue = unicode(strvalue, self.getCharset()).encode("UTF-8")
231                    except UnicodeError :   
232                        pass
233                    strvalue = saxutils.escape(strvalue, { "'" : "&apos;", \
234                                                           '"' : "&quot;" })
235                x.attribute(strvalue, type=typval, name=header)
[2016]236            x._pop()   
237        x._output(self.outfile)
Note: See TracBrowser for help on using the browser.