~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/datasources/drivers/csv/csv/Connection.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# This file is part of GNU Enterprise.
 
3
#
 
4
# GNU Enterprise is free software; you can redistribute it
 
5
# and/or modify it under the terms of the GNU General Public
 
6
# License as published by the Free Software Foundation; either
 
7
# version 2, or (at your option) any later version.
 
8
#
 
9
# GNU Enterprise is distributed in the hope that it will be
 
10
# useful, but WITHOUT ANY WARRANTY; without even the implied
 
11
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 
12
# PURPOSE. See the GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public
 
15
# License along with program; see the file COPYING. If not,
 
16
# write to the Free Software Foundation, Inc., 59 Temple Place
 
17
# - Suite 330, Boston, MA 02111-1307, USA.
 
18
#
 
19
# Copyright 2001-2005 Free Software Foundation
 
20
#
 
21
# FILE:
 
22
# csv/Connection.py
 
23
#
 
24
# DESCRIPTION:
 
25
# Static database driver for loading data from a CSV file
 
26
#
 
27
# NOTES:
 
28
# Requires Python >= 2.3
 
29
 
 
30
 
 
31
__all__ = ['Connection']
 
32
 
 
33
####                                   ####
 
34
#### IF YOU MODIFY ANY CONNECTION      ####
 
35
#### ATTRIBUTES, PLEASE UPDATE info.py ####
 
36
####                                   ####
 
37
 
 
38
import string
 
39
import os.path
 
40
import csv
 
41
from gnue.common.apps import GDebug
 
42
from gnue.common.datasources.drivers.Base.Connection import Connection as BaseConnection
 
43
from gnue.common.datasources.drivers.csv.Schema.Discovery.Introspection import Introspection
 
44
from DataObject import CSV_DataObject
 
45
 
 
46
 
 
47
class Csv_Error(StandardError):
 
48
  pass
 
49
 
 
50
class Connection(BaseConnection):
 
51
  defaultBehavior = Introspection
 
52
  supportedDataObjects = {'object': CSV_DataObject,
 
53
                           'static': CSV_DataObject} # TODO: Why static?!?
 
54
  _DatabaseError = Csv_Error
 
55
 
 
56
  # We don't do connections (we are connectionless)
 
57
  def connect(self, connectData={}):
 
58
    try:
 
59
      GDebug.printMesg(9,'Open file: %s' % (connectData['dbname']))
 
60
      self.native = csv.DictReader(file(connectData['dbname']),[])
 
61
    except IOError:
 
62
      tmsg = _('CSV file not found.')
 
63
      raise self._DatabaseError, tmsg
 
64
    except TypeError:
 
65
      tmsg = _('Wrong file format.')
 
66
      raise self._DatabaseError, tmsg
 
67
 
 
68
    # build fieldnames list
 
69
    self._reader = csv.reader(file(connectData['dbname']))
 
70
    if connectData.has_key('firstrowisheader'):
 
71
      self.native.fieldnames = map(string.lower, self.native.reader.next())
 
72
    else:
 
73
      self.native.fieldnames = ["x%s" % i for i in range(len(self._reader.next()))]
 
74
 
 
75
    self.native.fname = os.path.split(connectData['dbname'])[1]
 
76
 
 
77
  # no authentification required
 
78
  def getLoginFields(self):
 
79
    return []
 
80