2
# This file is part of GNU Enterprise.
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.
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.
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.
19
# Copyright 2001-2005 Free Software Foundation
25
# Static database driver for loading data from a CSV file
28
# Requires Python >= 2.3
31
__all__ = ['Connection']
34
#### IF YOU MODIFY ANY CONNECTION ####
35
#### ATTRIBUTES, PLEASE UPDATE info.py ####
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
47
class Csv_Error(StandardError):
50
class Connection(BaseConnection):
51
defaultBehavior = Introspection
52
supportedDataObjects = {'object': CSV_DataObject,
53
'static': CSV_DataObject} # TODO: Why static?!?
54
_DatabaseError = Csv_Error
56
# We don't do connections (we are connectionless)
57
def connect(self, connectData={}):
59
GDebug.printMesg(9,'Open file: %s' % (connectData['dbname']))
60
self.native = csv.DictReader(file(connectData['dbname']),[])
62
tmsg = _('CSV file not found.')
63
raise self._DatabaseError, tmsg
65
tmsg = _('Wrong file format.')
66
raise self._DatabaseError, tmsg
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())
73
self.native.fieldnames = ["x%s" % i for i in range(len(self._reader.next()))]
75
self.native.fname = os.path.split(connectData['dbname'])[1]
77
# no authentification required
78
def getLoginFields(self):