35 | | def getPrinterNames() : |
36 | | """Retrieve the printer's names.""" |
37 | | arguments = { "report" : 1, |
38 | | "format" : "csv", |
39 | | "datatype" : "printers", |
40 | | } |
41 | | return retrieveDatas(arguments, ["printername"])["printername"] |
42 | | |
43 | | def getUserInfo(username) : |
44 | | """Retrieve the user's information.""" |
45 | | arguments = { "datatype" : "users", |
46 | | "filter" : "username=%s" % username, |
47 | | } |
48 | | return retrieveDatas(arguments, ("limitby", "balance", "lifetimepaid")) |
49 | | |
50 | | def getUserPQuotas(username) : |
51 | | """Retrieve the user's print quota information.""" |
52 | | arguments = { "datatype" : "upquotas", |
53 | | "filter" : "username=%s" % username, |
54 | | } |
55 | | return retrieveDatas(arguments, ("printername", "pagecounter", "softlimit", "datelimit")) |
56 | | |
57 | | def retrieveDatas(args, fieldnames) : |
58 | | """Retrieve datas from the CGI script.""" |
59 | | arguments = { "report" : 1, |
60 | | "format" : "csv", |
61 | | } |
62 | | arguments.update(args) |
63 | | answer = {} |
64 | | try : |
65 | | url = "%s?%s" % (DUMPYKOTA_URL, urllib.urlencode(arguments)) |
66 | | u = urllib2.urlopen(url) |
67 | | lines = u.readlines() |
68 | | except : |
69 | | sys.stderr.write("Unable to retrieve %s\n" % url) |
70 | | else : |
71 | | u.close() |
72 | | try : |
73 | | lines = [ line.strip().split(",") for line in lines ] |
74 | | fields = [field[1:-1] for field in lines[0]] |
75 | | indices = [fields.index(fname) for fname in fieldnames] |
76 | | answer = dict([ (fieldname, \ |
77 | | [ line[fields.index(fieldname)][1:-1] for line in lines[1:] ]) \ |
78 | | for fieldname in fieldnames ]) |
79 | | except : |
80 | | sys.stderr.write("Invalid datas retrieved from %s\n" % url) |
81 | | return answer |
| 35 | class CGINetworkInterface : |
| 36 | """A class for all network interactions.""" |
| 37 | def __init__(self, url, username=None, password=None) : |
| 38 | """Initialize CGI connection datas.""" |
| 39 | self.url = url |
| 40 | self.username = username |
| 41 | self.password = password |
| 42 | |
| 43 | def retrieveDatas(self, arguments, fieldnames) : |
| 44 | """Retrieve datas from the CGI script.""" |
| 45 | args = { "report" : 1, |
| 46 | "format" : "csv", |
| 47 | } |
| 48 | args.update(arguments) |
| 49 | answer = {} |
| 50 | try : |
| 51 | url = "%s?%s" % (self.url, urllib.urlencode(args)) |
| 52 | u = urllib2.urlopen(url) |
| 53 | lines = u.readlines() |
| 54 | except IOError, msg : |
| 55 | raise IOError, "Unable to retrieve %s : %s" % (url, msg) |
| 56 | else : |
| 57 | u.close() |
| 58 | try : |
| 59 | lines = [ line.strip().split(",") for line in lines ] |
| 60 | fields = [field[1:-1] for field in lines[0]] |
| 61 | indices = [fields.index(fname) for fname in fieldnames] |
| 62 | answer = dict([ (fieldname, \ |
| 63 | [ line[fields.index(fieldname)][1:-1] for line in lines[1:] ]) \ |
| 64 | for fieldname in fieldnames ]) |
| 65 | except : |
| 66 | raise ValueError, "Invalid datas retrieved from %s" % url |
| 67 | return answer |
| 68 | |
| 69 | def getPrinterNames(self) : |
| 70 | """Retrieve the printer's names.""" |
| 71 | arguments = { "report" : 1, |
| 72 | "format" : "csv", |
| 73 | "datatype" : "printers", |
| 74 | } |
| 75 | return self.retrieveDatas(arguments, ["printername"])["printername"] |
| 76 | |
| 77 | def getUserInfo(self, username) : |
| 78 | """Retrieve the user's information.""" |
| 79 | arguments = { "datatype" : "users", |
| 80 | "filter" : "username=%s" % username, |
| 81 | } |
| 82 | return self.retrieveDatas(arguments, ("limitby", "balance", "lifetimepaid")) |
| 83 | |
| 84 | def getUserPQuotas(self, username) : |
| 85 | """Retrieve the user's print quota information.""" |
| 86 | arguments = { "datatype" : "upquotas", |
| 87 | "filter" : "username=%s" % username, |
| 88 | } |
| 89 | return self.retrieveDatas(arguments, ("printername", "pagecounter", "softlimit", "datelimit")) |