Style enhancements

This commit is contained in:
Lonami Exo 2017-10-09 09:54:05 +02:00
parent bc500d6272
commit cedb4302b4
3 changed files with 27 additions and 22 deletions

View File

@ -2,8 +2,6 @@ import os
from datetime import datetime, timedelta from datetime import datetime, timedelta
from mimetypes import guess_type from mimetypes import guess_type
from telethon.tl.custom import Draft
try: try:
import socks import socks
except ImportError: except ImportError:
@ -17,6 +15,7 @@ from .errors import (
) )
from .network import ConnectionMode from .network import ConnectionMode
from .tl import TLObject from .tl import TLObject
from .tl.custom import Draft
from .tl.entity_database import EntityDatabase from .tl.entity_database import EntityDatabase
from .tl.functions.account import ( from .tl.functions.account import (
GetPasswordRequest GetPasswordRequest

View File

@ -1 +1 @@
from telethon.tl.custom.draft import Draft from .draft import Draft

View File

@ -1,15 +1,14 @@
from telethon.tl.functions.messages import SaveDraftRequest from ..functions.messages import SaveDraftRequest
from telethon.tl.types import UpdateDraftMessage from ..types import UpdateDraftMessage
class Draft: class Draft:
"""
Custom class that encapsulates a draft on the Telegram servers, providing
an abstraction to change the message conveniently. The library will return
instances of this class when calling `client.get_drafts()`.
"""
def __init__(self, client, peer, draft): def __init__(self, client, peer, draft):
"""
A custom class that encapsulates a draft on the Telegram servers, providing an
abstraction to change the message conveniently. The library will return instances of this
class when executing `client.get_drafts`.
"""
self._client = client self._client = client
self._peer = peer self._peer = peer
@ -22,8 +21,11 @@ class Draft:
@classmethod @classmethod
def _from_update(cls, client, update): def _from_update(cls, client, update):
if not isinstance(update, UpdateDraftMessage): if not isinstance(update, UpdateDraftMessage):
raise ValueError("You can only create a new `Draft` from a corresponding " raise ValueError(
"`UpdateDraftMessage` object.") 'You can only create a new `Draft` from a corresponding '
'`UpdateDraftMessage` object.'
)
return cls(client=client, peer=update.peer, draft=update.draft) return cls(client=client, peer=update.peer, draft=update.draft)
@property @property
@ -36,14 +38,17 @@ class Draft:
def set_message(self, text, no_webpage=None, reply_to_msg_id=None, entities=None): def set_message(self, text, no_webpage=None, reply_to_msg_id=None, entities=None):
""" """
Changes the draft message on the Telegram servers. The changes are reflected in this Changes the draft message on the Telegram servers. The changes are
object. Changing only individual attributes like for example the `reply_to_msg_id` should be reflected in this object. Changing only individual attributes like for
done by providing the current values of this object, like so:: example the `reply_to_msg_id` should be done by providing the current
values of this object, like so:
draft.set_message(draft.text, draft.set_message(
no_webpage=draft.no_webpage, draft.text,
reply_to_msg_id=NEW_VALUE, no_webpage=draft.no_webpage,
entities=draft.entities) reply_to_msg_id=NEW_VALUE,
entities=draft.entities
)
:param str text: New text of the draft :param str text: New text of the draft
:param bool no_webpage: Whether to attach a web page preview :param bool no_webpage: Whether to attach a web page preview
@ -56,9 +61,10 @@ class Draft:
message=text, message=text,
no_webpage=no_webpage, no_webpage=no_webpage,
reply_to_msg_id=reply_to_msg_id, reply_to_msg_id=reply_to_msg_id,
entities=entities)) entities=entities
))
if result is True: if result:
self.text = text self.text = text
self.no_webpage = no_webpage self.no_webpage = no_webpage
self.reply_to_msg_id = reply_to_msg_id self.reply_to_msg_id = reply_to_msg_id