On Jun 21, 7:34 pm, "John Krukoff" <jkruk...@ltgc.comwrote:
Is there anyway, I can include multi-line value in the configfile? I
Following the link to RFC 822 (http://www.faqs.org/rfcs/rfc822.html)
indicates that you can spread values out over multiple lines as long as
there is a space or tab character imeediately after the CRLF.
Thanks for the response. It did work!
>>config = ConfigParser()
>>config.read("Testcases.txt")
['Testcases.txt']
>>output = config.get("Information", "Testcases")
>>print output
tct123
tct124
tct125
>>output
'\ntct123\ntct124\ntct125'
However, as I am going to provide Testcases.txt to be "user editable",
I cannot assume or "ask users" to provide value testcases surronded by
spaces. I got to figure out a workaround here.
Sounds like you're stuck modifying ConfigParser to do what you want, or
writing your own configuration file parsing utilities.
From looking through the ConfigParser source, looks like all the parsing
work is inside the _read method, so shouldn't be too painful to make a
subclass that does what you want.
I took the approach of Subclassing ConfigParser to support multiline
values without leading white-spaces, but am struct at which position
in _read I should modify to accomodate the non-leading whitespace
based multiline values.
I can guess, this portion in the _read function will require change,
any change to this affects the whole of parsing. :-( Can someone who
has done this before or understands ConfigParser better help me?
# Section I am talking about
if line[0].isspace() and cursect is not None and optname:
value = line.strip()
if value:
cursect[optname] = "%s\n%s" % (cursect[optname], value)
# _read method
def _read(self, fp, fpname):
cursect = None
optname = None
lineno = 0
e = None
while True:
line = fp.readline()
if not line:
break
lineno = lineno + 1
# comment or blank line?
if line.strip() == '' or line[0] in '#;':
continue
if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR":
# no leading whitespace
continue
# continuation line
print "line:%s\tcursect:%s\toptname:%s"%(line,cursect,op tname)
if line[0].isspace() and cursect is not None and optname:
value = line.strip()
if value:
cursect[optname] = "%s\n%s" % (cursect[optname], value)
# a section header or option header?
else:
# is it a section header?
mo = self.SECTCRE.match(line)
if mo:
sectname = mo.group('header')
if sectname in self._sections:
cursect = self._sections[sectname]
elif sectname == ConfigParser.DEFAULTSECT:
cursect = self._defaults
else:
cursect = {'__name__':sectname}
self._sections[sectname] = cursect
# So sections can't start with a continuation line
optname = None
elif cursect is None:
raise ConfigParser.MissingSectionHeaderError(fpname, lineno,
line)
# an option line?
else:
mo = self.OPTCRE.match(line)
if mo:
optname, vi, optval = mo.group('option','vi','value')
if vi in ('=',':') and ';' in optval:
# ';' is a comment delimiter only if it follows
# a spacing character
pos = optval.find(';')
if pos != -1 and optval[pos-1].isspace():
optval = optval[:pos]
optval = optval.strip()
# allow empty values
if optval == '""':
optval = ''
optname = self.optionxform(optname.rstrip())
cursect[optname] = optval
else:
if not e:
e = ConfigParser.ParsingError(fpname)
e.append(lineno, repr(line))
if e:
raise e
--
Senthil