[1021] | 1 | # PyKota |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[1021] | 3 | # |
---|
| 4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
[1257] | 6 | # (c) 2003-2004 Jerome Alet <alet@librelogiciel.com> |
---|
[1021] | 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$ |
---|
[2031] | 24 | # Revision 1.38 2005/01/19 08:48:17 jalet |
---|
| 25 | # Fix exception's name wrt newer versions of PygreSQL |
---|
| 26 | # |
---|
[1717] | 27 | # Revision 1.37 2004/09/14 22:29:12 jalet |
---|
| 28 | # First version of dumpykota. Works fine but only with PostgreSQL backend |
---|
| 29 | # for now. |
---|
| 30 | # |
---|
[1520] | 31 | # Revision 1.36 2004/06/03 23:14:11 jalet |
---|
| 32 | # Now stores the job's size in bytes in the database. |
---|
| 33 | # Preliminary work on payments storage : database schemas are OK now, |
---|
| 34 | # but no code to store payments yet. |
---|
| 35 | # Removed schema picture, not relevant anymore. |
---|
| 36 | # |
---|
[1327] | 37 | # Revision 1.35 2004/02/02 22:44:16 jalet |
---|
| 38 | # Preliminary work on Relationnal Database Independance via DB-API 2.0 |
---|
| 39 | # |
---|
[1277] | 40 | # Revision 1.34 2004/01/12 15:12:50 jalet |
---|
| 41 | # Small fix for history |
---|
| 42 | # |
---|
[1275] | 43 | # Revision 1.33 2004/01/12 14:44:47 jalet |
---|
| 44 | # Missing space in SQL query |
---|
| 45 | # |
---|
[1274] | 46 | # Revision 1.32 2004/01/12 14:35:02 jalet |
---|
| 47 | # Printing history added to CGI script. |
---|
| 48 | # |
---|
[1269] | 49 | # Revision 1.31 2004/01/10 09:44:02 jalet |
---|
| 50 | # Fixed potential accuracy problem if a user printed on several printers at |
---|
| 51 | # the very same time. |
---|
| 52 | # |
---|
[1259] | 53 | # Revision 1.30 2004/01/08 16:33:27 jalet |
---|
| 54 | # Additionnal check to not create a circular printers group. |
---|
| 55 | # |
---|
[1258] | 56 | # Revision 1.29 2004/01/08 16:24:49 jalet |
---|
| 57 | # edpykota now supports adding printers to printer groups. |
---|
| 58 | # |
---|
[1257] | 59 | # Revision 1.28 2004/01/08 14:10:33 jalet |
---|
| 60 | # Copyright year changed. |
---|
| 61 | # |
---|
[1249] | 62 | # Revision 1.27 2004/01/06 14:24:59 jalet |
---|
| 63 | # Printer groups should be cached now, if caching is enabled. |
---|
| 64 | # |
---|
[1244] | 65 | # Revision 1.26 2003/12/29 14:12:48 uid67467 |
---|
| 66 | # Tries to workaround possible integrity violations when retrieving printer groups |
---|
| 67 | # |
---|
[1240] | 68 | # Revision 1.25 2003/12/27 16:49:25 uid67467 |
---|
| 69 | # Should be ok now. |
---|
| 70 | # |
---|
[1228] | 71 | # Revision 1.24 2003/11/29 22:02:14 jalet |
---|
| 72 | # Don't try to retrieve the user print quota information if current printer |
---|
| 73 | # doesn't exist. |
---|
| 74 | # |
---|
[1203] | 75 | # Revision 1.23 2003/11/23 19:01:37 jalet |
---|
| 76 | # Job price added to history |
---|
| 77 | # |
---|
[1200] | 78 | # Revision 1.22 2003/11/21 14:28:46 jalet |
---|
| 79 | # More complete job history. |
---|
| 80 | # |
---|
[1179] | 81 | # Revision 1.21 2003/11/12 13:06:38 jalet |
---|
| 82 | # Bug fix wrt no user/group name command line argument to edpykota |
---|
| 83 | # |
---|
[1156] | 84 | # Revision 1.20 2003/10/09 21:25:26 jalet |
---|
| 85 | # Multiple printer names or wildcards can be passed on the command line |
---|
| 86 | # separated with commas. |
---|
| 87 | # Beta phase. |
---|
| 88 | # |
---|
[1149] | 89 | # Revision 1.19 2003/10/08 07:01:20 jalet |
---|
| 90 | # Job history can be disabled. |
---|
| 91 | # Some typos in README. |
---|
| 92 | # More messages in setup script. |
---|
| 93 | # |
---|
[1144] | 94 | # Revision 1.18 2003/10/07 09:07:30 jalet |
---|
| 95 | # Character encoding added to please latest version of Python |
---|
| 96 | # |
---|
[1137] | 97 | # Revision 1.17 2003/10/06 13:12:28 jalet |
---|
| 98 | # More work on caching |
---|
| 99 | # |
---|
[1136] | 100 | # Revision 1.16 2003/10/03 18:01:49 jalet |
---|
| 101 | # Nothing interesting... |
---|
| 102 | # |
---|
[1133] | 103 | # Revision 1.15 2003/10/03 12:27:03 jalet |
---|
| 104 | # Several optimizations, especially with LDAP backend |
---|
| 105 | # |
---|
[1131] | 106 | # Revision 1.14 2003/10/03 08:57:55 jalet |
---|
| 107 | # Caching mechanism now caches all that's cacheable. |
---|
| 108 | # |
---|
[1130] | 109 | # Revision 1.13 2003/10/02 20:23:18 jalet |
---|
| 110 | # Storage caching mechanism added. |
---|
| 111 | # |
---|
[1115] | 112 | # Revision 1.12 2003/08/17 14:20:25 jalet |
---|
| 113 | # Bug fix by Oleg Biteryakov |
---|
| 114 | # |
---|
[1113] | 115 | # Revision 1.11 2003/07/29 20:55:17 jalet |
---|
| 116 | # 1.14 is out ! |
---|
| 117 | # |
---|
[1087] | 118 | # Revision 1.10 2003/07/16 21:53:08 jalet |
---|
| 119 | # Really big modifications wrt new configuration file's location and content. |
---|
| 120 | # |
---|
[1085] | 121 | # Revision 1.9 2003/07/14 17:20:15 jalet |
---|
| 122 | # Bug in postgresql storage when modifying the prices for a printer |
---|
| 123 | # |
---|
[1084] | 124 | # Revision 1.8 2003/07/14 14:18:17 jalet |
---|
| 125 | # Wrong documentation strings |
---|
| 126 | # |
---|
[1079] | 127 | # Revision 1.7 2003/07/09 20:17:07 jalet |
---|
| 128 | # Email field added to PostgreSQL schema |
---|
| 129 | # |
---|
[1068] | 130 | # Revision 1.6 2003/07/07 11:49:24 jalet |
---|
| 131 | # Lots of small fixes with the help of PyChecker |
---|
| 132 | # |
---|
[1067] | 133 | # Revision 1.5 2003/07/07 08:33:19 jalet |
---|
| 134 | # Bug fix due to a typo in LDAP code |
---|
| 135 | # |
---|
[1051] | 136 | # Revision 1.4 2003/06/30 13:54:21 jalet |
---|
| 137 | # Sorts by user / group name |
---|
| 138 | # |
---|
[1041] | 139 | # Revision 1.3 2003/06/25 14:10:01 jalet |
---|
| 140 | # Hey, it may work (edpykota --reset excepted) ! |
---|
| 141 | # |
---|
[1024] | 142 | # Revision 1.2 2003/06/12 21:09:57 jalet |
---|
| 143 | # wrongly placed code. |
---|
| 144 | # |
---|
[1021] | 145 | # Revision 1.1 2003/06/10 16:37:54 jalet |
---|
| 146 | # Deletion of the second user which is not needed anymore. |
---|
| 147 | # Added a debug configuration field in /etc/pykota.conf |
---|
| 148 | # All queries can now be sent to the logger in debug mode, this will |
---|
| 149 | # greatly help improve performance when time for this will come. |
---|
| 150 | # |
---|
| 151 | # |
---|
| 152 | # |
---|
| 153 | # |
---|
| 154 | |
---|
[1274] | 155 | from pykota.storage import PyKotaStorageError,BaseStorage,StorageObject,StorageUser,StorageGroup,StoragePrinter,StorageJob,StorageLastJob,StorageUserPQuota,StorageGroupPQuota |
---|
[1327] | 156 | from pykota.storages.sql import SQLStorage |
---|
[1021] | 157 | |
---|
| 158 | try : |
---|
| 159 | import pg |
---|
| 160 | except ImportError : |
---|
| 161 | import sys |
---|
| 162 | # TODO : to translate or not to translate ? |
---|
| 163 | raise PyKotaStorageError, "This python version (%s) doesn't seem to have the PygreSQL module installed correctly." % sys.version.split()[0] |
---|
[2031] | 164 | else : |
---|
| 165 | try : |
---|
| 166 | PGError = pg.Error |
---|
| 167 | except AttributeError : |
---|
| 168 | PGError = pg.error |
---|
[1021] | 169 | |
---|
[1327] | 170 | class Storage(BaseStorage, SQLStorage) : |
---|
[1021] | 171 | def __init__(self, pykotatool, host, dbname, user, passwd) : |
---|
| 172 | """Opens the PostgreSQL database connection.""" |
---|
[1130] | 173 | BaseStorage.__init__(self, pykotatool) |
---|
[1021] | 174 | try : |
---|
| 175 | (host, port) = host.split(":") |
---|
| 176 | port = int(port) |
---|
| 177 | except ValueError : |
---|
| 178 | port = -1 # Use PostgreSQL's default tcp/ip port (5432). |
---|
| 179 | |
---|
| 180 | try : |
---|
| 181 | self.database = pg.connect(host=host, port=port, dbname=dbname, user=user, passwd=passwd) |
---|
[2031] | 182 | except PGError, msg : |
---|
[1021] | 183 | raise PyKotaStorageError, msg |
---|
| 184 | else : |
---|
[1024] | 185 | self.closed = 0 |
---|
[1130] | 186 | self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user)) |
---|
[1021] | 187 | |
---|
[1113] | 188 | def close(self) : |
---|
[1021] | 189 | """Closes the database connection.""" |
---|
| 190 | if not self.closed : |
---|
| 191 | self.database.close() |
---|
| 192 | self.closed = 1 |
---|
[1130] | 193 | self.tool.logdebug("Database closed.") |
---|
[1021] | 194 | |
---|
[1041] | 195 | def beginTransaction(self) : |
---|
| 196 | """Starts a transaction.""" |
---|
| 197 | self.database.query("BEGIN;") |
---|
[1130] | 198 | self.tool.logdebug("Transaction begins...") |
---|
[1041] | 199 | |
---|
| 200 | def commitTransaction(self) : |
---|
| 201 | """Commits a transaction.""" |
---|
| 202 | self.database.query("COMMIT;") |
---|
[1130] | 203 | self.tool.logdebug("Transaction committed.") |
---|
[1041] | 204 | |
---|
| 205 | def rollbackTransaction(self) : |
---|
| 206 | """Rollbacks a transaction.""" |
---|
| 207 | self.database.query("ROLLBACK;") |
---|
[1130] | 208 | self.tool.logdebug("Transaction aborted.") |
---|
[1041] | 209 | |
---|
[1717] | 210 | def doRawSearch(self, query) : |
---|
| 211 | """Does a raw search query.""" |
---|
[1021] | 212 | query = query.strip() |
---|
| 213 | if not query.endswith(';') : |
---|
| 214 | query += ';' |
---|
| 215 | try : |
---|
[1130] | 216 | self.tool.logdebug("QUERY : %s" % query) |
---|
[1021] | 217 | result = self.database.query(query) |
---|
[2031] | 218 | except PGError, msg : |
---|
[1021] | 219 | raise PyKotaStorageError, msg |
---|
| 220 | else : |
---|
[1717] | 221 | return result |
---|
[1041] | 222 | |
---|
[1717] | 223 | def doSearch(self, query) : |
---|
| 224 | """Does a search query.""" |
---|
| 225 | result = self.doRawSearch(query) |
---|
| 226 | if (result is not None) and (result.ntuples() > 0) : |
---|
| 227 | return result.dictresult() |
---|
| 228 | |
---|
[1041] | 229 | def doModify(self, query) : |
---|
| 230 | """Does a (possibly multiple) modify query.""" |
---|
| 231 | query = query.strip() |
---|
| 232 | if not query.endswith(';') : |
---|
| 233 | query += ';' |
---|
| 234 | try : |
---|
[1130] | 235 | self.tool.logdebug("QUERY : %s" % query) |
---|
[1041] | 236 | result = self.database.query(query) |
---|
[2031] | 237 | except PGError, msg : |
---|
[1041] | 238 | raise PyKotaStorageError, msg |
---|
[1068] | 239 | else : |
---|
| 240 | return result |
---|
[1041] | 241 | |
---|
[1021] | 242 | def doQuote(self, field) : |
---|
| 243 | """Quotes a field for use as a string in SQL queries.""" |
---|
| 244 | if type(field) == type(0.0) : |
---|
| 245 | typ = "decimal" |
---|
| 246 | elif type(field) == type(0) : |
---|
| 247 | typ = "int" |
---|
[1520] | 248 | elif type(field) == type(0L) : |
---|
| 249 | typ = "int" |
---|
[1021] | 250 | else : |
---|
| 251 | typ = "text" |
---|
| 252 | return pg._quote(field, typ) |
---|