mirror of
https://github.com/deltachat/deltachat-core.git
synced 2025-10-04 02:09:17 +02:00
better names for the converseion to/from dc char pointers
This commit is contained in:
parent
1f0812c907
commit
929d6da2ef
4 changed files with 18 additions and 12 deletions
|
@ -14,7 +14,7 @@ from attr import validators as v
|
|||
|
||||
import deltachat
|
||||
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 .chatting import Contact, Chat, Message
|
||||
|
||||
|
@ -62,10 +62,11 @@ class Account(object):
|
|||
|
||||
:param name: configuration key to lookup (eg "addr" or "mail_pw")
|
||||
:returns: unicode value
|
||||
:raises: KeyError if no config value was found.
|
||||
"""
|
||||
name = name.encode("utf8")
|
||||
res = lib.dc_get_config(self._dc_context.p, name, b'')
|
||||
return ffi_unicode(res)
|
||||
return from_dc_charpointer(res)
|
||||
|
||||
def is_configured(self):
|
||||
""" determine if the account is configured already.
|
||||
|
@ -96,8 +97,8 @@ class Account(object):
|
|||
:param name: display name for this contact (optional)
|
||||
:returns: :class:`Contact` instance.
|
||||
"""
|
||||
name = convert_to_bytes_utf8(name)
|
||||
email = convert_to_bytes_utf8(email)
|
||||
name = as_dc_charpointer(name)
|
||||
email = as_dc_charpointer(email)
|
||||
contact_id = lib.dc_create_contact(self._dc_context.p, name, email)
|
||||
return Contact(self._dc_context, contact_id)
|
||||
|
||||
|
@ -111,7 +112,7 @@ class Account(object):
|
|||
:returns: list of :class:`Message` objects.
|
||||
"""
|
||||
flags = 0
|
||||
query = convert_to_bytes_utf8(query)
|
||||
query = as_dc_charpointer(query)
|
||||
if only_verified:
|
||||
flags |= lib.DC_GCL_VERIFIED_ONLY
|
||||
if with_self:
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
""" 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 .types import cached_property, property_with_doc
|
||||
from .types import DC_Context, DC_Contact, DC_Chat, DC_Msg
|
||||
|
@ -24,12 +24,12 @@ class Contact(object):
|
|||
@property_with_doc
|
||||
def addr(self):
|
||||
""" 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
|
||||
def display_name(self):
|
||||
""" 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):
|
||||
""" Return True if the contact is blocked. """
|
||||
|
@ -63,7 +63,7 @@ class Chat(object):
|
|||
:param msg: unicode text
|
||||
: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)
|
||||
return Message(self._dc_context, msg_id)
|
||||
|
||||
|
@ -120,7 +120,7 @@ class Message(object):
|
|||
@property_with_doc
|
||||
def text(self):
|
||||
"""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
|
||||
def chat(self):
|
||||
|
|
|
@ -2,7 +2,7 @@ from .capi import lib
|
|||
from .capi import ffi
|
||||
|
||||
|
||||
def convert_to_bytes_utf8(obj):
|
||||
def as_dc_charpointer(obj):
|
||||
if obj == ffi.NULL or obj is None:
|
||||
return ffi.NULL
|
||||
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)
|
||||
|
||||
|
||||
def ffi_unicode(obj):
|
||||
def from_dc_charpointer(obj):
|
||||
return ffi.string(obj).decode("utf8")
|
||||
|
|
|
@ -15,6 +15,11 @@ class TestOfflineAccount:
|
|||
with pytest.raises(ValueError):
|
||||
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):
|
||||
ac1 = acfactory.get_offline_account()
|
||||
contact1 = ac1.create_contact(email="some1@hello.com", name="some1")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue