Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
python_examples: Add auto-rendering of extra imports.
Suyash Vardhan Mathur7bbafbc881fc
diff --git a/zerver/openapi/markdown_extension.py b/zerver/openapi/markdown_extension.pyindex 0268d1cb6d5..5f2959fdd8c 100644--- a/zerver/openapi/markdown_extension.py+++ b/zerver/openapi/markdown_extension.py@@ -19,6 +19,7 @@ from markdown.preprocessors import Preprocessorimport zerver.openapi.python_examplesfrom zerver.openapi.openapi import (+ check_additional_imports,check_requires_administrator,generate_openapi_fixture,get_curl_include_exclude,@@ -135,9 +136,18 @@ def render_python_code_example(function_source_lines = inspect.getsourcelines(method)[0]if admin_config:- config = PYTHON_CLIENT_ADMIN_CONFIG.splitlines()+ config_string = PYTHON_CLIENT_ADMIN_CONFIGelse:- config = PYTHON_CLIENT_CONFIG.splitlines()+ config_string = PYTHON_CLIENT_CONFIG++ endpoint, endpoint_method = function.split(":")+ extra_imports = check_additional_imports(endpoint, endpoint_method)+ if extra_imports:+ extra_imports = sorted(extra_imports + ["zulip"])+ extra_imports = [f"import {each_import}" for each_import in extra_imports]+ config_string = config_string.replace("import zulip", "\n".join(extra_imports))++ config = config_string.splitlines()snippets = extract_code_example(function_source_lines, [], PYTHON_EXAMPLE_REGEX)diff --git a/zerver/openapi/openapi.py b/zerver/openapi/openapi.pyindex f6623e8d8ff..d9fab253b3a 100644--- a/zerver/openapi/openapi.py+++ b/zerver/openapi/openapi.py@@ -233,6 +233,13 @@ def check_requires_administrator(endpoint: str, method: str) -> bool:)+def check_additional_imports(endpoint: str, method: str) -> Optional[List[str]]:+ """Fetch the additional imports required for an endpoint."""+ return openapi_spec.openapi()["paths"][endpoint][method.lower()].get(+ "x-python-examples-extra-imports", None+ )++def get_responses_description(endpoint: str, method: str) -> str:"""Fetch responses description of an endpoint."""return openapi_spec.openapi()["paths"][endpoint][method.lower()].get(diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yamlindex a9857ec8a02..c7b1cc24f56 100644--- a/zerver/openapi/zulip.yaml+++ b/zerver/openapi/zulip.yaml@@ -165,6 +165,7 @@ paths:`{{ api_url}}/v1/register` endpoint, you could pass the parameters for[the `{{ api_url}}/v1/register` endpoint](/api/register-queue) to thisendpoint and a queue would be registered in the absence of a `queue_id`.+ x-python-examples-extra-imports: ["sys"]parameters:- $ref: "#/components/parameters/QueueId"- name: last_event_id