access abook addressbook with curses
Ben C
spamspam at spam.eggs
Tue Aug 8 03:39:15 EDT 2006
On 2006-08-08, Fabian Braennstroem <f.braennstroem at gmx.de> wrote:
> Hi Ben,
>
> * Ben C <spamspam at spam.eggs> wrote:
>> On 2006-08-06, Fabian Braennstroem <f.braennstroem at gmx.de> wrote:
>>> Hi Ben,
>>>
>>> * Ben C <spamspam at spam.eggs> wrote:
>>>> On 2006-08-05, Fabian Braennstroem <f.braennstroem at gmx.de> wrote:
>>>>> Hi,
>>>>>
>>>>> I want to get access to my abook address file with python.
>>>>> Does anyone have some python lines to achive this using
>>>>> curses? If not, maybe anybody has small python program doing
>>>>> it with a gui!?
>>>>
>>>> You can just parse the abook addressbook with the ConfigParser, try
>>>> this:
>>>>
>>>> import os
>>>> from ConfigParser import *
>>>>
>>>> abook = ConfigParser()
>>>> abook.read(os.environ["HOME"] + "/.abook/addressbook")
>>>>
>>>> for s in abook.sections():
>>>> print abook.items(s)
>>>
>>> Thanks! I found a different example too:
>>>
>>> import ConfigParser
>>> import string
>>>
>>> config = ConfigParser.ConfigParser()
>>>
>>> config.read("/home/fab/.abook/addressbook")
>>>
>>> # print summary
>>> print
>>> for number in [2,200]:
>>> print string.upper(config.get(str(number), "email"))
>>> print string.upper(config.get(str(number), "name"))
>>> print string.upper(config.get(str(number), "city"))
>>> print string.upper(config.get(str(number), "address"))
>>>
>>> but the problem seems to be that abook does not write every
>>> field, so I get an exception when there is a field missing:
>>>
>>> Traceback (most recent call last):
>>> File "configparser-example-1.py", line 13, in ?
>>> print string.upper(config.get(str(number), "city"))
>>> File "/usr/lib/python2.4/ConfigParser.py", line 520, in get
>>> raise NoOptionError(option, section)
>>> ConfigParser.NoOptionError: No option 'city' in section: '2'
>>>
>>> Section 2 looks like:
>>>
>>> [2]
>>> name=Andrs Gzi
>>> email=anes.oi at ik.e
>>> nick=oz
>>>
>>> Is there a workaround?
>>
>> You can construct the parser with a dictionary of defaults:
>>
>> config = ConfigParser.ConfigParser({"city" : "unknown",
>> "zip" : "unknown"})
>>
>> that kind of thing.
>>
>> Or catch the exceptions. Or use config.options("2") to see what options
>> exist in section 2 before you try to read them.
>
> Thanks! I will try it out!
Looking at the bigger picture here, though, I may be giving you the
wrong advice. Mutt just invokes abook to get the addresses I think,
that's why you put
set query_command="abook --mutt-query '%s'"
So you could do the same (if what you're trying to do is write a mutt
clone in Python):
import subprocess
name = "Andrs"
subprocess.Popen("abook --mutt-query " + name,
stdout=subprocess.PIPE, shell=True).communicate()[0]
The difference is that this "leverages" abook to do the search, not just
to store the data, which is a logical approach.
On the other hand, this way, you require that abook is installed on the
machine, which is no good if the object of the exercise is a portable
Python-only solution.
More information about the Python-list
mailing list