![]() |
2010-04-10
, 15:11
|
Posts: 86 |
Thanked: 362 times |
Joined on Dec 2007
@ Paris / France
|
#2
|
![]() |
2010-09-13
, 13:57
|
Posts: 56 |
Thanked: 31 times |
Joined on Jul 2008
@ Austria
|
#3
|
#!/usr/bin/env python import ctypes import sys import gtk # needed, otherwise abook won't work. import glib import osso def from_GList(native, B_free_list, B_unref_items): """ given a native C GList, returns the items of the list. """ class _GList(ctypes.Structure): _fields_ = [("data", ctypes.c_void_p), ("next", ctypes.c_void_p)] try: l = native while l: l = _GList.from_address(l) yield l.data l = l.next finally: l = native if B_unref_items else None while l: l = _GList.from_address(l) c_gobject.g_object_unref(l.data) l = l.next if B_free_list: c_glib.g_list_free(native) # ctypes wrapper for pygobject_new(), based on code snippet from # http://faq.pygtk.org/index.py?req=sh...=faq23.041.htp class _PyGObject_Functions(ctypes.Structure): _fields_ = [ ('register_class', ctypes.PYFUNCTYPE(ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int, ctypes.py_object, ctypes.py_object)), ('register_wrapper', ctypes.PYFUNCTYPE(ctypes.c_void_p, ctypes.py_object)), ('register_sinkfunc', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)), ('lookupclass', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_int)), ('newgobj', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)), ] class PyGObjectCAPI(object): def __init__(self): import gobject py_obj = ctypes.py_object(gobject._PyGObject_API) addr = ctypes.pythonapi.PyCObject_AsVoidPtr(py_obj) self._api = _PyGObject_Functions.from_address(addr) def pygobject_new(self, addr): return self._api.newgobj(addr) c_pyapi = PyGObjectCAPI() context = osso.Context("test_abook", "0.1") c_gobject = ctypes.CDLL("libgobject-2.0.so.0") c_glib = ctypes.CDLL("libglib-2.0.so.0") book = ctypes.CDLL("libosso-abook-1.0.so.0") argv_type = ctypes.c_char_p * len(sys.argv) argv = argv_type(*sys.argv) argc = ctypes.c_int(len(sys.argv)) book.osso_abook_init(ctypes.byref(argc), ctypes.byref(argv), hash(context)) c_None = ctypes.c_void_p() roster = book.osso_abook_aggregator_new(c_None, c_None) # TODO GError book.osso_abook_roster_start(roster) book.osso_abook_waitable_run(roster, c_glib.g_main_context_default(), None) # TODO GError if not book.osso_abook_waitable_is_ready(roster, c_None): # TODO GError print("whoops") sys.exit(1) for i in range(2): contacts = map(c_pyapi.pygobject_new, from_GList(book.osso_abook_aggregator_list_master_contacts(roster), True, False)) book.osso_abook_contact_get_display_name.restype = ctypes.c_char_p for contact in contacts: #print(contact) #print(dir(contact)) print(contact.props.display_name) # note do not free resulting string. #print(book.osso_abook_contact_get_display_name(contact))
I've been following this tutorial PyMaemo/Accessing APIs Without Python Bindings
and I'm only trying to get the number of contacts, or get GList of them. I just want anything that shows it works (except for the example in the tutorial because it simple works
Attached is the pygobject class