Generate and fetch new errors from setup.py

This commit is contained in:
Lonami Exo 2017-10-20 17:29:45 +02:00
parent 38ccd6d1d9
commit be9358282a
3 changed files with 19 additions and 9 deletions

2
.gitignore vendored
View File

@ -2,7 +2,7 @@
telethon/tl/functions/ telethon/tl/functions/
telethon/tl/types/ telethon/tl/types/
telethon/tl/all_tlobjects.py telethon/tl/all_tlobjects.py
telethon/tl/errors/rpc_error_list.py telethon/errors/rpc_error_list.py
# User session # User session
*.session *.session

View File

@ -42,17 +42,25 @@ class TempWorkDir:
os.chdir(self.original) 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(): def gen_tl():
from telethon_generator.tl_generator import TLGenerator from telethon_generator.tl_generator import TLGenerator
from telethon_generator.error_generator import generate_code
generator = TLGenerator('telethon/tl') generator = TLGenerator('telethon/tl')
if generator.tlobjects_exist(): if generator.tlobjects_exist():
print('Detected previous TLObjects. Cleaning...') print('Detected previous TLObjects. Cleaning...')
generator.clean_tlobjects() generator.clean_tlobjects()
print('Generating TLObjects...') print('Generating TLObjects...')
generator.generate_tlobjects( generator.generate_tlobjects(SCHEME_TL, import_depth=IMPORT_DEPTH)
'telethon_generator/scheme.tl', import_depth=2 print('Generating errors...')
) generate_code(ERROR_LIST, json_file=ERRORS_JSON, errors_desc=ERRORS_DESC)
print('Done.') print('Done.')
@ -80,6 +88,10 @@ def main():
for x in ('build', 'dist', 'Telethon.egg-info'): for x in ('build', 'dist', 'Telethon.egg-info'):
rmtree(x, ignore_errors=True) 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: else:
if not TelegramClient: if not TelegramClient:
gen_tl() gen_tl()

View File

@ -4,8 +4,6 @@ import urllib.request
from collections import defaultdict from collections import defaultdict
URL = 'https://rpc.pwrtelegram.xyz/?all' URL = 'https://rpc.pwrtelegram.xyz/?all'
OUTPUT = '../telethon/errors/rpc_error_list.py'
JSON_OUTPUT = 'errors.json'
known_base_classes = { known_base_classes = {
303: 'InvalidDCError', 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, '...') print('Opening a connection to', url, '...')
r = urllib.request.urlopen(url) r = urllib.request.urlopen(url)
print('Checking response...') print('Checking response...')
@ -78,7 +76,7 @@ def write_error(f, code, name, desc, capture_name):
f.write(')\n') 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: with open(json_file, encoding='utf-8') as f:
data = json.load(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 # Prefer the descriptions that are related with Telethon way of coding to
# those that PWRTelegram's API provides. # those that PWRTelegram's API provides.
telethon_descriptions = {} telethon_descriptions = {}
with open('error_descriptions', encoding='utf-8') as f: with open(errors_desc, encoding='utf-8') as f:
for line in f: for line in f:
line = line.strip() line = line.strip()
if line and not line.startswith('#'): if line and not line.startswith('#'):