MTPyroger/compiler/docs/compiler.py

143 lines
4.3 KiB
Python
Raw Normal View History

2018-01-03 17:30:58 +00:00
# Pyrogram - Telegram MTProto API Client Library for Python
2019-01-01 11:36:16 +00:00
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
2018-01-03 17:30:58 +00:00
#
# 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/>.
2018-01-05 02:19:01 +00:00
2018-01-05 01:16:30 +00:00
import ast
2018-01-03 17:27:43 +00:00
import os
2019-05-30 22:19:18 +00:00
import re
2018-01-03 17:27:43 +00:00
import shutil
HOME = "compiler/docs"
2019-05-12 17:26:55 +00:00
DESTINATION = "docs/source/telegram"
2018-01-05 01:16:30 +00:00
FUNCTIONS_PATH = "pyrogram/api/functions"
TYPES_PATH = "pyrogram/api/types"
2018-01-03 17:27:43 +00:00
FUNCTIONS_BASE = "functions"
TYPES_BASE = "types"
2018-01-03 17:27:43 +00:00
2019-05-30 22:19:18 +00:00
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()
2018-01-03 17:27:43 +00:00
def generate(source_path, base):
all_entities = {}
def build(path, level=0):
last = path.split("/")[-1]
for i in os.listdir(path):
try:
if not i.startswith("__"):
build("/".join([path, i]), level=level + 1)
except NotADirectoryError:
with open(path + "/" + i, encoding="utf-8") as f:
2018-01-05 01:16:30 +00:00
p = ast.parse(f.read())
for node in ast.walk(p):
if isinstance(node, ast.ClassDef):
name = node.name
2019-05-30 22:19:18 +00:00
break
else:
continue
2018-01-05 01:16:30 +00:00
2019-05-30 22:19:18 +00:00
full_path = os.path.basename(path) + "/" + snek(name).replace("_", "-") + ".rst"
2018-01-03 17:27:43 +00:00
if level:
full_path = base + "/" + full_path
os.makedirs(os.path.dirname(DESTINATION + "/" + full_path), exist_ok=True)
2018-01-03 17:27:43 +00:00
with open(DESTINATION + "/" + full_path, "w", encoding="utf-8") as f:
2018-01-03 17:27:43 +00:00
f.write(
page_template.format(
title=name,
title_markup="=" * len(name),
full_class_path="pyrogram.api.{}".format(
2019-05-30 22:19:18 +00:00
".".join(full_path.split("/")[:-1]) + "." + name
2018-01-03 17:27:43 +00:00
)
)
)
if last not in all_entities:
all_entities[last] = []
all_entities[last].append(name)
build(source_path)
2018-06-27 13:27:15 +00:00
for k, v in sorted(all_entities.items()):
v = sorted(v)
2018-01-03 17:27:43 +00:00
entities = []
for i in v:
2019-05-30 22:19:18 +00:00
entities.append(snek(i).replace("_", "-"))
2018-01-03 17:27:43 +00:00
if k != base:
inner_path = base + "/" + k + "/index" + ".rst"
module = "pyrogram.api.{}.{}".format(base, k)
2018-01-03 17:27:43 +00:00
else:
2018-09-29 09:38:58 +00:00
for i in sorted(list(all_entities), reverse=True):
2018-01-03 17:27:43 +00:00
if i != base:
entities.insert(0, "{0}/index".format(i))
inner_path = base + "/index" + ".rst"
module = "pyrogram.api.{}".format(base)
2018-01-03 17:27:43 +00:00
with open(DESTINATION + "/" + inner_path, "w", encoding="utf-8") as f:
2018-01-08 06:15:38 +00:00
if k == base:
f.write(":tocdepth: 1\n\n")
2019-05-30 22:19:18 +00:00
k = "Raw " + k
2018-01-08 04:17:55 +00:00
2018-01-03 17:27:43 +00:00
f.write(
toctree.format(
title=k.title(),
title_markup="=" * len(k),
module=module,
2018-01-03 17:27:43 +00:00
entities="\n ".join(entities)
)
)
f.write("\n")
2018-01-05 01:16:30 +00:00
def start():
global page_template
global toctree
2019-05-30 22:19:18 +00:00
shutil.rmtree(DESTINATION, ignore_errors=True)
with open(HOME + "/template/page.txt", encoding="utf-8") as f:
page_template = f.read()
with open(HOME + "/template/toctree.txt", encoding="utf-8") as f:
toctree = f.read()
generate(TYPES_PATH, TYPES_BASE)
generate(FUNCTIONS_PATH, FUNCTIONS_BASE)
2018-01-05 01:16:30 +00:00
if "__main__" == __name__:
FUNCTIONS_PATH = "../../pyrogram/api/functions"
TYPES_PATH = "../../pyrogram/api/types"
HOME = "."
2019-05-12 17:26:55 +00:00
DESTINATION = "../../docs/source/telegram"
2018-01-05 01:16:30 +00:00
start()