Extending ConfigParser
Berthold Höllmann
bhoel at server.python.net
Sun Jul 11 11:53:45 EDT 1999
Hello,
For my application I need a config file parser. ConfigParser comes close
to my needs, so I made my own Parser class derived from the
ConfigParser:
class MyParser(ConfigParser.ConfigParser):
First I wanted to change the parsing routine to allow white space in
section and option names, so I added the lines:
__SECTCRE = re.compile(r'\[(?P<header>[-\w ]+)\]')
__OPTCRE = re.compile(r'(?P<option>[-.\w ]+)(?P<value>.*)$')
but section names containing blanks still lead to an error. I found the
problem when I tried to add my own "read" method. Looking to the
original class I found, that the main work is done in a method "__read",
so I added
def __read(self, fp):
print "TEST"
return
to my class but still got:
>python cfg_tst.py
Traceback (innermost last):
File "cfg_tst.py", line 5, in ?
C.read("dat/Lines_my.cfg")
File "/usr/local/lib/python1.5/ConfigParser.py", line 175, in read
self.__read(fp)
File "/usr/local/lib/python1.5/ConfigParser.py", line 326, in __read
raise e
ConfigParser.ParsingError: File contains parsing errors:
dat/Lines_my.cfg
[line 47]: '[station -3]\012'
So it seems, that class members whos names begin with "__" can't be
overloaded? Is this right, or have I overseen something?
Greetings
Berthold
--
bhoel at starship.python.net / http://starship.python.net/crew/bhoel/
It is unlawful to use this email address for unsolicited ads
(USC Title 47 Sec.227). I will assess a US$500 charge for
reviewing and deleting each unsolicited ad.
More information about the Python-list
mailing list