|
From: Alex T. <ale...@us...> - 2004-09-04 11:46:55
|
Update of /cvsroot/pythoncard/PythonCard/samples/dbBrowser In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23821/samples/dbBrowser Modified Files: dbLogin.py dbLogin.rsrc.py Added Files: csvBrowse.py Log Message: Added support for CSV files in dbBrowser sample. --- NEW FILE: csvBrowse.py --- #!/usr/bin/python __version__="$Revision $"[11:-2] __date__="$Date $" __author__="Andy Todd <an...@ha...>" """ Module Name: csvBrowse Description: Plug in for PythonCard application dbBrowse to provide simple CSV functionality Constant/configuration values are currently maintained in the source code. If we are to optimise this application they should be split into seperate configuration files (as per PythonCard/Webware style guidelines) The structure of this module should be replicated for different RDBMS so that they can be interchanged by dbBrowse - hopefully. """ import csv, os class browse: # Connection should be a dictionary with at least two keys, # 'databasename' and 'directory' # This is wildly different to other database modules def __init__(self, connection): "Setup the database connection" self._system_tables=[] try: name = os.path.join(connection["directory"], connection["databasename"]) self.reader = csv.reader(file(name, "rb")) self.headers = self.reader.next() self._db = "ok" self._cursor="ok" except: self._db = None self._cursor = None # This one is used in getRow self._tableName='TheOnlyTable' def getTables(self): "Return a list of all of the non-system tables in <database>" return [ "TheOnlyTable" ] def getColumns(self, tableName): "Get the definition of the columns in tableName" # format of dbBrowser column definitions is # column name, data type, length (for display), nullable, key, default columnDefs = [] for column in self.headers: columnName = column dataType, nullable, key, default = "varchar", "", "", "" # Dodgy default, but it works for me precision = 255 columnDefs.append((columnName, dataType, precision, nullable, key, default)) return columnDefs def getQueryString(self, tableName): "Return a SQL statement which queries all of the columns in tableName" # only used internally, not needed for csv files return "" def getRow(self, tableName): "Get a row from tableName" if tableName!=self._tableName: self._tableName=tableName try: result = self.reader.next() except: result = None return result def getRows(self, tableName): "Get all of the rows from tableName" if tableName!=self._tableName: self._tableName=tableName result = [] for row in self.reader: result.append(row) return result if __name__ == '__main__': # We are in an interactive session so run our test routines # Connect to the database connection={ 'databasename':'testfile.csv' ,'directory':'.'} dbHolder = browse(connection) # Return all of our table names into user_tables user_tables = dbHolder.getTables() # Print out the structure of each table and its first row print "--------------------------------------------------" for table in user_tables: print dbHolder.getQueryString(table) print dbHolder.getRow(table) print "--------------------------------------------------" Index: dbLogin.py =================================================================== RCS file: /cvsroot/pythoncard/PythonCard/samples/dbBrowser/dbLogin.py,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** dbLogin.py 12 Aug 2004 19:18:50 -0000 1.10 --- dbLogin.py 4 Sep 2004 11:46:39 -0000 1.11 *************** *** 18,22 **** ,'MetaKit': 'metakitBrowse' ,'PySQLite': 'pysqliteBrowse' ! ,'PostgreSQL': 'postgreBrowse'} class dbLogin(model.CustomDialog): --- 18,23 ---- ,'MetaKit': 'metakitBrowse' ,'PySQLite': 'pysqliteBrowse' ! ,'PostgreSQL': 'postgreBrowse' ! ,'CSV': 'csvBrowse' } class dbLogin(model.CustomDialog): *************** *** 44,47 **** --- 45,51 ---- self.parent.connection={'databasename':self.components["txtUsername"].text, 'directory':self.components["txtPassword"].text} + elif self.components.choice.stringSelection == 'CSV': + self.parent.connection={'databasename':self.components["txtUsername"].text, + 'directory':self.components["txtPassword"].text} else: self.parent.connection={'username':self.components["txtUsername"].text, *************** *** 70,73 **** --- 74,81 ---- wildcard = wildcard = "Gadfly files (*.gfd)|*.gfd" result = dialog.openFileDialog(wildcard=wildcard) + elif self.components.choice.stringSelection == 'CSV': + wildcard = "CSV files (*.csv)|*.csv" + ## wildcard = "*.csv" + result = dialog.openFileDialog(wildcard=wildcard) else: result = dialog.openFileDialog() *************** *** 87,90 **** --- 95,104 ---- self.components.txtDatabase.visible = False self.components.btnFile.visible = True + elif self.components.choice.stringSelection in ('CSV'): + self.components.lblUsername.text = "File" + self.components.lblPassword.text = "Directory" + self.components.lblDatabase.visible = False + self.components.txtDatabase.visible = False + self.components.btnFile.visible = True else: self.components.lblUsername.text = 'Username' Index: dbLogin.rsrc.py =================================================================== RCS file: /cvsroot/pythoncard/PythonCard/samples/dbBrowser/dbLogin.rsrc.py,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** dbLogin.rsrc.py 22 Apr 2004 16:42:42 -0000 1.14 --- dbLogin.rsrc.py 4 Sep 2004 11:46:39 -0000 1.15 *************** *** 15,19 **** 'name':'choice', 'position':(110, 0), ! 'items':['MySQL', 'Oracle', 'Gadfly', 'MetaKit', 'PySQLite', 'PostgreSQL'], 'stringSelection':'MySQL', }, --- 15,19 ---- 'name':'choice', 'position':(110, 0), ! 'items':['MySQL', 'Oracle', 'Gadfly', 'MetaKit', 'PySQLite', 'PostgreSQL', 'CSV'], 'stringSelection':'MySQL', }, |