2017-05-20 16:58:44 +03:00
|
|
|
#!/usr/bin/env python3
|
2018-04-15 11:47:55 +03:00
|
|
|
import functools
|
2019-01-26 14:46:13 +03:00
|
|
|
import os
|
2019-07-17 11:11:52 +03:00
|
|
|
import pathlib
|
2017-04-07 19:30:54 +03:00
|
|
|
import re
|
2017-04-08 15:04:32 +03:00
|
|
|
import shutil
|
2018-04-15 12:18:26 +03:00
|
|
|
from collections import defaultdict
|
2018-12-21 15:24:16 +03:00
|
|
|
from pathlib import Path
|
2017-04-07 19:30:54 +03:00
|
|
|
|
2018-06-18 22:02:42 +03:00
|
|
|
from ..docswriter import DocsWriter
|
2018-12-03 18:17:37 +03:00
|
|
|
from ..parsers import TLObject, Usability
|
2018-04-15 12:47:33 +03:00
|
|
|
from ..utils import snake_to_camel_case
|
2017-04-07 19:30:54 +03:00
|
|
|
|
2018-04-15 13:15:43 +03:00
|
|
|
CORE_TYPES = {
|
|
|
|
'int', 'long', 'int128', 'int256', 'double',
|
|
|
|
'vector', 'string', 'bool', 'true', 'bytes', 'date'
|
|
|
|
}
|
|
|
|
|
2017-04-07 19:30:54 +03:00
|
|
|
|
2018-04-15 13:15:43 +03:00
|
|
|
def _get_file_name(tlobject):
|
|
|
|
"""``ClassName -> class_name.html``."""
|
|
|
|
name = tlobject.name if isinstance(tlobject, TLObject) else tlobject
|
2017-04-07 19:30:54 +03:00
|
|
|
# Courtesy of http://stackoverflow.com/a/1176023/4759433
|
|
|
|
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
|
|
|
|
result = re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
|
2018-04-15 13:15:43 +03:00
|
|
|
return '{}.html'.format(result)
|
2017-04-07 19:30:54 +03:00
|
|
|
|
|
|
|
|
2017-06-04 20:57:20 +03:00
|
|
|
def get_import_code(tlobject):
|
2018-04-15 13:15:43 +03:00
|
|
|
"""``TLObject -> from ... import ...``."""
|
2017-06-04 20:57:20 +03:00
|
|
|
kind = 'functions' if tlobject.is_function else 'types'
|
|
|
|
ns = '.' + tlobject.namespace if tlobject.namespace else ''
|
|
|
|
return 'from telethon.tl.{}{} import {}'\
|
2018-04-15 12:47:33 +03:00
|
|
|
.format(kind, ns, tlobject.class_name)
|
2017-06-04 20:57:20 +03:00
|
|
|
|
|
|
|
|
2019-07-17 11:11:52 +03:00
|
|
|
def _get_path_for(tlobject):
|
|
|
|
"""Returns the path for the given TLObject."""
|
|
|
|
out_dir = pathlib.Path('methods' if tlobject.is_function else 'constructors')
|
2017-04-07 19:30:54 +03:00
|
|
|
if tlobject.namespace:
|
2018-12-21 15:24:16 +03:00
|
|
|
out_dir /= tlobject.namespace
|
2017-04-07 19:30:54 +03:00
|
|
|
|
2018-12-21 15:24:16 +03:00
|
|
|
return out_dir / _get_file_name(tlobject)
|
2017-07-08 14:35:10 +03:00
|
|
|
|
|
|
|
|
2018-12-21 20:18:18 +03:00
|
|
|
def _get_path_for_type(type_):
|
2018-12-24 20:16:18 +03:00
|
|
|
"""Similar to `_get_path_for` but for only type names."""
|
2018-04-15 13:15:43 +03:00
|
|
|
if type_.lower() in CORE_TYPES:
|
2018-12-21 20:18:18 +03:00
|
|
|
return Path('index.html#%s' % type_.lower())
|
2017-05-21 14:59:16 +03:00
|
|
|
elif '.' in type_:
|
|
|
|
namespace, name = type_.split('.')
|
2018-12-21 20:18:18 +03:00
|
|
|
return Path('types', namespace, _get_file_name(name))
|
2017-04-07 19:30:54 +03:00
|
|
|
else:
|
2018-12-21 20:18:18 +03:00
|
|
|
return Path('types', _get_file_name(type_))
|
2017-04-07 19:30:54 +03:00
|
|
|
|
|
|
|
|
2018-04-15 13:15:43 +03:00
|
|
|
def _find_title(html_file):
|
|
|
|
"""Finds the <title> for the given HTML file, or (Unknown)."""
|
2018-12-21 20:18:18 +03:00
|
|
|
# TODO Is it necessary to read files like this?
|
|
|
|
with html_file.open() as f:
|
|
|
|
for line in f:
|
2017-04-07 22:06:49 +03:00
|
|
|
if '<title>' in line:
|
|
|
|
# + 7 to skip len('<title>')
|
|
|
|
return line[line.index('<title>') + 7:line.index('</title>')]
|
|
|
|
|
|
|
|
return '(Unknown)'
|
|
|
|
|
|
|
|
|
2018-12-21 20:18:18 +03:00
|
|
|
def _build_menu(docs):
|
|
|
|
"""
|
|
|
|
Builds the menu used for the current ``DocumentWriter``.
|
|
|
|
"""
|
|
|
|
|
|
|
|
paths = []
|
|
|
|
current = docs.filename
|
2019-07-17 11:11:52 +03:00
|
|
|
top = pathlib.Path('.')
|
|
|
|
while current != top:
|
2018-12-21 20:18:18 +03:00
|
|
|
current = current.parent
|
|
|
|
paths.append(current)
|
2017-04-08 13:21:38 +03:00
|
|
|
|
2018-12-21 20:18:18 +03:00
|
|
|
for path in reversed(paths):
|
2019-07-17 11:11:52 +03:00
|
|
|
docs.add_menu(path.stem.title() or 'API', link=path / 'index.html')
|
2018-12-21 20:18:18 +03:00
|
|
|
|
|
|
|
if docs.filename.stem != 'index':
|
|
|
|
docs.add_menu(docs.title, link=docs.filename)
|
2017-04-08 13:21:38 +03:00
|
|
|
|
|
|
|
docs.end_menu()
|
|
|
|
|
|
|
|
|
2019-07-17 11:11:52 +03:00
|
|
|
def _generate_index(folder, paths,
|
2018-06-11 14:36:32 +03:00
|
|
|
bots_index=False, bots_index_paths=()):
|
2017-04-07 22:06:49 +03:00
|
|
|
"""Generates the index file for the specified folder"""
|
2017-05-21 14:59:16 +03:00
|
|
|
# Determine the namespaces listed here (as sub folders)
|
2017-04-07 22:06:49 +03:00
|
|
|
# and the files (.html files) that we should link to
|
|
|
|
namespaces = []
|
|
|
|
files = []
|
2018-06-11 14:36:32 +03:00
|
|
|
INDEX = 'index.html'
|
|
|
|
BOT_INDEX = 'botindex.html'
|
|
|
|
|
2018-12-21 20:18:18 +03:00
|
|
|
for item in (bots_index_paths or folder.iterdir()):
|
|
|
|
if item.is_dir():
|
|
|
|
namespaces.append(item)
|
|
|
|
elif item.name not in (INDEX, BOT_INDEX):
|
|
|
|
files.append(item)
|
2017-04-07 22:06:49 +03:00
|
|
|
|
|
|
|
# Now that everything is setup, write the index.html file
|
2018-12-21 15:24:16 +03:00
|
|
|
filename = folder / (BOT_INDEX if bots_index else INDEX)
|
2019-07-17 11:11:52 +03:00
|
|
|
with DocsWriter(filename, _get_path_for_type) as docs:
|
2017-04-07 22:06:49 +03:00
|
|
|
# Title should be the current folder name
|
2019-03-16 19:17:25 +03:00
|
|
|
docs.write_head(str(folder).replace(os.path.sep, '/').title(),
|
2018-12-21 20:18:18 +03:00
|
|
|
css_path=paths['css'],
|
|
|
|
default_css=paths['default_css'])
|
2017-04-08 13:21:38 +03:00
|
|
|
|
|
|
|
docs.set_menu_separator(paths['arrow'])
|
2018-12-21 20:18:18 +03:00
|
|
|
_build_menu(docs)
|
2019-07-17 11:11:52 +03:00
|
|
|
docs.write_title(str(filename.parent)
|
2019-03-16 19:17:25 +03:00
|
|
|
.replace(os.path.sep, '/').title())
|
2018-12-21 15:24:16 +03:00
|
|
|
|
2018-06-11 14:36:32 +03:00
|
|
|
if bots_index:
|
2018-12-03 18:17:37 +03:00
|
|
|
docs.write_text('These are the methods that you may be able to '
|
|
|
|
'use as a bot. Click <a href="{}">here</a> to '
|
2018-06-11 14:36:32 +03:00
|
|
|
'view them all.'.format(INDEX))
|
|
|
|
else:
|
|
|
|
docs.write_text('Click <a href="{}">here</a> to view the methods '
|
|
|
|
'that you can use as a bot.'.format(BOT_INDEX))
|
2017-04-07 22:06:49 +03:00
|
|
|
if namespaces:
|
|
|
|
docs.write_title('Namespaces', level=3)
|
2017-04-08 13:21:38 +03:00
|
|
|
docs.begin_table(4)
|
2017-05-19 11:36:20 +03:00
|
|
|
namespaces.sort()
|
2017-04-07 22:06:49 +03:00
|
|
|
for namespace in namespaces:
|
|
|
|
# For every namespace, also write the index of it
|
2018-06-11 14:36:32 +03:00
|
|
|
namespace_paths = []
|
|
|
|
if bots_index:
|
|
|
|
for item in bots_index_paths:
|
2018-12-21 20:18:18 +03:00
|
|
|
if item.parent == namespace:
|
|
|
|
namespace_paths.append(item)
|
|
|
|
|
2019-07-17 11:11:52 +03:00
|
|
|
_generate_index(namespace, paths,
|
2018-06-11 14:36:32 +03:00
|
|
|
bots_index, namespace_paths)
|
2018-12-21 20:18:18 +03:00
|
|
|
|
|
|
|
docs.add_row(
|
|
|
|
namespace.stem.title(),
|
|
|
|
link=namespace / (BOT_INDEX if bots_index else INDEX))
|
2017-04-07 22:06:49 +03:00
|
|
|
|
|
|
|
docs.end_table()
|
|
|
|
|
|
|
|
docs.write_title('Available items')
|
|
|
|
docs.begin_table(2)
|
2017-05-19 11:36:20 +03:00
|
|
|
|
2018-12-21 20:18:18 +03:00
|
|
|
files = [(f, _find_title(f)) for f in files]
|
2017-05-19 11:36:20 +03:00
|
|
|
files.sort(key=lambda t: t[1])
|
|
|
|
|
|
|
|
for file, title in files:
|
|
|
|
docs.add_row(title, link=file)
|
|
|
|
|
2017-04-07 22:06:49 +03:00
|
|
|
docs.end_table()
|
|
|
|
docs.end_body()
|
|
|
|
|
|
|
|
|
2018-04-15 13:15:43 +03:00
|
|
|
def _get_description(arg):
|
|
|
|
"""Generates a proper description for the given argument."""
|
2017-09-04 18:10:04 +03:00
|
|
|
desc = []
|
2017-09-29 13:14:09 +03:00
|
|
|
otherwise = False
|
2017-09-04 18:10:04 +03:00
|
|
|
if arg.can_be_inferred:
|
2017-09-29 13:14:09 +03:00
|
|
|
desc.append('If left unspecified, it will be inferred automatically.')
|
|
|
|
otherwise = True
|
2022-05-12 11:20:42 +03:00
|
|
|
elif arg.flag:
|
2018-07-25 03:21:44 +03:00
|
|
|
desc.append('This argument defaults to '
|
|
|
|
'<code>None</code> and can be omitted.')
|
2017-09-29 13:14:09 +03:00
|
|
|
otherwise = True
|
|
|
|
|
2018-09-22 11:56:37 +03:00
|
|
|
if arg.type in {'InputPeer', 'InputUser', 'InputChannel',
|
|
|
|
'InputNotifyPeer', 'InputDialogPeer'}:
|
2018-02-14 16:06:45 +03:00
|
|
|
desc.append(
|
|
|
|
'Anything entity-like will work if the library can find its '
|
|
|
|
'<code>Input</code> version (e.g., usernames, <code>Peer</code>, '
|
|
|
|
'<code>User</code> or <code>Channel</code> objects, etc.).'
|
|
|
|
)
|
|
|
|
|
2017-09-04 18:10:04 +03:00
|
|
|
if arg.is_vector:
|
2017-09-29 13:14:09 +03:00
|
|
|
if arg.is_generic:
|
|
|
|
desc.append('A list of other Requests must be supplied.')
|
|
|
|
else:
|
|
|
|
desc.append('A list must be supplied.')
|
|
|
|
elif arg.is_generic:
|
2017-09-04 18:10:04 +03:00
|
|
|
desc.append('A different Request must be supplied for this argument.')
|
2017-09-29 13:14:09 +03:00
|
|
|
else:
|
|
|
|
otherwise = False # Always reset to false if no other text is added
|
|
|
|
|
|
|
|
if otherwise:
|
|
|
|
desc.insert(1, 'Otherwise,')
|
|
|
|
desc[-1] = desc[-1][:1].lower() + desc[-1][1:]
|
2017-09-04 18:10:04 +03:00
|
|
|
|
2018-02-14 16:06:45 +03:00
|
|
|
return ' '.join(desc).replace(
|
|
|
|
'list',
|
|
|
|
'<span class="tooltip" title="Any iterable that supports len() '
|
|
|
|
'will work too">list</span>'
|
|
|
|
)
|
2017-09-04 18:10:04 +03:00
|
|
|
|
|
|
|
|
2018-04-15 13:15:43 +03:00
|
|
|
def _copy_replace(src, dst, replacements):
|
2018-01-20 15:11:22 +03:00
|
|
|
"""Copies the src file into dst applying the replacements dict"""
|
2019-01-06 23:36:32 +03:00
|
|
|
with src.open() as infile, dst.open('w') as outfile:
|
2018-01-20 15:11:22 +03:00
|
|
|
outfile.write(re.sub(
|
|
|
|
'|'.join(re.escape(k) for k in replacements),
|
|
|
|
lambda m: str(replacements[m.group(0)]),
|
|
|
|
infile.read()
|
|
|
|
))
|
|
|
|
|
|
|
|
|
2019-07-17 11:11:52 +03:00
|
|
|
def _write_html_pages(tlobjects, methods, layer, input_res):
|
2018-04-15 13:15:43 +03:00
|
|
|
"""
|
|
|
|
Generates the documentation HTML files from from ``scheme.tl``
|
|
|
|
to ``/methods`` and ``/constructors``, etc.
|
2017-09-04 18:10:04 +03:00
|
|
|
"""
|
2018-04-15 11:47:55 +03:00
|
|
|
# Save 'Type: [Constructors]' for use in both:
|
|
|
|
# * Seeing the return type or constructors belonging to the same type.
|
|
|
|
# * Generating the types documentation, showing available constructors.
|
2019-07-17 11:11:52 +03:00
|
|
|
paths = {k: pathlib.Path(v) for k, v in (
|
2018-12-21 20:18:18 +03:00
|
|
|
('css', 'css'),
|
|
|
|
('arrow', 'img/arrow.svg'),
|
|
|
|
('search.js', 'js/search.js'),
|
|
|
|
('404', '404.html'),
|
|
|
|
('index_all', 'index.html'),
|
|
|
|
('bot_index', 'botindex.html'),
|
|
|
|
('index_types', 'types/index.html'),
|
|
|
|
('index_methods', 'methods/index.html'),
|
|
|
|
('index_constructors', 'constructors/index.html')
|
|
|
|
)}
|
|
|
|
paths['default_css'] = 'light' # docs.<name>.css, local path
|
2018-06-18 19:41:06 +03:00
|
|
|
type_to_constructors = defaultdict(list)
|
|
|
|
type_to_functions = defaultdict(list)
|
2017-05-24 20:22:03 +03:00
|
|
|
for tlobject in tlobjects:
|
2018-04-15 11:47:55 +03:00
|
|
|
d = type_to_functions if tlobject.is_function else type_to_constructors
|
2019-07-15 11:05:26 +03:00
|
|
|
d[tlobject.innermost_result].append(tlobject)
|
2018-04-15 11:47:55 +03:00
|
|
|
|
|
|
|
for t, cs in type_to_constructors.items():
|
|
|
|
type_to_constructors[t] = list(sorted(cs, key=lambda c: c.name))
|
2017-05-24 20:22:03 +03:00
|
|
|
|
2018-11-30 23:47:18 +03:00
|
|
|
methods = {m.name: m for m in methods}
|
2018-06-11 14:36:32 +03:00
|
|
|
bot_docs_paths = []
|
2017-06-04 21:21:45 +03:00
|
|
|
|
2017-04-07 19:30:54 +03:00
|
|
|
for tlobject in tlobjects:
|
2019-07-17 11:11:52 +03:00
|
|
|
filename = _get_path_for(tlobject)
|
|
|
|
with DocsWriter(filename, _get_path_for_type) as docs:
|
2018-04-15 12:47:33 +03:00
|
|
|
docs.write_head(title=tlobject.class_name,
|
2018-12-21 20:18:18 +03:00
|
|
|
css_path=paths['css'],
|
|
|
|
default_css=paths['default_css'])
|
2017-04-07 19:30:54 +03:00
|
|
|
|
2017-04-07 21:18:01 +03:00
|
|
|
# Create the menu (path to the current TLObject)
|
|
|
|
docs.set_menu_separator(paths['arrow'])
|
2018-12-21 20:18:18 +03:00
|
|
|
_build_menu(docs)
|
2017-04-07 19:30:54 +03:00
|
|
|
|
2017-04-07 21:18:01 +03:00
|
|
|
# Create the page title
|
2018-04-15 12:47:33 +03:00
|
|
|
docs.write_title(tlobject.class_name)
|
2017-04-07 19:30:54 +03:00
|
|
|
|
2018-06-06 18:35:06 +03:00
|
|
|
if tlobject.is_function:
|
2018-12-03 18:17:37 +03:00
|
|
|
if tlobject.usability == Usability.USER:
|
|
|
|
start = '<strong>Only users</strong> can'
|
|
|
|
elif tlobject.usability == Usability.BOT:
|
2018-06-11 14:36:32 +03:00
|
|
|
bot_docs_paths.append(filename)
|
2018-12-03 18:17:37 +03:00
|
|
|
start = '<strong>Only bots</strong> can'
|
|
|
|
elif tlobject.usability == Usability.BOTH:
|
|
|
|
bot_docs_paths.append(filename)
|
|
|
|
start = '<strong>Both users and bots</strong> can'
|
|
|
|
else:
|
|
|
|
bot_docs_paths.append(filename)
|
|
|
|
start = \
|
|
|
|
'Both users and bots <strong>may</strong> be able to'
|
|
|
|
|
|
|
|
docs.write_text('{} use this method. <a href="#examples">'
|
|
|
|
'See code examples.</a>'.format(start))
|
2018-06-06 18:35:06 +03:00
|
|
|
|
2017-04-07 21:18:01 +03:00
|
|
|
# Write the code definition for this TLObject
|
|
|
|
docs.write_code(tlobject)
|
2017-06-04 20:57:20 +03:00
|
|
|
docs.write_copy_button('Copy import to the clipboard',
|
|
|
|
get_import_code(tlobject))
|
2017-04-07 19:30:54 +03:00
|
|
|
|
2017-05-24 20:22:03 +03:00
|
|
|
# Write the return type (or constructors belonging to the same type)
|
|
|
|
docs.write_title('Returns' if tlobject.is_function
|
|
|
|
else 'Belongs to', level=3)
|
|
|
|
|
|
|
|
generic_arg = next((arg.name for arg in tlobject.args
|
|
|
|
if arg.generic_definition), None)
|
|
|
|
|
|
|
|
if tlobject.result == generic_arg:
|
|
|
|
# We assume it's a function returning a generic type
|
|
|
|
generic_arg = next((arg.name for arg in tlobject.args
|
|
|
|
if arg.is_generic))
|
|
|
|
docs.write_text('This function returns the result of whatever '
|
|
|
|
'the result from invoking the request passed '
|
|
|
|
'through <i>{}</i> is.'.format(generic_arg))
|
|
|
|
else:
|
|
|
|
if re.search('^vector<', tlobject.result, re.IGNORECASE):
|
|
|
|
docs.write_text('A list of the following type is returned.')
|
2019-07-15 11:05:26 +03:00
|
|
|
inner = tlobject.innermost_result
|
2017-05-24 20:22:03 +03:00
|
|
|
else:
|
|
|
|
inner = tlobject.result
|
|
|
|
|
|
|
|
docs.begin_table(column_count=1)
|
2019-07-17 11:11:52 +03:00
|
|
|
docs.add_row(inner, link=_get_path_for_type(inner))
|
2017-05-24 20:22:03 +03:00
|
|
|
docs.end_table()
|
|
|
|
|
2018-04-15 11:47:55 +03:00
|
|
|
cs = type_to_constructors.get(inner, [])
|
|
|
|
if not cs:
|
2017-05-24 20:22:03 +03:00
|
|
|
docs.write_text('This type has no instances available.')
|
2018-04-15 11:47:55 +03:00
|
|
|
elif len(cs) == 1:
|
2017-05-24 20:22:03 +03:00
|
|
|
docs.write_text('This type can only be an instance of:')
|
|
|
|
else:
|
|
|
|
docs.write_text('This type can be an instance of either:')
|
|
|
|
|
|
|
|
docs.begin_table(column_count=2)
|
2018-04-15 11:47:55 +03:00
|
|
|
for constructor in cs:
|
2019-07-17 11:11:52 +03:00
|
|
|
link = _get_path_for(constructor)
|
2018-04-15 12:47:33 +03:00
|
|
|
docs.add_row(constructor.class_name, link=link)
|
2017-05-24 20:22:03 +03:00
|
|
|
docs.end_table()
|
|
|
|
|
|
|
|
# Return (or similar types) written. Now parameters/members
|
2017-09-04 18:10:04 +03:00
|
|
|
docs.write_title(
|
|
|
|
'Parameters' if tlobject.is_function else 'Members', level=3
|
|
|
|
)
|
2017-04-07 19:30:54 +03:00
|
|
|
|
2017-09-04 18:10:04 +03:00
|
|
|
# Sort the arguments in the same way they're sorted
|
|
|
|
# on the generated code (flags go last)
|
2017-06-11 20:16:59 +03:00
|
|
|
args = [
|
|
|
|
a for a in tlobject.sorted_args()
|
|
|
|
if not a.flag_indicator and not a.generic_definition
|
|
|
|
]
|
|
|
|
|
2017-04-07 19:30:54 +03:00
|
|
|
if args:
|
|
|
|
# Writing parameters
|
2017-04-07 21:18:01 +03:00
|
|
|
docs.begin_table(column_count=3)
|
2017-04-07 19:30:54 +03:00
|
|
|
|
|
|
|
for arg in args:
|
2017-04-07 21:18:01 +03:00
|
|
|
# Name row
|
|
|
|
docs.add_row(arg.name,
|
|
|
|
bold=True)
|
2017-04-07 19:30:54 +03:00
|
|
|
|
2017-04-07 21:18:01 +03:00
|
|
|
# Type row
|
2018-07-25 03:21:44 +03:00
|
|
|
friendly_type = 'flag' if arg.type == 'true' else arg.type
|
2017-04-08 13:41:31 +03:00
|
|
|
if arg.is_generic:
|
2018-07-25 03:21:44 +03:00
|
|
|
docs.add_row('!' + friendly_type, align='center')
|
2017-04-08 13:41:31 +03:00
|
|
|
else:
|
2017-09-04 18:10:04 +03:00
|
|
|
docs.add_row(
|
2018-12-21 20:18:18 +03:00
|
|
|
friendly_type, align='center',
|
2019-07-17 11:11:52 +03:00
|
|
|
link=_get_path_for_type(arg.type)
|
2017-09-04 18:10:04 +03:00
|
|
|
)
|
2017-04-07 19:30:54 +03:00
|
|
|
|
2017-09-04 18:10:04 +03:00
|
|
|
# Add a description for this argument
|
2018-04-15 13:15:43 +03:00
|
|
|
docs.add_row(_get_description(arg))
|
2017-04-07 19:30:54 +03:00
|
|
|
|
2017-04-07 21:18:01 +03:00
|
|
|
docs.end_table()
|
2017-04-07 19:30:54 +03:00
|
|
|
else:
|
|
|
|
if tlobject.is_function:
|
2017-04-07 21:18:01 +03:00
|
|
|
docs.write_text('This request takes no input parameters.')
|
2017-04-07 19:30:54 +03:00
|
|
|
else:
|
2017-04-07 21:18:01 +03:00
|
|
|
docs.write_text('This type has no members.')
|
2017-04-07 19:30:54 +03:00
|
|
|
|
2018-04-15 12:18:26 +03:00
|
|
|
if tlobject.is_function:
|
|
|
|
docs.write_title('Known RPC errors')
|
2018-11-30 23:47:18 +03:00
|
|
|
method_info = methods.get(tlobject.fullname)
|
|
|
|
errors = method_info and method_info.errors
|
2018-04-15 12:18:26 +03:00
|
|
|
if not errors:
|
|
|
|
docs.write_text("This request can't cause any RPC error "
|
|
|
|
"as far as we know.")
|
|
|
|
else:
|
|
|
|
docs.write_text(
|
|
|
|
'This request can cause {} known error{}:'.format(
|
|
|
|
len(errors), '' if len(errors) == 1 else 's'
|
|
|
|
))
|
|
|
|
docs.begin_table(column_count=2)
|
|
|
|
for error in errors:
|
|
|
|
docs.add_row('<code>{}</code>'.format(error.name))
|
|
|
|
docs.add_row('{}.'.format(error.description))
|
|
|
|
docs.end_table()
|
|
|
|
docs.write_text('You can import these from '
|
|
|
|
'<code>telethon.errors</code>.')
|
|
|
|
|
2018-10-16 10:29:48 +03:00
|
|
|
docs.write_title('Example', id='examples')
|
2019-05-26 22:11:24 +03:00
|
|
|
if tlobject.friendly:
|
|
|
|
ns, friendly = tlobject.friendly
|
|
|
|
docs.write_text(
|
|
|
|
'Please refer to the documentation of <a href="'
|
|
|
|
'https://docs.telethon.dev/en/latest/modules/client.html'
|
|
|
|
'#telethon.client.{0}.{1}"><code>client.{1}()</code></a> '
|
|
|
|
'to learn about the parameters and see several code '
|
|
|
|
'examples on how to use it.'
|
|
|
|
.format(ns, friendly)
|
|
|
|
)
|
|
|
|
docs.write_text(
|
|
|
|
'The method above is the recommended way to do it. '
|
|
|
|
'If you need more control over the parameters or want '
|
|
|
|
'to learn how it is implemented, open the details by '
|
|
|
|
'clicking on the "Details" text.'
|
|
|
|
)
|
|
|
|
docs.write('<details>')
|
|
|
|
|
2019-03-16 19:17:25 +03:00
|
|
|
docs.write('''<pre>\
|
|
|
|
<strong>from</strong> telethon.sync <strong>import</strong> TelegramClient
|
|
|
|
<strong>from</strong> telethon <strong>import</strong> functions, types
|
|
|
|
|
|
|
|
<strong>with</strong> TelegramClient(name, api_id, api_hash) <strong>as</strong> client:
|
|
|
|
result = client(''')
|
2018-10-16 10:29:48 +03:00
|
|
|
tlobject.as_example(docs, indent=1)
|
|
|
|
docs.write(')\n')
|
|
|
|
if tlobject.result.startswith('Vector'):
|
2019-03-16 19:17:25 +03:00
|
|
|
docs.write('''\
|
|
|
|
<strong>for</strong> x <strong>in</strong> result:
|
|
|
|
print(x''')
|
2018-10-16 10:29:48 +03:00
|
|
|
else:
|
|
|
|
docs.write(' print(result')
|
|
|
|
if tlobject.result != 'Bool' \
|
|
|
|
and not tlobject.result.startswith('Vector'):
|
|
|
|
docs.write('.stringify()')
|
|
|
|
|
|
|
|
docs.write(')</pre>')
|
2019-05-26 22:11:24 +03:00
|
|
|
if tlobject.friendly:
|
|
|
|
docs.write('</details>')
|
2018-10-16 10:29:48 +03:00
|
|
|
|
2018-01-20 15:11:22 +03:00
|
|
|
depth = '../' * (2 if tlobject.namespace else 1)
|
|
|
|
docs.add_script(src='prependPath = "{}";'.format(depth))
|
2019-01-26 14:46:13 +03:00
|
|
|
docs.add_script(path=paths['search.js'])
|
2017-04-07 21:18:01 +03:00
|
|
|
docs.end_body()
|
2017-04-07 19:30:54 +03:00
|
|
|
|
2017-04-07 20:08:00 +03:00
|
|
|
# Find all the available types (which are not the same as the constructors)
|
2017-09-04 18:10:04 +03:00
|
|
|
# Each type has a list of constructors associated to it, hence is a map
|
2018-04-15 11:47:55 +03:00
|
|
|
for t, cs in type_to_constructors.items():
|
2019-07-17 11:11:52 +03:00
|
|
|
filename = _get_path_for_type(t)
|
2018-12-21 15:24:16 +03:00
|
|
|
out_dir = filename.parent
|
2017-09-23 22:00:01 +03:00
|
|
|
if out_dir:
|
2018-12-21 15:24:16 +03:00
|
|
|
out_dir.mkdir(parents=True, exist_ok=True)
|
2017-04-07 20:08:00 +03:00
|
|
|
|
2017-09-04 18:10:04 +03:00
|
|
|
# Since we don't have access to the full TLObject, split the type
|
2018-04-15 11:47:55 +03:00
|
|
|
if '.' in t:
|
|
|
|
namespace, name = t.split('.')
|
2017-04-07 20:08:00 +03:00
|
|
|
else:
|
2018-04-15 11:47:55 +03:00
|
|
|
namespace, name = None, t
|
2017-04-07 20:08:00 +03:00
|
|
|
|
2019-07-17 11:11:52 +03:00
|
|
|
with DocsWriter(filename, _get_path_for_type) as docs:
|
2018-05-23 19:50:28 +03:00
|
|
|
docs.write_head(title=snake_to_camel_case(name),
|
2018-12-21 20:18:18 +03:00
|
|
|
css_path=paths['css'],
|
|
|
|
default_css=paths['default_css'])
|
2017-04-07 20:08:00 +03:00
|
|
|
|
2017-04-07 21:18:01 +03:00
|
|
|
docs.set_menu_separator(paths['arrow'])
|
2018-12-21 20:18:18 +03:00
|
|
|
_build_menu(docs)
|
2017-04-07 20:08:00 +03:00
|
|
|
|
2017-04-07 21:18:01 +03:00
|
|
|
# Main file title
|
2018-04-15 12:47:33 +03:00
|
|
|
docs.write_title(snake_to_camel_case(name))
|
2017-04-07 20:08:00 +03:00
|
|
|
|
2017-05-22 18:28:06 +03:00
|
|
|
# List available constructors for this type
|
2017-04-07 21:18:01 +03:00
|
|
|
docs.write_title('Available constructors', level=3)
|
2018-04-15 11:47:55 +03:00
|
|
|
if not cs:
|
2017-04-07 21:18:01 +03:00
|
|
|
docs.write_text('This type has no constructors available.')
|
2018-04-15 11:47:55 +03:00
|
|
|
elif len(cs) == 1:
|
2017-04-07 21:18:01 +03:00
|
|
|
docs.write_text('This type has one constructor available.')
|
2017-04-07 20:08:00 +03:00
|
|
|
else:
|
2017-09-04 18:10:04 +03:00
|
|
|
docs.write_text('This type has %d constructors available.' %
|
2018-04-15 11:47:55 +03:00
|
|
|
len(cs))
|
2017-04-07 20:08:00 +03:00
|
|
|
|
2017-04-08 13:26:48 +03:00
|
|
|
docs.begin_table(2)
|
2018-04-15 11:47:55 +03:00
|
|
|
for constructor in cs:
|
2017-04-07 20:08:00 +03:00
|
|
|
# Constructor full name
|
2019-07-17 11:11:52 +03:00
|
|
|
link = _get_path_for(constructor)
|
2018-04-15 12:47:33 +03:00
|
|
|
docs.add_row(constructor.class_name, link=link)
|
2017-04-07 21:18:01 +03:00
|
|
|
docs.end_table()
|
2017-04-07 20:08:00 +03:00
|
|
|
|
2017-05-22 18:28:06 +03:00
|
|
|
# List all the methods which return this type
|
2017-04-07 21:18:01 +03:00
|
|
|
docs.write_title('Methods returning this type', level=3)
|
2018-04-15 11:47:55 +03:00
|
|
|
functions = type_to_functions.get(t, [])
|
2017-04-07 20:08:00 +03:00
|
|
|
if not functions:
|
2017-04-07 21:18:01 +03:00
|
|
|
docs.write_text('No method returns this type.')
|
2017-04-07 20:08:00 +03:00
|
|
|
elif len(functions) == 1:
|
2017-04-07 21:18:01 +03:00
|
|
|
docs.write_text('Only the following method returns this type.')
|
2017-04-07 20:08:00 +03:00
|
|
|
else:
|
2017-09-04 18:10:04 +03:00
|
|
|
docs.write_text(
|
|
|
|
'The following %d methods return this type as a result.' %
|
|
|
|
len(functions)
|
|
|
|
)
|
2017-04-07 20:08:00 +03:00
|
|
|
|
2017-04-08 13:26:48 +03:00
|
|
|
docs.begin_table(2)
|
2017-05-21 14:59:16 +03:00
|
|
|
for func in functions:
|
2019-07-17 11:11:52 +03:00
|
|
|
link = _get_path_for(func)
|
2018-04-15 12:47:33 +03:00
|
|
|
docs.add_row(func.class_name, link=link)
|
2017-04-07 21:18:01 +03:00
|
|
|
docs.end_table()
|
2017-05-22 18:28:06 +03:00
|
|
|
|
2017-07-08 18:31:57 +03:00
|
|
|
# List all the methods which take this type as input
|
|
|
|
docs.write_title('Methods accepting this type as input', level=3)
|
|
|
|
other_methods = sorted(
|
2018-04-28 13:21:55 +03:00
|
|
|
(u for u in tlobjects
|
|
|
|
if any(a.type == t for a in u.args) and u.is_function),
|
|
|
|
key=lambda u: u.name
|
2017-07-08 18:31:57 +03:00
|
|
|
)
|
|
|
|
if not other_methods:
|
|
|
|
docs.write_text(
|
|
|
|
'No methods accept this type as an input parameter.')
|
|
|
|
elif len(other_methods) == 1:
|
|
|
|
docs.write_text(
|
|
|
|
'Only this method has a parameter with this type.')
|
|
|
|
else:
|
|
|
|
docs.write_text(
|
|
|
|
'The following %d methods accept this type as an input '
|
|
|
|
'parameter.' % len(other_methods))
|
|
|
|
|
|
|
|
docs.begin_table(2)
|
|
|
|
for ot in other_methods:
|
2019-07-17 11:11:52 +03:00
|
|
|
link = _get_path_for(ot)
|
2018-04-15 12:47:33 +03:00
|
|
|
docs.add_row(ot.class_name, link=link)
|
2017-07-08 18:31:57 +03:00
|
|
|
docs.end_table()
|
|
|
|
|
2017-05-22 18:28:06 +03:00
|
|
|
# List every other type which has this type as a member
|
|
|
|
docs.write_title('Other types containing this type', level=3)
|
2017-07-08 18:31:57 +03:00
|
|
|
other_types = sorted(
|
2018-04-28 13:21:55 +03:00
|
|
|
(u for u in tlobjects
|
|
|
|
if any(a.type == t for a in u.args) and not u.is_function),
|
|
|
|
key=lambda u: u.name
|
2017-07-08 18:31:57 +03:00
|
|
|
)
|
2017-05-22 18:28:06 +03:00
|
|
|
|
|
|
|
if not other_types:
|
|
|
|
docs.write_text(
|
|
|
|
'No other types have a member of this type.')
|
|
|
|
elif len(other_types) == 1:
|
|
|
|
docs.write_text(
|
|
|
|
'You can find this type as a member of this other type.')
|
|
|
|
else:
|
|
|
|
docs.write_text(
|
|
|
|
'You can find this type as a member of any of '
|
|
|
|
'the following %d types.' % len(other_types))
|
|
|
|
|
|
|
|
docs.begin_table(2)
|
|
|
|
for ot in other_types:
|
2019-07-17 11:11:52 +03:00
|
|
|
link = _get_path_for(ot)
|
2018-04-15 12:47:33 +03:00
|
|
|
docs.add_row(ot.class_name, link=link)
|
2017-05-22 18:28:06 +03:00
|
|
|
docs.end_table()
|
2017-04-07 21:18:01 +03:00
|
|
|
docs.end_body()
|
2017-04-07 20:08:00 +03:00
|
|
|
|
2017-09-04 18:10:04 +03:00
|
|
|
# After everything's been written, generate an index.html per folder.
|
|
|
|
# This will be done automatically and not taking into account any extra
|
2017-04-07 22:06:49 +03:00
|
|
|
# information that we have available, simply a file listing all the others
|
|
|
|
# accessible by clicking on their title
|
|
|
|
for folder in ['types', 'methods', 'constructors']:
|
2019-07-17 11:11:52 +03:00
|
|
|
_generate_index(pathlib.Path(folder), paths)
|
2017-04-07 22:06:49 +03:00
|
|
|
|
2019-07-17 11:11:52 +03:00
|
|
|
_generate_index(pathlib.Path('methods'), paths, True,
|
2018-12-21 20:18:18 +03:00
|
|
|
bot_docs_paths)
|
2018-06-11 14:36:32 +03:00
|
|
|
|
2017-04-08 14:58:42 +03:00
|
|
|
# Write the final core index, the main index for the rest of files
|
2017-04-15 14:23:17 +03:00
|
|
|
types = set()
|
|
|
|
methods = []
|
2018-04-15 11:47:55 +03:00
|
|
|
cs = []
|
2017-04-15 14:23:17 +03:00
|
|
|
for tlobject in tlobjects:
|
|
|
|
if tlobject.is_function:
|
|
|
|
methods.append(tlobject)
|
2017-07-08 14:28:23 +03:00
|
|
|
else:
|
2018-04-15 11:47:55 +03:00
|
|
|
cs.append(tlobject)
|
2017-04-15 14:23:17 +03:00
|
|
|
|
2018-04-15 13:15:43 +03:00
|
|
|
if not tlobject.result.lower() in CORE_TYPES:
|
2017-07-08 14:35:10 +03:00
|
|
|
if re.search('^vector<', tlobject.result, re.IGNORECASE):
|
2019-07-15 11:05:26 +03:00
|
|
|
types.add(tlobject.innermost_result)
|
2017-07-08 14:35:10 +03:00
|
|
|
else:
|
|
|
|
types.add(tlobject.result)
|
2017-04-15 14:23:17 +03:00
|
|
|
|
2017-04-15 14:58:21 +03:00
|
|
|
types = sorted(types)
|
|
|
|
methods = sorted(methods, key=lambda m: m.name)
|
2018-04-15 11:47:55 +03:00
|
|
|
cs = sorted(cs, key=lambda c: c.name)
|
2017-04-15 14:58:21 +03:00
|
|
|
|
2018-12-21 20:18:18 +03:00
|
|
|
shutil.copy(str(input_res / '404.html'), str(paths['404']))
|
|
|
|
_copy_replace(input_res / 'core.html', paths['index_all'], {
|
2018-04-15 12:54:34 +03:00
|
|
|
'{type_count}': len(types),
|
|
|
|
'{method_count}': len(methods),
|
|
|
|
'{constructor_count}': len(tlobjects) - len(methods),
|
|
|
|
'{layer}': layer,
|
|
|
|
})
|
|
|
|
|
2017-09-23 22:01:49 +03:00
|
|
|
def fmt(xs):
|
|
|
|
zs = {} # create a dict to hold those which have duplicated keys
|
2018-04-15 12:54:34 +03:00
|
|
|
for x in xs:
|
|
|
|
zs[x.class_name] = x.class_name in zs
|
2017-09-23 22:01:49 +03:00
|
|
|
return ', '.join(
|
2018-04-15 12:54:34 +03:00
|
|
|
'"{}.{}"'.format(x.namespace, x.class_name)
|
|
|
|
if zs[x.class_name] and x.namespace
|
|
|
|
else '"{}"'.format(x.class_name) for x in xs
|
2017-09-23 22:01:49 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
request_names = fmt(methods)
|
2018-04-15 11:47:55 +03:00
|
|
|
constructor_names = fmt(cs)
|
2017-09-23 22:01:49 +03:00
|
|
|
|
2017-09-04 18:10:04 +03:00
|
|
|
def fmt(xs, formatter):
|
2019-01-26 14:46:13 +03:00
|
|
|
return ', '.join('"{}"'.format(
|
|
|
|
formatter(x)).replace(os.path.sep, '/') for x in xs)
|
2017-09-04 18:10:04 +03:00
|
|
|
|
2018-04-15 12:54:34 +03:00
|
|
|
type_names = fmt(types, formatter=lambda x: x)
|
2018-04-15 15:59:07 +03:00
|
|
|
|
2019-07-17 11:11:52 +03:00
|
|
|
request_urls = fmt(methods, _get_path_for)
|
2018-12-21 20:18:18 +03:00
|
|
|
type_urls = fmt(types, _get_path_for_type)
|
2019-07-17 11:11:52 +03:00
|
|
|
constructor_urls = fmt(cs, _get_path_for)
|
2017-04-15 14:58:21 +03:00
|
|
|
|
2018-12-21 20:18:18 +03:00
|
|
|
paths['search.js'].parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
_copy_replace(input_res / 'js/search.js', paths['search.js'], {
|
2018-01-20 15:11:22 +03:00
|
|
|
'{request_names}': request_names,
|
|
|
|
'{type_names}': type_names,
|
|
|
|
'{constructor_names}': constructor_names,
|
|
|
|
'{request_urls}': request_urls,
|
|
|
|
'{type_urls}': type_urls,
|
|
|
|
'{constructor_urls}': constructor_urls
|
|
|
|
})
|
2017-04-08 14:58:42 +03:00
|
|
|
|
2017-04-08 15:04:32 +03:00
|
|
|
|
2019-07-17 11:11:52 +03:00
|
|
|
def _copy_resources(res_dir):
|
2018-05-23 19:50:28 +03:00
|
|
|
for dirname, files in [('css', ['docs.light.css', 'docs.dark.css']),
|
|
|
|
('img', ['arrow.svg'])]:
|
2019-07-17 11:11:52 +03:00
|
|
|
dirpath = pathlib.Path(dirname)
|
2018-12-21 15:24:16 +03:00
|
|
|
dirpath.mkdir(parents=True, exist_ok=True)
|
2018-04-14 22:15:58 +03:00
|
|
|
for file in files:
|
2018-12-21 15:24:16 +03:00
|
|
|
shutil.copy(str(res_dir / dirname / file), str(dirpath))
|
2017-04-08 15:04:32 +03:00
|
|
|
|
|
|
|
|
2019-07-17 11:11:52 +03:00
|
|
|
def _create_structure(tlobjects):
|
2018-12-24 20:16:18 +03:00
|
|
|
"""
|
2019-07-17 11:11:52 +03:00
|
|
|
Pre-create the required directory structure.
|
2018-12-24 20:16:18 +03:00
|
|
|
"""
|
|
|
|
types_ns = set()
|
|
|
|
method_ns = set()
|
|
|
|
for obj in tlobjects:
|
|
|
|
if obj.namespace:
|
|
|
|
if obj.is_function:
|
|
|
|
method_ns.add(obj.namespace)
|
|
|
|
else:
|
|
|
|
types_ns.add(obj.namespace)
|
|
|
|
|
2019-07-17 11:11:52 +03:00
|
|
|
output_dir = pathlib.Path('.')
|
2018-12-24 20:16:18 +03:00
|
|
|
type_dir = output_dir / 'types'
|
|
|
|
type_dir.mkdir(exist_ok=True)
|
|
|
|
|
|
|
|
cons_dir = output_dir / 'constructors'
|
|
|
|
cons_dir.mkdir(exist_ok=True)
|
|
|
|
for ns in types_ns:
|
|
|
|
(type_dir / ns).mkdir(exist_ok=True)
|
|
|
|
(cons_dir / ns).mkdir(exist_ok=True)
|
|
|
|
|
|
|
|
meth_dir = output_dir / 'methods'
|
|
|
|
meth_dir.mkdir(exist_ok=True)
|
|
|
|
for ns in types_ns:
|
|
|
|
(meth_dir / ns).mkdir(exist_ok=True)
|
|
|
|
|
|
|
|
|
2019-07-17 11:11:52 +03:00
|
|
|
def generate_docs(tlobjects, methods_info, layer, input_res):
|
|
|
|
_create_structure(tlobjects)
|
|
|
|
_write_html_pages(tlobjects, methods_info, layer, input_res)
|
|
|
|
_copy_resources(input_res)
|