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

@@ -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