#! /usr/bin/python # -*- coding: ISO-8859-15 -*- # PyKota Print Quota Reports generator # # PyKota - Print Quotas for CUPS and LPRng # # (c) 2003, 2004, 2005 Jerome Alet # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. # # $Id$ # # $Log$ # Revision 1.5 2005/01/19 08:49:41 jalet # Now dumpykota.cgi behaves like printquota.cgi wrt the REMOTE_USER environment # variables if the script is username+password protected. # Small fix in printquota.cgi wrt ldap auth with Apache : the workaround was # not used everywhere. # # Revision 1.4 2005/01/17 08:44:24 jalet # Modified copyright years # # Revision 1.3 2005/01/10 14:57:29 jalet # Fixed the default datatype which is now set to 'history' # # Revision 1.2 2005/01/08 19:47:00 jalet # Fixed a bad copy&paste # # Revision 1.1 2005/01/08 19:13:11 jalet # dumpykota.cgi was added to allow the use of dumpykota through the web. # This makes real time interfacing with the third party software phpPrintAnalyzer # a breeze ! # # import sys import os import cgi import urllib from pykota import version from pykota.tool import PyKotaToolError from pykota.dumper import DumPyKota from pykota.cgifuncs import getLanguagePreference, getCharsetPreference header = """Content-type: text/html %s

PyKota's Logo
PyKota v%s

%s

%s

""" footer = """
""" class PyKotaDumperGUI(DumPyKota) : """PyKota Dumper GUI""" def guiDisplay(self) : """Displays the dumper interface.""" global header, footer print header % (self.getCharset(), _("PyKota Data Dumper"), version.__version__, _("PyKota Data Dumper"), _("Dump"), _("Please click on the above button")) print self.htmlListDataTypes(self.options.get("data", "")) print "
" print self.htmlListFormats(self.options.get("format", "")) print "
" print self.htmlFilterInput(" ".join(self.arguments)) print footer % _("Dump") def htmlListDataTypes(self, selected="") : """Displays the datatype selection list.""" message = '
%s :
' return message def htmlListFormats(self, selected="") : """Displays the formats selection list.""" message = '
%s :
' return message def htmlFilterInput(self, value="") : """Input the optional dump filter.""" return _("Filter") + (' : e.g. username=jerome printername=HP2100' % (value or "")) def guiAction(self) : """Main function""" try : wantreport = self.form.has_key("report") except TypeError : pass # WebDAV request probably, seen when trying to open a csv file in OOo else : if wantreport : if self.form.has_key("datatype") : self.options["data"] = self.form["datatype"].value if self.form.has_key("format") : self.options["format"] = self.form["format"].value if self.form.has_key("filter") : self.arguments = self.form["filter"].value.split() # when no authentication is done, or when the remote username # is 'root' (even if not run as root of course), then unrestricted # dump is allowed. remuser = os.environ.get("REMOTE_USER", "root") # special hack to accomodate mod_auth_ldap Apache module try : remuser = remuser.split("=")[1].split(",")[0] except IndexError : pass if remuser != "root" : # non-'root' users when the script is password protected # can not dump any data as they like, we restrict them # to their own datas. if self.options["data"] not in ["printers", "pmembers", "groups", "gpquotas"] : self.arguments.append("username=%s" % remuser) if self.options["format"] in ("csv", "ssv") : #ctype = "application/vnd.sun.xml.calc" # OpenOffice.org ctype = "text/comma-separated-values" fname = "dump.csv" elif self.options["format"] == "tsv" : #ctype = "application/vnd.sun.xml.calc" # OpenOffice.org ctype = "text/tab-separated-values" fname = "dump.tsv" elif self.options["format"] == "xml" : ctype = "text/xml" fname = "dump.xml" elif self.options["format"] == "cups" : ctype = "text/plain" fname = "page_log" print "Content-type: %s" % ctype print "Content-disposition: attachment; filename=%s" % fname print try : self.main(self.arguments, self.options, restricted=0) except PyKotaToolError, msg : print msg else : self.guiDisplay() if __name__ == "__main__" : os.environ["LC_ALL"] = getLanguagePreference() admin = PyKotaDumperGUI(lang=os.environ["LC_ALL"], charset=getCharsetPreference()) admin.form = cgi.FieldStorage() admin.options = { "output" : "-", "data" : "history", "format" : "cups", } admin.arguments = [] admin.guiAction() try : admin.storage.close() except (TypeError, NameError, AttributeError) : pass sys.exit(0)