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 | # |
---|
22 | # $Log$ |
---|
23 | # Revision 1.2 2005/01/19 08:49:41 jalet |
---|
24 | # Now dumpykota.cgi behaves like printquota.cgi wrt the REMOTE_USER environment |
---|
25 | # variables if the script is username+password protected. |
---|
26 | # Small fix in printquota.cgi wrt ldap auth with Apache : the workaround was |
---|
27 | # not used everywhere. |
---|
28 | # |
---|
29 | # Revision 1.1 2005/01/08 17:03:07 jalet |
---|
30 | # "--format cups" output more resembling CUPS' page_log. |
---|
31 | # Split into a command line tool and a module, to allow easier coding of |
---|
32 | # a CGI interface. |
---|
33 | # |
---|
34 | # |
---|
35 | # |
---|
36 | |
---|
37 | import sys |
---|
38 | import os |
---|
39 | import pwd |
---|
40 | from mx import DateTime |
---|
41 | |
---|
42 | try : |
---|
43 | import jaxml |
---|
44 | except ImportError : |
---|
45 | sys.stderr.write("The jaxml Python module is not installed. XML output is disabled.\n") |
---|
46 | sys.stderr.write("Download jaxml from http://www.librelogiciel.com/software/ or from your Debian archive of choice\n") |
---|
47 | hasJAXML = 0 |
---|
48 | else : |
---|
49 | hasJAXML = 1 |
---|
50 | |
---|
51 | from pykota import version |
---|
52 | from pykota.tool import PyKotaTool, PyKotaToolError, N_ |
---|
53 | |
---|
54 | class DumPyKota(PyKotaTool) : |
---|
55 | """A class for dumpykota.""" |
---|
56 | validdatatypes = { "history" : N_("History"), |
---|
57 | "users" : N_("Users"), |
---|
58 | "groups" : N_("Groups"), |
---|
59 | "printers" : N_("Printers"), |
---|
60 | "upquotas" : N_("Users Print Quotas"), |
---|
61 | "gpquotas" : N_("Users Groups Print Quotas"), |
---|
62 | "payments" : N_("History of Payments"), |
---|
63 | "pmembers" : N_("Printers Groups Membership"), |
---|
64 | "umembers" : N_("Users Groups Membership"), |
---|
65 | } |
---|
66 | validformats = { "csv" : N_("Comma Separated Values"), |
---|
67 | "ssv" : N_("Semicolon Separated Values"), |
---|
68 | "tsv" : N_("Tabulation Separated Values"), |
---|
69 | "xml" : N_("eXtensible Markup Language"), |
---|
70 | "cups" : N_("CUPS' page_log"), |
---|
71 | } |
---|
72 | validfilterkeys = [ "username", |
---|
73 | "groupname", |
---|
74 | "printername", |
---|
75 | "pgroupname", |
---|
76 | ] |
---|
77 | def main(self, arguments, options, restricted=1) : |
---|
78 | """Print Quota Data Dumper.""" |
---|
79 | if restricted and not self.config.isAdmin : |
---|
80 | raise PyKotaToolError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command.")) |
---|
81 | |
---|
82 | extractonly = {} |
---|
83 | for filterexp in arguments : |
---|
84 | if filterexp.strip() : |
---|
85 | try : |
---|
86 | (filterkey, filtervalue) = [part.strip() for part in filterexp.split("=")] |
---|
87 | if filterkey not in self.validfilterkeys : |
---|
88 | raise ValueError |
---|
89 | except ValueError : |
---|
90 | raise PyKotaToolError, _("Invalid value [%s] for --filter command line option, see help.") % filterexp |
---|
91 | else : |
---|
92 | extractonly.update({ filterkey : filtervalue }) |
---|
93 | |
---|
94 | datatype = options["data"] |
---|
95 | if datatype not in self.validdatatypes.keys() : |
---|
96 | raise PyKotaToolError, _("Invalid modifier [%s] for --data command line option, see help.") % datatype |
---|
97 | |
---|
98 | format = options["format"] |
---|
99 | if (format not in self.validformats.keys()) \ |
---|
100 | or ((format == "cups") and (datatype != "history")) : |
---|
101 | raise PyKotaToolError, _("Invalid modifier [%s] for --format command line option, see help.") % format |
---|
102 | |
---|
103 | if (format == "xml") and not hasJAXML : |
---|
104 | raise PyKotaToolError, _("XML output is disabled because the jaxml module is not available.") |
---|
105 | |
---|
106 | entries = getattr(self.storage, "extract%s" % datatype.title())(extractonly) |
---|
107 | if entries : |
---|
108 | mustclose = 0 |
---|
109 | if options["output"].strip() == "-" : |
---|
110 | self.outfile = sys.stdout |
---|
111 | else : |
---|
112 | self.outfile = open(options["output"], "w") |
---|
113 | mustclose = 1 |
---|
114 | |
---|
115 | retcode = getattr(self, "dump%s" % format.title())(entries, datatype) |
---|
116 | |
---|
117 | if mustclose : |
---|
118 | self.outfile.close() |
---|
119 | |
---|
120 | return retcode |
---|
121 | return 0 |
---|
122 | |
---|
123 | def dumpWithSeparator(self, separator, entries) : |
---|
124 | """Dumps datas with a separator.""" |
---|
125 | for entry in entries : |
---|
126 | line = separator.join([ '"%s"' % field for field in entry ]) |
---|
127 | try : |
---|
128 | self.outfile.write("%s\n" % line) |
---|
129 | except IOError, msg : |
---|
130 | sys.stderr.write("%s : %s\n" % (_("PyKota data dumper failed : I/O error"), msg)) |
---|
131 | return -1 |
---|
132 | return 0 |
---|
133 | |
---|
134 | def dumpCsv(self, entries, dummy) : |
---|
135 | """Dumps datas with a comma as the separator.""" |
---|
136 | return self.dumpWithSeparator(",", entries) |
---|
137 | |
---|
138 | def dumpSsv(self, entries, dummy) : |
---|
139 | """Dumps datas with a comma as the separator.""" |
---|
140 | return self.dumpWithSeparator(";", entries) |
---|
141 | |
---|
142 | def dumpTsv(self, entries, dummy) : |
---|
143 | """Dumps datas with a comma as the separator.""" |
---|
144 | return self.dumpWithSeparator("\t", entries) |
---|
145 | |
---|
146 | def dumpCups(self, entries, dummy) : |
---|
147 | """Dumps history datas as CUPS' page_log format.""" |
---|
148 | fieldnames = entries[0] |
---|
149 | fields = {} |
---|
150 | for i in range(len(fieldnames)) : |
---|
151 | fields[fieldnames[i]] = i |
---|
152 | sortindex = fields["jobdate"] |
---|
153 | entries = entries[1:] |
---|
154 | entries.sort(lambda m,n : cmp(m[sortindex], n[sortindex])) |
---|
155 | for entry in entries[1:] : |
---|
156 | printername = entry[fields["printername"]] |
---|
157 | username = entry[fields["username"]] |
---|
158 | jobid = entry[fields["jobid"]] |
---|
159 | jobdate = DateTime.ISO.ParseDateTime(entry[fields["jobdate"]]) |
---|
160 | gmtoffset = jobdate.gmtoffset() |
---|
161 | jobdate = "%s %+03i00" % (jobdate.strftime("%d/%b/%Y:%H:%M:%S"), gmtoffset.hour) |
---|
162 | jobsize = entry[fields["jobsize"]] or 0 |
---|
163 | copies = entry[fields["copies"]] or 1 |
---|
164 | hostname = entry[fields["hostname"]] or "" |
---|
165 | self.outfile.write("%s %s %s [%s] %s %s - %s\n" % (printername, username, jobid, jobdate, jobsize, copies, hostname)) |
---|
166 | |
---|
167 | def dumpXml(self, entries, datatype) : |
---|
168 | """Dumps datas as XML.""" |
---|
169 | x = jaxml.XML_document(encoding="UTF-8") |
---|
170 | x.pykota(version=version.__version__, author=version.__author__) |
---|
171 | x.dump(storage=self.config.getStorageBackend()["storagebackend"], type=datatype) |
---|
172 | headers = entries[0] |
---|
173 | for entry in entries[1:] : |
---|
174 | x._push() |
---|
175 | x.entry() |
---|
176 | for i in range(len(entry)) : |
---|
177 | value = str(entry[i]) |
---|
178 | typval = type(entry[i]).__name__ |
---|
179 | try : |
---|
180 | value = unicode(value, self.getCharset()).encode("UTF-8") |
---|
181 | except UnicodeError : |
---|
182 | pass |
---|
183 | x.attribute(value, type=typval, name=headers[i]) |
---|
184 | x._pop() |
---|
185 | x._output(self.outfile) |
---|