How to access desktopcouch contacts from outside evolution?
I'd like to be able to query my desktopcouch contacts list from mutt or emacs; this is because, while I use evolution on my full-powered system, on my small bring-it-everywhere laptop I run a minimal X session and emacs. I'd like to have my contacts available on both, though. Can you guys point me to a resource somewhere that documents how i could perform a query on the db externally, say from bash or python; i could then hook into mutt or emacs using their own interfaces.
thanks, i'm really looking forward to desktopcouch!
matt
Question information
- Language:
- English Edit question
- Status:
- Solved
- Assignee:
- Stuart Langridge Edit question
- Solved by:
- Matt Price
- Solved:
- 2009-10-15
- Last query:
- 2009-10-15
- Last reply:
- 2009-10-15
- Whiteboard:
- Stuart, Can you answer this question for Matt? Thanks!
Nicola Larosa (teknico) said : | #1 |
Hi Matt, there are some initial Python API docs in the desktopcouch/
Once you have any integration working with mutt or emacs, it'd be great to have a look at it!
Stuart Langridge (sil) said : | #2 |
Matt: those docs should be on your machine as well as /usr/share/
Matt Price (matt-price) said : | #3 |
Nicola and Stuart, thanks for the pointers. Looking at the
seem entirely straightforward to query the database, say, looking for a
name or an email. so, for instance, i have this couchdb record in my
contacts database (ugly text copied from the html view):
_id "pas-id-
description
address
if i know this in advance i can access the individual fields like this:
>>> db = CouchDatabase(
>>> fetched = db.get_
>>> print fetched[
but what if i want to search for people named 'me', or addresses
containing "gmail"? Will i need to create a design document, then write
a view, and a function that iterates overthe rows returned?
And do the map/reduce functions need to be written in
javascript?
by evolution -- that i can query directly from python? (i noticed the
web interface doesn't think there are any permanent design docs in the
contacts db - i'm wondering also if that's one reason why email-address
completion is currently so slow in evolution).
thanks much for your help. i'll keep mining the web for more info, but
any further assistance you guys can give is much appreciated.
Matt
Matt Price (matt-price) said : | #4 |
hey sorry for the lousy formatting on that last response, copied and
pasted from evolution after my last email was rejected...
Stuart Langridge (sil) said : | #5 |
Yep; the way to query the database is by writing a view to do so. In your example, your view would look like:
function(doc) {
emit(
}
and then you access that view by name
result = db.execute_
print result["me"]
Matt Price (matt-price) said : | #6 |
On Thu, 2009-10-15 at 18:06 +0000, Stuart Langridge wrote:
> Your question #85917 on desktopcouch changed:
> https:/
>
> Stuart Langridge posted a new comment:
> Yep; the way to query the database is by writing a view to do so. In
> your example, your view would look like:
>
> function(doc) {
> emit(doc.
> }
>
> and then you access that view by name
>
> result = db.execute_
> print result["me"]
>
since you're generous enough to answer,
this works great, thusly:
>>> map_js = "function(doc) {emit (doc.last_
>>> db.add_
>>> result=
>>> print result[
[<Row id='pas-
'me', 'last_name': 'gmail', '_rev':
'1-74cb956983d1
'http://
'_id': 'pas-id-
{'eed38e30-
'address': '<email address hidden>'}}}>]
print result just gave me the object description:
<ViewResults <PermanentView
'_design/
since what i really want is a completion function, i think i need to
return something like a list of dictionaries, or maybe just of lists:
[['Matt Price', '<email address hidden>'],['Matt
Price','<email address hidden>
What do you think would be the best way to get this information? i
thought from the way you responded last time that there's an efficient
way of grabbing the data directly from couchdb, rather than getting a
big old python object and then massaging it slowly in my python code.
thanks for being patient with me. it's fun to learn this stuff.
matt
--
Matt Price
<email address hidden>
Matt Price (matt-price) said : | #7 |
> Stuart Langridge posted a new comment:
> Yep; the way to query the database is by writing a view to do so. In
> your example, your view would look like:
>
> function(doc) {
> emit(doc.
> }
>
> and then you access that view by name
>
> result = db.execute_
> print result["me"]
getting closer to understanding this. if i do this:
result=
then result.rows returns a tuple of row objects that look like this:
<Row id='pas-
but if instead i define a very simple mapping function:
map_js = "function(doc) {emit (doc.last_
db.add_
result=
then the tuple returned looks like this:
<Row id='pas-
and the key is now a useful value. nonetheless i can't just execute
print result["me"]
and get the values i'm looking for. instead i have a somewhat
ugly-feeling selection logic:
for x in result:
if x.key="me":
for y in x.value[
print x.value[
this seems a little clumsy to me and i'm wondering whether i'm missing a
class somewhere that would make my code less convoluted and ultimately
more reusable by other people.
it seems a bit odd to keep posting to this question, and i know you're
all busy with the release coming up; so just ignore this if it's the
wrong forum.
thanks for all the help,
matt
Matt Price (matt-price) said : | #8 |
I'm returning to this after a while and I have some very simple code
that works OK for a query on last name:
Matt Price (matt-price) said : | #9 |
[oops, hit ctrl-return]
here's the code:
!/usr/bin/python
import sys
from desktopcouch.
from desktopcouch.
# define the search string
searchString=""
if len(sys.argv) > 1:
searchString= sys.argv[1]
# initialize db
db = CouchDatabase(
#create view
design_doc = "matts_cli_client"
map_js = "function(doc) {emit (doc.last_
db.add_
# results=
results=
# print matching results
for x in results:
if searchString.
print x.value[
if "email_addresses" in x.value:
for y in x.value[
-------
This seems pretty functional for now, and I can figure out the best way
to format the output for emacs or mutt a little later. First, though,
I'd like to improve the search a little. This very simple function lets
me search on last name only, but it would probably be better if it
checked for matches in a variety of fields -- say first, last, and all
the email addresses. It'd be great if my key, in the view, were a
combination of those fields (so that key looked like, say, "Matt Price
<email address hidden>"). In fact, if I could just get the map/reduce
functions to produce a sequence such keys, that's really be all i need.
So I just wondered whether you could tell me where the evolution
contact-search design document is, so I could usei t as a model -- I
assume it's somewhere, but i didn't find it in a quick search through
the source of evolution-couchdb and desktopcouch.
Thanks as always!
Stuart Langridge (sil) said : | #10 |
Matt,
You may not know that a map function can emit more than one document. So:
import sys
from desktopcouch.
from desktopcouch.
# define the search string
searchString=""
if len(sys.argv) > 1:
searchstring= sys.argv[1]
# initialize db
db = CouchDatabase(
#create view
design_doc = "matts_cli_client"
map_js = """function(doc) {
if (doc.last_name) emit(doc.
if (doc.first_name) emit(doc.
for (k in doc.email_
}
}"""
db.add_
# results=
results=
# print matching results
for contact in results[
print "%(first_name)s %(last_name)s" % contact.value
if "email_addresses" in contact.value:
for eml in contact.
print " %s" % contact.