"""Various helpers not related to the Telegram API itself""" import asyncio import io import enum import os import struct import inspect import logging import functools from pathlib import Path from hashlib import sha1 class _EntityType(enum.Enum): USER = 0 CHAT = 1 CHANNEL = 2 _log = logging.getLogger(__name__) # region Multiple utilities def generate_random_long(signed=True): """Generates a random long integer (8 bytes), which is optionally signed""" return int.from_bytes(os.urandom(8), signed=signed, byteorder='little') def ensure_parent_dir_exists(file_path): """Ensures that the parent directory exists""" parent = os.path.dirname(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('