From fc1bc05ca137fd3db6de40fa629f70027b102e6c Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sat, 14 Apr 2018 15:16:13 +0200 Subject: [PATCH] Move tlobject and source_builder into their correct folders --- setup.py | 5 +- telethon_generator/parser/__init__.py | 3 -- telethon_generator/parser/tl_parser.py | 51 ------------------- telethon_generator/parsers/__init__.py | 1 + .../tl_object.py => parsers/tlobject.py} | 44 ++++++++++++++++ .../{parser => }/source_builder.py | 0 telethon_generator/tl_generator.py | 7 +-- 7 files changed, 52 insertions(+), 59 deletions(-) delete mode 100644 telethon_generator/parser/__init__.py delete mode 100644 telethon_generator/parser/tl_parser.py rename telethon_generator/{parser/tl_object.py => parsers/tlobject.py} (89%) rename telethon_generator/{parser => }/source_builder.py (100%) diff --git a/setup.py b/setup.py index 7b0a5db5..4da847ef 100755 --- a/setup.py +++ b/setup.py @@ -47,7 +47,8 @@ IMPORT_DEPTH = 2 def gen_tl(force=True): from telethon_generator.tl_generator import TLGenerator - from telethon_generator.error_generator import generate_code + # TODO Generate errors again + #from telethon_generator.error_generator import generate_code generator = TLGenerator(GENERATOR_DIR) if generator.tlobjects_exist(): if not force: @@ -58,7 +59,7 @@ def gen_tl(force=True): print('Generating TLObjects...') generator.generate_tlobjects(SCHEME_TL, import_depth=IMPORT_DEPTH) print('Generating errors...') - generate_code(ERROR_LIST, json_file=ERRORS_JSON, errors_desc=ERRORS_DESC) + #generate_code(ERROR_LIST, json_file=ERRORS_JSON, errors_desc=ERRORS_DESC) print('Done.') diff --git a/telethon_generator/parser/__init__.py b/telethon_generator/parser/__init__.py deleted file mode 100644 index 6f1a2a9d..00000000 --- a/telethon_generator/parser/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -from .source_builder import SourceBuilder -from .tl_parser import TLParser -from .tl_object import TLObject diff --git a/telethon_generator/parser/tl_parser.py b/telethon_generator/parser/tl_parser.py deleted file mode 100644 index 8c24cbf4..00000000 --- a/telethon_generator/parser/tl_parser.py +++ /dev/null @@ -1,51 +0,0 @@ -import re - -from .tl_object import TLObject - - -class TLParser: - """Class used to parse .tl files""" - - @staticmethod - def parse_file(file_path, ignore_core=False): - """This method yields TLObjects from a given .tl file""" - - with open(file_path, encoding='utf-8') as file: - # Start by assuming that the next found line won't - # be a function (and will hence be a type) - is_function = False - - # Read all the lines from the .tl file - for line in file: - # Strip comments from the line - comment_index = line.find('//') - if comment_index != -1: - line = line[:comment_index] - - line = line.strip() - if line: - # Check whether the line is a type change - # (types <-> functions) or not - match = re.match('---(\w+)---', line) - if match: - following_types = match.group(1) - is_function = following_types == 'functions' - - else: - try: - result = TLObject.from_tl(line, is_function) - if not ignore_core or not result.is_core_type(): - yield result - except ValueError as e: - if 'vector#1cb5c415' not in str(e): - raise - - @staticmethod - def find_layer(file_path): - """Finds the layer used on the specified scheme.tl file""" - layer_regex = re.compile(r'^//\s*LAYER\s*(\d+)$') - with open(file_path, encoding='utf-8') as file: - for line in file: - match = layer_regex.match(line) - if match: - return int(match.group(1)) diff --git a/telethon_generator/parsers/__init__.py b/telethon_generator/parsers/__init__.py index d8550c73..9034450e 100644 --- a/telethon_generator/parsers/__init__.py +++ b/telethon_generator/parsers/__init__.py @@ -1 +1,2 @@ from .errors import Error, parse_errors +from .tlobject import TLObject, parse_tl, find_layer diff --git a/telethon_generator/parser/tl_object.py b/telethon_generator/parsers/tlobject.py similarity index 89% rename from telethon_generator/parser/tl_object.py rename to telethon_generator/parsers/tlobject.py index 0e0045d7..5692b1a6 100644 --- a/telethon_generator/parser/tl_object.py +++ b/telethon_generator/parsers/tlobject.py @@ -321,3 +321,47 @@ class TLArg: return str(self)\ .replace(':date', ':int')\ .replace('?date', '?int') + + +def parse_tl(file_path, ignore_core=False): + """This method yields TLObjects from a given .tl file""" + + with open(file_path, encoding='utf-8') as file: + # Start by assuming that the next found line won't + # be a function (and will hence be a type) + is_function = False + + # Read all the lines from the .tl file + for line in file: + # Strip comments from the line + comment_index = line.find('//') + if comment_index != -1: + line = line[:comment_index] + + line = line.strip() + if line: + # Check whether the line is a type change + # (types <-> functions) or not + match = re.match('---(\w+)---', line) + if match: + following_types = match.group(1) + is_function = following_types == 'functions' + + else: + try: + result = TLObject.from_tl(line, is_function) + if not ignore_core or not result.is_core_type(): + yield result + except ValueError as e: + if 'vector#1cb5c415' not in str(e): + raise + + +def find_layer(file_path): + """Finds the layer used on the specified scheme.tl file""" + layer_regex = re.compile(r'^//\s*LAYER\s*(\d+)$') + with open(file_path, encoding='utf-8') as file: + for line in file: + match = layer_regex.match(line) + if match: + return int(match.group(1)) diff --git a/telethon_generator/parser/source_builder.py b/telethon_generator/source_builder.py similarity index 100% rename from telethon_generator/parser/source_builder.py rename to telethon_generator/source_builder.py diff --git a/telethon_generator/tl_generator.py b/telethon_generator/tl_generator.py index 85ee98a0..cc62c91d 100644 --- a/telethon_generator/tl_generator.py +++ b/telethon_generator/tl_generator.py @@ -5,7 +5,8 @@ import struct from zlib import crc32 from collections import defaultdict -from .parser import SourceBuilder, TLParser, TLObject +from .source_builder import SourceBuilder +from .parsers import TLObject, parse_tl, find_layer AUTO_GEN_NOTICE = \ '"""File generated by TLObjects\' generator. All changes will be ERASED"""' @@ -59,7 +60,7 @@ class TLGenerator: os.makedirs(self._get_file('types'), exist_ok=True) # Step 0: Cache the parsed file on a tuple - tlobjects = tuple(TLParser.parse_file(scheme_file, ignore_core=True)) + tlobjects = tuple(parse_tl(scheme_file, ignore_core=True)) # Step 1: Group everything by {namespace: [tlobjects]} so we can # easily generate __init__.py files with all the TLObjects on them. @@ -98,7 +99,7 @@ class TLGenerator: builder.writeln() # Create a constant variable to indicate which layer this is - builder.writeln('LAYER = {}', TLParser.find_layer(scheme_file)) + builder.writeln('LAYER = {}', find_layer(scheme_file)) builder.writeln() # Then create the dictionary containing constructor_id: class