[47] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | |
---|
[48] | 4 | # PyKotIcon - Windows System Tray Icon for PyKota |
---|
[47] | 5 | # |
---|
| 6 | # (c) 2003-2004 Jerome Alet <alet@librelogiciel.com> |
---|
| 7 | # This program is free software; you can redistribute it and/or modify |
---|
| 8 | # it under the terms of the GNU General Public License as published by |
---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 10 | # (at your option) any later version. |
---|
| 11 | # |
---|
| 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
| 18 | # along with this program; if not, write to the Free Software |
---|
| 19 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 20 | # |
---|
[53] | 21 | # $Id$ |
---|
[47] | 22 | # |
---|
| 23 | # |
---|
| 24 | |
---|
| 25 | import sys |
---|
| 26 | import urllib |
---|
| 27 | import urllib2 |
---|
| 28 | |
---|
[57] | 29 | try : |
---|
| 30 | import win32api |
---|
| 31 | import win32net |
---|
| 32 | import win32netcon |
---|
| 33 | hasWindowsExtensions = 1 |
---|
| 34 | except ImportError : |
---|
| 35 | hasWindowsExtensions = 0 |
---|
| 36 | |
---|
| 37 | |
---|
[47] | 38 | DUMPYKOTA_URL = "http://cgi.librelogiciel.com/cgi-bin/dumpykota.cgi" |
---|
| 39 | |
---|
[56] | 40 | class UserQuota : |
---|
| 41 | """A class for PyKota User Print Quota entries.""" |
---|
| 42 | def __init__(self, printername, pagecount, softlimit, hardlimit, datelimit): |
---|
| 43 | """Initialize user print quota datas.""" |
---|
| 44 | self.PrinterName = printername |
---|
| 45 | try : |
---|
| 46 | self.PageCounter = int(pagecount) |
---|
| 47 | except (ValueError, TypeError) : |
---|
| 48 | self.PageCounter = 0 |
---|
| 49 | try : |
---|
| 50 | self.SoftLimit = int(softlimit) |
---|
| 51 | except (ValueError, TypeError) : |
---|
| 52 | self.SoftLimit = None |
---|
| 53 | try : |
---|
| 54 | self.HardLimit = int(hardlimit) |
---|
| 55 | except (ValueError, TypeError) : |
---|
| 56 | self.HardLimit = None |
---|
| 57 | self.DateLimit = datelimit |
---|
| 58 | |
---|
| 59 | class User : |
---|
| 60 | """A class for PyKota users.""" |
---|
| 61 | def __init__(self, username, userinfo, userquotas) : |
---|
| 62 | """Initialize user datas.""" |
---|
| 63 | self.Name = username |
---|
| 64 | self.LimitBy = userinfo["limitby"][0].lower() |
---|
| 65 | try : |
---|
| 66 | self.Balance = float(userinfo["balance"][0]) |
---|
| 67 | except (ValueError, TypeError) : |
---|
| 68 | self.Balance = 0.0 |
---|
| 69 | try : |
---|
| 70 | self.LifeTimePaid = float(userinfo["lifetimepaid"][0]) |
---|
| 71 | except (ValueError, TypeError) : |
---|
| 72 | self.LifeTimePaid = 0.0 |
---|
| 73 | self.Quotas = [] |
---|
| 74 | for i in range(len(userquotas["printername"])) : |
---|
| 75 | printername = userquotas["printername"][i] |
---|
| 76 | pagecounter = userquotas["pagecounter"][i] |
---|
| 77 | softlimit = userquotas["softlimit"][i] |
---|
| 78 | hardlimit = userquotas["hardlimit"][i] |
---|
| 79 | datelimit = userquotas["datelimit"][i] |
---|
| 80 | self.Quotas.append(UserQuota(printername, pagecounter, softlimit, \ |
---|
| 81 | hardlimit, datelimit)) |
---|
| 82 | |
---|
[55] | 83 | class CGINetworkInterface : |
---|
| 84 | """A class for all network interactions.""" |
---|
[56] | 85 | def __init__(self, cgiurl, authname=None, authpw=None) : |
---|
[55] | 86 | """Initialize CGI connection datas.""" |
---|
[56] | 87 | self.cgiurl = cgiurl |
---|
| 88 | self.authname = authname |
---|
| 89 | self.authpw = authpw |
---|
[55] | 90 | |
---|
| 91 | def retrieveDatas(self, arguments, fieldnames) : |
---|
| 92 | """Retrieve datas from the CGI script.""" |
---|
| 93 | args = { "report" : 1, |
---|
| 94 | "format" : "csv", |
---|
| 95 | } |
---|
| 96 | args.update(arguments) |
---|
| 97 | answer = {} |
---|
| 98 | try : |
---|
[56] | 99 | url = "%s?%s" % (self.cgiurl, urllib.urlencode(args)) |
---|
[55] | 100 | u = urllib2.urlopen(url) |
---|
| 101 | lines = u.readlines() |
---|
| 102 | except IOError, msg : |
---|
| 103 | raise IOError, "Unable to retrieve %s : %s" % (url, msg) |
---|
| 104 | else : |
---|
| 105 | u.close() |
---|
| 106 | try : |
---|
| 107 | lines = [ line.strip().split(",") for line in lines ] |
---|
| 108 | fields = [field[1:-1] for field in lines[0]] |
---|
| 109 | indices = [fields.index(fname) for fname in fieldnames] |
---|
| 110 | answer = dict([ (fieldname, \ |
---|
[56] | 111 | [ line[fields.index(fieldname)][1:-1] \ |
---|
| 112 | for line in lines[1:] ]) \ |
---|
[55] | 113 | for fieldname in fieldnames ]) |
---|
| 114 | except : |
---|
| 115 | raise ValueError, "Invalid datas retrieved from %s" % url |
---|
| 116 | return answer |
---|
| 117 | |
---|
| 118 | def getPrinterNames(self) : |
---|
| 119 | """Retrieve the printer's names.""" |
---|
| 120 | arguments = { "report" : 1, |
---|
| 121 | "format" : "csv", |
---|
| 122 | "datatype" : "printers", |
---|
| 123 | } |
---|
| 124 | return self.retrieveDatas(arguments, ["printername"])["printername"] |
---|
| 125 | |
---|
| 126 | def getUserInfo(self, username) : |
---|
| 127 | """Retrieve the user's information.""" |
---|
| 128 | arguments = { "datatype" : "users", |
---|
| 129 | "filter" : "username=%s" % username, |
---|
| 130 | } |
---|
| 131 | return self.retrieveDatas(arguments, ("limitby", "balance", "lifetimepaid")) |
---|
| 132 | |
---|
| 133 | def getUserPQuotas(self, username) : |
---|
| 134 | """Retrieve the user's print quota information.""" |
---|
| 135 | arguments = { "datatype" : "upquotas", |
---|
| 136 | "filter" : "username=%s" % username, |
---|
| 137 | } |
---|
[56] | 138 | return self.retrieveDatas(arguments, ("printername", "pagecounter", \ |
---|
| 139 | "softlimit", "hardlimit", "datelimit")) |
---|
| 140 | |
---|
| 141 | def getUser(self, username) : |
---|
| 142 | """Retrieves the user account and quota information.""" |
---|
| 143 | info = self.getUserInfo(username) |
---|
| 144 | quotas = self.getUserPQuotas(username) |
---|
| 145 | return User(username, info, quotas) |
---|
[47] | 146 | |
---|
[57] | 147 | def getWindowsUserName() : |
---|
| 148 | """Retrieves the current user's name under MS Windows.""" |
---|
| 149 | dc = win32net.NetServerEnum(None, 100, win32netcon.SV_TYPE_DOMAIN_CTRL) |
---|
| 150 | user = win32api.GetUserName() |
---|
| 151 | if dc[0] : |
---|
| 152 | dcname = dc[0][0]['name'] |
---|
| 153 | return win32net.NetUserGetInfo("\\\\" + dcname, user, 1) |
---|
| 154 | else: |
---|
| 155 | return win32net.NetUserGetInfo(None, user, 1) |
---|
| 156 | |
---|
[47] | 157 | if __name__ == "__main__" : |
---|
[55] | 158 | net = CGINetworkInterface(DUMPYKOTA_URL) |
---|
[56] | 159 | #print "List of printers : ", net.getPrinterNames() |
---|
[47] | 160 | |
---|
[56] | 161 | #print "User : ", net.getUserInfo("jerome") |
---|
[47] | 162 | |
---|
[56] | 163 | #print "User print quotas : ", net.getUserPQuotas("jerome") |
---|
[57] | 164 | #jerome = net.getUser("jerome") |
---|
| 165 | print getWindowsUserName() |
---|