473,698 Members | 2,841 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\tcurse ct:%s\toptname: %s"%
(line,cursect,o ptname)

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.ma tch(line)
if mo:
sectname = mo.group('heade r')
if sectname in self._sections:
cursect =
self._sections[sectname]
elif sectname ==
ConfigParser.DE FAULTSECT:
cursect =
self._defaults
else:
cursect =
{'__name__':sec tname}

self._sections[sectname] = cursect
# So sections can't start with
a continuation line
optname = None
elif cursect is None:
raise
ConfigParser.Mi ssingSectionHea derError(fpname , lineno,
line)
# an option line?
else:
mo = self.OPTCRE.mat ch(line)
if mo:
optname, vi, optval =
mo.group('optio n','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.optionxfor m(optname.rstri p())
cursect[optname] =
optval
else:
if not e:
e =
ConfigParser.Pa rsingError(fpna me)
e.append(lineno ,
repr(line))
if e:
raise e

--
Senthil

Jun 25 '07 #1
3 2839
En Mon, 25 Jun 2007 11:06:04 -0300, Phoe6 <or*******@gmai l.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.ma tch(line) and not self.OPTCRE.mat ch(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*******@yaho o.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.ma tch(line) and not self.OPTCRE.mat ch(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*******@user s.sourceforge.n etescribió:
* Gabriel Genellina <ga*******@yaho o.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.ma tch(line) and not self.OPTCRE.mat ch(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
2171
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 block and ensuing output. Seems like the output should be all interleaved and of course it's not. Running Python 2.2 from ActiveState on Windows XP (also doesn't work on Windows 2000). Thanks in advance! adeger
4
2892
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 know of WxPython but I don't know if I can create a WxPython window, use gl rendering code in it and then put widgets on top of that...
7
2700
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 that. Problem is that when program is started many threads are created (see output section), when only two should be running at any time. Can you please help me to identify me where the problem is? Best regards
4
5429
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 server. Data is sent and received over these connections using the asynchronous model. The server is currently in beta testing. Sporadically over the course of the day, I'll observe the thread count on the process (via perfmon) start climbing....
5
2826
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 references to it. But what about threads? If I start a new thread that only does 1+1, is it garbage collected after that? If so, do all threads get garbage collected eventually that are 'done'? And if not, why not? Are there references to the thread that...
16
3298
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 have a thread "_itTaskThread" running. The application is listening on a TCP port. It accepts 2 connection from a client. I simulate a crash on the client side (using "Debug->StopDebugging").
9
3071
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 System.Threading.ThreadInterruptedException System.Threading.ThreadStateException to see if I could get more information about why the thread stops running but that code is never executed. Any ideas on how I can debug this?
13
5079
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 only 1 thread per line Trunk_Thread.ApartmentState = ApartmentState.STA
7
2688
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 a lock pending, for example. I want to be able to stop a thread temporarily, and then optionally resume it or stop it for good.
3
35253
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 accessing the database and servicing queries, aren't slowed as much. John Nagle
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.