root / pykota / trunk / cgi-bin / dumpykota.cgi @ 2028

Revision 2028, 6.9 kB (checked in by jalet, 20 years ago)

Modified copyright years

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#! /usr/bin/python
2# -*- coding: ISO-8859-15 -*-
3
4# PyKota Print Quota Reports generator
5#
6# PyKota - Print Quotas for CUPS and LPRng
7#
8# (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com>
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22#
23# $Id$
24#
25# $Log$
26# Revision 1.4  2005/01/17 08:44:24  jalet
27# Modified copyright years
28#
29# Revision 1.3  2005/01/10 14:57:29  jalet
30# Fixed the default datatype which is now set to 'history'
31#
32# Revision 1.2  2005/01/08 19:47:00  jalet
33# Fixed a bad copy&paste
34#
35# Revision 1.1  2005/01/08 19:13:11  jalet
36# dumpykota.cgi was added to allow the use of dumpykota through the web.
37# This makes real time interfacing with the third party software phpPrintAnalyzer
38# a breeze !
39#
40#
41
42import sys
43import os
44import cgi
45import urllib
46
47from pykota import version
48from pykota.tool import PyKotaToolError
49from pykota.dumper import DumPyKota
50from pykota.cgifuncs import getLanguagePreference, getCharsetPreference
51
52header = """Content-type: text/html
53
54<?xml version="1.0" encoding="%s"?>
55<html>
56  <head>
57    <title>%s</title>
58    <link rel="stylesheet" type="text/css" href="/pykota.css" />
59  </head>
60  <body>
61    <form action="dumpykota.cgi" method="GET">
62      <table>
63        <tr>
64          <td>
65            <p>
66              <a href="http://www.librelogiciel.com/software/"><img src="http://www.librelogiciel.com/software/PyKota/pykota.png" alt="PyKota's Logo" /></a>
67              <br />
68              <a href="http://www.librelogiciel.com/software/">PyKota v%s</a>
69            </p>
70          </td>
71          <td colspan="2">
72            <h1>%s</h1>
73          </td>
74        </tr>
75        <tr>
76          <td colspan="3" align="center">
77            <input type="submit" name="report" value="%s" />
78          </td>
79        </tr>
80      </table>
81      <p>
82        %s
83      </p>"""
84   
85footer = """
86      <table>
87        <tr>
88          <td colspan="3" align="center">
89            <input type="submit" name="report" value="%s" />
90          </td>
91        </tr>
92      </table> 
93    </form>
94  </body>
95</html>""" 
96
97class PyKotaDumperGUI(DumPyKota) :
98    """PyKota Dumper GUI"""
99    def guiDisplay(self) :
100        """Displays the dumper interface."""
101        global header, footer
102        print header % (self.getCharset(), _("PyKota Data Dumper"), version.__version__, _("PyKota Data Dumper"), _("Dump"), _("Please click on the above button"))
103        print self.htmlListDataTypes(self.options.get("data", ""))
104        print "<br />"
105        print self.htmlListFormats(self.options.get("format", ""))
106        print "<br />"
107        print self.htmlFilterInput(" ".join(self.arguments))
108        print footer % _("Dump")
109       
110    def htmlListDataTypes(self, selected="") :   
111        """Displays the datatype selection list."""
112        message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="datatype">' % _("Data Type")
113        for dt in self.validdatatypes.items() :
114            if dt[0] == selected :
115                message += '<option value="%s" selected="selected">%s (%s)</option>' % (dt[0], dt[0], dt[1])
116            else :
117                message += '<option value="%s">%s (%s)</option>' % (dt[0], dt[0], dt[1])
118        message += '</select></td></tr></table>'
119        return message
120       
121    def htmlListFormats(self, selected="") :   
122        """Displays the formats selection list."""
123        message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="format">' % _("Output Format")
124        for fmt in self.validformats.items() :
125            if fmt[0] == selected :
126                message += '<option value="%s" selected="selected">%s (%s)</option>' % (fmt[0], fmt[0], fmt[1])
127            else :
128                message += '<option value="%s">%s (%s)</option>' % (fmt[0], fmt[0], fmt[1])
129        message += '</select></td></tr></table>'
130        return message
131       
132    def htmlFilterInput(self, value="") :   
133        """Input the optional dump filter."""
134        return _("Filter") + (' : <input type="text" name="filter" size="40" value="%s" /> <em>e.g. <strong>username=jerome printername=HP2100</strong></em>' % (value or ""))
135       
136    def guiAction(self) :
137        """Main function"""
138        try :
139            wantreport = self.form.has_key("report")
140        except TypeError :   
141            pass # WebDAV request probably, seen when trying to open a csv file in OOo
142        else :   
143            if wantreport :
144                if self.form.has_key("datatype") :
145                    self.options["data"] = self.form["datatype"].value
146                if self.form.has_key("format") :
147                    self.options["format"] = self.form["format"].value
148                if self.form.has_key("filter") :   
149                    self.arguments = self.form["filter"].value.split()
150                   
151                if self.options["format"] in ("csv", "ssv") :
152                    #ctype = "application/vnd.sun.xml.calc"
153                    ctype = "text/comma-separated-values"
154                    fname = "dump.csv"
155                elif self.options["format"] == "tsv" :
156                    #ctype = "application/vnd.sun.xml.calc"
157                    ctype = "text/tab-separated-values"
158                    fname = "dump.tsv"
159                elif self.options["format"] == "xml" :
160                    ctype = "text/xml"
161                    fname = "dump.xml"
162                elif self.options["format"] == "cups" :
163                    ctype = "text/plain"
164                    fname = "page_log"
165                print "Content-type: %s" % ctype   
166                print "Content-disposition: attachment; filename=%s" % fname
167                print
168                try :
169                    self.main(self.arguments, self.options)
170                except PyKotaToolError, msg :   
171                    print msg
172            else :       
173                self.guiDisplay()
174           
175if __name__ == "__main__" :
176    os.environ["LC_ALL"] = getLanguagePreference()
177    admin = PyKotaDumperGUI(lang=os.environ["LC_ALL"], charset=getCharsetPreference())
178    admin.form = cgi.FieldStorage()
179    admin.options = { "output" : "-",
180                "data" : "history",
181                "format" : "cups",
182              }
183    admin.arguments = []
184    admin.guiAction()
185    try :
186        admin.storage.close()
187    except (TypeError, NameError, AttributeError) :   
188        pass
189       
190    sys.exit(0)
Note: See TracBrowser for help on using the browser.