ConfigParser and $'s in the options

Steffen Ries steffen.ries at sympatico.ca
Fri Sep 14 08:12:05 EDT 2001


HariNam Singh <hsingh at elite.com> writes:

> I'm pretty new to Python (Java background). It's a pretty cool language so
> far. I was happy to find that there is already code that handles dealing
> with configuration files: ConfigParser.
> 
> Though, I run into an issue with it. It refuses to parse configuration files
> that have $ - signs in the option names. As I'm working on a 3rd party app,
> which I don't have the source code for, I'll have to use the $ signs in the
> option names. Is there a way to make ConfigParser do that, without changing
> the code?

You can subclass ConfigParser and provide your own regular expression
for parsing options:

--8<--
import ConfigParser
import re

class ConfigParserDollar(ConfigParser.ConfigParser):
    def __init__(self, defaults=None):
        ConfigParser.ConfigParser.__init__(self, defaults)
        self.OPTCRE = re.compile(
            r'(?P<option>[]\-[\w_.*,(){}$]+)'
            #              added '$'   ^^^
            r'[ \t]*(?P<vi>[:=])[ \t]*'
            r'(?P<value>.*)$'
            )
--8<--

The value of OPTCRE is copied from the implementation + the '$' as
valid option letter.

Alternatively you can override OPTCRE in your instance of
ConfigParser.

hth,
/steffen
-- 
steffen.ries at sympatico.ca	<> Gravity is a myth -- the Earth sucks!



More information about the Python-list mailing list