Avoid cyclic imports on older Python versions

This commit is contained in:
Lonami Exo 2018-06-29 11:04:42 +02:00
parent 3c2ff45b0b
commit d64eb7ea2b
3 changed files with 23 additions and 25 deletions

View File

@ -5,15 +5,12 @@ since they seem to count as two characters and it's a bit strange.
"""
import re
from ..helpers import add_surrogate, del_surrogate
from ..tl import TLObject
from ..tl.types import (
MessageEntityBold, MessageEntityItalic, MessageEntityCode,
MessageEntityPre, MessageEntityTextUrl
)
from ..utils import (
add_surrogate as _add_surrogate,
del_surrogate as _del_surrogate
)
DEFAULT_DELIMITERS = {
'**': 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.
# The offset will just be half the index we're at.
message = _add_surrogate(message)
message = add_surrogate(message)
while i < len(message):
if url_re and current is None:
# 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(
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))
# Next loop iteration, don't check delimiters, since
@ -128,7 +125,7 @@ def parse(message, delimiters=None, url_re=None):
+ message[current.offset:]
)
return _del_surrogate(message), result
return del_surrogate(message), result
def unparse(text, entities, delimiters=None, url_fmt=None):
@ -156,7 +153,7 @@ def unparse(text, entities, delimiters=None, url_fmt=None):
else:
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()}
for entity in entities:
s = entity.offset
@ -167,8 +164,8 @@ def unparse(text, entities, delimiters=None, url_fmt=None):
elif isinstance(entity, MessageEntityTextUrl) and url_fmt:
text = (
text[:s] +
_add_surrogate(url_fmt.format(text[s:e], entity.url)) +
add_surrogate(url_fmt.format(text[s:e], entity.url)) +
text[e:]
)
return _del_surrogate(text)
return del_surrogate(text)

View File

@ -1,5 +1,6 @@
"""Various helpers not related to the Telegram API itself"""
import os
import struct
from hashlib import sha1, sha256
@ -17,6 +18,20 @@ def ensure_parent_dir_exists(file_path):
if parent:
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
# region Cryptographic related utils

View File

@ -7,13 +7,12 @@ import math
import mimetypes
import os
import re
import struct
import types
from collections import UserList
from mimetypes import guess_extension
from .extensions import markdown, html
from .tl import TLObject
from .helpers import add_surrogate, del_surrogate
from .tl.types import (
Channel, ChannelForbidden, Chat, ChatEmpty, ChatForbidden, ChatFull,
ChatPhoto, InputPeerChannel, InputPeerChat, InputPeerUser, InputPeerEmpty,
@ -586,19 +585,6 @@ def _fix_peer_id(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):
"""
Gets the inner text that's surrounded by the given entities.