472,958 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

New Thread- Supporting Multiline values in ConfigParser

Hi,
Am starting a new thread as I fear the old thread which more than a
week old can go unnoticed.
Sorry for the multiple mails.

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,optname)

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

Jun 25 '07 #1
3 2799
En Mon, 25 Jun 2007 11:06:04 -0300, Phoe6 <or*******@gmail.comescribió:
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.
And how would you detect a multiline value?
Because it is not a section nor looks like a new option?
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)
Yes, I guess so.
I'd try using this:

if not self.SECTCRE.match(line) and not self.OPTCRE.match(line) and
cursect is not None and optname:

(that is, if the line is not a section header and it's not a new option
and there is a current section and option; those two last conditions same
as the previous version).
But you'll have to experiment - I've not tested it.

--
Gabriel Genellina

Jun 26 '07 #2
* Gabriel Genellina <ga*******@yahoo.com.ar[2007-06-25 22:26:47]:
And how would you detect a multiline value?
Because it is not a section nor looks like a new option?
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)

Yes, I guess so.
I'd try using this:

if not self.SECTCRE.match(line) and not self.OPTCRE.match(line) and
cursect is not None and optname:
Thanks Gabriel, this suggestion worked and helped me understand the
ConfigParser a bit better as well.

--
O.R.Senthil Kumaran
http://uthcode.sarovar.org
Jun 27 '07 #3
En Wed, 27 Jun 2007 01:35:27 -0300, O.R.Senthil Kumaran
<or*******@users.sourceforge.netescribió:
* Gabriel Genellina <ga*******@yahoo.com.ar[2007-06-25 22:26:47]:
>And how would you detect a multiline value?
Because it is not a section nor looks like a new option?

I'd try using this:

if not self.SECTCRE.match(line) and not self.OPTCRE.match(line) and
cursect is not None and optname:

Thanks Gabriel, this suggestion worked and helped me understand the
ConfigParser a bit better as well.
Good to know it worked - I was not entirely sure it would :)

--
Gabriel Genellina
Jun 27 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: adeger | last post by:
Having trouble with my first forays into threads. Basically, the threads don't seem to be working in parallel (or you might say are blocking). I've boiled my problems to the following short code...
4
by: Gilles Leblanc | last post by:
Hi I have started a small project with PyOpenGL. I am wondering what are the options for a GUI. So far I checked PyUI but it has some problems with 3d rendering outside the Windows platform. I...
7
by: Ivan | last post by:
Hi I have following problem: I'm creating two threads who are performing some tasks. When one thread finished I would like to restart her again (e.g. new job). Following example demonstrates...
4
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the...
5
by: Razzie | last post by:
Hi all, A question from someone on a website got me thinking about this, and I wondered if anyone could explain this. A System.Threading.Timer object is garbage collected if it has no...
16
by: droopytoon | last post by:
Hi, I start a new thread (previous one was "thread timing") because I have isolated my problem. It has nothing to do with calling unmanaged C++ code (I removed it in a test application). I...
9
by: mareal | last post by:
I have noticed how the thread I created just stops running. I have added several exceptions to the thread System.Threading.SynchronizationLockException System.Threading.ThreadAbortException...
13
by: Bob Day | last post by:
Using vs2003, vb.net I start a thread, giving it a name before start. Code snippet: 'give each thread a unique name (for later identification) Trunk_Thread.Name = "Trunk_0_Thread" ' allow...
7
by: Charles Law | last post by:
My first thought was to call WorkerThread.Suspend but the help cautions against this (for good reason) because the caller has no control over where the thread actually stops, and it might have...
3
by: John Nagle | last post by:
There's no way to set thread priorities within Python, is there? We have some threads that go compute-bound, and would like to reduce their priority slightly so the other operations, like...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.