refine example and make Contact accept Account object

This commit is contained in:
holger krekel
2020-03-03 19:15:50 +01:00
parent f61b9f7964
commit d8e14d9993
6 changed files with 35 additions and 18 deletions

View File

@@ -13,8 +13,10 @@ class SimpleEchoPlugin:
if message.text.strip() == "/quit": if message.text.strip() == "/quit":
message.account.shutdown() message.account.shutdown()
else: else:
ch = message.account.create_chat_by_contact(message.get_sender_contact()) ch = message.get_sender_chat()
ch.send_text("echoing from {}:\n{}".format(message.get_sender_contact().addr, message.text)) addr = message.get_sender_contact().addr
text = message.text
ch.send_text("echoing from {}:\n{}".format(addr, text))
@deltachat.hookspec.account_hookimpl @deltachat.hookspec.account_hookimpl
def process_message_delivered(self, message): def process_message_delivered(self, message):
@@ -23,7 +25,7 @@ class SimpleEchoPlugin:
def main(argv): def main(argv):
p = optparse.OptionParser("simple-echo") p = optparse.OptionParser("simple-echo")
p.add_option("-l", action="store_true", help="show low-level events") p.add_option("-l", action="store_true", help="show ffi")
p.add_option("--db", type="str", help="database file") p.add_option("--db", type="str", help="database file")
p.add_option("--email", type="str", help="email address") p.add_option("--email", type="str", help="email address")
p.add_option("--password", type="str", help="password") p.add_option("--password", type="str", help="password")
@@ -34,10 +36,13 @@ def main(argv):
ac = deltachat.Account(opts.db) ac = deltachat.Account(opts.db)
if opts.l: if opts.l:
ac.add_account_plugin(deltachat.eventlogger.FFIEventLogger(ac, "echo")) log = deltachat.eventlogger.FFIEventLogger(ac, "echo")
ac.add_account_plugin(log)
if not ac.is_configured(): if not ac.is_configured():
assert opts.email and opts.password, "you must specify --email and --password" assert opts.email and opts.password, (
"you must specify --email and --password"
)
ac.set_config("addr", opts.email) ac.set_config("addr", opts.email)
ac.set_config("mail_pw", opts.password) ac.set_config("mail_pw", opts.password)
ac.set_config("mvbox_watch", "0") ac.set_config("mvbox_watch", "0")
@@ -45,10 +50,10 @@ def main(argv):
ac.add_account_plugin(SimpleEchoPlugin()) ac.add_account_plugin(SimpleEchoPlugin())
# start IO threads and perform configure if neccessary # start IO threads and configure if neccessary
ac.start(callback_thread=True) ac.start()
print("waiting for /quit or message to echo on: {}".format(ac.get_config("addr"))) print("{}: waiting for message".format(ac.get_config("addr")))
ac.wait_shutdown() ac.wait_shutdown()

View File

@@ -214,7 +214,7 @@ class Account(object):
:returns: :class:`deltachat.contact.Contact` :returns: :class:`deltachat.contact.Contact`
""" """
self.check_is_configured() self.check_is_configured()
return Contact(self._dc_context, const.DC_CONTACT_ID_SELF) return Contact(self, const.DC_CONTACT_ID_SELF)
def create_contact(self, email, name=None): def create_contact(self, email, name=None):
""" create a (new) Contact. If there already is a Contact """ create a (new) Contact. If there already is a Contact
@@ -229,7 +229,7 @@ class Account(object):
email = as_dc_charpointer(email) email = as_dc_charpointer(email)
contact_id = lib.dc_create_contact(self._dc_context, name, email) contact_id = lib.dc_create_contact(self._dc_context, name, email)
assert contact_id > const.DC_CHAT_ID_LAST_SPECIAL assert contact_id > const.DC_CHAT_ID_LAST_SPECIAL
return Contact(self._dc_context, contact_id) return Contact(self, contact_id)
def delete_contact(self, contact): def delete_contact(self, contact):
""" delete a Contact. """ delete a Contact.
@@ -261,7 +261,7 @@ class Account(object):
lib.dc_get_contacts(self._dc_context, flags, query), lib.dc_get_contacts(self._dc_context, flags, query),
lib.dc_array_unref lib.dc_array_unref
) )
return list(iter_array(dc_array, lambda x: Contact(self._dc_context, x))) return list(iter_array(dc_array, lambda x: Contact(self, x)))
def create_chat_by_contact(self, contact): def create_chat_by_contact(self, contact):
""" create or get an existing 1:1 chat object for the specified contact or contact id. """ create or get an existing 1:1 chat object for the specified contact or contact id.
@@ -340,7 +340,7 @@ class Account(object):
:param contact_id: integer id of this contact. :param contact_id: integer id of this contact.
:returns: None or :class:`deltachat.contact.Contact` instance. :returns: None or :class:`deltachat.contact.Contact` instance.
""" """
return Contact(self._dc_context, contact_id) return Contact(self, contact_id)
def get_chat_by_id(self, chat_id): def get_chat_by_id(self, chat_id):
""" return Chat instance. """ return Chat instance.
@@ -542,7 +542,7 @@ class Account(object):
If this account is not configured, an internal configuration If this account is not configured, an internal configuration
job will be scheduled if config values are sufficiently specified. job will be scheduled if config values are sufficiently specified.
You may call :method:`wait_shutdown` or `shutdown` after the You may call `wait_shutdown` or `shutdown` after the
account is in started mode. account is in started mode.
:raises MissingCredentials: if `addr` and `mail_pw` values are not set. :raises MissingCredentials: if `addr` and `mail_pw` values are not set.

View File

@@ -363,7 +363,7 @@ class Chat(object):
lib.dc_array_unref lib.dc_array_unref
) )
return list(iter_array( return list(iter_array(
dc_array, lambda id: Contact(self._dc_context, id)) dc_array, lambda id: Contact(self.account, id))
) )
def set_profile_image(self, img_path): def set_profile_image(self, img_path):

View File

@@ -10,8 +10,9 @@ class Contact(object):
You obtain instances of it through :class:`deltachat.account.Account`. You obtain instances of it through :class:`deltachat.account.Account`.
""" """
def __init__(self, dc_context, id): def __init__(self, account, id):
self._dc_context = dc_context self.account = account
self._dc_context = account._dc_context
self.id = id self.id = id
def __eq__(self, other): def __eq__(self, other):
@@ -57,3 +58,7 @@ class Contact(object):
if dc_res == ffi.NULL: if dc_res == ffi.NULL:
return None return None
return from_dc_charpointer(dc_res) return from_dc_charpointer(dc_res)
def get_chat(self):
"""return 1:1 chat for this contact. """
return self.account.create_chat_by_contact(self)

View File

@@ -159,6 +159,13 @@ class Message(object):
chat_id = lib.dc_msg_get_chat_id(self._dc_msg) chat_id = lib.dc_msg_get_chat_id(self._dc_msg)
return Chat(self.account, chat_id) return Chat(self.account, chat_id)
def get_sender_chat(self):
"""return the 1:1 chat with the sender of this message.
:returns: :class:`deltachat.chat.Chat` instance
"""
return self.get_sender_contact().get_chat()
def get_sender_contact(self): def get_sender_contact(self):
"""return the contact of who wrote the message. """return the contact of who wrote the message.
@@ -166,7 +173,7 @@ class Message(object):
""" """
from .contact import Contact from .contact import Contact
contact_id = lib.dc_msg_get_from_id(self._dc_msg) contact_id = lib.dc_msg_get_from_id(self._dc_msg)
return Contact(self._dc_context, contact_id) return Contact(self.account, contact_id)
# #
# Message State query methods # Message State query methods

View File

@@ -8,7 +8,7 @@ envlist =
[testenv] [testenv]
commands = commands =
pytest -n6 --reruns 2 --reruns-delay 5 -v -rsXx --ignored {posargs:tests} pytest -n6 --reruns 2 --reruns-delay 5 -v -rsXx --ignored {posargs:tests}
pytest examples pytest examples/test_examples.py
python tests/package_wheels.py {toxworkdir}/wheelhouse python tests/package_wheels.py {toxworkdir}/wheelhouse
passenv = passenv =
TRAVIS TRAVIS