[Zope-Coders] Re: New API for Zope configuration info

Fred L. Drake, Jr. fred@zope.com
Tue, 11 Feb 2003 10:07:47 -0500


R. David Murray writes:
 > Question:  why a function?  Why can't I just import the config
 > object that has the attributes from the module, and save myself a line
 > of code in every module I need it in? 

This is because I want to be able to separate the import of the
feature from the use of the configuration feature.  If you imported a
configuration object directly into your module, it could not easily be
replaced with a different configuration.  Being able to do that would
be very useful for testing -- a unit test could set up an alternate
configurtion object and have that be used instead of the original in
its setUp() method, and then restore the original in tearDown().

(What I omitted in my original post was that there's also an API to
set the configuration object.  Application code that runs post-startup
should never need to be aware of that.)

I envision a module that needs to use the configuration data will look
something like this:

import os
from App.config import getConfiguration

class MyThing:
    def myMethod(self, reldir):
        cfg = getConfiguration()
        path = os.path.join(cfg.instancehome, reldir, "mything.dat")
        # use path...


  -Fred

-- 
Fred L. Drake, Jr.  <fred at zope.com>
PythonLabs at Zope Corporation