[TransWarp] component config info
Phillip J. Eby
pje at telecommunity.com
Mon Feb 10 18:51:59 EST 2003
At 03:50 PM 2/10/03 -0800, Joel Boehland wrote:
>Hello Phillip,
>Thanks for your explanation on loading config info for components. I just
>got it to work, and I thought I'd send a little code example to the list
>in case others aren't clear on this.
>
>file conf/component.ini:
>[mycomponent.dottedpath]
>somekey="blah"
>
>
>file componentconfigtest.py:
>
>from peak.api import *
>
>class ConfigComponent(binding.Component):
> def getKey(self):
> key = "mycomponent.dottedpath.somekey"
> return self._getConfigData(key,self)
>
>if __name__ == "__main__":
> cc = ConfigComponent()
> config.loadConfigFile(cc, "conf/component.ini")
> key = cc.getKey()
> print key #should print "blah"
Although _getConfigData() is part of the IConfigSource interface, it is
intended for use only via the 'config' API functions, e.g.
config.getProperty(). A simpler version of your example would be:
class ConfigComponent(binding.Component):
myKey = binding.bindToProperty('mycomponent.dottedpath.somekey')
if __name__ == "__main__":
cc = ConfigComponent()
config.loadConfigFile(cc, "conf/component.ini")
print cc.myKey
One reason to use the config functions rather than _getConfigData()
directly is that _getConfigData() *only* looks up data in the immediately
referenced object; it will not acquire utilities or properties defined
higher up in the component hierarchy.
Depending on what you're trying to do, you may wish to define a base class
that automatically loads the configuration file upon __init__(), or perhaps
that loads it when __instance_implements__ is first
accessed. peak.config.config_components has an example of this,
'BasicConfig', where a 'setup()' method is called upon the first attempt to
retrieve configuration data.
More information about the PEAK
mailing list