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/types/
telethon/tl/all_tlobjects.py
telethon/tl/errors/rpc_error_list.py
telethon/errors/rpc_error_list.py
# User session
*.session

View File

@ -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()

View File

@ -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('#'):