mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-01-25 00:34:19 +03:00
Fix import errors for the documentation generator
This commit is contained in:
parent
daebf5b7e8
commit
1f8316c0bb
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,5 +1,6 @@
|
||||||
# Docs
|
# Docs
|
||||||
_build/
|
_build/
|
||||||
|
docs/
|
||||||
|
|
||||||
# Generated code
|
# Generated code
|
||||||
telethon/tl/functions/
|
telethon/tl/functions/
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from telethon_generator.parsers import parse_errors, parse_tl, find_layer
|
from telethon_generator.parsers import parse_errors, parse_tl, find_layer
|
||||||
from telethon_generator.generators import generate_errors, generate_tlobjects
|
from telethon_generator.generators import\
|
||||||
|
generate_errors, generate_tlobjects, generate_docs
|
||||||
|
|
||||||
|
|
||||||
ERRORS_INPUT_JSON = 'data/errors.json'
|
ERRORS_INPUT_JSON = 'data/errors.json'
|
||||||
|
@ -9,16 +10,17 @@ ERRORS_OUTPUT = '../telethon/errors/rpc_error_list.py'
|
||||||
TLOBJECT_INPUT_TL = 'data/scheme.tl'
|
TLOBJECT_INPUT_TL = 'data/scheme.tl'
|
||||||
TLOBJECT_OUTPUT = '../telethon/tl'
|
TLOBJECT_OUTPUT = '../telethon/tl'
|
||||||
|
|
||||||
|
DOCS_INPUT_RES = 'data/html'
|
||||||
|
DOCS_OUTPUT = '../docs'
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
generate_tlobjects(
|
tlobjects = list(parse_tl(TLOBJECT_INPUT_TL, ignore_core=True))
|
||||||
tlobjects=list(parse_tl(TLOBJECT_INPUT_TL, ignore_core=True)),
|
errors = list(parse_errors(ERRORS_INPUT_JSON, ERRORS_INPUT_DESC))
|
||||||
layer=find_layer((TLOBJECT_INPUT_TL)),
|
layer = find_layer(TLOBJECT_INPUT_TL)
|
||||||
output_dir=TLOBJECT_OUTPUT
|
|
||||||
)
|
|
||||||
|
|
||||||
|
generate_tlobjects(tlobjects, layer, TLOBJECT_OUTPUT)
|
||||||
with open(ERRORS_OUTPUT, 'w', encoding='utf-8') as file:
|
with open(ERRORS_OUTPUT, 'w', encoding='utf-8') as file:
|
||||||
generate_errors(
|
generate_errors(errors, file)
|
||||||
errors=list(parse_errors(ERRORS_INPUT_JSON, ERRORS_INPUT_DESC)),
|
|
||||||
f=file
|
generate_docs(tlobjects, errors, layer, DOCS_INPUT_RES, DOCS_OUTPUT)
|
||||||
)
|
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
from .errors import generate_errors
|
from .errors import generate_errors
|
||||||
from .tlobject import generate_tlobjects
|
from .tlobject import generate_tlobjects
|
||||||
|
from .docs import generate_docs
|
||||||
|
|
|
@ -1,17 +1,10 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
|
||||||
import shutil
|
import shutil
|
||||||
try:
|
|
||||||
from .docs_writer import DocsWriter
|
|
||||||
except (ImportError, SystemError):
|
|
||||||
from docs_writer import DocsWriter
|
|
||||||
|
|
||||||
# Small trick so importing telethon_generator works
|
from ..docs_writer import DocsWriter
|
||||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
|
from ..parsers import TLObject
|
||||||
|
|
||||||
from telethon_generator.parser import TLParser, TLObject
|
|
||||||
|
|
||||||
|
|
||||||
# TLObject -> Python class name
|
# TLObject -> Python class name
|
||||||
|
@ -245,7 +238,7 @@ def copy_replace(src, dst, replacements):
|
||||||
))
|
))
|
||||||
|
|
||||||
|
|
||||||
def generate_documentation(scheme_file):
|
def _write_html_pages(tlobjects, errors, layer, input_res, output_dir):
|
||||||
"""Generates the documentation HTML files from from scheme.tl to
|
"""Generates the documentation HTML files from from scheme.tl to
|
||||||
/methods and /constructors, etc.
|
/methods and /constructors, etc.
|
||||||
"""
|
"""
|
||||||
|
@ -259,9 +252,8 @@ def generate_documentation(scheme_file):
|
||||||
'index_methods': 'methods/index.html',
|
'index_methods': 'methods/index.html',
|
||||||
'index_constructors': 'constructors/index.html'
|
'index_constructors': 'constructors/index.html'
|
||||||
}
|
}
|
||||||
tlobjects = tuple(TLParser.parse_file(scheme_file))
|
original_paths = {k: os.path.join(output_dir, v)
|
||||||
|
for k, v in original_paths.items()}
|
||||||
print('Generating constructors and functions documentation...')
|
|
||||||
|
|
||||||
# Save 'Type: [Constructors]' for use in both:
|
# Save 'Type: [Constructors]' for use in both:
|
||||||
# * Seeing the return type or constructors belonging to the same type.
|
# * Seeing the return type or constructors belonging to the same type.
|
||||||
|
@ -281,6 +273,7 @@ def generate_documentation(scheme_file):
|
||||||
for tltype, constructors in tltypes.items():
|
for tltype, constructors in tltypes.items():
|
||||||
tltypes[tltype] = list(sorted(constructors, key=lambda c: c.name))
|
tltypes[tltype] = list(sorted(constructors, key=lambda c: c.name))
|
||||||
|
|
||||||
|
# TODO Fix the fact that get_create_path_for doesn't know about out_dir
|
||||||
for tlobject in tlobjects:
|
for tlobject in tlobjects:
|
||||||
filename = get_create_path_for(tlobject)
|
filename = get_create_path_for(tlobject)
|
||||||
|
|
||||||
|
@ -396,7 +389,6 @@ def generate_documentation(scheme_file):
|
||||||
|
|
||||||
# Find all the available types (which are not the same as the constructors)
|
# Find all the available types (which are not the same as the constructors)
|
||||||
# Each type has a list of constructors associated to it, hence is a map
|
# Each type has a list of constructors associated to it, hence is a map
|
||||||
print('Generating types documentation...')
|
|
||||||
for tltype, constructors in tltypes.items():
|
for tltype, constructors in tltypes.items():
|
||||||
filename = get_path_for_type(tltype)
|
filename = get_path_for_type(tltype)
|
||||||
out_dir = os.path.dirname(filename)
|
out_dir = os.path.dirname(filename)
|
||||||
|
@ -519,12 +511,10 @@ def generate_documentation(scheme_file):
|
||||||
# This will be done automatically and not taking into account any extra
|
# This will be done automatically and not taking into account any extra
|
||||||
# information that we have available, simply a file listing all the others
|
# information that we have available, simply a file listing all the others
|
||||||
# accessible by clicking on their title
|
# accessible by clicking on their title
|
||||||
print('Generating indices...')
|
|
||||||
for folder in ['types', 'methods', 'constructors']:
|
for folder in ['types', 'methods', 'constructors']:
|
||||||
generate_index(folder, original_paths)
|
generate_index(folder, original_paths)
|
||||||
|
|
||||||
# Write the final core index, the main index for the rest of files
|
# Write the final core index, the main index for the rest of files
|
||||||
layer = TLParser.find_layer(scheme_file)
|
|
||||||
types = set()
|
types = set()
|
||||||
methods = []
|
methods = []
|
||||||
constructors = []
|
constructors = []
|
||||||
|
@ -566,8 +556,9 @@ def generate_documentation(scheme_file):
|
||||||
type_urls = fmt(types, get_path_for_type)
|
type_urls = fmt(types, get_path_for_type)
|
||||||
constructor_urls = fmt(constructors, get_create_path_for)
|
constructor_urls = fmt(constructors, get_create_path_for)
|
||||||
|
|
||||||
shutil.copy('../res/404.html', original_paths['404'])
|
shutil.copy(os.path.join(input_res, '404.html'), original_paths['404'])
|
||||||
copy_replace('../res/core.html', original_paths['index_all'], {
|
copy_replace(os.path.join(input_res, 'core.html'),
|
||||||
|
original_paths['index_all'], {
|
||||||
'{type_count}': len(types),
|
'{type_count}': len(types),
|
||||||
'{method_count}': len(methods),
|
'{method_count}': len(methods),
|
||||||
'{constructor_count}': len(tlobjects) - len(methods),
|
'{constructor_count}': len(tlobjects) - len(methods),
|
||||||
|
@ -576,7 +567,8 @@ def generate_documentation(scheme_file):
|
||||||
os.makedirs(os.path.abspath(os.path.join(
|
os.makedirs(os.path.abspath(os.path.join(
|
||||||
original_paths['search.js'], os.path.pardir
|
original_paths['search.js'], os.path.pardir
|
||||||
)), exist_ok=True)
|
)), exist_ok=True)
|
||||||
copy_replace('../res/js/search.js', original_paths['search.js'], {
|
copy_replace(os.path.join(input_res, 'js', 'search.js'),
|
||||||
|
original_paths['search.js'], {
|
||||||
'{request_names}': request_names,
|
'{request_names}': request_names,
|
||||||
'{type_names}': type_names,
|
'{type_names}': type_names,
|
||||||
'{constructor_names}': constructor_names,
|
'{constructor_names}': constructor_names,
|
||||||
|
@ -585,23 +577,16 @@ def generate_documentation(scheme_file):
|
||||||
'{constructor_urls}': constructor_urls
|
'{constructor_urls}': constructor_urls
|
||||||
})
|
})
|
||||||
|
|
||||||
# Everything done
|
|
||||||
print('Documentation generated.')
|
def _copy_resources(res_dir, out_dir):
|
||||||
|
for dirname, files in [('css', ['docs.css']), ('img', ['arrow.svg'])]:
|
||||||
|
dirpath = os.path.join(out_dir, dirname)
|
||||||
|
os.makedirs(dirpath, exist_ok=True)
|
||||||
|
for file in files:
|
||||||
|
shutil.copy(os.path.join(res_dir, dirname, file), dirpath)
|
||||||
|
|
||||||
|
|
||||||
def copy_resources():
|
def generate_docs(tlobjects, errors, layer, input_res, output_dir):
|
||||||
for d in ('css', 'img'):
|
os.makedirs(output_dir, exist_ok=True)
|
||||||
os.makedirs(d, exist_ok=True)
|
_write_html_pages(tlobjects, errors, layer, input_res, output_dir)
|
||||||
|
_copy_resources(input_res, output_dir)
|
||||||
shutil.copy('../res/img/arrow.svg', 'img')
|
|
||||||
shutil.copy('../res/css/docs.css', 'css')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
os.makedirs('generated', exist_ok=True)
|
|
||||||
os.chdir('generated')
|
|
||||||
try:
|
|
||||||
generate_documentation('../../telethon_generator/scheme.tl')
|
|
||||||
copy_resources()
|
|
||||||
finally:
|
|
||||||
os.chdir(os.pardir)
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user