Telethon/tools/copy_init_imports.py

64 lines
1.7 KiB
Python
Raw Permalink Normal View History

2023-11-02 14:43:06 +03:00
"""
Scan the `client/` directory for __init__.py files.
For every depth-1 import, add it, in order, to the __all__ variable.
"""
2024-03-16 21:05:58 +03:00
2023-11-02 14:43:06 +03:00
import ast
import os
import re
from pathlib import Path
class ImportVisitor(ast.NodeVisitor):
def __init__(self) -> None:
2024-03-17 15:06:03 +03:00
self.imported_names: list[str] = []
2023-11-02 14:43:06 +03:00
def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
if node.level == 1:
for name in node.names:
self.imported_names.append(name.asname or name.name)
def main() -> None:
impl_root = Path.cwd() / "client/src/telethon/_impl"
autogenerated_re = re.compile(
rf"(tl|mtproto){re.escape(os.path.sep)}(abcs|functions|types)"
)
2024-03-17 15:06:03 +03:00
files: list[str] = []
2023-11-02 14:43:06 +03:00
for file in impl_root.rglob("__init__.py"):
file_str = str(file)
if autogenerated_re.search(file_str):
continue
files.append(file_str)
with file.open(encoding="utf-8") as fd:
contents = fd.read()
lines = contents.splitlines(True)
module = ast.parse(contents)
visitor = ImportVisitor()
visitor.visit(module)
for stmt in module.body:
match stmt:
case ast.Assign(targets=[ast.Name(id="__all__")], value=ast.List()):
# Not rewriting the AST to preserve comments and formatting.
lines[stmt.lineno - 1 : stmt.end_lineno] = [
"__all__ = ["
+ ", ".join(map(repr, visitor.imported_names))
+ "]\n"
]
break
2024-03-16 21:05:58 +03:00
case _:
pass
2023-11-02 14:43:06 +03:00
with file.open("w", encoding="utf-8", newline="\n") as fd:
fd.writelines(lines)
if __name__ == "__main__":
main()