From be9358282a06b456ef0112a64c2d11e4bd89abcc Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Fri, 20 Oct 2017 17:29:45 +0200 Subject: [PATCH] Generate and fetch new errors from setup.py --- .gitignore | 2 +- setup.py | 18 +++++++++++++++--- telethon_generator/error_generator.py | 8 +++----- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index dd5de4c3..156d23e3 100755 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ telethon/tl/functions/ telethon/tl/types/ telethon/tl/all_tlobjects.py -telethon/tl/errors/rpc_error_list.py +telethon/errors/rpc_error_list.py # User session *.session diff --git a/setup.py b/setup.py index 695ad1a5..3ac7a90b 100755 --- a/setup.py +++ b/setup.py @@ -42,17 +42,25 @@ class TempWorkDir: os.chdir(self.original) +ERROR_LIST = 'telethon/errors/rpc_error_list.py' +ERRORS_JSON = 'telethon_generator/errors.json' +ERRORS_DESC = 'telethon_generator/error_descriptions' +SCHEME_TL = 'telethon_generator/scheme.tl' +IMPORT_DEPTH = 2 + + def gen_tl(): from telethon_generator.tl_generator import TLGenerator + from telethon_generator.error_generator import generate_code generator = TLGenerator('telethon/tl') if generator.tlobjects_exist(): print('Detected previous TLObjects. Cleaning...') generator.clean_tlobjects() print('Generating TLObjects...') - generator.generate_tlobjects( - 'telethon_generator/scheme.tl', import_depth=2 - ) + generator.generate_tlobjects(SCHEME_TL, import_depth=IMPORT_DEPTH) + print('Generating errors...') + generate_code(ERROR_LIST, json_file=ERRORS_JSON, errors_desc=ERRORS_DESC) print('Done.') @@ -80,6 +88,10 @@ def main(): for x in ('build', 'dist', 'Telethon.egg-info'): rmtree(x, ignore_errors=True) + if len(argv) >= 2 and argv[1] == 'fetch_errors': + from telethon_generator.error_generator import fetch_errors + fetch_errors(ERRORS_JSON) + else: if not TelegramClient: gen_tl() diff --git a/telethon_generator/error_generator.py b/telethon_generator/error_generator.py index fb5d0ef7..81bcda5c 100644 --- a/telethon_generator/error_generator.py +++ b/telethon_generator/error_generator.py @@ -4,8 +4,6 @@ import urllib.request from collections import defaultdict URL = 'https://rpc.pwrtelegram.xyz/?all' -OUTPUT = '../telethon/errors/rpc_error_list.py' -JSON_OUTPUT = 'errors.json' known_base_classes = { 303: 'InvalidDCError', @@ -26,7 +24,7 @@ known_codes = { } -def fetch_errors(url=URL, output=JSON_OUTPUT): +def fetch_errors(output, url=URL): print('Opening a connection to', url, '...') r = urllib.request.urlopen(url) print('Checking response...') @@ -78,7 +76,7 @@ def write_error(f, code, name, desc, capture_name): f.write(')\n') -def generate_code(json_file=JSON_OUTPUT, output=OUTPUT): +def generate_code(output, json_file, errors_desc): with open(json_file, encoding='utf-8') as f: data = json.load(f) @@ -113,7 +111,7 @@ def generate_code(json_file=JSON_OUTPUT, output=OUTPUT): # Prefer the descriptions that are related with Telethon way of coding to # those that PWRTelegram's API provides. telethon_descriptions = {} - with open('error_descriptions', encoding='utf-8') as f: + with open(errors_desc, encoding='utf-8') as f: for line in f: line = line.strip() if line and not line.startswith('#'):