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

« back to all changes in this revision

Viewing changes to src/datasources/drivers/special/configfile/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 2000-2005 Free Software Foundation
 
20
#
 
21
# FILE:
 
22
# config/Connection.py
 
23
#
 
24
# DESCRIPTION:
 
25
# Implementation of dbdriver for use with configuration files
 
26
#
 
27
# NOTES:
 
28
#
 
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.
 
36
#
 
37
# TODO:
 
38
# 1. add a restriction to edit only one section (f.e. just edit the [gnue-forms]
 
39
#    section, or [gnue-designer] section etc.
 
40
#
 
41
# HISTORY:
 
42
# 04/02/05 updated to new db driver API
 
43
# 05/02/12 updated to work with api changes (commit->connection obj.)
 
44
 
 
45
####                                   ####
 
46
#### IF YOU MODIFY ANY CONNECTION      ####
 
47
#### ATTRIBUTES, PLEASE UPDATE info.py ####
 
48
####                                   ####
 
49
 
 
50
VERSION="0.0.3"
 
51
 
 
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
 
59
 
 
60
class Connection(BaseConnection):
 
61
 
 
62
  # TODO: do we have/need Introspection?
 
63
  defaultBehavior = Configfile_Error #Introspection
 
64
 
 
65
  supportedDataObjects = {
 
66
    'object' : Configfile_DataObject
 
67
  }
 
68
 
 
69
  _DatabaseError = Configfile_Error
 
70
 
 
71
  def connect(self, connectData={}):
 
72
    GDebug.printMesg(9,"Configfile database driver connecting...")
 
73
    try:
 
74
      # get filename
 
75
      if connectData.has_key("filename"):
 
76
        self._filename=connectData['filename']
 
77
      elif connectData.has_key("file"):
 
78
        self._filename=connectData['file']
 
79
      else:
 
80
        self._filename=connectData['dbname']
 
81
 
 
82
      # compute path (home directory)
 
83
      if self._filename[0]=="~":
 
84
        self._filename=os.environ["HOME"]+self._filename[1:]
 
85
 
 
86
      # replace placeholder "%configdir%"
 
87
      if self._filename[0:11]=="%configdir%":
 
88
        self._filename=paths.config+self._filename[11:]
 
89
 
 
90
      # try to open and parse file
 
91
      self.native = ConfigParser.ConfigParser()
 
92
      file=open(self._filename)
 
93
      self.native.readfp(file)
 
94
      file.close()
 
95
 
 
96
      # add a fake "cursor()" function to the ConfigParser
 
97
      self.native.cursor = lambda :None
 
98
 
 
99
    except:
 
100
      tmsg = u_("Error opening config file %s") % self._filename
 
101
      raise Exceptions.ConnectionError, tmsg
 
102
 
 
103
  # no authentification required
 
104
  def getLoginFields(self):
 
105
    return []
 
106
 
 
107
  def commit(self): 
 
108
    GDebug.printMesg (9,"Configfile database driver: commit()")
 
109
    GDebug.printMesg (9,"Writing data to: '%s'" % self._filename )
 
110
    try:
 
111
      file=open(self._filename,"w+")
 
112
      self.native.write(file)
 
113
      file.close()
 
114
    except Exception,value:
 
115
      raise Exceptions.ConnectionError, value
 
116
 
 
117
  def rollback(self): 
 
118
    GDebug.printMesg (9,"Configfile database driver: rollback()")
 
119
    return