Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"))
like image 678
Murali Avatar asked Dec 10 '22 20:12

Murali


2 Answers

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.

like image 65
Murali Avatar answered Dec 13 '22 09:12

Murali


Interpolation allows you to refer to values of the configuration while defining others. For example, in this config file:

[bug_tracker]
protocol = http
server = localhost
port = 8080
url = %(protocol)s://%(server)s:%(port)s/bugs/
username = dhellmann
password = SECRET

if you read like this:

from ConfigParser import SafeConfigParser

parser = SafeConfigParser()
parser.read('interpolation.ini')

print 'value =', parser.get('bug_tracker', 'url')

you will get:

value = http://localhost:8080/bugs/

that can be very useful, the problem seems that that you pass some values in run time. I mean you need the format string to actually substitute the values by yourself.

You can use RawConfigParser instead of SafeConfigParser but then you loose all the interpolations.

Instead you can suppress the interpolation for one specific value:

print 'value =', parser.get('bug_tracker', 'url', raw=True)

and the result would be:

value = %(protocol)s://%(server)s:%(port)s/bugs/

There is also the possibility that you need to combine interpolated values with some of them given in evaluation time. For example if you want to give the user in evaluation time, you can also include it into the config expression:

[bug_tracker]
protocol = http
server = localhost
port = 8080
url = %(protocol)s://%(user)s@%(server)s:%(port)s/bugs/
username = dhellmann
password = SECRET

and then you need to make something like this:

from ConfigParser import SafeConfigParser

parser = SafeConfigParser()
parser.read('interpolation.ini')

parser.set('bug_tracker', 'user', 'admin')
print 'value =', parser.get('bug_tracker', 'url')

and you'll get:

value = http://admin@localhost:8080/bugs/

Sorry that I didn't use your example. I did take the one in the documentation of another project. See the section: Combining Values with Interpolation

like image 37
yucer Avatar answered Dec 13 '22 09:12

yucer