1 | #! /usr/bin/env python |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | |
---|
4 | # PyKotIcon - Windows System Tray Icon for PyKota |
---|
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 | # |
---|
21 | # $Id$ |
---|
22 | # |
---|
23 | # |
---|
24 | |
---|
25 | import sys |
---|
26 | import urllib |
---|
27 | import urllib2 |
---|
28 | |
---|
29 | DUMPYKOTA_URL = "http://cgi.librelogiciel.com/cgi-bin/dumpykota.cgi" |
---|
30 | |
---|
31 | class UserQuota : |
---|
32 | """A class for PyKota User Print Quota entries.""" |
---|
33 | def __init__(self, printername, pagecount, softlimit, hardlimit, datelimit): |
---|
34 | """Initialize user print quota datas.""" |
---|
35 | self.PrinterName = printername |
---|
36 | try : |
---|
37 | self.PageCounter = int(pagecount) |
---|
38 | except (ValueError, TypeError) : |
---|
39 | self.PageCounter = 0 |
---|
40 | try : |
---|
41 | self.SoftLimit = int(softlimit) |
---|
42 | except (ValueError, TypeError) : |
---|
43 | self.SoftLimit = None |
---|
44 | try : |
---|
45 | self.HardLimit = int(hardlimit) |
---|
46 | except (ValueError, TypeError) : |
---|
47 | self.HardLimit = None |
---|
48 | self.DateLimit = datelimit |
---|
49 | |
---|
50 | class User : |
---|
51 | """A class for PyKota users.""" |
---|
52 | def __init__(self, username, userinfo, userquotas) : |
---|
53 | """Initialize user datas.""" |
---|
54 | self.Name = username |
---|
55 | self.LimitBy = userinfo["limitby"][0].lower() |
---|
56 | try : |
---|
57 | self.Balance = float(userinfo["balance"][0]) |
---|
58 | except (ValueError, TypeError) : |
---|
59 | self.Balance = 0.0 |
---|
60 | try : |
---|
61 | self.LifeTimePaid = float(userinfo["lifetimepaid"][0]) |
---|
62 | except (ValueError, TypeError) : |
---|
63 | self.LifeTimePaid = 0.0 |
---|
64 | self.Quotas = [] |
---|
65 | for i in range(len(userquotas["printername"])) : |
---|
66 | printername = userquotas["printername"][i] |
---|
67 | pagecounter = userquotas["pagecounter"][i] |
---|
68 | softlimit = userquotas["softlimit"][i] |
---|
69 | hardlimit = userquotas["hardlimit"][i] |
---|
70 | datelimit = userquotas["datelimit"][i] |
---|
71 | self.Quotas.append(UserQuota(printername, pagecounter, softlimit, \ |
---|
72 | hardlimit, datelimit)) |
---|
73 | |
---|
74 | class CGINetworkInterface : |
---|
75 | """A class for all network interactions.""" |
---|
76 | def __init__(self, cgiurl, authname=None, authpw=None) : |
---|
77 | """Initialize CGI connection datas.""" |
---|
78 | self.cgiurl = cgiurl |
---|
79 | self.authname = authname |
---|
80 | self.authpw = authpw |
---|
81 | |
---|
82 | def retrieveDatas(self, arguments, fieldnames) : |
---|
83 | """Retrieve datas from the CGI script.""" |
---|
84 | args = { "report" : 1, |
---|
85 | "format" : "csv", |
---|
86 | } |
---|
87 | args.update(arguments) |
---|
88 | answer = {} |
---|
89 | try : |
---|
90 | url = "%s?%s" % (self.cgiurl, urllib.urlencode(args)) |
---|
91 | u = urllib2.urlopen(url) |
---|
92 | lines = u.readlines() |
---|
93 | except IOError, msg : |
---|
94 | raise IOError, "Unable to retrieve %s : %s" % (url, msg) |
---|
95 | else : |
---|
96 | u.close() |
---|
97 | try : |
---|
98 | lines = [ line.strip().split(",") for line in lines ] |
---|
99 | fields = [field[1:-1] for field in lines[0]] |
---|
100 | indices = [fields.index(fname) for fname in fieldnames] |
---|
101 | answer = dict([ (fieldname, \ |
---|
102 | [ line[fields.index(fieldname)][1:-1] \ |
---|
103 | for line in lines[1:] ]) \ |
---|
104 | for fieldname in fieldnames ]) |
---|
105 | except : |
---|
106 | raise ValueError, "Invalid datas retrieved from %s" % url |
---|
107 | return answer |
---|
108 | |
---|
109 | def getPrinterNames(self) : |
---|
110 | """Retrieve the printer's names.""" |
---|
111 | arguments = { "report" : 1, |
---|
112 | "format" : "csv", |
---|
113 | "datatype" : "printers", |
---|
114 | } |
---|
115 | return self.retrieveDatas(arguments, ["printername"])["printername"] |
---|
116 | |
---|
117 | def getUserInfo(self, username) : |
---|
118 | """Retrieve the user's information.""" |
---|
119 | arguments = { "datatype" : "users", |
---|
120 | "filter" : "username=%s" % username, |
---|
121 | } |
---|
122 | return self.retrieveDatas(arguments, ("limitby", "balance", "lifetimepaid")) |
---|
123 | |
---|
124 | def getUserPQuotas(self, username) : |
---|
125 | """Retrieve the user's print quota information.""" |
---|
126 | arguments = { "datatype" : "upquotas", |
---|
127 | "filter" : "username=%s" % username, |
---|
128 | } |
---|
129 | return self.retrieveDatas(arguments, ("printername", "pagecounter", \ |
---|
130 | "softlimit", "hardlimit", "datelimit")) |
---|
131 | |
---|
132 | def getUser(self, username) : |
---|
133 | """Retrieves the user account and quota information.""" |
---|
134 | info = self.getUserInfo(username) |
---|
135 | quotas = self.getUserPQuotas(username) |
---|
136 | return User(username, info, quotas) |
---|
137 | |
---|
138 | if __name__ == "__main__" : |
---|
139 | net = CGINetworkInterface(DUMPYKOTA_URL) |
---|
140 | #print "List of printers : ", net.getPrinterNames() |
---|
141 | |
---|
142 | #print "User : ", net.getUserInfo("jerome") |
---|
143 | |
---|
144 | #print "User print quotas : ", net.getUserPQuotas("jerome") |
---|
145 | jerome = net.getUser("jerome") |
---|