Skip to content

Commit ed4e775

Browse files
merge: merge pull request #35 from hydrogram/compiler-improvements-v2
Many improvements to the compiler
2 parents 26550a9 + b7482d6 commit ed4e775

5 files changed

Lines changed: 148 additions & 127 deletions

File tree

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ repos:
1111
- id: check-yaml
1212

1313
- repo: https://github.com/astral-sh/ruff-pre-commit
14-
rev: v0.4.7
14+
rev: v0.4.10
1515
hooks:
1616
- id: ruff-format
1717
- id: ruff

compiler/api/compiler.py

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@
2525
from pathlib import Path
2626
from typing import NamedTuple
2727

28-
HOME_PATH = Path("compiler/api")
29-
DESTINATION_PATH = Path("hydrogram/raw")
30-
NOTICE_PATH = "NOTICE"
28+
API_HOME_PATH = Path(__file__).parent.resolve()
29+
REPO_HOME_PATH = API_HOME_PATH.parent.parent
30+
31+
DESTINATION_PATH = REPO_HOME_PATH / "hydrogram" / "raw"
32+
NOTICE_PATH = REPO_HOME_PATH / "NOTICE"
33+
3134

3235
SECTION_RE = re.compile(r"---(\w+)---")
3336
LAYER_RE = re.compile(r"//\sLAYER\s(\d+)")
@@ -60,15 +63,15 @@
6063

6164
open = partial(open, encoding="utf-8")
6265

63-
types_to_constructors = {}
64-
types_to_functions = {}
65-
constructors_to_functions = {}
66-
namespaces_to_types = {}
67-
namespaces_to_constructors = {}
68-
namespaces_to_functions = {}
66+
types_to_constructors: dict[str, list[str]] = {}
67+
types_to_functions: dict[str, list[str]] = {}
68+
constructors_to_functions: dict[str, list[str]] = {}
69+
namespaces_to_types: dict[str, list[str]] = {}
70+
namespaces_to_constructors: dict[str, list[str]] = {}
71+
namespaces_to_functions: dict[str, list[str]] = {}
6972

7073
try:
71-
with open("docs.json") as f:
74+
with open(API_HOME_PATH / "docs.json") as f:
7275
docs = json.load(f)
7376
except FileNotFoundError:
7477
docs = {"type": {}, "constructor": {}, "method": {}}
@@ -94,7 +97,7 @@ def snake(s: str):
9497

9598

9699
def camel(s: str):
97-
return "".join([i[0].upper() + i[1:] for i in s.split("_")])
100+
return "".join(i[0].upper() + i[1:] for i in s.split("_"))
98101

99102

100103
def get_type_hint(type: str) -> str:
@@ -135,7 +138,7 @@ def get_type_hint(type: str) -> str:
135138
return f'{type}{" = None" if is_flag else ""}'
136139

137140

138-
def sort_args(args):
141+
def sort_args(args: list[tuple[str, str]]):
139142
"""Put flags at the end"""
140143
args = args.copy()
141144
flags = [i for i in args if FLAGS_RE.match(i[1])]
@@ -186,13 +189,13 @@ def get_docstring_arg_type(t: str):
186189

187190
def get_references(t: str, kind: str):
188191
if kind == "constructors":
189-
t = constructors_to_functions.get(t)
192+
items = constructors_to_functions.get(t)
190193
elif kind == "types":
191-
t = types_to_functions.get(t)
194+
items = types_to_functions.get(t)
192195
else:
193196
raise ValueError("Invalid kind")
194197

195-
return ("\n ".join(t), len(t)) if t else (None, 0)
198+
return ("\n ".join(items), len(items)) if items else (None, 0)
196199

197200

198201
def start(format: bool = False):
@@ -201,25 +204,25 @@ def start(format: bool = False):
201204
shutil.rmtree(DESTINATION_PATH / "base", ignore_errors=True)
202205

203206
with (
204-
open(HOME_PATH / "source/auth_key.tl") as f1,
205-
open(HOME_PATH / "source/sys_msgs.tl") as f2,
206-
open(HOME_PATH / "source/main_api.tl") as f3,
207+
open(API_HOME_PATH / "source/auth_key.tl") as f1,
208+
open(API_HOME_PATH / "source/sys_msgs.tl") as f2,
209+
open(API_HOME_PATH / "source/main_api.tl") as f3,
207210
):
208211
schema = (f1.read() + f2.read() + f3.read()).splitlines()
209212

210213
with (
211-
open(HOME_PATH / "template/type.txt") as f1,
212-
open(HOME_PATH / "template/combinator.txt") as f2,
214+
open(API_HOME_PATH / "template/type.txt") as f1,
215+
open(API_HOME_PATH / "template/combinator.txt") as f2,
213216
):
214217
type_tmpl = f1.read()
215218
combinator_tmpl = f2.read()
216219

217-
with open(NOTICE_PATH, encoding="utf-8") as f:
220+
with open(NOTICE_PATH) as f:
218221
notice = [f"# {line}".strip() for line in f]
219222
notice = "\n".join(notice)
220223

221224
layer = None
222-
combinators = []
225+
combinators: list[Combinator] = []
223226

224227
section = None
225228
for line in schema:
@@ -295,7 +298,7 @@ def start(format: bool = False):
295298
with contextlib.suppress(KeyError):
296299
constructors_to_functions[i] = types_to_functions[k]
297300

298-
for qualtype in types_to_constructors:
301+
for qualtype, qualval in types_to_constructors.items():
299302
typespace, type = qualtype.split(".") if "." in qualtype else ("", qualtype)
300303
dir_path = DESTINATION_PATH / "base" / typespace
301304

@@ -304,9 +307,9 @@ def start(format: bool = False):
304307
if module == "Updates":
305308
module = "UpdatesT"
306309

307-
Path(dir_path).mkdir(parents=True, exist_ok=True)
310+
dir_path.mkdir(parents=True, exist_ok=True)
308311

309-
constructors = sorted(types_to_constructors[qualtype])
312+
constructors = sorted(qualval)
310313
constr_count = len(constructors)
311314
items = "\n ".join([f"{c}" for c in constructors])
312315

@@ -508,7 +511,7 @@ def start(format: bool = False):
508511

509512
dir_path = DESTINATION_PATH / directory / c.namespace
510513

511-
Path(dir_path).mkdir(exist_ok=True, parents=True)
514+
dir_path.mkdir(exist_ok=True, parents=True)
512515

513516
module = c.name
514517

@@ -530,49 +533,82 @@ def start(format: bool = False):
530533
f.write(f"{notice}\n\n")
531534
f.write(f"{WARNING}\n\n")
532535

536+
all = []
537+
533538
for t in types:
534539
module = t
535540

536541
if module == "Updates":
537542
module = "UpdatesT"
538543

544+
all.append(t)
545+
539546
f.write(f"from .{snake(module)} import {t}\n")
540547

541548
if not namespace:
542549
f.write(f"from . import {', '.join(filter(bool, namespaces_to_types))}")
543550

551+
all.extend(filter(bool, namespaces_to_types))
552+
553+
f.write("\n\n__all__ = [\n")
554+
for it in all:
555+
f.write(f' "{it}",\n')
556+
f.write("]\n")
557+
544558
for namespace, types in namespaces_to_constructors.items():
545559
with open(DESTINATION_PATH / "types" / namespace / "__init__.py", "w") as f:
546560
f.write(f"{notice}\n\n")
547561
f.write(f"{WARNING}\n\n")
548562

563+
all = []
564+
549565
for t in types:
550566
module = t
551567

552568
if module == "Updates":
553569
module = "UpdatesT"
554570

571+
all.append(t)
572+
555573
f.write(f"from .{snake(module)} import {t}\n")
556574

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

578+
all.extend(filter(bool, namespaces_to_constructors))
579+
580+
f.write("\n\n__all__ = [\n")
581+
for it in all:
582+
f.write(f' "{it}",\n')
583+
f.write("]\n")
584+
560585
for namespace, types in namespaces_to_functions.items():
561586
with open(DESTINATION_PATH / "functions" / namespace / "__init__.py", "w") as f:
562587
f.write(f"{notice}\n\n")
563588
f.write(f"{WARNING}\n\n")
564589

590+
all = []
591+
565592
for t in types:
566593
module = t
567594

568595
if module == "Updates":
569596
module = "UpdatesT"
570597

598+
all.append(t)
599+
571600
f.write(f"from .{snake(module)} import {t}\n")
572601

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

605+
all.extend(filter(bool, namespaces_to_functions))
606+
607+
f.write("\n\n__all__ = [\n")
608+
for it in all:
609+
f.write(f' "{it}",\n')
610+
f.write("]\n")
611+
576612
with open(DESTINATION_PATH / "all.py", "w", encoding="utf-8") as f:
577613
f.write(notice + "\n\n")
578614
f.write(WARNING + "\n\n")
@@ -595,8 +631,4 @@ def start(format: bool = False):
595631

596632

597633
if __name__ == "__main__":
598-
HOME_PATH = Path()
599-
DESTINATION_PATH = Path("../../hydrogram/raw")
600-
NOTICE_PATH = Path("../../NOTICE")
601-
602634
start(format=False)

0 commit comments

Comments
 (0)