~toolpart/+junk/pythoncard

« back to all changes in this revision

Viewing changes to samples/dbBrowser/csvBrowse.py

  • Committer: Bazaar Package Importer
  • Author(s): Mohammed Adnène Trojette
  • Date: 2006-11-12 17:52:13 UTC
  • mfrom: (2.1.5 feisty)
  • Revision ID: james.westby@ubuntu.com-20061112175213-tv8bnl6rtpa2qw1o
Tags: 0.8.1-8.1
* Non-maintainer upload.
* Fix path to findfiles, codeEditor and resourceEditor:
   + patch from Ernest ter Kuile <ernestjw@xs4all.nl>. (Closes: #397018)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
__version__="$Revision $"[11:-2]
 
4
__date__="$Date $"
 
5
__author__="Andy Todd <andy47@halfcooked.com>"
 
6
 
 
7
"""
 
8
Module Name: csvBrowse
 
9
Description: Plug in for PythonCard application dbBrowse to provide simple CSV functionality
 
10
 
 
11
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)
 
12
 
 
13
The structure of this module should be replicated for different RDBMS so that they can be interchanged by dbBrowse - hopefully.
 
14
"""
 
15
 
 
16
import csv, os
 
17
 
 
18
class browse:
 
19
    # Connection should be a dictionary with at least two keys, 
 
20
    # 'databasename' and 'directory'
 
21
    # This is wildly different to other database modules 
 
22
    def __init__(self, connection):
 
23
        "Setup the database connection"
 
24
        self._system_tables=[]
 
25
        try:
 
26
            name = os.path.join(connection["directory"], connection["databasename"])
 
27
            self.reader = csv.reader(file(name, "rb"))
 
28
            self.headers = self.reader.next()
 
29
            self._db = "ok"
 
30
            self._cursor="ok"
 
31
        except:
 
32
            self._db = None
 
33
            self._cursor = None
 
34
        # This one is used in getRow
 
35
        self._tableName='TheOnlyTable'
 
36
 
 
37
    def getTables(self):
 
38
        "Return a list of all of the non-system tables in <database>"
 
39
        return [ "TheOnlyTable" ]
 
40
 
 
41
    def getColumns(self, tableName):
 
42
        "Get the definition of the columns in tableName"
 
43
        # format of dbBrowser column definitions is
 
44
        #  column name, data type, length (for display), nullable, key, default
 
45
        columnDefs = []
 
46
        for column in self.headers:
 
47
            columnName = column
 
48
            dataType, nullable, key, default  = "varchar", "", "", ""
 
49
            # Dodgy default, but it works for me
 
50
            precision = 255
 
51
            columnDefs.append((columnName, dataType, precision, nullable, key, default))
 
52
        return columnDefs
 
53
 
 
54
    def getQueryString(self, tableName):
 
55
        "Return a SQL statement which queries all of the columns in tableName"
 
56
        # only used internally, not needed for csv files
 
57
        return ""
 
58
 
 
59
    def getRow(self, tableName):
 
60
        "Get a row from tableName"
 
61
        if tableName!=self._tableName:
 
62
            self._tableName=tableName
 
63
        try:
 
64
            result = self.reader.next()
 
65
        except:
 
66
            result = None
 
67
        return result
 
68
 
 
69
    def getRows(self, tableName):
 
70
        "Get all of the rows from tableName"
 
71
        if tableName!=self._tableName:
 
72
            self._tableName=tableName
 
73
        result = []
 
74
        for row in self.reader:
 
75
            result.append(row)
 
76
        return result
 
77
 
 
78
if __name__ == '__main__':
 
79
    # We are in an interactive session so run our test routines
 
80
    # Connect to the database
 
81
    connection={ 'databasename':'testfile.csv'
 
82
                ,'directory':'.'}
 
83
    dbHolder = browse(connection)
 
84
    # Return all of our table names into user_tables
 
85
    user_tables = dbHolder.getTables()
 
86
 
 
87
    # Print out the structure of each table and its first row
 
88
    print "--------------------------------------------------"
 
89
    for table in user_tables:
 
90
        print dbHolder.getQueryString(table)
 
91
        print dbHolder.getRow(table)
 
92
        print "--------------------------------------------------"