root / pykoticon / trunk / bin / pykoticon @ 55

Revision 55, 3.5 kB (checked in by jerome, 19 years ago)

Transformed skeleton into a networking class

  • Property svn:keywords set to Id
Line 
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
25import sys
26import os
27import pwd
28import time
29import urllib
30import urllib2
31
32DUMPYKOTA_URL = "http://cgi.librelogiciel.com/cgi-bin/dumpykota.cgi"
33USERNAME = "jerome"
34
35class 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"))
90   
91if __name__ == "__main__" :
92    net = CGINetworkInterface(DUMPYKOTA_URL)
93    print "List of printers : ", net.getPrinterNames()
94   
95    print "User : ", net.getUserInfo("jerome")
96   
97    print "User print quotas : ", net.getUserPQuotas("jerome")
Note: See TracBrowser for help on using the browser.