From 823d1283ace52729dc88a501f2c19db5c24f0f02 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Fri, 7 Sep 2018 20:53:12 +0200 Subject: [PATCH] a first multi-account test, no real checks yet --- python/src/deltachat/_build.py | 13 ++------ python/src/deltachat/account.py | 56 +++++++++++++++++++++++++++------ python/tests/test_account.py | 23 ++++++++++++++ python/tests/test_lowlevel.py | 1 + python/tox.ini | 4 +-- 5 files changed, 76 insertions(+), 21 deletions(-) diff --git a/python/src/deltachat/_build.py b/python/src/deltachat/_build.py index 90993c9e..84d69721 100644 --- a/python/src/deltachat/_build.py +++ b/python/src/deltachat/_build.py @@ -1,6 +1,7 @@ import distutils.ccompiler import distutils.sysconfig import tempfile +import re from os.path import dirname, abspath from os.path import join as joinpath @@ -11,16 +12,8 @@ deltah = joinpath(dirname(dirname(dirname(here))), "src", "deltachat.h") def read_event_defines(): - for line in open(deltah): - if line.startswith("#define"): - parts = line.split() - if len(parts) >= 3: - if parts[1].startswith("DC_EVENT"): - try: - int(parts[2]) - except ValueError: - continue - yield line + rex = re.compile(r'#define\s+(?:DC_EVENT_|DC_CONTACT_ID_)\S+\s+(\d+).*') + return filter(rex.match, open(deltah)) def ffibuilder(): diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index 96fd3569..e9cb1c02 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -6,11 +6,16 @@ from .capi import ffi import deltachat -def eventprinter(evt): - evt_name, data1, data2 = evt - t = threading.currentThread() - tname = getattr(t, "name", t) - print("[" + tname + "]", evt_name, data1, data2) +class EventPrinter: + def __init__(self, dc_context): + self.dc_context = dc_context + self.info = str(self.dc_context).strip(">").split()[-1] + + def __call__(self, evt): + evt_name, data1, data2 = evt + t = threading.currentThread() + tname = getattr(t, "name", t) + print("[%s-%s]" % (tname, self.info), evt_name, data1, data2) class EventHandler: @@ -46,11 +51,11 @@ class Contact: @property def addr(self): - return ffi.string(capi.lib.dc_contact_get_addr(self.dc_contact_t)) + return ffi_unicode(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)) + return ffi_unicode(capi.lib.dc_contact_get_display_name(self.dc_contact_t)) @property def is_blocked(self): @@ -61,6 +66,16 @@ class Contact: return capi.lib.dc_contact_is_verified(self.dc_contact_t) +class Chat: + def __init__(self, dc_context, chat_id): + self.dc_context = dc_context + self.id = chat_id + + def send_text_message(self, msg): + msg = convert_bytes(msg) + return capi.lib.dc_send_text_msg(self.dc_context, self.id, msg) + + class Account: def __init__(self, db_path, logcallback=None, eventhandler=None): self.dc_context = ctx = capi.lib.dc_context_new( @@ -69,7 +84,9 @@ class Account: if hasattr(db_path, "encode"): db_path = db_path.encode("utf8") capi.lib.dc_open(ctx, db_path, capi.ffi.NULL) - self._logcallback = logcallback or eventprinter + if logcallback is None: + logcallback = EventPrinter(self.dc_context) + self._logcallback = logcallback if eventhandler is None: eventhandler = EventHandler(self.dc_context) self._eventhandler = eventhandler @@ -84,12 +101,21 @@ class Account: def get_config(self, name): name = name.encode("utf8") res = capi.lib.dc_get_config(self.dc_context, name, b'') - return capi.ffi.string(res).decode("utf8") + return ffi_unicode(res) + + def get_self_contact(self): + return Contact(self.dc_context, capi.lib.DC_CONTACT_ID_SELF) def create_contact(self, emailadr, name=ffi.NULL): + name = convert_bytes(name) + emailadr = convert_bytes(emailadr) contact_id = capi.lib.dc_create_contact(self.dc_context, name, emailadr) return Contact(self.dc_context, contact_id) + def create_chat_by_contact(self, contact): + chat_id = capi.lib.dc_create_chat_by_contact_id(self.dc_context, contact.id) + return Chat(self.dc_context, chat_id) + def start(self): deltachat.set_context_callback(self.dc_context, self._process_event) capi.lib.dc_configure(self.dc_context) @@ -154,3 +180,15 @@ class IOThreads: while not self._thread_quitflag: capi.lib.dc_perform_smtp_jobs(self.dc_context) capi.lib.dc_perform_smtp_idle(self.dc_context) + + +def convert_bytes(obj): + if obj == ffi.NULL: + return obj + if not isinstance(obj, bytes): + return obj.encode("utf8") + return obj + + +def ffi_unicode(obj): + return ffi.string(obj).decode("utf8") diff --git a/python/tests/test_account.py b/python/tests/test_account.py index f97dc7f5..6ddbe991 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -8,6 +8,13 @@ except ImportError: class TestLive: + + def test_selfcontact(self, acfactory): + ac1 = acfactory.get_live_account(started=False) + me = ac1.get_self_contact() + assert me.display_name + # assert me.addr # xxx why is this empty? + def test_contacts(self, acfactory): ac1 = acfactory.get_live_account(started=False) contact1 = ac1.create_contact("some1@hello.com", name="some1") @@ -17,6 +24,12 @@ class TestLive: assert not contact1.is_blocked assert not contact1.is_verified + def test_chat(self, acfactory): + ac1 = acfactory.get_live_account(started=False) + contact1 = ac1.create_contact("some1@hello.com", name="some1") + chat = ac1.create_chat_by_contact(contact1) + assert chat.id + def test_basic_configure_login_ok(self, acfactory): q = Queue() ac1 = acfactory.get_live_account(logcallback=q.put) @@ -32,3 +45,13 @@ class TestLive: if re.match("smtp-login.*ok.", data2.lower()): smtp_ok = True assert ac1.get_config("mail_pw") + + def test_send_message(self, acfactory): + ac1 = acfactory.get_live_account() + ac2 = acfactory.get_live_account() + c2 = ac2.get_self_contact() + chat = ac1.create_chat_by_contact(c2) + import time + time.sleep(5) + print ("sending test message") + chat.send_text_message("msg1") diff --git a/python/tests/test_lowlevel.py b/python/tests/test_lowlevel.py index 5e505e40..dcdf0b70 100644 --- a/python/tests/test_lowlevel.py +++ b/python/tests/test_lowlevel.py @@ -9,3 +9,4 @@ def test_empty_context(): def test_event_defines(): assert capi.lib.DC_EVENT_INFO == 100 + assert capi.lib.DC_CONTACT_ID_SELF diff --git a/python/tox.ini b/python/tox.ini index 681bb579..896a8716 100644 --- a/python/tox.ini +++ b/python/tox.ini @@ -3,7 +3,7 @@ minversion = 2.0 distshare = {homedir}/.tox/distshare # make sure to update environment list in travis.yml and appveyor.yml envlist = - linting + lint py27 py35 @@ -14,7 +14,7 @@ deps = pytest pdbpp -[testenv:linting] +[testenv:lint] skipsdist = True usedevelop = True basepython = python2.7