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 2000-2005 Free Software Foundation
22
# config/Connection.py
25
# Implementation of dbdriver for use with configuration files
29
# 1. every config section = one record
30
# 2. section name = field "_section_name" (handle it similar to a private key)
31
# i.e. throw an database error, if two sections have the same name
32
# 3. empty records wont be written, only if you write an '""' into it
33
# 4. The path and filename of the config file should be provided by the
34
# dbname, or a filename attribute of the <database> tag (both work).
35
# The place holder '~' and '%configdir%' will be resolved.
38
# 1. add a restriction to edit only one section (f.e. just edit the [gnue-forms]
39
# section, or [gnue-designer] section etc.
42
# 04/02/05 updated to new db driver API
43
# 05/02/12 updated to work with api changes (commit->connection obj.)
46
#### IF YOU MODIFY ANY CONNECTION ####
47
#### ATTRIBUTES, PLEASE UPDATE info.py ####
52
from gnue.common.datasources.drivers.Base.Connection import Connection as BaseConnection
53
from gnue.common.datasources import Exceptions
54
from DataObject import Configfile_DataObject
55
from RecordSet import Configfile_Error
56
from gnue.common.apps import GDebug
57
from gnue import paths
58
import os, ConfigParser
60
class Connection(BaseConnection):
62
# TODO: do we have/need Introspection?
63
defaultBehavior = Configfile_Error #Introspection
65
supportedDataObjects = {
66
'object' : Configfile_DataObject
69
_DatabaseError = Configfile_Error
71
def connect(self, connectData={}):
72
GDebug.printMesg(9,"Configfile database driver connecting...")
75
if connectData.has_key("filename"):
76
self._filename=connectData['filename']
77
elif connectData.has_key("file"):
78
self._filename=connectData['file']
80
self._filename=connectData['dbname']
82
# compute path (home directory)
83
if self._filename[0]=="~":
84
self._filename=os.environ["HOME"]+self._filename[1:]
86
# replace placeholder "%configdir%"
87
if self._filename[0:11]=="%configdir%":
88
self._filename=paths.config+self._filename[11:]
90
# try to open and parse file
91
self.native = ConfigParser.ConfigParser()
92
file=open(self._filename)
93
self.native.readfp(file)
96
# add a fake "cursor()" function to the ConfigParser
97
self.native.cursor = lambda :None
100
tmsg = u_("Error opening config file %s") % self._filename
101
raise Exceptions.ConnectionError, tmsg
103
# no authentification required
104
def getLoginFields(self):
108
GDebug.printMesg (9,"Configfile database driver: commit()")
109
GDebug.printMesg (9,"Writing data to: '%s'" % self._filename )
111
file=open(self._filename,"w+")
112
self.native.write(file)
114
except Exception,value:
115
raise Exceptions.ConnectionError, value
118
GDebug.printMesg (9,"Configfile database driver: rollback()")