Skip to content

Commit

Permalink
Add script to sync errors with Telegram's JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Lonami committed Jan 11, 2023
1 parent 6332690 commit 7a74ded
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions telethon_generator/parsers/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(self, codes, name, description):
# Should these be split into different files or doesn't really matter?
# Telegram isn't exactly consistent with returned errors anyway.
self.int_code = codes[0]
self.int_codes = codes
self.str_code = name
self.subclass = _get_class_name(codes[0])
self.subclass_exists = abs(codes[0]) in KNOWN_BASE_CLASSES
Expand Down
9 changes: 9 additions & 0 deletions telethon_generator/parsers/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ class Usability(enum.Enum):
BOT = 2
BOTH = 4

@property
def key(self):
return {
Usability.UNKNOWN: 'unknown',
Usability.USER: 'user',
Usability.BOT: 'bot',
Usability.BOTH: 'both',
}[self]


class MethodInfo:
def __init__(self, name, usability, errors, friendly):
Expand Down
53 changes: 53 additions & 0 deletions telethon_generator/syncerrors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Should be fed with the JSON obtained from https://core.telegram.org/api/errors#error-database
import re
import csv
import sys
import json
from pathlib import Path

sys.path.insert(0, '..')

from telethon_generator.parsers.errors import parse_errors, Error
from telethon_generator.parsers.methods import parse_methods, MethodInfo

ERRORS = Path('data/errors.csv')
METHODS = Path('data/methods.csv')
FRIENDLY = Path('data/friendly.csv')


def main():
new_errors = []
new_methods = []

self_errors = {e.str_code: e for e in parse_errors(ERRORS)}
self_methods = {m.name: m for m in parse_methods(METHODS, FRIENDLY, self_errors)}

tg_data = json.load(sys.stdin)

def get_desc(code):
return re.sub(r'\s*&\w+;\s*', '', (tg_data['descriptions'].get(code) or '').rstrip('.'))

for int_code, errors in tg_data['errors'].items():
int_code = int(int_code) # json does not support non-string keys
for code, methods in errors.items():
str_code = code.replace('%d', 'X')
if error := self_errors.get(str_code):
error.int_codes.append(int_code) # de-duplicated once later
if not error.description: # prefer our descriptions
if not error.has_captures: # need descriptions with specific text if error has captures
error.description = get_desc(code)
else:
self_errors[str_code] = Error([int_code], str_code, get_desc(code))

new_errors.extend((e.str_code, ' '.join(map(str, sorted(set(e.int_codes)))), e.description) for e in self_errors.values())
new_methods.extend((m.name, m.usability.key, ' '.join(sorted(e.str_code for e in m.errors))) for m in self_methods.values())

csv.register_dialect('plain', lineterminator='\n')
with ERRORS.open('w', encoding='utf-8', newline='') as fd:
csv.writer(fd, 'plain').writerows((('name', 'codes', 'description'), *sorted(new_errors)))
with METHODS.open('w', encoding='utf-8', newline='') as fd:
csv.writer(fd, 'plain').writerows((('method', 'usability', 'errors'), *sorted(new_methods)))


if __name__ == '__main__':
main()

0 comments on commit 7a74ded

Please sign in to comment.