Changes between Initial Version and Version 1 of QueryCommand/EvolutionSearch


Ignore:
Timestamp:
Feb 18, 2012 5:08:57 AM (6 years ago)
Author:
rado
Comment:

migrate wiki.mutt.org

Legend:

Unmodified
Added
Removed
Modified
  • QueryCommand/EvolutionSearch

    v1 v1  
     1= A python version using python-evolution bindings =
     2
     3Shameless ripoff from: http://commandline.org.uk/python/three-useful-python-bindings/
     4
     5{{{
     6import evolution
     7import sys
     8
     9query = ' '.join(sys.argv[1:])
     10
     11results_dict = {}
     12
     13for addrDesc, addrName in evolution.ebook.list_addressbooks():
     14    if "couchdb" in addrName:
     15        #Couchdb se suele colgar, lo excluyo de las busquedas
     16        continue
     17    addresses = evolution.ebook.open_addressbook(addrName)
     18    results = addresses.search(query) # Returns List of results
     19    for a in results:
     20        email = a.get_property('email-1').lower()
     21        fullName = a.get_property('full-name')
     22        nick = a.get_property('nickname')
     23        if email is not None:
     24            results_dict[email] = (fullName, nick)
     25
     26if len(results_dict) == 0:
     27    print " no matches"
     28    sys.exit(1)
     29elif len(results_dict) == 1:
     30    print " 1 match! you are lucky"
     31else:
     32    print " %d matches, now use your brain" % len(results_dict)
     33
     34for email, val in results_dict.items():
     35    print '%s\t%s\t%s' % (email, val[0], val[1])
     36
     37}}}
     38----
     39= Original Version =
     40{{{
     41#!/bin/env python
     42#
     43# Description: A Python script for use with the Mutt query_command
     44# which lets you search your Evolution address book for contacts.
     45#
     46# For additional information/help contact Homme Zwaagstra
     47# (<hrz@geodata.soton.ac.uk>)
     48
     49import cStringIO
     50import sys, os
     51
     52def die(msg):
     53    print >> sys.stderr, msg
     54    sys.exit(1)
     55
     56# Try and import required but non standard modules
     57try:
     58    import vobject
     59except ImportError:
     60    die("""The vobject module is required to access vcards.
     61It can be found at <http://vobject.skyhouseconsulting.com/>""")
     62
     63try:   
     64    import bsddb3
     65except ImportError:
     66    die("""The bsddb3 module is required to access the Evolution addresses.
     67It can be found at <http://pybsddb.sourceforge.net/>""")
     68
     69def read_evolution(address_file):
     70    """Read the evolution address book into a buffer, returning the buffer"""
     71
     72    buf = cStringIO.StringIO()
     73    contacts = bsddb3.hashopen(address_file)
     74    for val in contacts.db.values()[1:]:
     75        vcard = val.replace('\x00', "\n")
     76        buf.write(vcard)
     77
     78    # set the buffer cursor to the beginning
     79    buf.seek(0)
     80    return buf
     81
     82
     83# The location of the evolution address book
     84address_file = '~/.evolution/addressbook/local/system/addressbook.db'
     85
     86# The query as a case insensitive string
     87query = sys.argv[1].lower()
     88
     89# Could be a simple vcard file...
     90#vcards = file('~/personal/contacts.vcf', 'r')
     91# ...but we read the vcards from the evolution address book
     92vcards = read_evolution(os.path.expanduser(address_file))
     93
     94# iterate through the vcard, accumulating a list of matches to the query.
     95print "Searching contacts for %s..." % query,
     96contact_count = 0
     97matches = []
     98for vcard in vobject.readComponents(vcards):
     99    try:
     100        if query in vcard.fn.value.lower():
     101            try:
     102                for email in vcard.contents['email']:
     103                    matches.append((email.value, vcard.fn.value))
     104            except KeyError:
     105                pass
     106    except AttributeError:
     107        pass
     108    contact_count += 1
     109vcards.close()
     110
     111match_count = len(matches)
     112if not match_count:
     113    print " no matches in %d entries" % contact_count
     114    sys.exit(1)
     115
     116if match_count > 1:
     117    plural = 'es'
     118else:
     119    plural = ''
     120   
     121print " %d match%s in %d entries" % (match_count, plural, contact_count)
     122for contact in matches:
     123    print "%s\t%s" % contact
     124
     125}}}
     126
     127----
     128
     129= Another one using evolution-data-server in C =
     130
     131{{{
     132/* mutt-eds-query.c
     133 * Sertaç Ö. Y&#305;ld&#305;z <sertacyildizATgmailDOTcom>
     134 *
     135 * compilation (requires evolution-data-server development libraries):
     136 * gcc `pkg-config --cflags --libs libebook-1.2` -o mutt-eds-query mutt-eds-query.c
     137 */
     138
     139#include <libebook/e-book.h>
     140#include <libebook/e-contact.h>
     141#include <libebook/e-book-query.h>
     142
     143#define PROGRAM_NAME    "mutt-eds-query"
     144#define VERSION         "0.1"
     145
     146static void
     147print_mutt_line (const gchar *email, const gchar *name,
     148                 const gchar *nick, const gchar *notes)
     149{
     150        printf ("%s\t%s\t%-10s %s\n",
     151                email ? email : "",
     152                name  ?  name : "",
     153                nick  ?  nick : "",
     154                notes ?  notes: "");
     155}
     156
     157static void
     158process_results (gpointer data, gpointer user_data)
     159{
     160        EContact *contact;
     161        const gchar *fullname, *nickname, *notes;
     162        GList *emails, *e;
     163
     164        g_return_if_fail (E_IS_CONTACT(data));
     165        contact = E_CONTACT (data);
     166
     167        fullname = e_contact_get_const (contact, E_CONTACT_FULL_NAME);
     168        nickname = e_contact_get_const (contact, E_CONTACT_NICKNAME);
     169        notes    = e_contact_get_const (contact, E_CONTACT_NOTE);
     170        emails = e_contact_get (contact, E_CONTACT_EMAIL);
     171
     172        for (e = emails; e; e = e->next)
     173                print_mutt_line ((const char *) e->data, fullname,
     174                                 nickname, notes);
     175
     176        g_list_foreach (emails, (GFunc) g_free, NULL);
     177        g_list_free (emails);
     178}
     179
     180static void
     181usage (const gchar *me)
     182{
     183        fprintf (stderr, "%s (v" VERSION ") : simple e-d-s wrapper for mutt\n", me);
     184        fprintf (stderr, "Usage: %s <query>\n\n", me);
     185}
     186
     187int
     188main (int argc, char *argv[])
     189{
     190        EBook *book;
     191        EBookQuery *query;
     192        GList *contacts;
     193
     194        g_type_init();
     195
     196        if ( argc > 1 && g_strstr_len (argv[1], 3, "-h")) {
     197                usage (PROGRAM_NAME);
     198                exit (1);
     199        }
     200
     201        if (argc == 1)
     202                query = e_book_query_any_field_contains ("");
     203        else
     204                query = e_book_query_any_field_contains (argv[1]);
     205
     206        if (query == NULL) {
     207                fprintf (stderr, "Couldn't create query.\n");
     208                exit (2);
     209        }
     210
     211        book = e_book_new_system_addressbook (NULL);
     212        if (!e_book_open (book, TRUE, NULL)) {
     213                fprintf (stderr, "Couldn't load system addressbook.\n");
     214                exit (2);
     215        }
     216
     217        if (!e_book_get_contacts (book, query, &contacts, NULL)) {
     218                fprintf (stderr, "Couldn't get query results.\n");
     219                exit (2);
     220        }
     221
     222        e_book_query_unref (query);
     223        g_object_unref (book);
     224
     225        if (contacts == NULL) {
     226                printf ("No matches.\n");
     227                return (-1);
     228        } else {
     229                printf ("Matches for '%s'...\n", argv[1]);
     230                g_list_foreach (contacts, (GFunc) process_results, NULL);
     231        }
     232
     233        return 0;
     234}
     235}}}