python: Normalize quotes with Black.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2021-02-11 23:20:45 -08:00
committed by Tim Abbott
parent 11741543da
commit 6e4c3e41dc
989 changed files with 32792 additions and 32792 deletions

View File

@@ -10,15 +10,15 @@ from markdown.preprocessors import Preprocessor
from zerver.openapi.openapi import get_openapi_parameters, likely_deprecated_parameter
REGEXP = re.compile(r'\{generate_api_arguments_table\|\s*(.+?)\s*\|\s*(.+)\s*\}')
REGEXP = re.compile(r"\{generate_api_arguments_table\|\s*(.+?)\s*\|\s*(.+)\s*\}")
class MarkdownArgumentsTableGenerator(Extension):
def __init__(self, configs: Mapping[str, Any] = {}) -> None:
self.config = {
'base_path': [
'.',
'Default location from which to evaluate relative paths for the JSON files.',
"base_path": [
".",
"Default location from which to evaluate relative paths for the JSON files.",
],
}
for key, value in configs.items():
@@ -26,14 +26,14 @@ class MarkdownArgumentsTableGenerator(Extension):
def extendMarkdown(self, md: markdown.Markdown) -> None:
md.preprocessors.register(
APIArgumentsTablePreprocessor(md, self.getConfigs()), 'generate_api_arguments', 505
APIArgumentsTablePreprocessor(md, self.getConfigs()), "generate_api_arguments", 505
)
class APIArgumentsTablePreprocessor(Preprocessor):
def __init__(self, md: markdown.Markdown, config: Mapping[str, Any]) -> None:
super().__init__(md)
self.base_path = config['base_path']
self.base_path = config["base_path"]
def run(self, lines: List[str]) -> List[str]:
done = False
@@ -49,14 +49,14 @@ class APIArgumentsTablePreprocessor(Preprocessor):
doc_name = match.group(2)
filename = os.path.expanduser(filename)
is_openapi_format = filename.endswith('.yaml')
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)
endpoint, method = doc_name.rsplit(":", 1)
arguments: List[Dict[str, Any]] = []
try:
@@ -65,7 +65,7 @@ class APIArgumentsTablePreprocessor(Preprocessor):
# 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',):
if e.args != ("parameters",):
raise e
else:
with open(filename) as fp:
@@ -75,7 +75,7 @@ class APIArgumentsTablePreprocessor(Preprocessor):
if arguments:
text = self.render_table(arguments)
else:
text = ['This endpoint does not accept any parameters.']
text = ["This endpoint does not accept any parameters."]
# 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
@@ -104,52 +104,52 @@ class APIArgumentsTablePreprocessor(Preprocessor):
</div>"""
md_engine = markdown.Markdown(extensions=[])
arguments = sorted(arguments, key=lambda argument: 'deprecated' in argument)
arguments = sorted(arguments, key=lambda argument: "deprecated" in argument)
for argument in arguments:
description = argument['description']
oneof = ['`' + str(item) + '`' for item in argument.get('schema', {}).get('enum', [])]
description = argument["description"]
oneof = ["`" + str(item) + "`" for item in argument.get("schema", {}).get("enum", [])]
if oneof:
description += '\nMust be one of: {}.'.format(', '.join(oneof))
description += "\nMust be one of: {}.".format(", ".join(oneof))
default = argument.get('schema', {}).get('default')
default = argument.get("schema", {}).get("default")
if default is not None:
description += f'\nDefaults to `{json.dumps(default)}`.'
description += f"\nDefaults to `{json.dumps(default)}`."
data_type = ""
if 'schema' in argument:
data_type = generate_data_type(argument['schema'])
if "schema" in argument:
data_type = generate_data_type(argument["schema"])
else:
data_type = generate_data_type(argument['content']['application/json']['schema'])
data_type = generate_data_type(argument["content"]["application/json"]["schema"])
# TODO: OpenAPI allows indicating where the argument goes
# (path, querystring, form data...). We should document this detail.
example = ""
if 'example' in argument:
example = argument['example']
if "example" in argument:
example = argument["example"]
else:
example = json.dumps(argument['content']['application/json']['example'])
example = json.dumps(argument["content"]["application/json"]["example"])
required_string: str = "required"
if argument.get('in', '') == 'path':
if argument.get("in", "") == "path":
# Any path variable is required
assert argument['required']
required_string = 'required in path'
assert argument["required"]
required_string = "required in path"
if argument.get('required', False):
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>'
# Test to make sure deprecated parameters are marked so.
if likely_deprecated_parameter(description):
assert argument['deprecated']
if argument.get('deprecated', False):
assert argument["deprecated"]
if argument.get("deprecated", False):
deprecated_block = '<span class="api-argument-deprecated">Deprecated</span>'
else:
deprecated_block = ''
deprecated_block = ""
table.append(
argument_template.format(
argument=argument.get('argument') or argument.get('name'),
argument=argument.get("argument") or argument.get("name"),
example=escape_html(example),
required=required_block,
deprecated=deprecated_block,
@@ -167,12 +167,12 @@ def makeExtension(*args: Any, **kwargs: str) -> MarkdownArgumentsTableGenerator:
def generate_data_type(schema: Mapping[str, Any]) -> str:
data_type = ""
if 'oneOf' in schema:
for item in schema['oneOf']:
if "oneOf" in schema:
for item in schema["oneOf"]:
data_type = data_type + generate_data_type(item) + " | "
data_type = data_type[:-3]
elif 'items' in schema:
data_type = "(" + generate_data_type(schema['items']) + ")[]"
elif "items" in schema:
data_type = "(" + generate_data_type(schema["items"]) + ")[]"
else:
data_type = schema['type']
data_type = schema["type"]
return data_type