root / pykoticon / trunk / bin / pykoticon @ 47

Revision 47, 3.1 kB (checked in by jerome, 19 years ago)

Initial import

Line 
1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3
4# PyKota - Print Quotas for CUPS and LPRng
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# $Log: $
24#
25#
26
27import sys
28import os
29import pwd
30import time
31import urllib
32import urllib2
33
34DUMPYKOTA_URL = "http://cgi.librelogiciel.com/cgi-bin/dumpykota.cgi"
35USERNAME = "jerome"
36
37def getPrinterNames() :
38    """Retrieve the printer's names."""
39    arguments = { "report" : 1,
40                  "format" : "csv",
41                  "datatype" : "printers",
42                } 
43    return retrieveDatas(arguments, ["printername"])["printername"]
44   
45def getUserInfo(username) :
46    """Retrieve the user's information."""
47    arguments = { "datatype" : "users",
48                  "filter" : "username=%s" % username,
49                } 
50    return retrieveDatas(arguments, ("limitby", "balance", "lifetimepaid"))
51   
52def getUserPQuotas(username) :
53    """Retrieve the user's print quota information."""
54    arguments = { "datatype" : "upquotas",
55                  "filter" : "username=%s" % username,
56                } 
57    return retrieveDatas(arguments, ("printername", "pagecounter", "softlimit", "datelimit"))
58   
59def retrieveDatas(args, fieldnames) :
60    """Retrieve datas from the CGI script."""
61    arguments = { "report" : 1,
62                  "format" : "csv",
63                } 
64    arguments.update(args)           
65    answer = {}
66    try :           
67        url = "%s?%s" % (DUMPYKOTA_URL, urllib.urlencode(arguments))
68        u = urllib2.urlopen(url)
69        lines = u.readlines()
70    except :   
71        sys.stderr.write("Unable to retrieve %s\n" % url)
72    else :   
73        u.close()
74        try :
75            lines = [ line.strip().split(",") for line in lines ]
76            fields = [field[1:-1] for field in lines[0]]
77            indices = [fields.index(fname) for fname in fieldnames]
78            answer = dict([ (fieldname, \
79                            [ line[fields.index(fieldname)][1:-1] for line in lines[1:] ]) \
80                            for fieldname in fieldnames ])
81        except :   
82            sys.stderr.write("Invalid datas retrieved from %s\n" % url)
83    return answer
84   
85if __name__ == "__main__" :
86    #printernames = getPrinterNames()
87    #print "List of printers : ", printernames
88   
89    userinfo = getUserInfo("jerome")
90    print "User jerome : ", userinfo
91   
92    userpquotas = getUserPQuotas("jerome")
93    print "User jerome's print quotas : ", userpquotas
Note: See TracBrowser for help on using the browser.