diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index dfeeee93..96fd3569 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -2,6 +2,7 @@ from __future__ import print_function import threading import requests from . import capi +from .capi import ffi import deltachat @@ -36,6 +37,30 @@ class EventHandler: return 0 # always online +class Contact: + def __init__(self, dc_context, contact_id): + self.dc_context = dc_context + self.id = contact_id + # XXX do we need to free dc_contact_t? (we own it according to API) + self.dc_contact_t = capi.lib.dc_get_contact(self.dc_context, contact_id) + + @property + def addr(self): + return ffi.string(capi.lib.dc_contact_get_addr(self.dc_contact_t)) + + @property + def display_name(self): + return ffi.string(capi.lib.dc_contact_get_display_name(self.dc_contact_t)) + + @property + def is_blocked(self): + return capi.lib.dc_contact_is_blocked(self.dc_contact_t) + + @property + def is_verified(self): + return capi.lib.dc_contact_is_verified(self.dc_contact_t) + + class Account: def __init__(self, db_path, logcallback=None, eventhandler=None): self.dc_context = ctx = capi.lib.dc_context_new( @@ -48,6 +73,7 @@ class Account: if eventhandler is None: eventhandler = EventHandler(self.dc_context) self._eventhandler = eventhandler + self._threads = IOThreads(self.dc_context) def set_config(self, **kwargs): for name, value in kwargs.items(): @@ -60,10 +86,13 @@ class Account: res = capi.lib.dc_get_config(self.dc_context, name, b'') return capi.ffi.string(res).decode("utf8") + def create_contact(self, emailadr, name=ffi.NULL): + contact_id = capi.lib.dc_create_contact(self.dc_context, name, emailadr) + return Contact(self.dc_context, contact_id) + def start(self): - deltachat.set_context_callback(self.dc_context, self.process_event) + deltachat.set_context_callback(self.dc_context, self._process_event) capi.lib.dc_configure(self.dc_context) - self._threads = IOThreads(self.dc_context) self._threads.start() def shutdown(self): @@ -76,7 +105,7 @@ class Account: # threads might still need it. # capi.lib.dc_close(self.dc_context) - def process_event(self, ctx, evt_name, data1, data2): + def _process_event(self, ctx, evt_name, data1, data2): assert ctx == self.dc_context self._logcallback((evt_name, data1, data2)) method = getattr(self._eventhandler, evt_name.lower(), None) diff --git a/python/tests/conftest.py b/python/tests/conftest.py index 90286ae0..9caf9dbc 100644 --- a/python/tests/conftest.py +++ b/python/tests/conftest.py @@ -30,12 +30,13 @@ def acfactory(pytestconfig, tmpdir, request): self.configlist.append(d) self.count = 0 - def get_live_account(self, logcallback=None): + def get_live_account(self, logcallback=None, started=True): configdict = self.configlist.pop(0) tmpdb = tmpdir.join("testdb%d" % self.count) ac = Account(tmpdb.strpath, logcallback=logcallback) ac.set_config(**configdict) - ac.start() + if started: + ac.start() request.addfinalizer(ac.shutdown) return ac diff --git a/python/tests/test_account.py b/python/tests/test_account.py index 30da4e92..f97dc7f5 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -8,6 +8,15 @@ except ImportError: class TestLive: + def test_contacts(self, acfactory): + ac1 = acfactory.get_live_account(started=False) + contact1 = ac1.create_contact("some1@hello.com", name="some1") + assert contact1.id + assert contact1.addr == "some1@hello.com" + assert contact1.display_name == "some1" + assert not contact1.is_blocked + assert not contact1.is_verified + def test_basic_configure_login_ok(self, acfactory): q = Queue() ac1 = acfactory.get_live_account(logcallback=q.put) diff --git a/python/tox.ini b/python/tox.ini index 275b401e..681bb579 100644 --- a/python/tox.ini +++ b/python/tox.ini @@ -34,8 +34,6 @@ minversion = 2.0 python_files = tests/test_*.py norecursedirs = .tox ja .hg cx_freeze_source xfail_strict=true -filterwarnings = - # produced by path.local [flake8] max-line-length = 120