Retrieving comments

6 views
Skip to first unread message

Dwayne Bailey

unread,
Dec 12, 2007, 2:03:54 AM12/12/07
to iniparse-discuss
Hi,

I'm using iniparse to parse INI files that are to be translated.
Often the comments are then relevant to the translator e.g.:

; Don't translate Foo
name = You need to install Foo

In this case the comment is quite important to the translator. In the
traditional use of .ini to store config data its not that important I
would guess. But I could imagine a use case in which a config is
edited in a simple table and the comment is available to help the
user.

I've looked through the code and can't see an easy way to access to
comment data associated with an option. My assumption is that
anything before my current option but after the last option is a
comment that relates to the current option.

Is there some easy way to get those comments that I've missed? If not
what would be a good way to implement this?

Paramjit Oberoi

unread,
Dec 12, 2007, 10:45:00 AM12/12/07
to iniparse...@googlegroups.com
My feel that adding the comment for translators to the ini file as
data instead of as a comment might be a better way to solve the
problem. That way everything is explicit and easier to understand.
But, I don't know the full details of the situation, and you may be
dealing with existing files that you cannot change.

It is possible to access the comment, but keep in mind that there is
no supported API for it, and what I suggest below can stop working at
any time when internal details of iniparse change.

Take a look at the LineContainer class. INIConfig._data is a line
container, containing line containers:

>>> import iniparse
>>> inifile = """
... [section1]
... #comment 1
... option1 = value1
... #comment 2
... option2 = value2
... """
>>> from StringIO import StringIO
>>> i=iniparse.INIConfig(StringIO(inifile))
>>> i._data.find('section1')
<iniparse.ini.LineContainer object at 0x80de0ac>
>>> for x in i._data.find('section1').contents:
... print type(x), x
...
<class 'iniparse.ini.SectionLine'> [section1]
<class 'iniparse.ini.CommentLine'> #comment 1
<class 'iniparse.ini.LineContainer'> option1 = value1
<class 'iniparse.ini.CommentLine'> #comment 2
<class 'iniparse.ini.LineContainer'> option2 = value2

HTH,
-param

Dwayne Bailey

unread,
Dec 13, 2007, 5:01:50 AM12/13/07
to iniparse-discuss
On Dec 12, 5:45 pm, "Paramjit Oberoi" <psobe...@gmail.com> wrote:
> My feel that adding the comment for translators to the ini file as
> data instead of as a comment might be a better way to solve the
> problem. That way everything is explicit and easier to understand.
> But, I don't know the full details of the situation, and you may be
> dealing with existing files that you cannot change.

You guessed it, we're usually consuming .ini files that we have not
created and that we can't change much.

> It is possible to access the comment, but keep in mind that there is
> no supported API for it, and what I suggest below can stop working at
> any time when internal details of iniparse change.
>
> Take a look at the LineContainer class. INIConfig._data is a line
> container, containing line containers:
>
> >>> import iniparse
> >>> inifile = """
>
> ... [section1]
> ... #comment 1
> ... option1 = value1
> ... #comment 2
> ... option2 = value2
> ... """>>> from StringIO import StringIO
> >>> i=iniparse.INIConfig(StringIO(inifile))
> >>> i._data.find('section1')
>
> <iniparse.ini.LineContainer object at 0x80de0ac>>>> for x in i._data.find('section1').contents:
>
> ... print type(x), x
> ...
> <class 'iniparse.ini.SectionLine'> [section1]
> <class 'iniparse.ini.CommentLine'> #comment 1
> <class 'iniparse.ini.LineContainer'> option1 = value1
> <class 'iniparse.ini.CommentLine'> #comment 2
> <class 'iniparse.ini.LineContainer'> option2 = value2

Thanks for those hints. I'm happy to accept the risk of internal
changes, if I see a way to expose this more clearly then I'll try.

>
> HTH,
> -param
Reply all
Reply to author
Forward
0 new messages