2020-03-27 05:03:26 +01:00
|
|
|
import json
|
2020-06-11 00:54:34 +02:00
|
|
|
import os
|
|
|
|
import re
|
2020-06-11 21:35:13 +02:00
|
|
|
from typing import Any, Dict, List, Mapping, Sequence
|
2017-12-27 08:57:47 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
import markdown
|
2018-08-14 02:50:05 +02:00
|
|
|
from django.utils.html import escape as escape_html
|
2017-12-27 08:57:47 +01:00
|
|
|
from markdown.extensions import Extension
|
|
|
|
from markdown.preprocessors import Preprocessor
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2020-06-26 16:18:27 +02:00
|
|
|
from zerver.openapi.openapi import get_openapi_parameters, likely_deprecated_parameter
|
2017-12-27 08:57:47 +01:00
|
|
|
|
2018-05-15 19:28:42 +02:00
|
|
|
REGEXP = re.compile(r'\{generate_api_arguments_table\|\s*(.+?)\s*\|\s*(.+)\s*\}')
|
2017-12-27 08:57:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
class MarkdownArgumentsTableGenerator(Extension):
|
2020-06-13 03:34:01 +02:00
|
|
|
def __init__(self, configs: Mapping[str, Any] = {}) -> None:
|
2017-12-27 08:57:47 +01:00
|
|
|
self.config = {
|
|
|
|
'base_path': ['.', 'Default location from which to evaluate relative paths for the JSON files.'],
|
|
|
|
}
|
|
|
|
for key, value in configs.items():
|
|
|
|
self.setConfig(key, value)
|
|
|
|
|
|
|
|
def extendMarkdown(self, md: markdown.Markdown, md_globals: Dict[str, Any]) -> None:
|
|
|
|
md.preprocessors.add(
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
'generate_api_arguments', APIArgumentsTablePreprocessor(md, self.getConfigs()), '_begin',
|
2017-12-27 08:57:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class APIArgumentsTablePreprocessor(Preprocessor):
|
|
|
|
def __init__(self, md: markdown.Markdown, config: Dict[str, Any]) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
super().__init__(md)
|
2017-12-27 08:57:47 +01:00
|
|
|
self.base_path = config['base_path']
|
|
|
|
|
|
|
|
def run(self, lines: List[str]) -> List[str]:
|
|
|
|
done = False
|
|
|
|
while not done:
|
|
|
|
for line in lines:
|
|
|
|
loc = lines.index(line)
|
|
|
|
match = REGEXP.search(line)
|
|
|
|
|
2018-07-13 14:09:03 +02:00
|
|
|
if not match:
|
|
|
|
continue
|
|
|
|
|
|
|
|
filename = match.group(1)
|
|
|
|
doc_name = match.group(2)
|
|
|
|
filename = os.path.expanduser(filename)
|
|
|
|
|
|
|
|
is_openapi_format = filename.endswith('.yaml')
|
|
|
|
|
|
|
|
if not os.path.isabs(filename):
|
|
|
|
parent_dir = self.base_path
|
|
|
|
filename = os.path.normpath(os.path.join(parent_dir, filename))
|
|
|
|
|
|
|
|
if is_openapi_format:
|
|
|
|
endpoint, method = doc_name.rsplit(':', 1)
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
arguments: List[Dict[str, Any]] = []
|
2018-07-13 14:09:03 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
arguments = get_openapi_parameters(endpoint, method)
|
|
|
|
except KeyError as e:
|
|
|
|
# Don't raise an exception if the "parameters"
|
|
|
|
# field is missing; we assume that's because the
|
|
|
|
# endpoint doesn't accept any parameters
|
|
|
|
if e.args != ('parameters',):
|
|
|
|
raise e
|
|
|
|
else:
|
2020-04-09 21:51:58 +02:00
|
|
|
with open(filename) as fp:
|
2020-03-27 05:03:26 +01:00
|
|
|
json_obj = json.load(fp)
|
2018-07-13 14:09:03 +02:00
|
|
|
arguments = json_obj[doc_name]
|
|
|
|
|
|
|
|
if arguments:
|
|
|
|
text = self.render_table(arguments)
|
|
|
|
else:
|
2020-06-18 03:20:14 +02:00
|
|
|
text = ['This endpoint does not accept any parameters.']
|
2018-07-13 14:09:03 +02:00
|
|
|
# The line that contains the directive to include the macro
|
|
|
|
# may be preceded or followed by text or tags, in that case
|
|
|
|
# we need to make sure that any preceding or following text
|
|
|
|
# stays the same.
|
|
|
|
line_split = REGEXP.split(line, maxsplit=0)
|
|
|
|
preceding = line_split[0]
|
|
|
|
following = line_split[-1]
|
2020-09-02 06:59:07 +02:00
|
|
|
text = [preceding, *text, following]
|
2018-07-13 14:09:03 +02:00
|
|
|
lines = lines[:loc] + text + lines[loc+1:]
|
|
|
|
break
|
2017-12-27 08:57:47 +01:00
|
|
|
else:
|
|
|
|
done = True
|
|
|
|
return lines
|
|
|
|
|
2020-06-11 21:35:13 +02:00
|
|
|
def render_table(self, arguments: Sequence[Mapping[str, Any]]) -> List[str]:
|
2020-03-27 05:03:26 +01:00
|
|
|
# TODO: Fix naming now that this no longer renders a table.
|
2017-12-27 08:57:47 +01:00
|
|
|
table = []
|
2020-03-27 05:03:26 +01:00
|
|
|
argument_template = """
|
2020-09-21 19:06:40 +02:00
|
|
|
<div class="api-argument" id="parameter-{argument}">
|
|
|
|
<p class="api-argument-name"><strong>{argument}</strong> {required} {deprecated} <a href="#parameter-{argument}" class="api-argument-hover-link"><i class="fa fa-chain"></i></a></p>
|
2020-03-27 05:03:26 +01:00
|
|
|
<div class="api-example">
|
|
|
|
<span class="api-argument-example-label">Example</span>: <code>{example}</code>
|
|
|
|
</div>
|
|
|
|
<div class="api-description">{description}</div>
|
2020-04-19 12:52:45 +02:00
|
|
|
<hr>
|
2020-03-27 05:03:26 +01:00
|
|
|
</div>"""
|
2017-12-27 08:57:47 +01:00
|
|
|
|
|
|
|
md_engine = markdown.Markdown(extensions=[])
|
2020-06-26 16:18:27 +02:00
|
|
|
arguments = sorted(arguments, key=lambda argument: 'deprecated' in argument)
|
2017-12-27 08:57:47 +01:00
|
|
|
for argument in arguments:
|
2018-07-26 20:48:34 +02:00
|
|
|
description = argument['description']
|
2019-10-07 10:18:18 +02:00
|
|
|
oneof = ['`' + str(item) + '`'
|
2018-05-15 19:28:42 +02:00
|
|
|
for item in argument.get('schema', {}).get('enum', [])]
|
|
|
|
if oneof:
|
|
|
|
description += '\nMust be one of: {}.'.format(', '.join(oneof))
|
2018-07-26 20:48:34 +02:00
|
|
|
|
|
|
|
default = argument.get('schema', {}).get('default')
|
|
|
|
if default is not None:
|
2020-06-09 00:25:09 +02:00
|
|
|
description += f'\nDefaults to `{json.dumps(default)}`.'
|
2018-07-26 20:48:34 +02:00
|
|
|
|
2020-03-27 05:03:26 +01:00
|
|
|
# TODO: OpenAPI allows indicating where the argument goes
|
|
|
|
# (path, querystring, form data...). We should document this detail.
|
2020-05-11 16:26:33 +02:00
|
|
|
example = ""
|
|
|
|
if 'example' in argument:
|
|
|
|
example = argument['example']
|
|
|
|
else:
|
|
|
|
example = json.dumps(argument['content']['application/json']['example'])
|
|
|
|
|
2020-06-11 21:35:13 +02:00
|
|
|
required_string: str = "required"
|
|
|
|
if argument.get('in', '') == 'path':
|
|
|
|
# Any path variable is required
|
|
|
|
assert argument['required']
|
|
|
|
required_string = 'required in path'
|
|
|
|
|
|
|
|
if argument.get('required', False):
|
|
|
|
required_block = f'<span class="api-argument-required">{required_string}</span>'
|
|
|
|
else:
|
|
|
|
required_block = '<span class="api-argument-optional">optional</span>'
|
|
|
|
|
2020-06-26 16:18:27 +02:00
|
|
|
# Test to make sure deprecated parameters are marked so.
|
|
|
|
if likely_deprecated_parameter(description):
|
|
|
|
assert(argument['deprecated'])
|
|
|
|
if argument.get('deprecated', False):
|
|
|
|
deprecated_block = '<span class="api-argument-deprecated">Deprecated</span>'
|
|
|
|
else:
|
|
|
|
deprecated_block = ''
|
|
|
|
|
2020-03-27 05:03:26 +01:00
|
|
|
table.append(argument_template.format(
|
2018-05-15 19:28:42 +02:00
|
|
|
argument=argument.get('argument') or argument.get('name'),
|
2020-05-11 16:26:33 +02:00
|
|
|
example=escape_html(example),
|
2020-06-11 21:35:13 +02:00
|
|
|
required=required_block,
|
2020-06-26 16:18:27 +02:00
|
|
|
deprecated=deprecated_block,
|
2018-05-15 19:28:42 +02:00
|
|
|
description=md_engine.convert(description),
|
2017-12-27 08:57:47 +01:00
|
|
|
))
|
|
|
|
|
|
|
|
return table
|
|
|
|
|
|
|
|
def makeExtension(*args: Any, **kwargs: str) -> MarkdownArgumentsTableGenerator:
|
|
|
|
return MarkdownArgumentsTableGenerator(kwargs)
|