Preserving whitespace around line seperator

2 views
Skip to first unread message

Dwayne Bailey

unread,
Dec 6, 2007, 1:02:53 AM12/6/07
to iniparse-discuss
Hi,

I'm using iniparse to parse INI files that need translating. I chose
iniparse because it preserves whitespace, layout., order, etc.

The following patch was needed to ensure that iniparse does not mess
with the whitespace around the seperator. i.e:

bob=7

will now not change to:

bob = 7

on updating

Index: misc/ini.py
===================================================================
--- misc/ini.py (revision 6743)
+++ misc/ini.py (working copy)
@@ -105,7 +105,7 @@


class OptionLine(LineType):
- def __init__(self, name, value, separator='=', comment=None,
+ def __init__(self, name, value, separator=' = ', comment=None,
comment_separator=None, comment_offset=-1,
line=None):
super(OptionLine, self).__init__(line)
self.name = name
@@ -116,15 +116,15 @@
self.comment_offset = comment_offset

def to_string(self):
- out = '%s %s %s' % (self.name, self.separator, self.value)
+ out = '%s%s%s' % (self.name, self.separator, self.value)
if self.comment is not None:
# try to preserve indentation of comments
out = (out+' ').ljust(self.comment_offset)
out = out + self.comment_separator + self.comment
return out

- regex = re.compile(r'^(?P<name>[^:=\s[][^:=]*)'
- r'\s*(?P<sep>[:=])\s*'
+ regex = re.compile(r'^(?P<name>[^:=\s[][^:=\s]*)'
+ r'(?P<sep>\s*[:=]\s*)'
r'(?P<value>.*)$')

def parse(cls, line):

Dwayne Bailey

unread,
Dec 6, 2007, 1:07:35 AM12/6/07
to iniparse-discuss

Tim Lauridsen

unread,
Dec 6, 2007, 6:01:58 AM12/6/07
to iniparse...@googlegroups.com
You patch looks good to me, i nobody have any objections i will commit it

Tim

Paramjit Oberoi

unread,
Dec 7, 2007, 12:27:01 PM12/7/07
to iniparse...@googlegroups.com
Looks good to me. Let's add a test for it as well (I can do it
sometime this weekend unless Tim beats me to it).

-param

Tim Lauridsen

unread,
Dec 7, 2007, 1:43:20 PM12/7/07
to iniparse...@googlegroups.com
Paramjit Oberoi wrote:
> Looks good to me. Let's add a test for it as well (I can do it
> sometime this weekend unless Tim beats me to it).
>
> -param
>
Did some testing, it look like there is some of the tests there needs
fixing because the separator is now preserved in all case

'option = value' => 'option', ' = ','value'
'option=value' => 'option', '=','value'
'option = value' => 'option', ' = ','value'

before the separator was '=' in all cases.

i have some problems running the tests
test_compat.py:
this line fails for me.
from test import test_support

where do test come from ?

Tim

Paramjit Oberoi

unread,
Dec 7, 2007, 1:55:23 PM12/7/07
to iniparse...@googlegroups.com
test_compat comes from the Python test suite. The rest of the tests
are additional tests that I added.

I had to change the original python standard test a little bit -
mostly because that test was written to assume that the output would
be in the order of the internal hash table.

-param

Paramjit Oberoi

unread,
Dec 8, 2007, 3:49:37 PM12/8/07
to iniparse...@googlegroups.com
The earlier patch caused tests to fail because option names can
contain spaces. An alternative patch is attached.

-param

sepspace.diff

Tim Lauridsen

unread,
Dec 9, 2007, 3:00:09 AM12/9/07
to iniparse...@googlegroups.com
Look good.

Tim

Paramjit Oberoi

unread,
Dec 9, 2007, 5:10:24 AM12/9/07
to iniparse...@googlegroups.com
Committed to SVN.

There have been two changes since 0.2.2 - handling empty files, and
this one. Shall I release 0.2.3?

-param

Tim Lauridsen

unread,
Dec 10, 2007, 2:20:17 AM12/10/07
to iniparse...@googlegroups.com
That would be nice.

TIm

Dwayne Bailey

unread,
Dec 12, 2007, 1:56:17 AM12/12/07
to iniparse-discuss
Thanks for the quick fix!

On Dec 10, 9:20 am, Tim Lauridsen <tim.laurid...@googlemail.com>
wrote:
> Paramjit Oberoi wrote:
> > Committed to SVN.
>
> > There have been two changes since 0.2.2 - handling empty files, and
> > this one. Shall I release 0.2.3?
>
> > -param
>
Reply all
Reply to author
Forward
0 new messages