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

better names for the converseion to/from dc char pointers

This commit is contained in:
holger krekel 2018-09-15 01:17:45 +02:00
parent 1f0812c907
commit 929d6da2ef
4 changed files with 18 additions and 12 deletions

View file

@ -14,7 +14,7 @@ from attr import validators as v
import deltachat import deltachat
from .capi import ffi, lib from .capi import ffi, lib
from .cutil import convert_to_bytes_utf8, ffi_unicode, iter_array_and_unref from .cutil import as_dc_charpointer, from_dc_charpointer, iter_array_and_unref
from .types import DC_Context from .types import DC_Context
from .chatting import Contact, Chat, Message from .chatting import Contact, Chat, Message
@ -62,10 +62,11 @@ class Account(object):
:param name: configuration key to lookup (eg "addr" or "mail_pw") :param name: configuration key to lookup (eg "addr" or "mail_pw")
:returns: unicode value :returns: unicode value
:raises: KeyError if no config value was found.
""" """
name = name.encode("utf8") name = name.encode("utf8")
res = lib.dc_get_config(self._dc_context.p, name, b'') res = lib.dc_get_config(self._dc_context.p, name, b'')
return ffi_unicode(res) return from_dc_charpointer(res)
def is_configured(self): def is_configured(self):
""" determine if the account is configured already. """ determine if the account is configured already.
@ -96,8 +97,8 @@ class Account(object):
:param name: display name for this contact (optional) :param name: display name for this contact (optional)
:returns: :class:`Contact` instance. :returns: :class:`Contact` instance.
""" """
name = convert_to_bytes_utf8(name) name = as_dc_charpointer(name)
email = convert_to_bytes_utf8(email) email = as_dc_charpointer(email)
contact_id = lib.dc_create_contact(self._dc_context.p, name, email) contact_id = lib.dc_create_contact(self._dc_context.p, name, email)
return Contact(self._dc_context, contact_id) return Contact(self._dc_context, contact_id)
@ -111,7 +112,7 @@ class Account(object):
:returns: list of :class:`Message` objects. :returns: list of :class:`Message` objects.
""" """
flags = 0 flags = 0
query = convert_to_bytes_utf8(query) query = as_dc_charpointer(query)
if only_verified: if only_verified:
flags |= lib.DC_GCL_VERIFIED_ONLY flags |= lib.DC_GCL_VERIFIED_ONLY
if with_self: if with_self:

View file

@ -1,6 +1,6 @@
""" chatting related objects: Contact, Chat, Message. """ """ chatting related objects: Contact, Chat, Message. """
from .cutil import convert_to_bytes_utf8, ffi_unicode, iter_array_and_unref from .cutil import as_dc_charpointer, from_dc_charpointer, iter_array_and_unref
from .capi import lib from .capi import lib
from .types import cached_property, property_with_doc from .types import cached_property, property_with_doc
from .types import DC_Context, DC_Contact, DC_Chat, DC_Msg from .types import DC_Context, DC_Contact, DC_Chat, DC_Msg
@ -24,12 +24,12 @@ class Contact(object):
@property_with_doc @property_with_doc
def addr(self): def addr(self):
""" normalized e-mail address for this account. """ """ normalized e-mail address for this account. """
return ffi_unicode(lib.dc_contact_get_addr(self._dc_contact.p)) return from_dc_charpointer(lib.dc_contact_get_addr(self._dc_contact.p))
@property_with_doc @property_with_doc
def display_name(self): def display_name(self):
""" display name for this contact. """ """ display name for this contact. """
return ffi_unicode(lib.dc_contact_get_display_name(self._dc_contact.p)) return from_dc_charpointer(lib.dc_contact_get_display_name(self._dc_contact.p))
def is_blocked(self): def is_blocked(self):
""" Return True if the contact is blocked. """ """ Return True if the contact is blocked. """
@ -63,7 +63,7 @@ class Chat(object):
:param msg: unicode text :param msg: unicode text
:returns: the resulting :class:`Message` instance :returns: the resulting :class:`Message` instance
""" """
msg = convert_to_bytes_utf8(msg) msg = as_dc_charpointer(msg)
msg_id = lib.dc_send_text_msg(self._dc_context.p, self.id, msg) msg_id = lib.dc_send_text_msg(self._dc_context.p, self.id, msg)
return Message(self._dc_context, msg_id) return Message(self._dc_context, msg_id)
@ -120,7 +120,7 @@ class Message(object):
@property_with_doc @property_with_doc
def text(self): def text(self):
"""unicode representation. """ """unicode representation. """
return ffi_unicode(lib.dc_msg_get_text(self._dc_msg.p)) return from_dc_charpointer(lib.dc_msg_get_text(self._dc_msg.p))
@property @property
def chat(self): def chat(self):

View file

@ -2,7 +2,7 @@ from .capi import lib
from .capi import ffi from .capi import ffi
def convert_to_bytes_utf8(obj): def as_dc_charpointer(obj):
if obj == ffi.NULL or obj is None: if obj == ffi.NULL or obj is None:
return ffi.NULL return ffi.NULL
if not isinstance(obj, bytes): if not isinstance(obj, bytes):
@ -18,5 +18,5 @@ def iter_array_and_unref(dc_array_t, constructor):
lib.dc_array_unref(dc_array_t) lib.dc_array_unref(dc_array_t)
def ffi_unicode(obj): def from_dc_charpointer(obj):
return ffi.string(obj).decode("utf8") return ffi.string(obj).decode("utf8")

View file

@ -15,6 +15,11 @@ class TestOfflineAccount:
with pytest.raises(ValueError): with pytest.raises(ValueError):
ac1.get_self_contact() ac1.get_self_contact()
# def test_get_config_fails(self, acfactory):
# ac1 = acfactory.get_offline_account()
# with pytest.raises(KeyError):
# ac1.get_config("123123")
def test_contact_attr(self, acfactory): def test_contact_attr(self, acfactory):
ac1 = acfactory.get_offline_account() ac1 = acfactory.get_offline_account()
contact1 = ac1.create_contact(email="some1@hello.com", name="some1") contact1 = ac1.create_contact(email="some1@hello.com", name="some1")