Skip to content

Commit

Permalink
WIP Better single page class output
Browse files Browse the repository at this point in the history
Also have a skeleton test structure
  • Loading branch information
AWhetter committed Jan 22, 2024
1 parent e68622d commit 35305d8
Show file tree
Hide file tree
Showing 17 changed files with 527 additions and 589 deletions.
67 changes: 4 additions & 63 deletions autoapi/mappers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ class PythonMapperBase:

language = "base"
type = "base"
# Create a page in the output for this object.
top_level_object = False
_RENDER_LOG_LEVEL = "VERBOSE"

def __init__(self, obj, jinja_env, app, options=None):
Expand Down Expand Up @@ -130,20 +128,8 @@ def short_name(self):

@property
def pathname(self):
"""Sluggified path for filenames
Slugs to a filename using the follow steps
* Decode unicode to approximate ascii
* Remove existing hyphens
* Substitute hyphens for non-word characters
* Break up the string as paths
"""
slug = self.name
slug = anyascii.anyascii(slug)
slug = slug.replace("-", "")
slug = re.sub(r"[^\w\.]+", "-", slug).strip("-")
return os.path.join(*slug.split("."))
"""Sluggified path for filenames."""
return os.path.join(*self.name.split("."))

def include_dir(self, root):
"""Return directory of file"""
Expand Down Expand Up @@ -308,6 +294,8 @@ def add_object(self, obj):
while child_stack:
child = child_stack.pop()
self.all_objects[child.id] = child
if child.type in self.own_page_types:
self.objects_to_render[child.id] = child
child_stack.extend(getattr(child, "children", ()))

def map(self, options=None):
Expand All @@ -329,50 +317,6 @@ def create_class(self, data, options=None, **kwargs):
"""
raise NotImplementedError

def output_child_rst(self, obj, obj_parent, detail_dir, source_suffix):

if not obj.display:
return

# Skip nested cases like functions in functions or clases in clases
if obj.type == obj_parent.type:
return

obj_child_page_level = _OWN_PAGE_LEVELS.index(obj.type)
desired_page_level = _OWN_PAGE_LEVELS.index(self.app.config.autoapi_own_page_level)
is_own_page = obj_child_page_level <= desired_page_level
if not is_own_page:
return

obj_child_rst = obj.render(
is_own_page=is_own_page,
)
if not obj_child_rst:
return

function_page_level = _OWN_PAGE_LEVELS.index("function")
is_level_beyond_function = function_page_level < desired_page_level
if obj.type in ["exception", "class"]:
if not is_level_beyond_function:
outfile = f"{obj.short_name}{source_suffix}"
path = os.path.join(detail_dir, outfile)
else:
outdir = os.path.join(detail_dir, obj.short_name)
ensuredir(outdir)
path = os.path.join(outdir, f"index{source_suffix}")
else:
is_parent_in_detail_dir = detail_dir.endswith(obj_parent.short_name)
outdir = detail_dir if is_parent_in_detail_dir else os.path.join(detail_dir, obj_parent.short_name)
ensuredir(outdir)
path = os.path.join(outdir, f"{obj.short_name}{source_suffix}")

with open(path, "wb+") as obj_child_detail_file:
obj_child_detail_file.write(obj_child_rst.encode("utf-8"))

for obj_child in obj.children:
child_detail_dir = os.path.join(detail_dir, obj.name)
self.output_child_rst(obj_child, obj, child_detail_dir, source_suffix)

def output_rst(self, root, source_suffix):
for _, obj in status_iterator(
self.objects_to_render.items(),
Expand All @@ -393,9 +337,6 @@ def output_rst(self, root, source_suffix):
path = os.path.join(detail_dir, f"index{source_suffix}")
with open(path, "wb+") as detail_file:
detail_file.write(rst.encode("utf-8"))

for child in obj.children:
self.output_child_rst(child, obj, detail_dir, source_suffix)

if self.app.config.autoapi_add_toctree_entry:
self._output_top_rst(root)
Expand Down
2 changes: 1 addition & 1 deletion autoapi/mappers/python/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def map(self, options=None):

top_level_objects = {obj.id: obj for obj in self.all_objects.values() if isinstance(obj, TopLevelPythonPythonMapper)}
parents = {obj.name: obj for obj in top_level_objects.values()}
for obj in self.objects_to_render.values():
for obj in top_level_objects.values():
parent_name = obj.name.rsplit(".", 1)[0]
if parent_name in parents and parent_name != obj.name:
parent = parents[parent_name]
Expand Down
8 changes: 0 additions & 8 deletions autoapi/mappers/python/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,6 @@ class TopLevelPythonPythonMapper(PythonPythonMapper):
def __init__(self, obj, **kwargs):
super().__init__(obj, **kwargs)

self.top_level_object = "." not in self.name
"""Whether this object is at the very top level (True) or not (False).
This will be False for subpackages and submodules.
:type: bool
"""

self.subpackages = []
self.submodules = []
self.all = obj["all"]
Expand Down
2 changes: 1 addition & 1 deletion autoapi/templates/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This page contains auto-generated API reference documentation [#f1]_.
:titlesonly:

{% for page in pages %}
{% if page.top_level_object and page.display %}
{% if page.display %}
{{ page.include_path }}
{% endif %}
{% endfor %}
Expand Down
143 changes: 70 additions & 73 deletions autoapi/templates/python/class.rst
Original file line number Diff line number Diff line change
@@ -1,105 +1,102 @@
{% if obj.display %}
{% if is_own_page %}
{% if is_own_page %}
{{ obj.name }}
{{ "=" * obj.name | length }}
{% endif %}

{% endif %}
{% set visible_children = obj.children|selectattr("display")|list %}
{% set own_page_children = visible_children|selectattr("type", "in", own_page_types|list) %}
{% if own_page_children %}
.. toctree::
:hidden:

{% for child in own_page_children %}
{{ child.short_name }}/index
{% endfor %}

{% endif %}
.. py:{{ obj.type }}:: {{ obj.short_name }}{% if obj.args %}({{ obj.args }}){% endif %}
{% for (args, return_annotation) in obj.overloads %}
{% for (args, return_annotation) in obj.overloads %}
{{ " " * (obj.type | length) }} {{ obj.short_name }}{% if args %}({{ args }}){% endif %}

{% endfor %}


{% endfor %}
{% if obj.bases %}
{% if "show-inheritance" in autoapi_options %}
{% if "show-inheritance" in autoapi_options %}
Bases: {% for base in obj.bases %}{{ base|link_objs }}{% if not loop.last %}, {% endif %}{% endfor %}
{% endif %}
{% endif %}


{% if "show-inheritance-diagram" in autoapi_options and obj.bases != ["object"] %}
{% if "show-inheritance-diagram" in autoapi_options and obj.bases != ["object"] %}
.. autoapi-inheritance-diagram:: {{ obj.obj["full_name"] }}
:parts: 1
{% if "private-members" in autoapi_options %}
{% if "private-members" in autoapi_options %}
:private-bases:
{% endif %}
{% endif %}

{% endif %}
{% endif %}
{% endif %}
{% if obj.docstring %}
{{ obj.docstring|indent(3) }}

{% endif %}
{# TODO: Rendering of all children below this line must be conditional on own_page_types #}
{% if "inherited-members" in autoapi_options %}
{% set visible_classes = obj.classes|selectattr("display")|list %}
{% else %}
{% set visible_classes = obj.classes|rejectattr("inherited")|selectattr("display")|list %}
{% endif %}
{% for klass in visible_classes %}
{{ klass.render(own_page_types=[])|indent(3) }}
{% for obj_item in visible_children %}
{% if obj_item.type not in own_page_types %}
{{ obj_item.render()|indent(3) }}
{% endif %}
{% endfor %}
{% if "inherited-members" in autoapi_options %}
{% set visible_properties = obj.properties|selectattr("display")|list %}
{% else %}
{% set visible_properties = obj.properties|rejectattr("inherited")|selectattr("display")|list %}
{% endif %}
{% if "property" in own_page_types and visible_properties %}
{% if is_own_page and own_page_children %}
{% set visible_attributes = own_page_children|selectattr("type", "equalto", "attribute")|list %}
{% if visible_attributes %}
Attributes
----------

Properties
----------
.. autoapisummary::

.. toctree::
:hidden:
{% for attribute in visible_attributes %}
{{ attribute.id }}
{% endfor %}

{% for property in visible_properties %}
{{ property.name }}
{% endfor %}
{% else %}
{% for property in visible_properties %}
{{ property.render()|indent(3) }}
{% endfor %}
{% endif %}
{% if "inherited-members" in autoapi_options %}
{% set visible_attributes = obj.attributes|selectattr("display")|list %}
{% else %}
{% set visible_attributes = obj.attributes|rejectattr("inherited")|selectattr("display")|list %}
{% endif %}
{% if "attribute" in own_page_types and visible_attributes %}

Attributes
----------
{% endif %}
{% set visible_exceptions = own_page_children|selectattr("type", "equalto", "exception")|list %}
{% if visible_exceptions %}
Exceptions
----------

.. toctree::
:hidden:
.. autoapisummary::

{% for attribute in visible_attributes %}
{{ attribute.name }}
{% endfor %}
{% else %}
{% for attribute in visible_attributes %}
{{ attribute.render()|indent(3) }}
{% endfor %}
{% endif %}
{% if "inherited-members" in autoapi_options %}
{% set visible_methods = obj.methods|selectattr("display")|list %}
{% else %}
{% set visible_methods = obj.methods|rejectattr("inherited")|selectattr("display")|list %}
{% endif %}
{% if "method" in own_page_types and visible_methods %}
{% for exception in visible_exceptions %}
{{ exception.id }}
{% endfor %}

Methods
-------

.. toctree::
:hidden:
{% endif %}
{% set visible_classes = own_page_children|selectattr("type", "equalto", "class")|list %}
{% if visible_classes %}
Classes
-------

{% for method in visible_methods %}
{{ method.name }}
{% endfor %}
{% else %}
{% for method in visible_methods %}
{{ method.render()|indent(3) }}
{% endfor %}
.. autoapisummary::

{% for klass in visible_classes %}
{{ klass.id }}
{% endfor %}


{% endif %}
{% set visible_methods = own_page_children|selectattr("type", "equalto", "method")|list %}
{% if visible_methods %}
Methods
-------

.. autoapisummary::

{% for method in visible_methods %}
{{ method.id }}
{% endfor %}


{% endif %}
{% endif %}
{% endif %}
Loading

0 comments on commit 35305d8

Please sign in to comment.