#! /usr/bin/env python # -*- coding: ISO-8859-15 -*- # PyKotIcon - Windows System Tray Icon for PyKota # # (c) 2003-2004 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$ # # import sys import os import pwd import time import urllib import urllib2 DUMPYKOTA_URL = "http://cgi.librelogiciel.com/cgi-bin/dumpykota.cgi" USERNAME = "jerome" class CGINetworkInterface : """A class for all network interactions.""" def __init__(self, url, username=None, password=None) : """Initialize CGI connection datas.""" self.url = url self.username = username self.password = password def retrieveDatas(self, arguments, fieldnames) : """Retrieve datas from the CGI script.""" args = { "report" : 1, "format" : "csv", } args.update(arguments) answer = {} try : url = "%s?%s" % (self.url, urllib.urlencode(args)) u = urllib2.urlopen(url) lines = u.readlines() except IOError, msg : raise IOError, "Unable to retrieve %s : %s" % (url, msg) else : u.close() try : lines = [ line.strip().split(",") for line in lines ] fields = [field[1:-1] for field in lines[0]] indices = [fields.index(fname) for fname in fieldnames] answer = dict([ (fieldname, \ [ line[fields.index(fieldname)][1:-1] for line in lines[1:] ]) \ for fieldname in fieldnames ]) except : raise ValueError, "Invalid datas retrieved from %s" % url return answer def getPrinterNames(self) : """Retrieve the printer's names.""" arguments = { "report" : 1, "format" : "csv", "datatype" : "printers", } return self.retrieveDatas(arguments, ["printername"])["printername"] def getUserInfo(self, username) : """Retrieve the user's information.""" arguments = { "datatype" : "users", "filter" : "username=%s" % username, } return self.retrieveDatas(arguments, ("limitby", "balance", "lifetimepaid")) def getUserPQuotas(self, username) : """Retrieve the user's print quota information.""" arguments = { "datatype" : "upquotas", "filter" : "username=%s" % username, } return self.retrieveDatas(arguments, ("printername", "pagecounter", "softlimit", "datelimit")) if __name__ == "__main__" : net = CGINetworkInterface(DUMPYKOTA_URL) print "List of printers : ", net.getPrinterNames() print "User : ", net.getUserInfo("jerome") print "User print quotas : ", net.getUserPQuotas("jerome")