1 | # PyKota |
---|
2 | # |
---|
3 | # PyKota : Print Quotas for CUPS |
---|
4 | # |
---|
5 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
6 | # You're welcome to redistribute this software under the |
---|
7 | # terms of the GNU General Public Licence version 2.0 |
---|
8 | # or, at your option, any higher version. |
---|
9 | # |
---|
10 | # You can read the complete GNU GPL in the file COPYING |
---|
11 | # which should come along with this software, or visit |
---|
12 | # the Free Software Foundation's WEB site http://www.fsf.org |
---|
13 | # |
---|
14 | # $Id$ |
---|
15 | # |
---|
16 | # $Log$ |
---|
17 | # Revision 1.5 2003/03/22 13:11:33 jalet |
---|
18 | # The port on which the Quota Storage Sever is listening can now |
---|
19 | # be set in the configuration file (see sample). |
---|
20 | # Better error handling if PygreSQL is not installed. |
---|
21 | # Improved documentation. |
---|
22 | # Version number changed to 1.02alpha |
---|
23 | # |
---|
24 | # Revision 1.4 2003/02/17 22:05:50 jalet |
---|
25 | # Storage backend now supports admin and user passwords (untested) |
---|
26 | # |
---|
27 | # Revision 1.3 2003/02/06 14:49:04 jalet |
---|
28 | # edpykota should be ok now |
---|
29 | # |
---|
30 | # Revision 1.2 2003/02/05 22:28:38 jalet |
---|
31 | # More robust storage |
---|
32 | # |
---|
33 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
34 | # Initial import into CVS |
---|
35 | # |
---|
36 | # |
---|
37 | # |
---|
38 | |
---|
39 | try : |
---|
40 | import pg |
---|
41 | except ImportError : |
---|
42 | import sys |
---|
43 | sys.stderr.write("This python version (%s) doesn't seem to have the PygreSQL module installed correctly.\n" % sys.version.split()[0]) |
---|
44 | raise |
---|
45 | |
---|
46 | from pykota.storage import PyKotaStorageError |
---|
47 | from pykota.storages import sql |
---|
48 | |
---|
49 | class Storage(sql.SQLStorage) : |
---|
50 | def __init__(self, host, dbname, user, passwd) : |
---|
51 | """Opens the PostgreSQL database connection.""" |
---|
52 | self.closed = 1 |
---|
53 | try : |
---|
54 | (host, port) = host.split(":") |
---|
55 | port = int(port) |
---|
56 | except ValueError : |
---|
57 | port = -1 # Use PostgreSQL's default tcp/ip port (5432). |
---|
58 | |
---|
59 | try : |
---|
60 | self.database = pg.connect(host=host, port=port, dbname=dbname, user=user, passwd=passwd) |
---|
61 | self.closed = 0 |
---|
62 | except pg.error, msg : |
---|
63 | raise PyKotaStorageError, msg |
---|
64 | |
---|
65 | def __del__(self) : |
---|
66 | """Closes the database connection.""" |
---|
67 | if not self.closed : |
---|
68 | self.database.close() |
---|
69 | self.closed = 1 |
---|
70 | |
---|
71 | def doQuery(self, query) : |
---|
72 | """Does a query.""" |
---|
73 | try : |
---|
74 | return self.database.query(query) |
---|
75 | except pg.error, msg : |
---|
76 | raise PyKotaStorageError, msg |
---|
77 | |
---|
78 | def doQuote(self, field) : |
---|
79 | """Quotes a field for use as a string in SQL queries.""" |
---|
80 | if type(field) == type(0) : # TODO : do something safer |
---|
81 | typ = "decimal" |
---|
82 | else : |
---|
83 | typ = "text" |
---|
84 | return pg._quote(field, typ) |
---|
85 | |
---|
86 | def doParseResult(self, result) : |
---|
87 | """Returns the result as a list of Python mappings.""" |
---|
88 | if (result is not None) and (result.ntuples() > 0) : |
---|
89 | return result.dictresult() |
---|
90 | |
---|