2020-03-21 14:43:32 +00:00
|
|
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
2021-01-01 21:58:48 +00:00
|
|
|
# Copyright (C) 2017-2021 Dan <https://github.com/delivrance>
|
2017-12-05 11:21:55 +00:00
|
|
|
#
|
2020-03-21 14:43:32 +00:00
|
|
|
# This file is part of Pyrogram.
|
2017-12-05 11:21:55 +00:00
|
|
|
#
|
2020-03-21 14:43:32 +00:00
|
|
|
# Pyrogram is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2017-12-05 11:21:55 +00:00
|
|
|
#
|
2020-03-21 14:43:32 +00:00
|
|
|
# Pyrogram is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
2017-12-05 11:21:55 +00:00
|
|
|
#
|
2020-03-21 14:43:32 +00:00
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
2017-12-05 11:21:55 +00:00
|
|
|
|
|
|
|
import csv
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import shutil
|
|
|
|
|
2020-11-20 01:04:24 +00:00
|
|
|
HOME = "compiler/errors"
|
2019-03-25 10:10:46 +00:00
|
|
|
DEST = "pyrogram/errors/exceptions"
|
2018-02-28 00:28:31 +00:00
|
|
|
NOTICE_PATH = "NOTICE"
|
2017-12-05 11:21:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def snek(s):
|
|
|
|
# https://stackoverflow.com/questions/1175208/elegant-python-function-to-convert-camelcase-to-snake-case
|
|
|
|
s = re.sub(r"(.)([A-Z][a-z]+)", r"\1_\2", s)
|
|
|
|
return re.sub(r"([a-z0-9])([A-Z])", r"\1_\2", s).lower()
|
|
|
|
|
|
|
|
|
|
|
|
def caml(s):
|
|
|
|
s = snek(s).split("_")
|
|
|
|
return "".join([str(i.title()) for i in s])
|
|
|
|
|
|
|
|
|
|
|
|
def start():
|
2018-02-28 00:28:31 +00:00
|
|
|
shutil.rmtree(DEST, ignore_errors=True)
|
|
|
|
os.makedirs(DEST)
|
2017-12-05 11:21:55 +00:00
|
|
|
|
2018-02-28 00:28:31 +00:00
|
|
|
files = [i for i in os.listdir("{}/source".format(HOME))]
|
2017-12-05 11:21:55 +00:00
|
|
|
|
2018-02-28 00:28:31 +00:00
|
|
|
with open(NOTICE_PATH, encoding="utf-8") as f:
|
2017-12-05 11:21:55 +00:00
|
|
|
notice = []
|
|
|
|
|
|
|
|
for line in f.readlines():
|
|
|
|
notice.append("# {}".format(line).strip())
|
|
|
|
|
|
|
|
notice = "\n".join(notice)
|
|
|
|
|
2018-02-28 00:28:31 +00:00
|
|
|
with open("{}/all.py".format(DEST), "w", encoding="utf-8") as f_all:
|
2017-12-05 11:21:55 +00:00
|
|
|
f_all.write(notice + "\n\n")
|
|
|
|
f_all.write("count = {count}\n\n")
|
|
|
|
f_all.write("exceptions = {\n")
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
for i in files:
|
|
|
|
code, name = re.search(r"(\d+)_([A-Z_]+)", i).groups()
|
|
|
|
|
|
|
|
f_all.write(" {}: {{\n".format(code))
|
|
|
|
|
2018-02-28 00:28:31 +00:00
|
|
|
init = "{}/__init__.py".format(DEST)
|
2017-12-05 11:21:55 +00:00
|
|
|
|
|
|
|
if not os.path.exists(init):
|
2018-01-21 15:56:50 +00:00
|
|
|
with open(init, "w", encoding="utf-8") as f_init:
|
2017-12-05 11:21:55 +00:00
|
|
|
f_init.write(notice + "\n\n")
|
|
|
|
|
2018-01-21 15:56:50 +00:00
|
|
|
with open(init, "a", encoding="utf-8") as f_init:
|
2017-12-05 11:21:55 +00:00
|
|
|
f_init.write("from .{}_{} import *\n".format(name.lower(), code))
|
|
|
|
|
2018-02-28 00:28:31 +00:00
|
|
|
with open("{}/source/{}".format(HOME, i), encoding="utf-8") as f_csv, \
|
2019-03-16 16:51:37 +00:00
|
|
|
open("{}/{}_{}.py".format(DEST, name.lower(), code), "w", encoding="utf-8") as f_class:
|
2017-12-15 11:25:46 +00:00
|
|
|
reader = csv.reader(f_csv, delimiter="\t")
|
2017-12-05 11:21:55 +00:00
|
|
|
|
|
|
|
super_class = caml(name)
|
|
|
|
name = " ".join([str(i.capitalize()) for i in re.sub(r"_", " ", name).lower().split(" ")])
|
|
|
|
|
|
|
|
sub_classes = []
|
|
|
|
|
2019-06-27 09:59:44 +00:00
|
|
|
f_all.write(" \"_\": \"{}\",\n".format(super_class))
|
|
|
|
|
2017-12-05 11:21:55 +00:00
|
|
|
for j, row in enumerate(reader):
|
|
|
|
if j == 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
if not row: # Row is empty (blank line)
|
|
|
|
continue
|
|
|
|
|
2019-06-27 09:59:44 +00:00
|
|
|
error_id, error_message = row
|
2017-12-05 11:21:55 +00:00
|
|
|
|
2019-06-27 09:59:44 +00:00
|
|
|
sub_class = caml(re.sub(r"_X", "_", error_id))
|
2017-12-05 11:21:55 +00:00
|
|
|
|
2019-06-27 09:59:44 +00:00
|
|
|
f_all.write(" \"{}\": \"{}\",\n".format(error_id, sub_class))
|
2017-12-05 11:21:55 +00:00
|
|
|
|
2019-06-27 09:59:44 +00:00
|
|
|
sub_classes.append((sub_class, error_id, error_message))
|
2017-12-05 11:21:55 +00:00
|
|
|
|
2018-02-28 00:28:31 +00:00
|
|
|
with open("{}/template/class.txt".format(HOME), "r", encoding="utf-8") as f_class_template:
|
2017-12-05 11:21:55 +00:00
|
|
|
class_template = f_class_template.read()
|
|
|
|
|
2018-02-28 00:28:31 +00:00
|
|
|
with open("{}/template/sub_class.txt".format(HOME), "r", encoding="utf-8") as f_sub_class_template:
|
2017-12-05 11:21:55 +00:00
|
|
|
sub_class_template = f_sub_class_template.read()
|
|
|
|
|
|
|
|
class_template = class_template.format(
|
|
|
|
notice=notice,
|
|
|
|
super_class=super_class,
|
|
|
|
code=code,
|
2017-12-30 18:25:17 +00:00
|
|
|
docstring='"""{}"""'.format(name),
|
2017-12-05 11:21:55 +00:00
|
|
|
sub_classes="".join([sub_class_template.format(
|
|
|
|
sub_class=k[0],
|
|
|
|
super_class=super_class,
|
|
|
|
id="\"{}\"".format(k[1]),
|
2017-12-30 18:25:17 +00:00
|
|
|
docstring='"""{}"""'.format(k[2])
|
2017-12-05 11:21:55 +00:00
|
|
|
) for k in sub_classes])
|
|
|
|
)
|
|
|
|
|
|
|
|
f_class.write(class_template)
|
|
|
|
|
|
|
|
f_all.write(" },\n")
|
|
|
|
|
|
|
|
f_all.write("}\n")
|
|
|
|
|
2018-02-28 00:28:31 +00:00
|
|
|
with open("{}/all.py".format(DEST), encoding="utf-8") as f:
|
2017-12-05 11:21:55 +00:00
|
|
|
content = f.read()
|
|
|
|
|
2018-02-28 00:28:31 +00:00
|
|
|
with open("{}/all.py".format(DEST), "w", encoding="utf-8") as f:
|
2017-12-05 11:21:55 +00:00
|
|
|
f.write(re.sub("{count}", str(count), content))
|
|
|
|
|
|
|
|
print("Compiling Errors: [100%]")
|
|
|
|
|
|
|
|
|
|
|
|
if "__main__" == __name__:
|
2018-02-28 00:28:31 +00:00
|
|
|
HOME = "."
|
2019-03-25 10:10:46 +00:00
|
|
|
DEST = "../../pyrogram/errors/exceptions"
|
2018-02-28 00:28:31 +00:00
|
|
|
NOTICE_PATH = "../../NOTICE"
|
2017-12-05 11:21:55 +00:00
|
|
|
|
|
|
|
start()
|