Andrew Groom wrote:
Hi there,

I've got a few global constants that I'd like to be available to my application and a .zcml file seems to be a sensible place. I'm thinking of something like a .zcml file with, e.g.:

    <setting name="name1" value="value1"/>
    <setting name="name2" value="value2"/>

This would then be available in a component as a dictionary. Is anythign like this already available ?

You could just use <utility>, and thereby group your constants into logical chunks.

class IDatabaseLoginOptions(Interface):
    username = Attribute()
    password = Attribute()

class DatabaseLoginOptions(object):
    username = 'foo'
    password = 'bar'


<utility factory=".config.DatabaseLoginOptions" />


opts = getUtility(IDatabaseLoginOptions)

Obviously, this is a bit more work than just declaring some constants in ZCML, but global constants suffer the same problems whether they're defined in Python or XML. Parts of your application are making assumptions that they are there, with very specific names, which are not type checked.

With the solution outlined above, you can logically separate different types of options into different utilities, that may be provided in different ways. For example, your tests can just plug in a different utility using provideUtility(), no config file swapping to deal with. If you need some parts of your application to use different settings, you can provide a local utility, for example, and if you need to make this configurable by the user in the future, then a site-local (persistent) utility is the natural place to store it - it'll override the global registration.


Zope3-users mailing list

Reply via email to