Skip to content

Commit

Permalink
black code format
Browse files Browse the repository at this point in the history
  • Loading branch information
eyMarv committed Feb 7, 2024
1 parent de9065f commit ee8b8d5
Show file tree
Hide file tree
Showing 425 changed files with 5,503 additions and 4,255 deletions.
170 changes: 106 additions & 64 deletions compiler/api/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,26 @@

SECTION_RE = re.compile(r"---(\w+)---")
LAYER_RE = re.compile(r"//\sLAYER\s(\d+)")
COMBINATOR_RE = re.compile(r"^([\w.]+)#([0-9a-f]+)\s(?:.*)=\s([\w<>.]+);$", re.MULTILINE)
COMBINATOR_RE = re.compile(
r"^([\w.]+)#([0-9a-f]+)\s(?:.*)=\s([\w<>.]+);$", re.MULTILINE
)
ARGS_RE = re.compile(r"[^{](\w+):([\w?!.<>#]+)")
FLAGS_RE = re.compile(r"flags(\d?)\.(\d+)\?")
FLAGS_RE_2 = re.compile(r"flags(\d?)\.(\d+)\?([\w<>.]+)")
FLAGS_RE_3 = re.compile(r"flags(\d?):#")
INT_RE = re.compile(r"int(\d+)")

CORE_TYPES = ["int", "long", "int128", "int256", "double", "bytes", "string", "Bool", "true"]
CORE_TYPES = [
"int",
"long",
"int128",
"int256",
"double",
"bytes",
"string",
"Bool",
"true",
]

WARNING = """
# # # # # # # # # # # # # # # # # # # # # # # #
Expand All @@ -65,11 +77,7 @@
with open("docs.json") as f:
docs = json.load(f)
except FileNotFoundError:
docs = {
"type": {},
"constructor": {},
"method": {}
}
docs = {"type": {}, "constructor": {}, "method": {}}


class Combinator(NamedTuple):
Expand Down Expand Up @@ -206,13 +214,14 @@ def start(format: bool = False):
shutil.rmtree(DESTINATION_PATH / "functions", ignore_errors=True)
shutil.rmtree(DESTINATION_PATH / "base", ignore_errors=True)

with open(HOME_PATH / "source/auth_key.tl") as f1, \
open(HOME_PATH / "source/sys_msgs.tl") as f2, \
open(HOME_PATH / "source/main_api.tl") as f3:
with open(HOME_PATH / "source/auth_key.tl") as f1, open(
HOME_PATH / "source/sys_msgs.tl"
) as f2, open(HOME_PATH / "source/main_api.tl") as f3:
schema = (f1.read() + f2.read() + f3.read()).splitlines()

with open(HOME_PATH / "template/type.txt") as f1, \
open(HOME_PATH / "template/combinator.txt") as f2:
with open(HOME_PATH / "template/type.txt") as f1, open(
HOME_PATH / "template/combinator.txt"
) as f2:
type_tmpl = f1.read()
combinator_tmpl = f2.read()

Expand Down Expand Up @@ -277,7 +286,7 @@ def start(format: bool = False):
args=args,
qualtype=qualtype,
typespace=typespace,
type=type
type=type,
)

combinators.append(combinator)
Expand Down Expand Up @@ -338,22 +347,26 @@ def start(format: bool = False):

docstring = type_docs

docstring += f"\n\n Constructors:\n" \
f" This base type has {constr_count} constructor{'s' if constr_count > 1 else ''} available.\n\n" \
f" .. currentmodule:: pyrogram.raw.types\n\n" \
f" .. autosummary::\n" \
f" :nosignatures:\n\n" \
f" {items}"
docstring += (
f"\n\n Constructors:\n"
f" This base type has {constr_count} constructor{'s' if constr_count > 1 else ''} available.\n\n"
f" .. currentmodule:: pyrogram.raw.types\n\n"
f" .. autosummary::\n"
f" :nosignatures:\n\n"
f" {items}"
)

references, ref_count = get_references(qualtype, "types")

if references:
docstring += f"\n\n Functions:\n This object can be returned by " \
f"{ref_count} function{'s' if ref_count > 1 else ''}.\n\n" \
f" .. currentmodule:: pyrogram.raw.functions\n\n" \
f" .. autosummary::\n" \
f" :nosignatures:\n\n" \
f" " + references
docstring += (
f"\n\n Functions:\n This object can be returned by "
f"{ref_count} function{'s' if ref_count > 1 else ''}.\n\n"
f" .. currentmodule:: pyrogram.raw.functions\n\n"
f" .. autosummary::\n"
f" :nosignatures:\n\n"
f" " + references
)

with open(dir_path / f"{snake(module)}.py", "w") as f:
f.write(
Expand All @@ -364,25 +377,24 @@ def start(format: bool = False):
name=type,
qualname=qualtype,
types=", ".join([f"raw.types.{c}" for c in constructors]),
doc_name=snake(type).replace("_", "-")
doc_name=snake(type).replace("_", "-"),
)
)

for c in combinators:
sorted_args = sort_args(c.args)

arguments = (
(", *, " if c.args else "") +
(", ".join(
[f"{i[0]}: {get_type_hint(i[1])}"
for i in sorted_args]
) if sorted_args else "")
arguments = (", *, " if c.args else "") + (
", ".join([f"{i[0]}: {get_type_hint(i[1])}" for i in sorted_args])
if sorted_args
else ""
)

fields = "\n ".join(
[f"self.{i[0]} = {i[0]} # {i[1]}"
for i in sorted_args]
) if sorted_args else "pass"
fields = (
"\n ".join([f"self.{i[0]} = {i[0]} # {i[1]}" for i in sorted_args])
if sorted_args
else "pass"
)

docstring = ""
docstring_args = []
Expand Down Expand Up @@ -410,7 +422,7 @@ def start(format: bool = False):
arg_name,
get_docstring_arg_type(arg_type),
", *optional*".format(flag_number) if is_optional else "",
arg_docs
arg_docs,
)
)

Expand All @@ -433,21 +445,26 @@ def start(format: bool = False):
docstring += f"Telegram API function."

docstring += f"\n\n Details:\n - Layer: ``{layer}``\n - ID: ``{c.id[2:].upper()}``\n\n"
docstring += f" Parameters:\n " + \
(f"\n ".join(docstring_args) if docstring_args else "No parameters required.\n")
docstring += f" Parameters:\n " + (
f"\n ".join(docstring_args)
if docstring_args
else "No parameters required.\n"
)

if c.section == "functions":
docstring += "\n Returns:\n " + get_docstring_arg_type(c.qualtype)
else:
references, count = get_references(c.qualname, "constructors")

if references:
docstring += f"\n Functions:\n This object can be returned by " \
f"{count} function{'s' if count > 1 else ''}.\n\n" \
f" .. currentmodule:: pyrogram.raw.functions\n\n" \
f" .. autosummary::\n" \
f" :nosignatures:\n\n" \
f" " + references
docstring += (
f"\n Functions:\n This object can be returned by "
f"{count} function{'s' if count > 1 else ''}.\n\n"
f" .. currentmodule:: pyrogram.raw.functions\n\n"
f" .. autosummary::\n"
f" :nosignatures:\n\n"
f" " + references
)

write_types = read_types = "" if c.has_flags else "# No flags\n "

Expand All @@ -464,17 +481,24 @@ def start(format: bool = False):
if arg_name != f"flags{flag.group(1)}":
continue

if flag.group(3) == "true" or flag.group(3).startswith("Vector"):
write_flags.append(f"{arg_name} |= (1 << {flag.group(2)}) if self.{i[0]} else 0")
if flag.group(3) == "true" or flag.group(3).startswith(
"Vector"
):
write_flags.append(
f"{arg_name} |= (1 << {flag.group(2)}) if self.{i[0]} else 0"
)
else:
write_flags.append(
f"{arg_name} |= (1 << {flag.group(2)}) if self.{i[0]} is not None else 0")

write_flags = "\n ".join([
f"{arg_name} = 0",
"\n ".join(write_flags),
f"b.write(Int({arg_name}))\n "
])
f"{arg_name} |= (1 << {flag.group(2)}) if self.{i[0]} is not None else 0"
)

write_flags = "\n ".join(
[
f"{arg_name} = 0",
"\n ".join(write_flags),
f"b.write(Int({arg_name}))\n ",
]
)

write_types += write_flags
read_types += f"\n {arg_name} = Int.read(b)\n "
Expand All @@ -490,7 +514,9 @@ def start(format: bool = False):
elif flag_type in CORE_TYPES:
write_types += "\n "
write_types += f"if self.{arg_name} is not None:\n "
write_types += f"b.write({flag_type.title()}(self.{arg_name}))\n "
write_types += (
f"b.write({flag_type.title()}(self.{arg_name}))\n "
)

read_types += "\n "
read_types += f"{arg_name} = {flag_type.title()}.read(b) if flags{number} & (1 << {index}) else None"
Expand All @@ -500,12 +526,16 @@ def start(format: bool = False):
write_types += "\n "
write_types += f"if self.{arg_name} is not None:\n "
write_types += "b.write(Vector(self.{}{}))\n ".format(
arg_name, f", {sub_type.title()}" if sub_type in CORE_TYPES else ""
arg_name,
f", {sub_type.title()}" if sub_type in CORE_TYPES else "",
)

read_types += "\n "
read_types += "{} = TLObject.read(b{}) if flags{} & (1 << {}) else []\n ".format(
arg_name, f", {sub_type.title()}" if sub_type in CORE_TYPES else "", number, index
arg_name,
f", {sub_type.title()}" if sub_type in CORE_TYPES else "",
number,
index,
)
else:
write_types += "\n "
Expand All @@ -517,7 +547,9 @@ def start(format: bool = False):
else:
if arg_type in CORE_TYPES:
write_types += "\n "
write_types += f"b.write({arg_type.title()}(self.{arg_name}))\n "
write_types += (
f"b.write({arg_type.title()}(self.{arg_name}))\n "
)

read_types += "\n "
read_types += f"{arg_name} = {arg_type.title()}.read(b)\n "
Expand All @@ -526,12 +558,14 @@ def start(format: bool = False):

write_types += "\n "
write_types += "b.write(Vector(self.{}{}))\n ".format(
arg_name, f", {sub_type.title()}" if sub_type in CORE_TYPES else ""
arg_name,
f", {sub_type.title()}" if sub_type in CORE_TYPES else "",
)

read_types += "\n "
read_types += "{} = TLObject.read(b{})\n ".format(
arg_name, f", {sub_type.title()}" if sub_type in CORE_TYPES else ""
arg_name,
f", {sub_type.title()}" if sub_type in CORE_TYPES else "",
)
else:
write_types += "\n "
Expand All @@ -555,7 +589,7 @@ def start(format: bool = False):
fields=fields,
read_types=read_types,
write_types=write_types,
return_arguments=return_arguments
return_arguments=return_arguments,
)

directory = "types" if c.section == "types" else c.section
Expand All @@ -572,7 +606,11 @@ def start(format: bool = False):
with open(dir_path / f"{snake(module)}.py", "w") as f:
f.write(compiled_combinator)

d = namespaces_to_constructors if c.section == "types" else namespaces_to_functions
d = (
namespaces_to_constructors
if c.section == "types"
else namespaces_to_functions
)

if c.namespace not in d:
d[c.namespace] = []
Expand Down Expand Up @@ -609,7 +647,9 @@ def start(format: bool = False):
f.write(f"from .{snake(module)} import {t}\n")

if not namespace:
f.write(f"from . import {', '.join(filter(bool, namespaces_to_constructors))}\n")
f.write(
f"from . import {', '.join(filter(bool, namespaces_to_constructors))}\n"
)

for namespace, types in namespaces_to_functions.items():
with open(DESTINATION_PATH / "functions" / namespace / "__init__.py", "w") as f:
Expand All @@ -625,7 +665,9 @@ def start(format: bool = False):
f.write(f"from .{snake(module)} import {t}\n")

if not namespace:
f.write(f"from . import {', '.join(filter(bool, namespaces_to_functions))}")
f.write(
f"from . import {', '.join(filter(bool, namespaces_to_functions))}"
)

with open(DESTINATION_PATH / "all.py", "w", encoding="utf-8") as f:
f.write(notice + "\n\n")
Expand Down
41 changes: 28 additions & 13 deletions compiler/errors/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,22 @@ def start():
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:
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(" ")])
name = " ".join(
[
str(i.capitalize())
for i in re.sub(r"_", " ", name).lower().split(" ")
]
)

sub_classes = []

f_all.write(" \"_\": \"{}\",\n".format(super_class))
f_all.write(' "_": "{}",\n'.format(super_class))

for j, row in enumerate(reader):
if j == 0:
Expand All @@ -99,27 +105,36 @@ def start():
sub_class = re.sub(r"^2", "Two", sub_class)
sub_class = re.sub(r" ", "", sub_class)

f_all.write(" \"{}\": \"{}\",\n".format(error_id, sub_class))
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:
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:
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])
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)
Expand Down
Loading

0 comments on commit ee8b8d5

Please sign in to comment.