Hi Jonas,

Here is some code I use to create a DA during product installation:

from Products.mxODBCZopeDA.ZopeDA import ZopeConnection

   def manage_afterAdd(self, item, container):
       # add mxODBC connection

       conn_string = readDatabaseConfig()
       tmpConnection = ZopeConnection('dbConnection',
                       'Database Connection',

       self._setObject('dbConnection', tmpConnection)

It is in the manage_afterAdd method of my product, which basically creates a site object to hang the rest of my code and skins on. In this case it's creating an mxODBCDA connection, but the same principle would apply for any DA. I think for most other DA's the "ZopeConnection" method will be "Connection".

It uses a config file (using ZConfig) which looks like this to set the conn_string. The conn_string will vary somewhat depending on the DA. A typical config file looks like this:

dsn       development
user      master
password  xxxxxxxx

and a ZConfig schema:

<schema prefix="DatabaseConfig.database">

<key name="dsn"      datatype="string" required="yes"/>
<key name="user"     datatype="string" required="yes"/>
<key name="password" datatype="string" required="yes"/>


The readDatabaseConfig module is:

import ZConfig
import os
from Globals import package_home

def readDatabaseConfig(newlines=True):
   SCHEMA = os.path.join(package_home(globals()), 'schema_odbc.xml')
config_file = os.path.join(package_home(globals()), 'database_odbc.conf') schema = ZConfig.loadSchema(SCHEMA)
   cfg, nil = ZConfig.loadConfig(schema, config_file)

   if newlines:
       nl = '\n'
       nl = ''
   return 'DSN=%s;%sUID=%s;%sPWD=%s' \
          % (cfg.dsn, nl, cfg.user, nl, cfg.password)

I use this because we set up many separate sites all using this product and it saves the manual installation and also standardizes naming conventions (all the DA's get the same id: dbConnection). I also sometimes have sysadmins not familiar with Zope doing installs for me.

You should be able to modify this for your DA.


Bob Corriher
P-Wave Inc.
Zope-DB mailing list

Reply via email to