|
From: <ki...@us...> - 2003-01-03 23:22:59
|
Update of /cvsroot/pymerase/pymerase/output/PyTkWidgets/lib In directory sc8-pr-cvs1:/tmp/cvs-serv8042 Added Files: dbSession.py Log Message: First pass at database session and connection objects --- NEW FILE: dbSession.py --- #!/usr/bin/env python ########################################################################### # # # C O P Y R I G H T N O T I C E # # Copyright (c) 2003 by: # # * California Institute of Technology # # # # All Rights Reserved. # # # # Permission is hereby granted, free of charge, to any person # # obtaining a copy of this software and associated documentation files # # (the "Software"), to deal in the Software without restriction, # # including without limitation the rights to use, copy, modify, merge, # # publish, distribute, sublicense, and/or sell copies of the Software, # # and to permit persons to whom the Software is furnished to do so, # # subject to the following conditions: # # # # The above copyright notice and this permission notice shall be # # included in all copies or substantial portions of the Software. # # # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS # # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN # # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # # SOFTWARE. # ########################################################################### # # Author: Brandon King # URL: http://pymerase.sourceforge.net # Last Modified: $Date: 2003/01/03 23:22:56 $ # import Tkinter import tkMessageBox #FIXME: Import from proper DBAPI from DvdAPI import DBSession class dbConnect: """ Class used to create a connection to the database (used by sessionObj) """ def __init__(self, master, dsn=None, database=None, user=None, password=None): self.parent = master self.dbs = None if dsn is None and \ database is None and \ user is None and \ password is None: self.display() def display(self): self.top = Tkinter.Toplevel(self.parent) self.top.title("Database Connection") self.HeaderFrame = Tkinter.Frame(self.top) self.BodyFrame = Tkinter.Frame(self.top) self.FooterFrame = Tkinter.Frame(self.top) self.HeaderFrame.grid(row=0) self.BodyFrame.grid(row=1) self.FooterFrame.grid(row=2) self.dsnLabel = Tkinter.Label(self.BodyFrame, text="Host:") self.dsnLabel.grid(row=0, column=0, sticky=Tkinter.E) self.dsnEntry = Tkinter.Entry(self.BodyFrame) self.dsnEntry.grid(row=0, column=1, sticky=Tkinter.W) self.databaseLabel = Tkinter.Label(self.BodyFrame, text="Database:") self.databaseLabel.grid(row=1, column=0, sticky=Tkinter.E) self.databaseEntry = Tkinter.Entry(self.BodyFrame) self.databaseEntry.grid(row=1, column=1, sticky=Tkinter.W) self.userLabel = Tkinter.Label(self.BodyFrame, text="User:") self.userLabel.grid(row=2, column=0, sticky=Tkinter.E) self.userEntry = Tkinter.Entry(self.BodyFrame) self.userEntry.grid(row=2, column=1, sticky=Tkinter.W) self.passwordLabel = Tkinter.Label(self.BodyFrame, text="Password:") self.passwordLabel.grid(row=3, column=0, sticky=Tkinter.E) self.passwordEntry = Tkinter.Entry(self.BodyFrame) self.passwordEntry.grid(row=3, column=1, sticky=Tkinter.W) self.ConnectButton = Tkinter.Button(self.FooterFrame, text="Connect", bg='blue', fg='white', command=self.connect) self.ConnectButton.grid(row=1) self.parent.wait_window(self.top) def getDsn(self): return self.dsnEntry.get() def getDatabase(self): return self.databaseEntry.get() def getUser(self): return self.userEntry.get() def getPassword(self): return self.passwordEntry.get() def connect(self): dsn = self.getDsn() db = self.getDatabase() user = self.getUser() password = self.getPassword() if dsn == "": dsn = None if db == "": db = None if user == "": user = None if password == "": password = None try: self.dbs = DBSession(dsn, db, user, password) print "Connected" self.top.destroy() except: tkMessageBox.showwarning("Warning", "Unable to connect") def getDbs(self): while self.dbs is None: self.display() return self.dbs class sessionObj: """ Session Object for Database Connection """ def __init__(self, master): self.dbs = None self.parent = master def getDbs(self): if self.dbs is None: self.connect() return self.dbs def connect(self, dsn=None, database=None, user=None, password=None): dbCon = dbConnect(self.parent) self.dbs = dbCon.getDbs() if __name__ == '__main__': root = Tkinter.Tk() root.title('Database Connect') l = Tkinter.Label(root, text="Database Connection Test") l.pack() session = sessionObj(root) print session.getDbs() root.mainloop() |