# -*- coding: utf-8 -*- """ Created on Tue Sep 2 19:26:15 2014 @author: agrigoryev """ from binascii import crc32 import struct import socket import re from datetime import datetime import sys import io import configparser current_module = sys.modules[__name__] def vis(bs): l = len(bs) n = len(bs) // 8 for i in range(n): print(" ".join(["%02X" % b for b in bs[i*8:i*8+8]])) print(" ".join(["%02X" % b for b in bs[i*8+8:]])+"\n") class TlElement: def __init__(self, type_string): tl_re = re.compile("""([a-z]\w*) #name \#?([0-9a-f]{8})?\s+ #id (\{.*\}\s+)? #subtype (.*)\s+ #arguments list =\s+([A-Z]\w*) #result (\s+(\w+))?; #subresult""", re.X) assert isinstance(type_string, str) x = tl_re.match(type_string) if x is not None: self.name = x.groups()[0] if x.groups()[1] is not None: self.id = int(x.groups()[1], 16) else: self.id = crc32(re.sub("(#[0-9a-f]{8})|([;\n{}])", "", type_string).encode()) self.args = self.get_arg_list(x.groups()[3]) self.result = x.groups()[4] else: raise SyntaxError @staticmethod def get_arg_list(arg_string): arg_re = re.compile("([\w0-9]+)?:?([\w0-9]+)(<([\w0-0]+)>)?") res = [] for s in arg_re.findall(arg_string): d = {'name': s[0], 'type': s[1], 'subtype': s[3] if s[2] is not None else None} res.append(d) return res class TL: def __init__(self): self.func_dict_id = {} self.func_dict_name = {} self.obj_dict_id = {} self.obj_dict_name = {} # Read constructors f = open("TL_schema", 'r') for line in f: if line.startswith("---functions---"): break try: z = TlElement(line) self.obj_dict_id[z.id] = z self.obj_dict_name[z.result] = z except SyntaxError: pass # Read methods for line in f: if line.startswith("---functions---"): break try: z = TlElement(line) self.func_dict_id[z.id] = z self.func_dict_name[z.name] = z except SyntaxError: pass def tl_serialize(self, elem, kwargs): assert isinstance(elem, TlElement) for arg in elem.args: pass return struct.pack(">') vis(message) #self.number += 1 data = self.header(message) + message step1 = struct.pack(' 0: # if we have smth. in the socket packet_length = struct.unpack("