Telethon/telethon_generator/generators/docs.py

657 lines
26 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import functools
2017-04-07 19:30:54 +03:00
import os
import re
import shutil
from collections import defaultdict
2017-04-07 19:30:54 +03:00
2018-06-18 22:02:42 +03:00
from ..docswriter import DocsWriter
from ..parsers import TLObject
2018-04-15 12:47:33 +03:00
from ..utils import snake_to_camel_case
2017-04-07 19:30:54 +03:00
CORE_TYPES = {
'int', 'long', 'int128', 'int256', 'double',
'vector', 'string', 'bool', 'true', 'bytes', 'date'
}
2017-04-07 19:30:54 +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()
return '{}.html'.format(result)
2017-04-07 19:30:54 +03:00
def get_import_code(tlobject):
"""``TLObject -> from ... import ...``."""
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)
def _get_create_path_for(root, tlobject, make=True):
"""Creates and returns the path for the given TLObject at root."""
out_dir = 'methods' if tlobject.is_function else 'constructors'
2017-04-07 19:30:54 +03:00
if tlobject.namespace:
out_dir = os.path.join(out_dir, tlobject.namespace)
out_dir = os.path.join(root, out_dir)
if make:
os.makedirs(out_dir, exist_ok=True)
return os.path.join(out_dir, _get_file_name(tlobject))
2018-04-15 15:59:07 +03:00
def _get_path_for_type(root, type_, relative_to='.'):
"""Similar to `_get_create_path_for` but for only type names."""
if type_.lower() in CORE_TYPES:
2017-05-21 14:59:16 +03:00
path = 'index.html#%s' % type_.lower()
elif '.' in type_:
namespace, name = type_.split('.')
path = 'types/%s/%s' % (namespace, _get_file_name(name))
2017-04-07 19:30:54 +03:00
else:
path = 'types/%s' % _get_file_name(type_)
2017-04-07 19:30:54 +03:00
return _get_relative_path(os.path.join(root, path), relative_to)
2017-04-07 19:30:54 +03:00
def _get_relative_path(destination, relative_to, folder=False):
"""Return the relative path to destination from relative_to."""
if not folder:
2017-04-07 19:30:54 +03:00
relative_to = os.path.dirname(relative_to)
return os.path.relpath(destination, start=relative_to)
def _find_title(html_file):
"""Finds the <title> for the given HTML file, or (Unknown)."""
with open(html_file, 'r') as fp:
for line in fp:
if '<title>' in line:
# + 7 to skip len('<title>')
return line[line.index('<title>') + 7:line.index('</title>')]
return '(Unknown)'
def _build_menu(docs, filename, root, relative_main_index):
"""Builds the menu using the given DocumentWriter up to 'filename',
which must be a file (it cannot be a directory)"""
filename = _get_relative_path(filename, root)
docs.add_menu('API', relative_main_index)
items = filename.split('/')
for i in range(len(items) - 1):
item = items[i]
link = '../' * (len(items) - (i + 2))
link += 'index.html'
docs.add_menu(item.title(), link=link)
if items[-1] != 'index.html':
docs.add_menu(os.path.splitext(items[-1])[0])
docs.end_menu()
def _generate_index(folder, original_paths, root,
bots_index=False, bots_index_paths=()):
"""Generates the index file for the specified folder"""
2017-05-21 14:59:16 +03:00
# Determine the namespaces listed here (as sub folders)
# and the files (.html files) that we should link to
namespaces = []
files = []
INDEX = 'index.html'
BOT_INDEX = 'botindex.html'
if not bots_index:
for item in os.listdir(folder):
if os.path.isdir(os.path.join(folder, item)):
namespaces.append(item)
elif item not in (INDEX, BOT_INDEX):
files.append(item)
else:
# bots_index_paths should be a list of "namespace/method.html"
# or "method.html"
for item in bots_index_paths:
dirname = os.path.dirname(item)
if dirname and dirname not in namespaces:
namespaces.append(dirname)
elif not dirname and item not in (INDEX, BOT_INDEX):
files.append(item)
paths = {k: _get_relative_path(v, folder, folder=True)
for k, v in original_paths.items()}
# Now that everything is setup, write the index.html file
filename = os.path.join(folder, BOT_INDEX if bots_index else INDEX)
2018-04-15 15:59:07 +03:00
with DocsWriter(filename, type_to_path=_get_path_for_type) as docs:
# Title should be the current folder name
2018-05-23 19:50:28 +03:00
docs.write_head(folder.title(),
relative_css_path=paths['css'],
default_css=original_paths['default_css'])
docs.set_menu_separator(paths['arrow'])
_build_menu(docs, filename, root,
relative_main_index=paths['index_all'])
docs.write_title(_get_relative_path(folder, root, folder=True).title())
if bots_index:
docs.write_text('These are the methods that you can use as a bot. '
'Click <a href="{}">here</a> to '
'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))
if namespaces:
docs.write_title('Namespaces', level=3)
docs.begin_table(4)
namespaces.sort()
for namespace in namespaces:
# For every namespace, also write the index of it
namespace_paths = []
if bots_index:
for item in bots_index_paths:
if os.path.dirname(item) == namespace:
namespace_paths.append(os.path.basename(item))
_generate_index(os.path.join(folder, namespace),
original_paths, root,
bots_index, namespace_paths)
if bots_index:
docs.add_row(namespace.title(),
2018-06-11 14:43:08 +03:00
link=os.path.join(namespace, BOT_INDEX))
else:
docs.add_row(namespace.title(),
link=os.path.join(namespace, INDEX))
docs.end_table()
docs.write_title('Available items')
docs.begin_table(2)
files = [(f, _find_title(os.path.join(folder, f))) for f in files]
files.sort(key=lambda t: t[1])
for file, title in files:
docs.add_row(title, link=file)
docs.end_table()
docs.end_body()
def _get_description(arg):
"""Generates a proper description for the given argument."""
2017-09-04 18:10:04 +03:00
desc = []
otherwise = False
2017-09-04 18:10:04 +03:00
if arg.can_be_inferred:
desc.append('If left unspecified, it will be inferred automatically.')
otherwise = True
elif arg.is_flag:
2018-07-25 03:21:44 +03:00
desc.append('This argument defaults to '
'<code>None</code> and can be omitted.')
otherwise = True
if arg.type in {'InputPeer', 'InputUser', 'InputChannel',
'InputNotifyPeer', 'InputDialogPeer'}:
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:
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.')
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
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
def _copy_replace(src, dst, replacements):
"""Copies the src file into dst applying the replacements dict"""
with open(src, 'r') as infile, open(dst, 'w') as outfile:
outfile.write(re.sub(
'|'.join(re.escape(k) for k in replacements),
lambda m: str(replacements[m.group(0)]),
infile.read()
))
def _write_html_pages(tlobjects, errors, layer, input_res, output_dir):
"""
Generates the documentation HTML files from from ``scheme.tl``
to ``/methods`` and ``/constructors``, etc.
2017-09-04 18:10:04 +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.
2017-04-07 19:30:54 +03:00
original_paths = {
2018-05-23 19:50:28 +03:00
'css': 'css',
2017-04-07 19:30:54 +03:00
'arrow': 'img/arrow.svg',
'search.js': 'js/search.js',
'404': '404.html',
'index_all': 'index.html',
'bot_index': 'botindex.html',
2017-04-07 19:30:54 +03:00
'index_types': 'types/index.html',
'index_methods': 'methods/index.html',
2017-04-07 19:30:54 +03:00
'index_constructors': 'constructors/index.html'
}
original_paths = {k: os.path.join(output_dir, v)
for k, v in original_paths.items()}
2018-05-24 12:48:15 +03:00
original_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)
for tlobject in tlobjects:
d = type_to_functions if tlobject.is_function else type_to_constructors
2018-06-18 19:41:06 +03:00
d[tlobject.result].append(tlobject)
for t, cs in type_to_constructors.items():
type_to_constructors[t] = list(sorted(cs, key=lambda c: c.name))
# Telegram may send errors with the same str_code but different int_code.
# They are all imported on telethon.errors anyway so makes no difference.
errors = list(sorted({e.str_code: e for e in errors}.values(),
key=lambda e: e.name))
method_causes_errors = defaultdict(list)
for error in errors:
for method in error.caused_by:
method_causes_errors[method].append(error)
# Since the output directory is needed everywhere partially apply it now
create_path_for = functools.partial(_get_create_path_for, output_dir)
2018-04-15 15:59:07 +03:00
path_for_type = functools.partial(_get_path_for_type, output_dir)
bot_docs_paths = []
2017-04-07 19:30:54 +03:00
for tlobject in tlobjects:
filename = create_path_for(tlobject)
paths = {k: _get_relative_path(v, filename)
for k, v in original_paths.items()}
2017-04-07 19:30:54 +03:00
with DocsWriter(filename, type_to_path=path_for_type) as docs:
2018-04-15 12:47:33 +03:00
docs.write_head(title=tlobject.class_name,
2018-05-23 19:50:28 +03:00
relative_css_path=paths['css'],
default_css=original_paths['default_css'])
2017-04-07 19:30:54 +03:00
# Create the menu (path to the current TLObject)
docs.set_menu_separator(paths['arrow'])
_build_menu(docs, filename, output_dir,
relative_main_index=paths['index_all'])
2017-04-07 19:30:54 +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-10-16 10:29:48 +03:00
docs.write_text('Bots <strong>can{}</strong> use this method. '
'<a href="#examples">See code examples.</a>'
2018-06-06 18:35:06 +03:00
.format("" if tlobject.bot_usable else "'t"))
if tlobject.is_function and tlobject.bot_usable:
bot_docs_paths.append(filename)
2018-06-06 18:35:06 +03:00
# Write the code definition for this TLObject
docs.write_code(tlobject)
docs.write_copy_button('Copy import to the clipboard',
get_import_code(tlobject))
2017-04-07 19:30:54 +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.')
_, inner = tlobject.result.split('<')
inner = inner.strip('>')
else:
inner = tlobject.result
docs.begin_table(column_count=1)
docs.add_row(inner, link=path_for_type(
2017-09-04 18:10:04 +03:00
inner, relative_to=filename
))
docs.end_table()
cs = type_to_constructors.get(inner, [])
if not cs:
docs.write_text('This type has no instances available.')
elif len(cs) == 1:
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)
for constructor in cs:
link = create_path_for(constructor)
link = _get_relative_path(link, relative_to=filename)
2018-04-15 12:47:33 +03:00
docs.add_row(constructor.class_name, link=link)
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)
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
docs.begin_table(column_count=3)
2017-04-07 19:30:54 +03:00
for arg in args:
# Name row
docs.add_row(arg.name,
bold=True)
2017-04-07 19:30:54 +03:00
# Type row
2018-07-25 03:21:44 +03:00
friendly_type = 'flag' if arg.type == 'true' else arg.type
if arg.is_generic:
2018-07-25 03:21:44 +03:00
docs.add_row('!' + friendly_type, align='center')
else:
2017-09-04 18:10:04 +03:00
docs.add_row(
2018-07-25 03:21:44 +03:00
friendly_type, align='center', link=
path_for_type(arg.type, relative_to=filename)
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
docs.add_row(_get_description(arg))
2017-04-07 19:30:54 +03:00
docs.end_table()
2017-04-07 19:30:54 +03:00
else:
if tlobject.is_function:
docs.write_text('This request takes no input parameters.')
2017-04-07 19:30:54 +03:00
else:
docs.write_text('This type has no members.')
2017-04-07 19:30:54 +03:00
if tlobject.is_function:
docs.write_title('Known RPC errors')
errors = method_causes_errors[tlobject.fullname]
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')
docs.write(
'<pre>from telethon.sync import TelegramClient\n'
'from telethon import functions, types\n'
'\n'
'with TelegramClient(name, api_id, api_hash) as client:\n'
' result = client(')
tlobject.as_example(docs, indent=1)
docs.write(')\n')
if tlobject.result.startswith('Vector'):
docs.write(
' for x in result:\n'
' print(x'
)
else:
docs.write(' print(result')
if tlobject.result != 'Bool' \
and not tlobject.result.startswith('Vector'):
docs.write('.stringify()')
docs.write(')</pre>')
depth = '../' * (2 if tlobject.namespace else 1)
docs.add_script(src='prependPath = "{}";'.format(depth))
docs.add_script(relative_src=paths['search.js'])
docs.end_body()
2017-04-07 19:30:54 +03:00
temp = []
for item in bot_docs_paths:
temp.append(os.path.sep.join(item.split(os.path.sep)[2:]))
bot_docs_paths = temp
# 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
for t, cs in type_to_constructors.items():
filename = path_for_type(t)
out_dir = os.path.dirname(filename)
if out_dir:
os.makedirs(out_dir, exist_ok=True)
2017-09-04 18:10:04 +03:00
# Since we don't have access to the full TLObject, split the type
if '.' in t:
namespace, name = t.split('.')
else:
namespace, name = None, t
paths = {k: _get_relative_path(v, out_dir, folder=True)
for k, v in original_paths.items()}
with DocsWriter(filename, type_to_path=path_for_type) as docs:
2018-05-23 19:50:28 +03:00
docs.write_head(title=snake_to_camel_case(name),
relative_css_path=paths['css'],
default_css=original_paths['default_css'])
docs.set_menu_separator(paths['arrow'])
_build_menu(docs, filename, output_dir,
relative_main_index=paths['index_all'])
# Main file title
2018-04-15 12:47:33 +03:00
docs.write_title(snake_to_camel_case(name))
# List available constructors for this type
docs.write_title('Available constructors', level=3)
if not cs:
docs.write_text('This type has no constructors available.')
elif len(cs) == 1:
docs.write_text('This type has one constructor available.')
else:
2017-09-04 18:10:04 +03:00
docs.write_text('This type has %d constructors available.' %
len(cs))
2017-04-08 13:26:48 +03:00
docs.begin_table(2)
for constructor in cs:
# Constructor full name
link = create_path_for(constructor)
link = _get_relative_path(link, relative_to=filename)
2018-04-15 12:47:33 +03:00
docs.add_row(constructor.class_name, link=link)
docs.end_table()
# List all the methods which return this type
docs.write_title('Methods returning this type', level=3)
functions = type_to_functions.get(t, [])
if not functions:
docs.write_text('No method returns this type.')
elif len(functions) == 1:
docs.write_text('Only the following method returns this type.')
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-08 13:26:48 +03:00
docs.begin_table(2)
2017-05-21 14:59:16 +03:00
for func in functions:
link = create_path_for(func)
link = _get_relative_path(link, relative_to=filename)
2018-04-15 12:47:33 +03:00
docs.add_row(func.class_name, link=link)
docs.end_table()
# List all the methods which take this type as input
docs.write_title('Methods accepting this type as input', level=3)
other_methods = sorted(
(u for u in tlobjects
if any(a.type == t for a in u.args) and u.is_function),
key=lambda u: u.name
)
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:
link = create_path_for(ot)
link = _get_relative_path(link, relative_to=filename)
2018-04-15 12:47:33 +03:00
docs.add_row(ot.class_name, link=link)
docs.end_table()
# List every other type which has this type as a member
docs.write_title('Other types containing this type', level=3)
other_types = sorted(
(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
)
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:
link = create_path_for(ot)
link = _get_relative_path(link, relative_to=filename)
2018-04-15 12:47:33 +03:00
docs.add_row(ot.class_name, link=link)
docs.end_table()
docs.end_body()
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
# information that we have available, simply a file listing all the others
# accessible by clicking on their title
for folder in ['types', 'methods', 'constructors']:
_generate_index(os.path.join(output_dir, folder), original_paths,
output_dir)
_generate_index(os.path.join(output_dir, 'methods'), original_paths,
output_dir, True, bot_docs_paths)
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 = []
cs = []
2017-04-15 14:23:17 +03:00
for tlobject in tlobjects:
if tlobject.is_function:
methods.append(tlobject)
else:
cs.append(tlobject)
2017-04-15 14:23:17 +03:00
if not tlobject.result.lower() in CORE_TYPES:
if re.search('^vector<', tlobject.result, re.IGNORECASE):
types.add(tlobject.result.split('<')[1].strip('>'))
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)
cs = sorted(cs, key=lambda c: c.name)
2017-04-15 14:58:21 +03:00
shutil.copy(os.path.join(input_res, '404.html'), original_paths['404'])
_copy_replace(os.path.join(input_res, 'core.html'),
original_paths['index_all'], {
'{type_count}': len(types),
'{method_count}': len(methods),
'{constructor_count}': len(tlobjects) - len(methods),
'{layer}': layer,
})
def fmt(xs):
zs = {} # create a dict to hold those which have duplicated keys
for x in xs:
zs[x.class_name] = x.class_name in zs
return ', '.join(
'"{}.{}"'.format(x.namespace, x.class_name)
if zs[x.class_name] and x.namespace
else '"{}"'.format(x.class_name) for x in xs
)
request_names = fmt(methods)
constructor_names = fmt(cs)
2017-09-04 18:10:04 +03:00
def fmt(xs, formatter):
return ', '.join('"{}"'.format(formatter(x)) for x in xs)
type_names = fmt(types, formatter=lambda x: x)
2018-04-15 15:59:07 +03:00
# Local URLs shouldn't rely on the output's root, so set empty root
create_path_for = functools.partial(_get_create_path_for, '', make=False)
2018-04-15 15:59:07 +03:00
path_for_type = functools.partial(_get_path_for_type, '')
request_urls = fmt(methods, create_path_for)
type_urls = fmt(types, path_for_type)
constructor_urls = fmt(cs, create_path_for)
2017-04-15 14:58:21 +03:00
os.makedirs(os.path.abspath(os.path.join(
original_paths['search.js'], os.path.pardir
)), exist_ok=True)
_copy_replace(os.path.join(input_res, 'js', 'search.js'),
original_paths['search.js'], {
'{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
def _copy_resources(res_dir, out_dir):
2018-05-23 19:50:28 +03:00
for dirname, files in [('css', ['docs.light.css', 'docs.dark.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 generate_docs(tlobjects, errors, layer, input_res, output_dir):
os.makedirs(output_dir, exist_ok=True)
_write_html_pages(tlobjects, errors, layer, input_res, output_dir)
_copy_resources(input_res, output_dir)