refactor(python): flatten the API of deltachat module

This commit is contained in:
link2xt
2023-07-02 01:23:25 +00:00
parent 9c68fac4b6
commit 88759c815b
8 changed files with 54 additions and 55 deletions

View File

@@ -2,34 +2,34 @@
high level API reference high level API reference
======================== ========================
- :class:`deltachat.account.Account` (your main entry point, creates the - :class:`deltachat.Account` (your main entry point, creates the
other classes) other classes)
- :class:`deltachat.contact.Contact` - :class:`deltachat.Contact`
- :class:`deltachat.chat.Chat` - :class:`deltachat.Chat`
- :class:`deltachat.message.Message` - :class:`deltachat.Message`
Account Account
------- -------
.. autoclass:: deltachat.account.Account .. autoclass:: deltachat.Account
:members: :members:
Contact Contact
------- -------
.. autoclass:: deltachat.contact.Contact .. autoclass:: deltachat.Contact
:members: :members:
Chat Chat
---- ----
.. autoclass:: deltachat.chat.Chat .. autoclass:: deltachat.Chat
:members: :members:
Message Message
------- -------
.. autoclass:: deltachat.message.Message .. autoclass:: deltachat.Message
:members: :members:

View File

@@ -6,7 +6,7 @@ from array import array
from contextlib import contextmanager from contextlib import contextmanager
from email.utils import parseaddr from email.utils import parseaddr
from threading import Event from threading import Event
from typing import Any, Dict, Generator, List, Optional, Union, TYPE_CHECKING from typing import TYPE_CHECKING, Any, Dict, Generator, List, Optional, Union
from . import const, hookspec from . import const, hookspec
from .capi import ffi, lib from .capi import ffi, lib

View File

@@ -9,11 +9,11 @@ from contextlib import contextmanager
from queue import Empty, Queue from queue import Empty, Queue
from . import const from . import const
from .account import Account
from .capi import ffi, lib from .capi import ffi, lib
from .cutil import from_optional_dc_charpointer from .cutil import from_optional_dc_charpointer
from .hookspec import account_hookimpl from .hookspec import account_hookimpl
from .message import map_system_message from .message import map_system_message
from .account import Account
def get_dc_event_name(integer, _DC_EVENTNAME_MAP={}): def get_dc_event_name(integer, _DC_EVENTNAME_MAP={}):

View File

@@ -9,7 +9,7 @@ import threading
import time import time
import weakref import weakref
from queue import Queue from queue import Queue
from typing import Callable, List, Optional, Dict, Set from typing import Callable, Dict, List, Optional, Set
import pytest import pytest
import requests import requests

View File

@@ -1,6 +1,6 @@
from queue import Queue from queue import Queue
from threading import Event from threading import Event
from typing import List, TYPE_CHECKING from typing import TYPE_CHECKING, List
from .hookspec import Global, account_hookimpl from .hookspec import Global, account_hookimpl

View File

@@ -7,9 +7,8 @@ from datetime import datetime, timezone
import pytest import pytest
from imap_tools import AND, U from imap_tools import AND, U
from deltachat import const import deltachat as dc
from deltachat.hookspec import account_hookimpl from deltachat import account_hookimpl, Message
from deltachat.message import Message
from deltachat.tracker import ImexTracker from deltachat.tracker import ImexTracker
@@ -36,8 +35,8 @@ def test_basic_imap_api(acfactory, tmp_path):
def test_configure_generate_key(acfactory, lp): def test_configure_generate_key(acfactory, lp):
# A slow test which will generate new keys. # A slow test which will generate new keys.
acfactory.remove_preconfigured_keys() acfactory.remove_preconfigured_keys()
ac1 = acfactory.new_online_configuring_account(key_gen_type=str(const.DC_KEY_GEN_RSA2048)) ac1 = acfactory.new_online_configuring_account(key_gen_type=str(dc.const.DC_KEY_GEN_RSA2048))
ac2 = acfactory.new_online_configuring_account(key_gen_type=str(const.DC_KEY_GEN_ED25519)) ac2 = acfactory.new_online_configuring_account(key_gen_type=str(dc.const.DC_KEY_GEN_ED25519))
acfactory.bring_accounts_online() acfactory.bring_accounts_online()
chat = acfactory.get_accepted_chat(ac1, ac2) chat = acfactory.get_accepted_chat(ac1, ac2)
@@ -175,7 +174,7 @@ def test_send_file_twice_unicode_filename_mangling(tmp_path, acfactory, lp):
lp.sec("ac2: receive message") lp.sec("ac2: receive message")
ev = ac2._evtracker.get_matching("DC_EVENT_INCOMING_MSG") ev = ac2._evtracker.get_matching("DC_EVENT_INCOMING_MSG")
assert ev.data2 > const.DC_CHAT_ID_LAST_SPECIAL assert ev.data2 > dc.const.DC_CHAT_ID_LAST_SPECIAL
return ac2.get_message_by_id(ev.data2) return ac2.get_message_by_id(ev.data2)
msg = send_and_receive_message() msg = send_and_receive_message()
@@ -207,7 +206,7 @@ def test_send_file_html_attachment(tmp_path, acfactory, lp):
lp.sec("ac2: receive message") lp.sec("ac2: receive message")
ev = ac2._evtracker.get_matching("DC_EVENT_INCOMING_MSG") ev = ac2._evtracker.get_matching("DC_EVENT_INCOMING_MSG")
assert ev.data2 > const.DC_CHAT_ID_LAST_SPECIAL assert ev.data2 > dc.const.DC_CHAT_ID_LAST_SPECIAL
msg = ac2.get_message_by_id(ev.data2) msg = ac2.get_message_by_id(ev.data2)
assert open(msg.filename).read() == content assert open(msg.filename).read() == content
@@ -351,7 +350,7 @@ def test_move_works(acfactory):
# Message is downloaded # Message is downloaded
ev = ac2._evtracker.get_matching("DC_EVENT_INCOMING_MSG") ev = ac2._evtracker.get_matching("DC_EVENT_INCOMING_MSG")
assert ev.data2 > const.DC_CHAT_ID_LAST_SPECIAL assert ev.data2 > dc.const.DC_CHAT_ID_LAST_SPECIAL
def test_move_works_on_self_sent(acfactory): def test_move_works_on_self_sent(acfactory):
@@ -534,8 +533,8 @@ def test_send_and_receive_message_markseen(acfactory, lp):
lp.step("1") lp.step("1")
for _i in range(2): for _i in range(2):
ev = ac1._evtracker.get_matching("DC_EVENT_MSG_READ") ev = ac1._evtracker.get_matching("DC_EVENT_MSG_READ")
assert ev.data1 > const.DC_CHAT_ID_LAST_SPECIAL assert ev.data1 > dc.const.DC_CHAT_ID_LAST_SPECIAL
assert ev.data2 > const.DC_MSG_ID_LAST_SPECIAL assert ev.data2 > dc.const.DC_MSG_ID_LAST_SPECIAL
lp.step("2") lp.step("2")
# Check that ac1 marks the read receipt as read. # Check that ac1 marks the read receipt as read.
@@ -1386,7 +1385,7 @@ def test_reaction_to_partially_fetched_msg(acfactory, lp, tmp_path):
lp.sec("wait for ac2 to receive a reaction") lp.sec("wait for ac2 to receive a reaction")
msg2 = ac2._evtracker.wait_next_reactions_changed() msg2 = ac2._evtracker.wait_next_reactions_changed()
assert msg2.get_sender_contact().addr == ac1_addr assert msg2.get_sender_contact().addr == ac1_addr
assert msg2.download_state == const.DC_DOWNLOAD_AVAILABLE assert msg2.download_state == dc.const.DC_DOWNLOAD_AVAILABLE
assert reactions_queue.get() == msg2 assert reactions_queue.get() == msg2
reactions = msg2.get_reactions() reactions = msg2.get_reactions()
contacts = reactions.get_contacts() contacts = reactions.get_contacts()
@@ -1471,7 +1470,7 @@ def test_import_export_online_all(acfactory, tmp_path, data, lp):
lp.sec(f"export all to {backupdir}") lp.sec(f"export all to {backupdir}")
with ac1.temp_plugin(ImexTracker()) as imex_tracker: with ac1.temp_plugin(ImexTracker()) as imex_tracker:
ac1.stop_io() ac1.stop_io()
ac1.imex(str(backupdir), const.DC_IMEX_EXPORT_BACKUP) ac1.imex(str(backupdir), dc.const.DC_IMEX_EXPORT_BACKUP)
# check progress events for export # check progress events for export
assert imex_tracker.wait_progress(1, progress_upper_limit=249) assert imex_tracker.wait_progress(1, progress_upper_limit=249)
@@ -1835,15 +1834,15 @@ def test_connectivity(acfactory, lp):
ac1, ac2 = acfactory.get_online_accounts(2) ac1, ac2 = acfactory.get_online_accounts(2)
ac1.set_config("scan_all_folders_debounce_secs", "0") ac1.set_config("scan_all_folders_debounce_secs", "0")
ac1._evtracker.wait_for_connectivity(const.DC_CONNECTIVITY_CONNECTED) ac1._evtracker.wait_for_connectivity(dc.const.DC_CONNECTIVITY_CONNECTED)
lp.sec("Test stop_io() and start_io()") lp.sec("Test stop_io() and start_io()")
ac1.stop_io() ac1.stop_io()
ac1._evtracker.wait_for_connectivity(const.DC_CONNECTIVITY_NOT_CONNECTED) ac1._evtracker.wait_for_connectivity(dc.const.DC_CONNECTIVITY_NOT_CONNECTED)
ac1.start_io() ac1.start_io()
ac1._evtracker.wait_for_connectivity(const.DC_CONNECTIVITY_CONNECTING) ac1._evtracker.wait_for_connectivity(dc.const.DC_CONNECTIVITY_CONNECTING)
ac1._evtracker.wait_for_connectivity_change(const.DC_CONNECTIVITY_CONNECTING, const.DC_CONNECTIVITY_CONNECTED) ac1._evtracker.wait_for_connectivity_change(dc.const.DC_CONNECTIVITY_CONNECTING, dc.const.DC_CONNECTIVITY_CONNECTED)
lp.sec( lp.sec(
"Test that after calling start_io(), maybe_network() and waiting for `all_work_done()`, " "Test that after calling start_io(), maybe_network() and waiting for `all_work_done()`, "
@@ -1864,8 +1863,8 @@ def test_connectivity(acfactory, lp):
ac2.create_chat(ac1).send_text("Hi 2") ac2.create_chat(ac1).send_text("Hi 2")
ac1._evtracker.wait_for_connectivity_change(const.DC_CONNECTIVITY_CONNECTED, const.DC_CONNECTIVITY_WORKING) ac1._evtracker.wait_for_connectivity_change(dc.const.DC_CONNECTIVITY_CONNECTED, dc.const.DC_CONNECTIVITY_WORKING)
ac1._evtracker.wait_for_connectivity_change(const.DC_CONNECTIVITY_WORKING, const.DC_CONNECTIVITY_CONNECTED) ac1._evtracker.wait_for_connectivity_change(dc.const.DC_CONNECTIVITY_WORKING, dc.const.DC_CONNECTIVITY_CONNECTED)
msgs = ac1.create_chat(ac2).get_messages() msgs = ac1.create_chat(ac2).get_messages()
assert len(msgs) == 2 assert len(msgs) == 2
@@ -1875,7 +1874,7 @@ def test_connectivity(acfactory, lp):
ac1.maybe_network() ac1.maybe_network()
while 1: while 1:
assert ac1.get_connectivity() == const.DC_CONNECTIVITY_CONNECTED assert ac1.get_connectivity() == dc.const.DC_CONNECTIVITY_CONNECTED
if ac1.all_work_done(): if ac1.all_work_done():
break break
ac1._evtracker.get_matching("DC_EVENT_CONNECTIVITY_CHANGED") ac1._evtracker.get_matching("DC_EVENT_CONNECTIVITY_CHANGED")
@@ -1890,7 +1889,7 @@ def test_connectivity(acfactory, lp):
ac1.maybe_network() ac1.maybe_network()
while 1: while 1:
assert ac1.get_connectivity() == const.DC_CONNECTIVITY_CONNECTED assert ac1.get_connectivity() == dc.const.DC_CONNECTIVITY_CONNECTED
if ac1.all_work_done(): if ac1.all_work_done():
break break
ac1._evtracker.get_matching("DC_EVENT_CONNECTIVITY_CHANGED") ac1._evtracker.get_matching("DC_EVENT_CONNECTIVITY_CHANGED")
@@ -1899,10 +1898,10 @@ def test_connectivity(acfactory, lp):
ac1.set_config("configured_mail_pw", "abc") ac1.set_config("configured_mail_pw", "abc")
ac1.stop_io() ac1.stop_io()
ac1._evtracker.wait_for_connectivity(const.DC_CONNECTIVITY_NOT_CONNECTED) ac1._evtracker.wait_for_connectivity(dc.const.DC_CONNECTIVITY_NOT_CONNECTED)
ac1.start_io() ac1.start_io()
ac1._evtracker.wait_for_connectivity(const.DC_CONNECTIVITY_CONNECTING) ac1._evtracker.wait_for_connectivity(dc.const.DC_CONNECTIVITY_CONNECTING)
ac1._evtracker.wait_for_connectivity(const.DC_CONNECTIVITY_NOT_CONNECTED) ac1._evtracker.wait_for_connectivity(dc.const.DC_CONNECTIVITY_NOT_CONNECTED)
def test_fetch_deleted_msg(acfactory, lp): def test_fetch_deleted_msg(acfactory, lp):
@@ -2385,9 +2384,9 @@ def test_archived_muted_chat(acfactory, lp):
lp.sec("wait for ac2 to receive DC_EVENT_MSGS_CHANGED for DC_CHAT_ID_ARCHIVED_LINK") lp.sec("wait for ac2 to receive DC_EVENT_MSGS_CHANGED for DC_CHAT_ID_ARCHIVED_LINK")
while 1: while 1:
ev = ac2._evtracker.get_matching("DC_EVENT_MSGS_CHANGED") ev = ac2._evtracker.get_matching("DC_EVENT_MSGS_CHANGED")
if ev.data1 == const.DC_CHAT_ID_ARCHIVED_LINK: if ev.data1 == dc.const.DC_CHAT_ID_ARCHIVED_LINK:
assert ev.data2 == 0 assert ev.data2 == 0
archive = ac2.get_chat_by_id(const.DC_CHAT_ID_ARCHIVED_LINK) archive = ac2.get_chat_by_id(dc.const.DC_CHAT_ID_ARCHIVED_LINK)
assert archive.count_fresh_messages() == 1 assert archive.count_fresh_messages() == 1
assert chat2.count_fresh_messages() == 1 assert chat2.count_fresh_messages() == 1
break break

View File

@@ -4,12 +4,11 @@ from datetime import datetime, timedelta, timezone
import pytest import pytest
from deltachat import Account, const import deltachat as dc
from deltachat.capi import ffi, lib from deltachat.capi import ffi, lib
from deltachat.cutil import iter_array from deltachat.cutil import iter_array
from deltachat.hookspec import account_hookimpl
from deltachat.message import Message
from deltachat.tracker import ImexFailed from deltachat.tracker import ImexFailed
from deltachat import Account, account_hookimpl, Message
@pytest.mark.parametrize( @pytest.mark.parametrize(
@@ -299,13 +298,13 @@ class TestOfflineChat:
assert not d["draft"] if chat.get_draft() is None else chat.get_draft() assert not d["draft"] if chat.get_draft() is None else chat.get_draft()
def test_group_chat_creation_with_translation(self, ac1): def test_group_chat_creation_with_translation(self, ac1):
ac1.set_stock_translation(const.DC_STR_GROUP_NAME_CHANGED_BY_YOU, "abc %1$s xyz %2$s") ac1.set_stock_translation(dc.const.DC_STR_GROUP_NAME_CHANGED_BY_YOU, "abc %1$s xyz %2$s")
ac1._evtracker.consume_events() ac1._evtracker.consume_events()
with pytest.raises(ValueError): with pytest.raises(ValueError):
ac1.set_stock_translation(const.DC_STR_FILE, "xyz %1$s") ac1.set_stock_translation(dc.const.DC_STR_FILE, "xyz %1$s")
ac1._evtracker.get_matching("DC_EVENT_WARNING") ac1._evtracker.get_matching("DC_EVENT_WARNING")
with pytest.raises(ValueError): with pytest.raises(ValueError):
ac1.set_stock_translation(const.DC_STR_CONTACT_NOT_VERIFIED, "xyz %2$s") ac1.set_stock_translation(dc.const.DC_STR_CONTACT_NOT_VERIFIED, "xyz %2$s")
ac1._evtracker.get_matching("DC_EVENT_WARNING") ac1._evtracker.get_matching("DC_EVENT_WARNING")
with pytest.raises(ValueError): with pytest.raises(ValueError):
ac1.set_stock_translation(500, "xyz %1$s") ac1.set_stock_translation(500, "xyz %1$s")
@@ -818,7 +817,7 @@ class TestOfflineChat:
lp.sec("check message count of only system messages (without daymarkers)") lp.sec("check message count of only system messages (without daymarkers)")
dc_array = ffi.gc( dc_array = ffi.gc(
lib.dc_get_chat_msgs(ac1._dc_context, chat.id, const.DC_GCM_INFO_ONLY, 0), lib.dc_get_chat_msgs(ac1._dc_context, chat.id, dc.const.DC_GCM_INFO_ONLY, 0),
lib.dc_array_unref, lib.dc_array_unref,
) )
assert len(list(iter_array(dc_array, lambda x: x))) == 2 assert len(list(iter_array(dc_array, lambda x: x))) == 2

View File

@@ -1,6 +1,7 @@
from queue import Queue from queue import Queue
from deltachat import capi, const, cutil, register_global_plugin import deltachat as dc
from deltachat import capi, cutil, register_global_plugin
from deltachat.capi import ffi, lib from deltachat.capi import ffi, lib
from deltachat.hookspec import global_hookimpl from deltachat.hookspec import global_hookimpl
from deltachat.testplugin import ( from deltachat.testplugin import (
@@ -132,20 +133,20 @@ def test_empty_blobdir(tmp_path):
def test_event_defines(): def test_event_defines():
assert const.DC_EVENT_INFO == 100 assert dc.const.DC_EVENT_INFO == 100
assert const.DC_CONTACT_ID_SELF assert dc.const.DC_CONTACT_ID_SELF
def test_sig(): def test_sig():
sig = capi.lib.dc_event_has_string_data sig = capi.lib.dc_event_has_string_data
assert not sig(const.DC_EVENT_MSGS_CHANGED) assert not sig(dc.const.DC_EVENT_MSGS_CHANGED)
assert sig(const.DC_EVENT_INFO) assert sig(dc.const.DC_EVENT_INFO)
assert sig(const.DC_EVENT_WARNING) assert sig(dc.const.DC_EVENT_WARNING)
assert sig(const.DC_EVENT_ERROR) assert sig(dc.const.DC_EVENT_ERROR)
assert sig(const.DC_EVENT_SMTP_CONNECTED) assert sig(dc.const.DC_EVENT_SMTP_CONNECTED)
assert sig(const.DC_EVENT_IMAP_CONNECTED) assert sig(dc.const.DC_EVENT_IMAP_CONNECTED)
assert sig(const.DC_EVENT_SMTP_MESSAGE_SENT) assert sig(dc.const.DC_EVENT_SMTP_MESSAGE_SENT)
assert sig(const.DC_EVENT_IMEX_FILE_WRITTEN) assert sig(dc.const.DC_EVENT_IMEX_FILE_WRITTEN)
def test_markseen_invalid_message_ids(acfactory): def test_markseen_invalid_message_ids(acfactory):