From 06ad65e3a035f9bb0a721cc79086bdedd380cc90 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 31 May 2019 00:19:18 +0200 Subject: [PATCH] Improve codegen scripts --- compiler/api/compiler.py | 4 ++-- compiler/docs/compiler.py | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/compiler/api/compiler.py b/compiler/api/compiler.py index 64e88c9d..6566bc8e 100644 --- a/compiler/api/compiler.py +++ b/compiler/api/compiler.py @@ -67,7 +67,7 @@ def get_docstring_arg_type(t: str, is_list: bool = False, is_pyrogram_type: bool n = len(t) - 1 t = (("e" if is_list else "E") + "ither " if n else "") + ", ".join( - ":obj:`{1} <{0}.{1}>`".format( + ":obj:`~{}.{}`".format( "pyrogram.types" if is_pyrogram_type else "pyrogram.api.types", i.replace("pyrogram.", "") ) @@ -88,7 +88,7 @@ def get_references(t: str): n = len(t) - 1 t = ", ".join( - ":obj:`{0} `".format(i) + ":obj:`~pyrogram.api.functions.{}`".format(i) for i in t ) diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 2c67e66c..b167fa57 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -18,6 +18,7 @@ import ast import os +import re import shutil HOME = "compiler/docs" @@ -29,8 +30,10 @@ TYPES_PATH = "pyrogram/api/types" FUNCTIONS_BASE = "functions" TYPES_BASE = "types" -shutil.rmtree(TYPES_BASE, ignore_errors=True) -shutil.rmtree(FUNCTIONS_BASE, ignore_errors=True) + +def snek(s: str): + 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 generate(source_path, base): @@ -50,9 +53,11 @@ def generate(source_path, base): for node in ast.walk(p): if isinstance(node, ast.ClassDef): name = node.name + break + else: + continue - # name = "".join([str(j.title()) for j in os.path.splitext(i)[0].split("_")]) - full_path = os.path.basename(path) + "/" + name + ".rst" + full_path = os.path.basename(path) + "/" + snek(name).replace("_", "-") + ".rst" if level: full_path = base + "/" + full_path @@ -65,7 +70,7 @@ def generate(source_path, base): title=name, title_markup="=" * len(name), full_class_path="pyrogram.api.{}".format( - os.path.splitext(full_path)[0].replace("/", ".") + ".".join(full_path.split("/")[:-1]) + "." + name ) ) ) @@ -82,7 +87,7 @@ def generate(source_path, base): entities = [] for i in v: - entities.append(i) + entities.append(snek(i).replace("_", "-")) if k != base: inner_path = base + "/" + k + "/index" + ".rst" @@ -98,6 +103,7 @@ def generate(source_path, base): with open(DESTINATION + "/" + inner_path, "w", encoding="utf-8") as f: if k == base: f.write(":tocdepth: 1\n\n") + k = "Raw " + k f.write( toctree.format( @@ -115,6 +121,8 @@ def start(): global page_template global toctree + shutil.rmtree(DESTINATION, ignore_errors=True) + with open(HOME + "/template/page.txt", encoding="utf-8") as f: page_template = f.read()