From e5192ba48b45576e636e7dce82ad9183051443ed Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sat, 25 Apr 2020 01:25:00 +0900 Subject: [PATCH] refactor: AutosummaryRender --- CHANGES | 3 +++ doc/extdev/deprecated.rst | 11 +++++++++++ sphinx/ext/autosummary/generate.py | 26 ++++++++++++++++++-------- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/CHANGES b/CHANGES index bebfc5500..183294fb7 100644 --- a/CHANGES +++ b/CHANGES @@ -14,6 +14,8 @@ Deprecated * The first argument for sphinx.ext.autosummary.generate.AutosummaryRenderer has been changed to Sphinx object +* ``sphinx.ext.autosummary.generate.AutosummaryRenderer`` takes an object type + as an argument * The ``template_dir`` argument of ``sphinx.ext.autosummary.generate. AutosummaryRenderer`` * The ``module`` argument of ``sphinx.ext.autosummary.generate. @@ -22,6 +24,7 @@ Deprecated generate_autosummary_docs()`` * The ``template_dir`` argument of ``sphinx.ext.autosummary.generate. generate_autosummary_docs()`` +* ``sphinx.ext.autosummary.generate.AutosummaryRenderer.exists()`` Features added -------------- diff --git a/doc/extdev/deprecated.rst b/doc/extdev/deprecated.rst index d39b00b37..35c7ec96a 100644 --- a/doc/extdev/deprecated.rst +++ b/doc/extdev/deprecated.rst @@ -33,6 +33,12 @@ The following is a list of deprecated interfaces. - 5.0 - N/A + * - ``sphinx.ext.autosummary.generate.AutosummaryRenderer`` takes an object + type as an argument + - 3.1 + - 5.0 + - N/A + * - The ``template_dir`` argument of ``sphinx.ext.autosummary.generate.AutosummaryRenderer`` - 3.1 @@ -57,6 +63,11 @@ The following is a list of deprecated interfaces. - 5.0 - N/A + * - ``sphinx.ext.autosummary.generate.AutosummaryRenderer.exists()`` + - 3.1 + - 5.0 + - N/A + * - ``desc_signature['first']`` - - 3.0 diff --git a/sphinx/ext/autosummary/generate.py b/sphinx/ext/autosummary/generate.py index df7ad4b1d..ad47439b5 100644 --- a/sphinx/ext/autosummary/generate.py +++ b/sphinx/ext/autosummary/generate.py @@ -147,15 +147,30 @@ class AutosummaryRenderer: def exists(self, template_name: str) -> bool: """Check if template file exists.""" + warnings.warn('AutosummaryRenderer.exists() is deprecated.', + RemovedInSphinx50Warning, stacklevel=2) try: self.env.get_template(template_name) return True except TemplateNotFound: return False - def render(self, template_name: str, context: Dict) -> str: + def render(self, objtype: str, context: Dict) -> str: """Render a template file.""" - return self.env.get_template(template_name).render(context) + if objtype.endswith('.rst'): + # old styled: template_name is given + warnings.warn('AutosummaryRenderer.render() takes an object type as an argument.', + RemovedInSphinx50Warning, stacklevel=2) + return self.env.get_template(objtype).render(context) + else: + # objtype is given + try: + template = self.env.get_template('autosummary/%s.rst' % objtype) + except TemplateNotFound: + # fallback to base.rst + template = self.env.get_template('autosummary/base.rst') + + return template.render(context) # -- Generating output --------------------------------------------------------- @@ -167,11 +182,6 @@ def generate_autosummary_content(name: str, obj: Any, parent: Any, recursive: bool) -> str: doc = get_documenter(app, obj, parent) - if template_name is None: - template_name = 'autosummary/%s.rst' % doc.objtype - if not template.exists(template_name): - template_name = 'autosummary/base.rst' - def skip_member(obj: Any, name: str, objtype: str) -> bool: try: return app.emit_firstresult('autodoc-skip-member', objtype, name, @@ -256,7 +266,7 @@ def generate_autosummary_content(name: str, obj: Any, parent: Any, ns['objtype'] = doc.objtype ns['underline'] = len(name) * '=' - return template.render(template_name, ns) + return template.render(doc.objtype, ns) def generate_autosummary_docs(sources: List[str], output_dir: str = None,