#! /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 urllib import urllib2 try : import win32api import win32net import win32netcon hasWindowsExtensions = 1 except ImportError : hasWindowsExtensions = 0 DUMPYKOTA_URL = "http://cgi.librelogiciel.com/cgi-bin/dumpykota.cgi" class UserQuota : """A class for PyKota User Print Quota entries.""" def __init__(self, printername, pagecount, softlimit, hardlimit, datelimit): """Initialize user print quota datas.""" self.PrinterName = printername try : self.PageCounter = int(pagecount) except (ValueError, TypeError) : self.PageCounter = 0 try : self.SoftLimit = int(softlimit) except (ValueError, TypeError) : self.SoftLimit = None try : self.HardLimit = int(hardlimit) except (ValueError, TypeError) : self.HardLimit = None self.DateLimit = datelimit class User : """A class for PyKota users.""" def __init__(self, username, userinfo, userquotas) : """Initialize user datas.""" self.Name = username self.LimitBy = userinfo["limitby"][0].lower() try : self.Balance = float(userinfo["balance"][0]) except (ValueError, TypeError) : self.Balance = 0.0 try : self.LifeTimePaid = float(userinfo["lifetimepaid"][0]) except (ValueError, TypeError) : self.LifeTimePaid = 0.0 self.Quotas = [] for i in range(len(userquotas["printername"])) : printername = userquotas["printername"][i] pagecounter = userquotas["pagecounter"][i] softlimit = userquotas["softlimit"][i] hardlimit = userquotas["hardlimit"][i] datelimit = userquotas["datelimit"][i] self.Quotas.append(UserQuota(printername, pagecounter, softlimit, \ hardlimit, datelimit)) class CGINetworkInterface : """A class for all network interactions.""" def __init__(self, cgiurl, authname=None, authpw=None) : """Initialize CGI connection datas.""" self.cgiurl = cgiurl self.authname = authname self.authpw = authpw 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.cgiurl, 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", "hardlimit", "datelimit")) def getUser(self, username) : """Retrieves the user account and quota information.""" info = self.getUserInfo(username) quotas = self.getUserPQuotas(username) return User(username, info, quotas) def getWindowsUserName() : """Retrieves the current user's name under MS Windows.""" dc = win32net.NetServerEnum(None, 100, win32netcon.SV_TYPE_DOMAIN_CTRL) user = win32api.GetUserName() if dc[0] : dcname = dc[0][0]['name'] return win32net.NetUserGetInfo("\\\\" + dcname, user, 1) else: return win32net.NetUserGetInfo(None, user, 1) 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") #jerome = net.getUser("jerome") print getWindowsUserName()