Search code examples
pythonconfigparser

Argparse - configparser.Interpolation missing option error


I am using Argparse for fetching the necessary value from config file.

For example:

python arg.py --event_conf=/opt/open-stack-tools/track_events.conf --openstack_conf=/etc/nova/nova.conf

I need to fetch value from two different files.

I can be able to get the results as needed for one local config file.

But In case of fetching the necessary values from nova.conf file, it results in following error:

       Traceback (most recent call last):
          File "arg.py", line 36, in <module>
            oslo_messaging_rabbit= dict(config.items("oslo_messaging_rabbit"))
          File "/usr/lib/python2.7/ConfigParser.py", line 655, in items
            for option in options]
          File "/usr/lib/python2.7/ConfigParser.py", line 691, in _interpolate
            self._interpolate_some(option, L, rawval, section, vars, 1)
          File "/usr/lib/python2.7/ConfigParser.py", line 723, in _interpolate_some
            option, section, rest, var)
ConfigParser.InterpolationMissingOptionError: Bad value substitution:
        section: [oslo_messaging_rabbit]
        option : logging_exception_prefix
        key    : color
        rawval : %(asctime)s.%(msecs)03d TRACE %(name)s %(instance)s

Is there any way to fix the same.

I have copied the necessary contents and created a new local file, I can see that it is working fine.

When I am using the nova.conf file it results in error.

I can't change the file which I am using.

So I need a fix for the particular error.

Note:

Adding more details as needed:

parser.add_argument("-c", "--event_conf",
                    help="Specify config file 1", metavar="FILE")
args1, remaining_argv1 = parser.parse_known_args()


parser.add_argument("-o", "--openstack_conf",
                    help="Specify config file 2", metavar="FILE")
args2, remaining_argv2 = parser.parse_known_args()


if args1.event_conf:
    config = ConfigParser.SafeConfigParser()
    print config.read([args1.event_conf])
    config.read([args1.event_conf])
    configdetails_section1 = dict(config.items("configdetails_section1"))

Solution

  • I found the solution for the same.

    Actually issue was with the configparser which I have used.

    Instead of SafeConfigParser I changed it to RawConfigParser.

    Then I can be able to see that it is working fine.