0

I am using the ConfigParser module like this:

from ConfigParser import ConfigParser
c = ConfigParser()
c.read("pymzq.ini")

However, the sections gets botched up like this:

>>> c.sections()
['pyzmq:platform.architecture()[0']

for the pymzq.ini file which has ] in tht title to mean something:

[pyzmq:platform.architecture()[0] == '64bit']
url = ${pkgserver:fullurl}/pyzmq/pyzmq-2.2.0-py2.7-linux-x86_64.egg
Nishant
  • 20,354
  • 18
  • 69
  • 101

2 Answers2

1

Looks like ConfigParser uses a regex that only parses section lines up to the first closing bracket, so this is as expected.

You should be able to subclass ConfigParser/RawConfigParser and change that regexp to something that better suits your case, such as ^\[(?P<header>.+)\]$, maybe.

AKX
  • 152,115
  • 15
  • 115
  • 172
1

Thanks for the pointer @AKX, I went with:

class MyConfigParser(ConfigParser):
    _SECT_TMPL = r"""
        \[                                 # [
        (?P<header>[^$]+)                  # Till the end of line
        \]                                 # ]
        """
    SECTCRE = re.compile(_SECT_TMPL, re.VERBOSE)

Please let me know if you have any better versions. Source code of the original ConfigParser.

Nishant
  • 20,354
  • 18
  • 69
  • 101