1 | # PyKota |
---|
2 | # |
---|
3 | # PyKota : Print Quotas for CUPS and LPRng |
---|
4 | # |
---|
5 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
6 | # This program is free software; you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation; either version 2 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
11 | # This program is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with this program; if not, write to the Free Software |
---|
18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | # |
---|
20 | # $Id$ |
---|
21 | # |
---|
22 | # $Log$ |
---|
23 | # Revision 1.10 2003/04/30 13:36:40 jalet |
---|
24 | # Stupid accounting method was added. |
---|
25 | # |
---|
26 | # Revision 1.9 2003/04/27 08:04:15 jalet |
---|
27 | # LDAP storage backend's skeleton added. DOESN'T WORK. |
---|
28 | # |
---|
29 | # Revision 1.8 2003/04/23 22:13:57 jalet |
---|
30 | # Preliminary support for LPRng added BUT STILL UNTESTED. |
---|
31 | # |
---|
32 | # Revision 1.7 2003/04/15 11:30:57 jalet |
---|
33 | # More work done on money print charging. |
---|
34 | # Minor bugs corrected. |
---|
35 | # All tools now access to the storage as priviledged users, repykota excepted. |
---|
36 | # |
---|
37 | # Revision 1.6 2003/03/29 13:45:27 jalet |
---|
38 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
39 | # Two README files were added. |
---|
40 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
41 | # |
---|
42 | # Revision 1.5 2003/03/22 13:11:33 jalet |
---|
43 | # The port on which the Quota Storage Sever is listening can now |
---|
44 | # be set in the configuration file (see sample). |
---|
45 | # Better error handling if PygreSQL is not installed. |
---|
46 | # Improved documentation. |
---|
47 | # Version number changed to 1.02alpha |
---|
48 | # |
---|
49 | # Revision 1.4 2003/02/17 22:05:50 jalet |
---|
50 | # Storage backend now supports admin and user passwords (untested) |
---|
51 | # |
---|
52 | # Revision 1.3 2003/02/06 14:49:04 jalet |
---|
53 | # edpykota should be ok now |
---|
54 | # |
---|
55 | # Revision 1.2 2003/02/05 22:28:38 jalet |
---|
56 | # More robust storage |
---|
57 | # |
---|
58 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
59 | # Initial import into CVS |
---|
60 | # |
---|
61 | # |
---|
62 | # |
---|
63 | |
---|
64 | |
---|
65 | from pykota.storage import PyKotaStorageError |
---|
66 | from pykota.storages import sql |
---|
67 | |
---|
68 | try : |
---|
69 | import pg |
---|
70 | except ImportError : |
---|
71 | import sys |
---|
72 | # TODO : to translate or not to translate ? |
---|
73 | raise PyKotaStorageError, "This python version (%s) doesn't seem to have the PygreSQL module installed correctly." % sys.version.split()[0] |
---|
74 | |
---|
75 | class Storage(sql.SQLStorage) : |
---|
76 | def __init__(self, host, dbname, user, passwd) : |
---|
77 | """Opens the PostgreSQL database connection.""" |
---|
78 | self.closed = 1 |
---|
79 | try : |
---|
80 | (host, port) = host.split(":") |
---|
81 | port = int(port) |
---|
82 | except ValueError : |
---|
83 | port = -1 # Use PostgreSQL's default tcp/ip port (5432). |
---|
84 | |
---|
85 | try : |
---|
86 | self.database = pg.connect(host=host, port=port, dbname=dbname, user=user, passwd=passwd) |
---|
87 | self.closed = 0 |
---|
88 | except pg.error, msg : |
---|
89 | raise PyKotaStorageError, msg |
---|
90 | |
---|
91 | def __del__(self) : |
---|
92 | """Closes the database connection.""" |
---|
93 | if not self.closed : |
---|
94 | self.database.close() |
---|
95 | self.closed = 1 |
---|
96 | |
---|
97 | def doQuery(self, query) : |
---|
98 | """Does a query.""" |
---|
99 | if type(query) in (type([]), type(())) : |
---|
100 | query = ";".join(query) |
---|
101 | query = query.strip() |
---|
102 | if not query.endswith(';') : |
---|
103 | query += ';' |
---|
104 | self.database.query("BEGIN;") |
---|
105 | try : |
---|
106 | result = self.database.query(query) |
---|
107 | except pg.error, msg : |
---|
108 | self.database.query("ROLLBACK;") |
---|
109 | raise PyKotaStorageError, msg |
---|
110 | else : |
---|
111 | self.database.query("COMMIT;") |
---|
112 | return result |
---|
113 | |
---|
114 | def doQuote(self, field) : |
---|
115 | """Quotes a field for use as a string in SQL queries.""" |
---|
116 | if type(field) in (type(0), type(0.0)) : # TODO : do something safer |
---|
117 | typ = "decimal" |
---|
118 | else : |
---|
119 | typ = "text" |
---|
120 | return pg._quote(field, typ) |
---|
121 | |
---|
122 | def doParseResult(self, result) : |
---|
123 | """Returns the result as a list of Python mappings.""" |
---|
124 | if (result is not None) and (result.ntuples() > 0) : |
---|
125 | return result.dictresult() |
---|
126 | |
---|