Changeset 379
- Timestamp:
- 06/30/07 16:13:34 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/jobarchived/jobarchived.py
r375 r379 174 174 from types import * 175 175 176 import DBClass177 176 import xml.sax, xml.sax.handler, socket, string, os, os.path, time, thread, threading, random, re 178 177 import rrdtool 178 from pyPgSQL import PgSQL 179 180 # Orginal from Andre van der Vlies <andre@vandervlies.xs4all.nl> for MySQL. Changed 181 # and added some more functions for postgres. 182 # 183 # 184 # Changed by: Bas van der Vlies <basv@sara.nl> 185 # 186 # SARA API for Postgres Database 187 # 188 # Changed by: Ramon Bastiaans for Job Monarch 189 # 190 191 class InitVars: 192 Vars = {} 193 194 def __init__(self, **key_arg): 195 for (key, value) in key_arg.items(): 196 if value: 197 self.Vars[key] = value 198 else: 199 self.Vars[key] = None 200 201 def __call__(self, *key): 202 key = "%s" % key 203 return self.Vars[key] 204 205 def __getitem__(self, key): 206 return self.Vars[key] 207 208 def __repr__(self): 209 return repr(self.Vars) 210 211 def keys(self): 212 barf = map(None, self.Vars.keys()) 213 return barf 214 215 def values(self): 216 barf = map(None, self.Vars.values()) 217 return barf 218 219 def has_key(self, key): 220 if self.Vars.has_key(key): 221 return 1 222 else: 223 return 0 224 225 class DBError(Exception): 226 def __init__(self, msg=''): 227 self.msg = msg 228 Exception.__init__(self, msg) 229 def __repr__(self): 230 return self.msg 231 __str__ = __repr__ 232 233 # 234 # Class to connect to a database 235 # and return the queury in a list or dictionairy. 236 # 237 class DB: 238 def __init__(self, db_vars): 239 240 self.dict = db_vars 241 242 if self.dict.has_key('User'): 243 self.user = self.dict['User'] 244 else: 245 self.user = 'postgres' 246 247 if self.dict.has_key('Host'): 248 self.host = self.dict['Host'] 249 else: 250 self.host = 'localhost' 251 252 if self.dict.has_key('Password'): 253 self.passwd = self.dict['Password'] 254 else: 255 self.passwd = '' 256 257 if self.dict.has_key('DataBaseName'): 258 self.db = self.dict['DataBaseName'] 259 else: 260 self.db = 'uva_cluster_db' 261 262 # connect_string = 'host:port:database:user:password: 263 dsn = "%s::%s:%s:%s" %(self.host, self.db, self.user, self.passwd) 264 265 try: 266 self.SQL = PgSQL.connect(dsn) 267 except PgSQL.Error, details: 268 str = "%s" %details 269 raise DBError(str) 270 271 def __repr__(self): 272 return repr(self.result) 273 274 def __nonzero__(self): 275 return not(self.result == None) 276 277 def __len__(self): 278 return len(self.result) 279 280 def __getitem__(self,i): 281 return self.result[i] 282 283 def __getslice__(self,i,j): 284 return self.result[i:j] 285 286 def Get(self, q_str): 287 c = self.SQL.cursor() 288 try: 289 c.execute(q_str) 290 result = c.fetchall() 291 except PgSQL.Error, details: 292 c.close() 293 str = "%s" %details 294 raise DBError(str) 295 296 c.close() 297 return result 298 299 def Set(self, q_str): 300 c = self.SQL.cursor() 301 try: 302 c.execute(q_str) 303 result = c.oidValue 304 305 except PgSQL.Error, details: 306 c.close() 307 str = "%s" %details 308 raise DBError(str) 309 310 c.close() 311 return result 312 313 def Commit(self): 314 self.SQL.commit() 179 315 180 316 class DataSQLStore: … … 185 321 def __init__( self, hostname, database ): 186 322 187 self.db_vars = DBClass.InitVars(DataBaseName=database,323 self.db_vars = InitVars(DataBaseName=database, 188 324 User='root', 189 325 Host=hostname, … … 192 328 193 329 try: 194 self.dbc = DB Class.DB(self.db_vars)195 except DB Class.DBError, details:330 self.dbc = DB(self.db_vars) 331 except DBError, details: 196 332 debug_msg( 0, 'FATAL ERROR: Unable to connect to database!: ' +str(details) ) 197 333 sys.exit(1) … … 215 351 result = self.dbc.Get( statement ) 216 352 217 except DB Class.DBError, detail:353 except DBError, detail: 218 354 operation = statement.split(' ')[0] 219 355 debug_msg( 0, 'FATAL ERROR: ' +operation+ ' on database failed while doing ['+statement+'] full msg: '+str(detail) )
Note: See TracChangeset
for help on using the changeset viewer.