[695] | 1 | # PyKota |
---|
| 2 | # |
---|
[952] | 3 | # PyKota : Print Quotas for CUPS and LPRng |
---|
[695] | 4 | # |
---|
| 5 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
[873] | 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. |
---|
[695] | 10 | # |
---|
[873] | 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. |
---|
[695] | 19 | # |
---|
| 20 | # $Id$ |
---|
| 21 | # |
---|
| 22 | # $Log$ |
---|
[967] | 23 | # Revision 1.9 2003/04/27 08:04:15 jalet |
---|
| 24 | # LDAP storage backend's skeleton added. DOESN'T WORK. |
---|
| 25 | # |
---|
[952] | 26 | # Revision 1.8 2003/04/23 22:13:57 jalet |
---|
| 27 | # Preliminary support for LPRng added BUT STILL UNTESTED. |
---|
| 28 | # |
---|
[915] | 29 | # Revision 1.7 2003/04/15 11:30:57 jalet |
---|
| 30 | # More work done on money print charging. |
---|
| 31 | # Minor bugs corrected. |
---|
| 32 | # All tools now access to the storage as priviledged users, repykota excepted. |
---|
| 33 | # |
---|
[873] | 34 | # Revision 1.6 2003/03/29 13:45:27 jalet |
---|
| 35 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
| 36 | # Two README files were added. |
---|
| 37 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
| 38 | # |
---|
[859] | 39 | # Revision 1.5 2003/03/22 13:11:33 jalet |
---|
| 40 | # The port on which the Quota Storage Sever is listening can now |
---|
| 41 | # be set in the configuration file (see sample). |
---|
| 42 | # Better error handling if PygreSQL is not installed. |
---|
| 43 | # Improved documentation. |
---|
| 44 | # Version number changed to 1.02alpha |
---|
| 45 | # |
---|
[800] | 46 | # Revision 1.4 2003/02/17 22:05:50 jalet |
---|
| 47 | # Storage backend now supports admin and user passwords (untested) |
---|
| 48 | # |
---|
[720] | 49 | # Revision 1.3 2003/02/06 14:49:04 jalet |
---|
| 50 | # edpykota should be ok now |
---|
| 51 | # |
---|
[700] | 52 | # Revision 1.2 2003/02/05 22:28:38 jalet |
---|
| 53 | # More robust storage |
---|
| 54 | # |
---|
[695] | 55 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
| 56 | # Initial import into CVS |
---|
| 57 | # |
---|
| 58 | # |
---|
| 59 | # |
---|
| 60 | |
---|
[967] | 61 | |
---|
| 62 | from pykota.storage import PyKotaStorageError |
---|
| 63 | from pykota.storages import sql |
---|
| 64 | |
---|
[859] | 65 | try : |
---|
| 66 | import pg |
---|
| 67 | except ImportError : |
---|
| 68 | import sys |
---|
[967] | 69 | # TODO : to translate or not to translate ? |
---|
| 70 | raise PyKotaStorageError, "This python version (%s) doesn't seem to have the PygreSQL module installed correctly." % sys.version.split()[0] |
---|
[695] | 71 | |
---|
| 72 | class Storage(sql.SQLStorage) : |
---|
[800] | 73 | def __init__(self, host, dbname, user, passwd) : |
---|
[695] | 74 | """Opens the PostgreSQL database connection.""" |
---|
| 75 | self.closed = 1 |
---|
| 76 | try : |
---|
[859] | 77 | (host, port) = host.split(":") |
---|
| 78 | port = int(port) |
---|
| 79 | except ValueError : |
---|
| 80 | port = -1 # Use PostgreSQL's default tcp/ip port (5432). |
---|
| 81 | |
---|
| 82 | try : |
---|
| 83 | self.database = pg.connect(host=host, port=port, dbname=dbname, user=user, passwd=passwd) |
---|
[695] | 84 | self.closed = 0 |
---|
| 85 | except pg.error, msg : |
---|
| 86 | raise PyKotaStorageError, msg |
---|
| 87 | |
---|
| 88 | def __del__(self) : |
---|
| 89 | """Closes the database connection.""" |
---|
| 90 | if not self.closed : |
---|
| 91 | self.database.close() |
---|
| 92 | self.closed = 1 |
---|
| 93 | |
---|
| 94 | def doQuery(self, query) : |
---|
| 95 | """Does a query.""" |
---|
[915] | 96 | if type(query) in (type([]), type(())) : |
---|
| 97 | query = ";".join(query) |
---|
| 98 | query = query.strip() |
---|
| 99 | if not query.endswith(';') : |
---|
| 100 | query += ';' |
---|
| 101 | self.database.query("BEGIN;") |
---|
[695] | 102 | try : |
---|
[915] | 103 | result = self.database.query(query) |
---|
[695] | 104 | except pg.error, msg : |
---|
[915] | 105 | self.database.query("ROLLBACK;") |
---|
[695] | 106 | raise PyKotaStorageError, msg |
---|
[915] | 107 | else : |
---|
| 108 | self.database.query("COMMIT;") |
---|
| 109 | return result |
---|
[695] | 110 | |
---|
| 111 | def doQuote(self, field) : |
---|
| 112 | """Quotes a field for use as a string in SQL queries.""" |
---|
| 113 | if type(field) == type(0) : # TODO : do something safer |
---|
| 114 | typ = "decimal" |
---|
| 115 | else : |
---|
| 116 | typ = "text" |
---|
| 117 | return pg._quote(field, typ) |
---|
| 118 | |
---|
| 119 | def doParseResult(self, result) : |
---|
[700] | 120 | """Returns the result as a list of Python mappings.""" |
---|
[720] | 121 | if (result is not None) and (result.ntuples() > 0) : |
---|
[700] | 122 | return result.dictresult() |
---|
[695] | 123 | |
---|