mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-06-17 10:36:37 +00:00
Avoid cyclic imports on older Python versions
This commit is contained in:
parent
3c2ff45b0b
commit
d64eb7ea2b
@ -5,15 +5,12 @@ since they seem to count as two characters and it's a bit strange.
|
|||||||
"""
|
"""
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
from ..helpers import add_surrogate, del_surrogate
|
||||||
from ..tl import TLObject
|
from ..tl import TLObject
|
||||||
from ..tl.types import (
|
from ..tl.types import (
|
||||||
MessageEntityBold, MessageEntityItalic, MessageEntityCode,
|
MessageEntityBold, MessageEntityItalic, MessageEntityCode,
|
||||||
MessageEntityPre, MessageEntityTextUrl
|
MessageEntityPre, MessageEntityTextUrl
|
||||||
)
|
)
|
||||||
from ..utils import (
|
|
||||||
add_surrogate as _add_surrogate,
|
|
||||||
del_surrogate as _del_surrogate
|
|
||||||
)
|
|
||||||
|
|
||||||
DEFAULT_DELIMITERS = {
|
DEFAULT_DELIMITERS = {
|
||||||
'**': MessageEntityBold,
|
'**': MessageEntityBold,
|
||||||
@ -57,7 +54,7 @@ def parse(message, delimiters=None, url_re=None):
|
|||||||
|
|
||||||
# Work on byte level with the utf-16le encoding to get the offsets right.
|
# Work on byte level with the utf-16le encoding to get the offsets right.
|
||||||
# The offset will just be half the index we're at.
|
# The offset will just be half the index we're at.
|
||||||
message = _add_surrogate(message)
|
message = add_surrogate(message)
|
||||||
while i < len(message):
|
while i < len(message):
|
||||||
if url_re and current is None:
|
if url_re and current is None:
|
||||||
# If we're not inside a previous match since Telegram doesn't allow
|
# If we're not inside a previous match since Telegram doesn't allow
|
||||||
@ -73,7 +70,7 @@ def parse(message, delimiters=None, url_re=None):
|
|||||||
|
|
||||||
result.append(MessageEntityTextUrl(
|
result.append(MessageEntityTextUrl(
|
||||||
offset=url_match.start(), length=len(url_match.group(1)),
|
offset=url_match.start(), length=len(url_match.group(1)),
|
||||||
url=_del_surrogate(url_match.group(2))
|
url=del_surrogate(url_match.group(2))
|
||||||
))
|
))
|
||||||
i += len(url_match.group(1))
|
i += len(url_match.group(1))
|
||||||
# Next loop iteration, don't check delimiters, since
|
# Next loop iteration, don't check delimiters, since
|
||||||
@ -128,7 +125,7 @@ def parse(message, delimiters=None, url_re=None):
|
|||||||
+ message[current.offset:]
|
+ message[current.offset:]
|
||||||
)
|
)
|
||||||
|
|
||||||
return _del_surrogate(message), result
|
return del_surrogate(message), result
|
||||||
|
|
||||||
|
|
||||||
def unparse(text, entities, delimiters=None, url_fmt=None):
|
def unparse(text, entities, delimiters=None, url_fmt=None):
|
||||||
@ -156,7 +153,7 @@ def unparse(text, entities, delimiters=None, url_fmt=None):
|
|||||||
else:
|
else:
|
||||||
entities = tuple(sorted(entities, key=lambda e: e.offset, reverse=True))
|
entities = tuple(sorted(entities, key=lambda e: e.offset, reverse=True))
|
||||||
|
|
||||||
text = _add_surrogate(text)
|
text = add_surrogate(text)
|
||||||
delimiters = {v: k for k, v in delimiters.items()}
|
delimiters = {v: k for k, v in delimiters.items()}
|
||||||
for entity in entities:
|
for entity in entities:
|
||||||
s = entity.offset
|
s = entity.offset
|
||||||
@ -167,8 +164,8 @@ def unparse(text, entities, delimiters=None, url_fmt=None):
|
|||||||
elif isinstance(entity, MessageEntityTextUrl) and url_fmt:
|
elif isinstance(entity, MessageEntityTextUrl) and url_fmt:
|
||||||
text = (
|
text = (
|
||||||
text[:s] +
|
text[:s] +
|
||||||
_add_surrogate(url_fmt.format(text[s:e], entity.url)) +
|
add_surrogate(url_fmt.format(text[s:e], entity.url)) +
|
||||||
text[e:]
|
text[e:]
|
||||||
)
|
)
|
||||||
|
|
||||||
return _del_surrogate(text)
|
return del_surrogate(text)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
"""Various helpers not related to the Telegram API itself"""
|
"""Various helpers not related to the Telegram API itself"""
|
||||||
import os
|
import os
|
||||||
|
import struct
|
||||||
from hashlib import sha1, sha256
|
from hashlib import sha1, sha256
|
||||||
|
|
||||||
|
|
||||||
@ -17,6 +18,20 @@ def ensure_parent_dir_exists(file_path):
|
|||||||
if parent:
|
if parent:
|
||||||
os.makedirs(parent, exist_ok=True)
|
os.makedirs(parent, exist_ok=True)
|
||||||
|
|
||||||
|
|
||||||
|
def add_surrogate(text):
|
||||||
|
return ''.join(
|
||||||
|
# SMP -> Surrogate Pairs (Telegram offsets are calculated with these).
|
||||||
|
# See https://en.wikipedia.org/wiki/Plane_(Unicode)#Overview for more.
|
||||||
|
''.join(chr(y) for y in struct.unpack('<HH', x.encode('utf-16le')))
|
||||||
|
if (0x10000 <= ord(x) <= 0x10FFFF) else x for x in text
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def del_surrogate(text):
|
||||||
|
return text.encode('utf-16', 'surrogatepass').decode('utf-16')
|
||||||
|
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
# region Cryptographic related utils
|
# region Cryptographic related utils
|
||||||
|
@ -7,13 +7,12 @@ import math
|
|||||||
import mimetypes
|
import mimetypes
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import struct
|
|
||||||
import types
|
import types
|
||||||
from collections import UserList
|
from collections import UserList
|
||||||
from mimetypes import guess_extension
|
from mimetypes import guess_extension
|
||||||
|
|
||||||
from .extensions import markdown, html
|
from .extensions import markdown, html
|
||||||
from .tl import TLObject
|
from .helpers import add_surrogate, del_surrogate
|
||||||
from .tl.types import (
|
from .tl.types import (
|
||||||
Channel, ChannelForbidden, Chat, ChatEmpty, ChatForbidden, ChatFull,
|
Channel, ChannelForbidden, Chat, ChatEmpty, ChatForbidden, ChatFull,
|
||||||
ChatPhoto, InputPeerChannel, InputPeerChat, InputPeerUser, InputPeerEmpty,
|
ChatPhoto, InputPeerChannel, InputPeerChat, InputPeerUser, InputPeerEmpty,
|
||||||
@ -586,19 +585,6 @@ def _fix_peer_id(peer_id):
|
|||||||
return int(peer_id)
|
return int(peer_id)
|
||||||
|
|
||||||
|
|
||||||
def add_surrogate(text):
|
|
||||||
return ''.join(
|
|
||||||
# SMP -> Surrogate Pairs (Telegram offsets are calculated with these).
|
|
||||||
# See https://en.wikipedia.org/wiki/Plane_(Unicode)#Overview for more.
|
|
||||||
''.join(chr(y) for y in struct.unpack('<HH', x.encode('utf-16le')))
|
|
||||||
if (0x10000 <= ord(x) <= 0x10FFFF) else x for x in text
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def del_surrogate(text):
|
|
||||||
return text.encode('utf-16', 'surrogatepass').decode('utf-16')
|
|
||||||
|
|
||||||
|
|
||||||
def get_inner_text(text, entities):
|
def get_inner_text(text, entities):
|
||||||
"""
|
"""
|
||||||
Gets the inner text that's surrounded by the given entities.
|
Gets the inner text that's surrounded by the given entities.
|
||||||
|
Loading…
Reference in New Issue
Block a user