access abook addressbook with curses

  • Thread starter Fabian Braennstroem
  • Start date
F

Fabian Braennstroem

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!?

Greetings!
Fabian
 
B

Ben C

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)
 
F

Fabian Braennstroem

Hi Ben,

* Ben C said:
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 protected]
nick=oz

Is there a workaround?


Greetings!
Fabian
 
B

Ben C

Hi Ben,

* Ben C said:
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 protected]
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.
 
F

Fabian Braennstroem

Hi Ben,

* Ben C said:
Hi Ben,

* Ben C said:
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 protected]
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!

Greetings!
Fabian
 
B

Ben C

Hi Ben,

* Ben C said:
Hi Ben,

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 protected]
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.
 
F

Fabian Braennstroem

Hi Ben,
* Ben C said:
Hi Ben,

* Ben C said:
Hi Ben,

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 protected]
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.

The biggest problem is a missing and not allowed
abook installation. But thanks for your tips!

Greetings!
Fabian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top