Skip to content

Commit

Permalink
Don't try to create inheritance diagram for non-class objects
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 19, 2024
1 parent 2732f55 commit 0ae6b2f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
3 changes: 1 addition & 2 deletions rst/qgis_pydoc_template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ Class: $CLASS

.. py:module:: $CLASS

.. inheritance-diagram:: qgis.$PACKAGE.$CLASS
:parts: 1
$HEADER_CONTENT

.. autoclass:: qgis.$PACKAGE.$CLASS
:special-members: __init__
Expand Down
42 changes: 40 additions & 2 deletions scripts/make_api_rst.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

import argparse
import inspect
import re
from collections import defaultdict
from os import makedirs
Expand Down Expand Up @@ -79,6 +80,33 @@ def ltr_tag(v):
r"""^(?:([\w.]+::)?([\w.]+\.)?(\w+)\s*(?:\((.*)\)(?:\s*->\s*([\w.]+(?:\[.*?\])?))?(?:\s*\[(signal)\])?)?)?$"""
)


class RecursiveTemplate(Template):
"""
Template subclass which performs recursive substitution on a string.
"""
def __init__(self, template):
super().__init__(template)
self.depth = 0
self.max_depth = 10 # Prevent infinite recursion

def substitute(self, **kws):
self.depth = 0
return self._recursive_substitute(**kws)

def _recursive_substitute(self, **kws):
if self.depth > self.max_depth:
raise ValueError("Max recursion depth exceeded")

self.depth += 1
result = super().safe_substitute(**kws)

if '$' in result:
return self.__class__(result)._recursive_substitute(**kws)

return result


# Make sure :numbered: is only specified in the top level index - see
# sphinx docs about this.
document_header = f"""
Expand Down Expand Up @@ -149,6 +177,11 @@ def ltr_tag(v):
"""

class_header = """
.. inheritance-diagram:: qgis.$PACKAGE.$CLASS
:parts: 1
"""

MODULE_TOC_MAX_COLUMN_SIZES = [300, 500]


Expand Down Expand Up @@ -221,7 +254,7 @@ def generate_docs():

with open("rst/qgis_pydoc_template.txt") as template_file:
template_text = template_file.read()
template = Template(template_text)
template = RecursiveTemplate(template_text)

# Iterate over every class in every package and write out an rst
# template based on standard rst template
Expand All @@ -237,10 +270,14 @@ def generate_docs():

for class_name, _class in extract_package_classes(package):
exclude_methods = set()
header = ''
for method in dir(_class):
if not hasattr(_class, method):
continue

if inspect.isclass(_class):
header = class_header

class_doc = getattr(_class, method).__doc__

if class_doc and all(
Expand All @@ -267,6 +304,7 @@ def generate_docs():
"PACKAGE": package_name,
"CLASS": class_name,
"EXCLUDE_METHODS": ",".join(exclude_methods),
"HEADER_CONTENT": header
}
class_template = template.substitute(**substitutions)
class_rst = open(f"api/{qgis_version}/{package_name}/{class_name}.rst", "w")
Expand Down Expand Up @@ -343,7 +381,7 @@ def extract_package_classes(package):

_class = getattr(package, class_name)
if hasattr(_class, "__name__") and class_name != _class.__name__:
print(f"Skipping alias {class_name}, {_class.__name__}")
# print(f"Skipping alias {class_name}, {_class.__name__}")
continue

# if not re.match('^Qgi?s', class_name):
Expand Down

0 comments on commit 0ae6b2f

Please sign in to comment.