3

I would like to get ConfigParser working for what seems to be a simple problem but I have an ancient 2.2 Jython system (that cannot be updated).

I would like to loop through the sections in my config file and use the same operation on its values. The first section reads fine but on the second iteration of the loop I am getting "exceptions.AttributeError".

[DEFAULT]
uHome=/opt/app/myapp/configs

[Domains]
DomainList=Dom1,Dom2

[Dom1]
userconfigFile=idm-JIT.config
userkeyFile=idm-JIT.key
admU=http://idmap01xj:7001

[Dom2]
userconfigFile=iam-JIT.config
userkeyFile=iam-JIT.key
admU=http://idmap01xjvip:7003

My (greatly) Simplified script:

import ConfigParser
config = ConfigParser.ConfigParser()

try:
    config.optionxform = str
    config.read(domainConfigFile);

    domainList = config.get("Domains","DomainList")
    domainNames = domainList.split(",")

    for dName in domainNames:
        UCF = config.get(dName,"uHome") + '/'+config.get(dName,"userConfigFile")
        UKF = config.get(dName,"uHome") + '/'+config.get(dName,"userKeyFile")
        admU = config.get(dName,"admU")

        print "UCF=["+UCF+"] UKF=["+UKF+"] admU=["+admU+"]"
except:
    print "Error occurred"

I'm not very fluent in Python yet (but this problem is making me so). I have been researching similar problems and playing with some snippets that modify the dict, and they work in a stock Python 2.6 interpreter but they all fail miserably in Jython 2.2.6. How can I fake the same key names in different sections so that they are all addressable?

user3723061
  • 31
  • 1
  • 3
  • The name of the exception is useful. The *complete* exception stack trace is much more useful. That way, one can see exactly where the exception was raised. – davidrmcharles May 11 '15 at 22:20
  • The error occurs right at the first config.get() statement inside the for loop. The first time through the loop it gets the first section info like I expect (UCF=/opt/app/myapp/configs/idm-JIT.config, UKF=/opt/app/myapp/configs/idm-JIT.key, admU=http://idmap01xj:7001). The second time through the loop, however, it stops at the first config.get() statement and throws the exception. I'm reusing the same option names but assigned to another section. – user3723061 May 12 '15 at 02:33
  • The `Domains` section is unnecessary. In your loop, try using `config.sections()` instead, and change your `except` to `except Exception, e: print e`. That should help you figure out the problem. If not, post the exception back here and we'll go from there – Tommy May 12 '15 at 02:45
  • I found the cause, it was a name collision. A subfunction I had called in my main script apparently overwrote the "config" object.Unexpected error: exceptions.AttributeError Traceback (innermost last): File "/opt/app/oracle/middleware/shared/config/scripts/startme.py", line 333, in ? File "/opt/app/oracle/middleware/shared/config/scripts/startme.py", line 303, in executeCommand File "/opt/app/oracle/middleware/shared/config/scripts/startme.py", line 232, in _startAll AttributeError: 'function' object has no attribute 'get' – user3723061 May 12 '15 at 18:23

0 Answers0