forked from pyrogram/pyrogram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.py
More file actions
142 lines (100 loc) · 4.71 KB
/
compiler.py
File metadata and controls
142 lines (100 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# 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.
#
# 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.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import csv
import os
import re
import shutil
HOME = "compiler/error"
DEST = "pyrogram/errors/exceptions"
NOTICE_PATH = "NOTICE"
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():
shutil.rmtree(DEST, ignore_errors=True)
os.makedirs(DEST)
files = [i for i in os.listdir("{}/source".format(HOME))]
with open(NOTICE_PATH, encoding="utf-8") as f:
notice = []
for line in f.readlines():
notice.append("# {}".format(line).strip())
notice = "\n".join(notice)
with open("{}/all.py".format(DEST), "w", encoding="utf-8") as f_all:
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))
init = "{}/__init__.py".format(DEST)
if not os.path.exists(init):
with open(init, "w", encoding="utf-8") as f_init:
f_init.write(notice + "\n\n")
with open(init, "a", encoding="utf-8") as f_init:
f_init.write("from .{}_{} import *\n".format(name.lower(), code))
with open("{}/source/{}".format(HOME, i), encoding="utf-8") as f_csv, \
open("{}/{}_{}.py".format(DEST, name.lower(), code), "w", encoding="utf-8") as f_class:
reader = csv.reader(f_csv, delimiter="\t")
super_class = caml(name)
name = " ".join([str(i.capitalize()) for i in re.sub(r"_", " ", name).lower().split(" ")])
sub_classes = []
f_all.write(" \"_\": \"{}\",\n".format(super_class))
for j, row in enumerate(reader):
if j == 0:
continue
count += 1
if not row: # Row is empty (blank line)
continue
error_id, error_message = row
sub_class = caml(re.sub(r"_X", "_", error_id))
f_all.write(" \"{}\": \"{}\",\n".format(error_id, sub_class))
sub_classes.append((sub_class, error_id, error_message))
with open("{}/template/class.txt".format(HOME), "r", encoding="utf-8") as f_class_template:
class_template = f_class_template.read()
with open("{}/template/sub_class.txt".format(HOME), "r", encoding="utf-8") as f_sub_class_template:
sub_class_template = f_sub_class_template.read()
class_template = class_template.format(
notice=notice,
super_class=super_class,
code=code,
docstring='"""{}"""'.format(name),
sub_classes="".join([sub_class_template.format(
sub_class=k[0],
super_class=super_class,
id="\"{}\"".format(k[1]),
docstring='"""{}"""'.format(k[2])
) for k in sub_classes])
)
f_class.write(class_template)
f_all.write(" },\n")
f_all.write("}\n")
with open("{}/all.py".format(DEST), encoding="utf-8") as f:
content = f.read()
with open("{}/all.py".format(DEST), "w", encoding="utf-8") as f:
f.write(re.sub("{count}", str(count), content))
print("Compiling Errors: [100%]")
if "__main__" == __name__:
HOME = "."
DEST = "../../pyrogram/errors/exceptions"
NOTICE_PATH = "../../NOTICE"
start()