1
0
Fork 0
mirror of https://github.com/deltachat/deltachat-core.git synced 2025-10-04 18:29:19 +02:00

- make set_config() more like the c-version but make configure() accept **kwargs

- some safety checks: you can't modify addr after successful configure
This commit is contained in:
holger krekel 2018-09-23 22:51:12 +02:00
parent bea807f012
commit da78552622
3 changed files with 41 additions and 15 deletions

View file

@ -50,7 +50,7 @@ def acfactory(pytestconfig, tmpdir, request):
tmpdb = tmpdir.join("livedb%d" % self.live_count) tmpdb = tmpdir.join("livedb%d" % self.live_count)
ac = Account(tmpdb.strpath, logid="ac{}".format(self.live_count)) ac = Account(tmpdb.strpath, logid="ac{}".format(self.live_count))
ac._evlogger.set_timeout(30) ac._evlogger.set_timeout(30)
ac.set_config(**configdict) ac.configure(**configdict)
if started: if started:
ac.start() ac.start()
request.addfinalizer(ac.shutdown) request.addfinalizer(ac.shutdown)

View file

@ -23,7 +23,6 @@ class Account(object):
by the underlying deltachat c-library. All public Account methods are by the underlying deltachat c-library. All public Account methods are
meant to be memory-safe and return memory-safe objects. meant to be memory-safe and return memory-safe objects.
""" """
def __init__(self, db_path, logid=None): def __init__(self, db_path, logid=None):
""" initialize account object. """ initialize account object.
@ -32,10 +31,9 @@ class Account(object):
:param logid: an optional logging prefix that should be used with :param logid: an optional logging prefix that should be used with
the default internal logging. the default internal logging.
""" """
self._dc_context = ffi.gc( self._dc_context = ffi.gc(
lib.dc_context_new(lib.py_dc_callback, ffi.NULL, ffi.NULL), lib.dc_context_new(lib.py_dc_callback, ffi.NULL, ffi.NULL),
lib.dc_context_unref, _destroy_dc_context,
) )
if hasattr(db_path, "encode"): if hasattr(db_path, "encode"):
db_path = db_path.encode("utf8") db_path = db_path.encode("utf8")
@ -43,18 +41,20 @@ class Account(object):
raise ValueError("Could not dc_open: {}".format(db_path)) raise ValueError("Could not dc_open: {}".format(db_path))
self._evhandler = EventHandler(self._dc_context) self._evhandler = EventHandler(self._dc_context)
self._evlogger = EventLogger(self._dc_context, logid) self._evlogger = EventLogger(self._dc_context, logid)
deltachat.set_context_callback(self._dc_context, self._process_event)
self._threads = IOThreads(self._dc_context) self._threads = IOThreads(self._dc_context)
def set_config(self, **kwargs): def set_config(self, name, value):
""" set configuration values. """ set configuration values.
:param kwargs: name=value settings for this account. :param name: config key name (unicode)
values need to be unicode. :param value: value to set (unicode)
:returns: None :returns: None
""" """
for name, value in kwargs.items():
name = name.encode("utf8") name = name.encode("utf8")
value = value.encode("utf8") value = value.encode("utf8")
if name == b"addr" and self.is_configured():
raise ValueError("can not change 'addr' after account is configured.")
lib.dc_set_config(self._dc_context, name, value) lib.dc_set_config(self._dc_context, name, value)
def get_config(self, name): def get_config(self, name):
@ -68,8 +68,20 @@ class Account(object):
res = lib.dc_get_config(self._dc_context, name, b'') res = lib.dc_get_config(self._dc_context, name, b'')
return from_dc_charpointer(res) return from_dc_charpointer(res)
def configure(self, **kwargs):
""" set config values and configure this account.
:param kwargs: name=value config settings for this account.
values need to be unicode.
:returns: None
"""
for name, value in kwargs.items():
self.set_config(name, value)
lib.dc_configure(self._dc_context)
def is_configured(self): def is_configured(self):
""" determine if the account is configured already. """ determine if the account is configured already; an initial connection
to SMTP/IMAP has been verified.
:returns: True if account is configured. :returns: True if account is configured.
""" """
@ -217,13 +229,12 @@ class Account(object):
def start(self): def start(self):
""" configure this account object, start receiving events, """ configure this account object, start receiving events,
start IMAP/SMTP threads. """ start IMAP/SMTP threads. """
deltachat.set_context_callback(self._dc_context, self._process_event) if not self.is_configured():
lib.dc_configure(self._dc_context) self.configure()
self._threads.start() self._threads.start()
def shutdown(self): def shutdown(self):
""" shutdown IMAP/SMTP threads and stop receiving events""" """ shutdown IMAP/SMTP threads and stop receiving events"""
deltachat.clear_context_callback(self._dc_context)
self._threads.stop(wait=True) self._threads.stop(wait=True)
def _process_event(self, ctx, evt_name, data1, data2): def _process_event(self, ctx, evt_name, data1, data2):
@ -347,3 +358,14 @@ class EventLogger:
tname = getattr(t, "name", t) tname = getattr(t, "name", t)
print("[{}-{}] {}({!r},{!r})".format( print("[{}-{}] {}({!r},{!r})".format(
tname, self.logid, evt_name, data1, data2)) tname, self.logid, evt_name, data1, data2))
def _destroy_dc_context(dc_context, dc_context_unref=lib.dc_context_unref):
# destructor for dc_context
dc_context_unref(dc_context)
try:
deltachat.clear_context_callback(dc_context)
except (TypeError, AttributeError):
# we are deep into Python Interpreter shutdown,
# so no need to clear the callback context mapping.
pass

View file

@ -125,6 +125,10 @@ class TestOnlineAccount:
self.wait_configuration_progress(ac1, 1000) self.wait_configuration_progress(ac1, 1000)
assert ac1.get_config("mail_pw") assert ac1.get_config("mail_pw")
assert ac1.is_configured() assert ac1.is_configured()
with pytest.raises(ValueError):
ac1.set_config("addr", "123@example.org")
with pytest.raises(ValueError):
ac1.configure(addr="123@example.org")
def test_forward_messages(self, acfactory): def test_forward_messages(self, acfactory):
ac1 = acfactory.get_live_account() ac1 = acfactory.get_live_account()