diff --git a/python/doc/api.rst b/python/doc/api.rst index 4e8e1e9a1..f032e7215 100644 --- a/python/doc/api.rst +++ b/python/doc/api.rst @@ -11,8 +11,6 @@ high level API reference - :class:`deltachat.chatting.Contact` - :class:`deltachat.chatting.Chat` - :class:`deltachat.message.Message` -- :class:`deltachat.message.MessageType` -- :class:`deltachat.message.MessageState` Account ------- diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index 6080239c9..02329c5f7 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -69,10 +69,6 @@ class Account(object): d[key.lower()] = value return d - def get_blob_dir(self): - """ return blob directory for this account. """ - return from_dc_charpointer(lib.dc_get_blobdir(self._dc_context)) - def set_config(self, name, value): """ set configuration values. diff --git a/python/src/deltachat/chatting.py b/python/src/deltachat/chatting.py index 9eb3d5a4a..81b64e2f5 100644 --- a/python/src/deltachat/chatting.py +++ b/python/src/deltachat/chatting.py @@ -191,7 +191,7 @@ class Chat(object): :param path: path to the file. :param mime_type: the mime-type of this file, defaults to auto-detection. - :param view_type: passed to :meth:`MessageType.new`. + :param view_type: "text", "image", "gif", "audio", "video", "file" :raises ValueError: if message can not be prepared/chat does not exist. :returns: the resulting :class:`Message` instance """ diff --git a/python/src/deltachat/message.py b/python/src/deltachat/message.py index 5e91ae66b..8f62dabde 100644 --- a/python/src/deltachat/message.py +++ b/python/src/deltachat/message.py @@ -40,8 +40,11 @@ class Message(object): @classmethod def new_empty(cls, account, view_type): - """ create a non-persistent message. """ - view_type_code = MessageType.get_typecode(view_type) + """ create a non-persistent message. + + :param: view_type is "text", "audio", "video", "file" + """ + view_type_code = get_viewtype_code_from_name(view_type) return Message(account, ffi.gc( lib.dc_msg_new(account._dc_context, view_type_code), lib.dc_msg_unref @@ -68,7 +71,7 @@ class Message(object): mtype = ffi.NULL if mime_type is None else as_dc_charpointer(mime_type) if not os.path.exists(path): raise ValueError("path does not exist: {!r}".format(path)) - blobdir = self.account.get_blob_dir() + blobdir = self.account.get_blobdir() if not path.startswith(blobdir): for i in range(50): ext = "" if i == 0 else "-" + str(i) @@ -93,15 +96,6 @@ class Message(object): """mime type of the file (if it exists)""" return from_dc_charpointer(lib.dc_msg_get_filemime(self._dc_msg)) - @props.with_doc - def view_type(self): - """the view type of this message. - - :returns: a :class:`deltachat.message.MessageType` instance. - """ - assert self.id > 0 - return MessageType(lib.dc_msg_get_viewtype(self._dc_msg)) - def is_setup_message(self): """ return True if this message is a setup message. """ return lib.dc_msg_is_setupmessage(self._dc_msg) @@ -239,56 +233,55 @@ class Message(object): """ return self._msgstate == const.DC_STATE_OUT_MDN_RCVD + # + # Message type query methods + # -class MessageType(object): - """ DeltaChat message type, with is_* methods. """ - _mapping = { - const.DC_MSG_TEXT: 'text', - const.DC_MSG_IMAGE: 'image', - const.DC_MSG_GIF: 'gif', - const.DC_MSG_AUDIO: 'audio', - const.DC_MSG_VIDEO: 'video', - const.DC_MSG_FILE: 'file' - } - - def __init__(self, _type): - self._type = _type - - def __eq__(self, other): - return self._type == getattr(other, "_type", None) - - @classmethod - def get_typecode(cls, view_type): - for code, value in cls._mapping.items(): - if value == view_type: - return code - raise ValueError("message typecode not found for {!r}".format(view_type)) - - @props.with_doc - def name(self): - """ human readable type name. """ - return self._mapping.get(self._type, "") + @property + def _view_type(self): + assert self.id > 0 + return lib.dc_msg_get_viewtype(self._dc_msg) def is_text(self): """ return True if it's a text message. """ - return self._type == const.DC_MSG_TEXT + return self._view_type == const.DC_MSG_TEXT def is_image(self): """ return True if it's an image message. """ - return self._type == const.DC_MSG_IMAGE + return self._view_type == const.DC_MSG_IMAGE def is_gif(self): """ return True if it's a gif message. """ - return self._type == const.DC_MSG_GIF + return self._view_type == const.DC_MSG_GIF def is_audio(self): """ return True if it's an audio message. """ - return self._type == const.DC_MSG_AUDIO + return self._view_type == const.DC_MSG_AUDIO def is_video(self): """ return True if it's a video message. """ - return self._type == const.DC_MSG_VIDEO + return self._view_type == const.DC_MSG_VIDEO def is_file(self): """ return True if it's a file message. """ - return self._type == const.DC_MSG_FILE + return self._view_type == const.DC_MSG_FILE + + +# some code for handling DC_MSG_* view types + +_view_type_mapping = { + const.DC_MSG_TEXT: 'text', + const.DC_MSG_IMAGE: 'image', + const.DC_MSG_GIF: 'gif', + const.DC_MSG_AUDIO: 'audio', + const.DC_MSG_VIDEO: 'video', + const.DC_MSG_FILE: 'file' +} + + +def get_viewtype_code_from_name(view_type_name): + for code, value in _view_type_mapping.items(): + if value == view_type_name: + return code + raise ValueError("message typecode not found for {!r}, " + "available {!r}".format(view_type_name, list(_view_type_mapping.values()))) diff --git a/python/tests/test_account.py b/python/tests/test_account.py index b6d38cdef..6e6c1e2c2 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -158,7 +158,7 @@ class TestOfflineChat: assert msg == ac1.get_message_by_id(msg.id) def test_prepare_file(self, ac1, chat1): - blobdir = ac1.get_blob_dir() + blobdir = ac1.get_blobdir() p = os.path.join(blobdir, "somedata.txt") with open(p, "w") as f: f.write("some data") @@ -180,13 +180,13 @@ class TestOfflineChat: def test_message_send_text(self, chat1): msg = chat1.send_text("msg1") assert msg - assert msg.view_type.is_text() - assert msg.view_type.name == "text" - assert not msg.view_type.is_audio() - assert not msg.view_type.is_video() - assert not msg.view_type.is_gif() - assert not msg.view_type.is_file() - assert not msg.view_type.is_image() + assert msg.is_text() + assert not msg.is_audio() + assert not msg.is_video() + assert not msg.is_gif() + assert not msg.is_file() + assert not msg.is_image() + assert not msg.is_in_fresh() assert not msg.is_in_noticed() assert not msg.is_in_seen() @@ -206,7 +206,7 @@ class TestOfflineChat: fn = data.get_path("d.png") lp.sec("sending image") msg = chat1.send_image(fn) - assert msg.view_type.name == "image" + assert msg.is_image() assert msg assert msg.id > 0 assert os.path.exists(msg.filename) @@ -223,8 +223,7 @@ class TestOfflineChat: msg = chat1.send_file(fn, typein) assert msg assert msg.id > 0 - assert msg.view_type.name == "file" - assert msg.view_type.is_file() + assert msg.is_file() assert os.path.exists(msg.filename) assert msg.filename.endswith(msg.basename) assert msg.filemime == typeout @@ -470,7 +469,7 @@ class TestOnlineAccount: ev = ac2._evlogger.get_matching("DC_EVENT_MSGS_CHANGED") assert ev[2] == msg_out.id msg_in = ac2.get_message_by_id(msg_out.id) - assert msg_in.view_type.is_image() + assert msg_in.is_image() assert os.path.exists(msg_in.filename) assert os.stat(msg_in.filename).st_size == os.stat(path).st_size