Files
yosys/docs/util/cell_documenter.py

416 lines
14 KiB
Python
Raw Permalink Normal View History

2024-05-17 17:54:08 +12:00
#!/usr/bin/env python3
from __future__ import annotations
2024-05-21 14:06:08 +12:00
from dataclasses import dataclass
import json
2024-05-20 15:50:28 +12:00
from pathlib import Path, PosixPath, WindowsPath
2024-05-17 17:54:08 +12:00
import re
from typing import Any
from sphinx.application import Sphinx
from sphinx.ext import autodoc
from sphinx.ext.autodoc import Documenter
from sphinx.util import logging
logger = logging.getLogger(__name__)
# cell signature
cell_ext_sig_re = re.compile(
2024-05-21 18:10:20 +12:00
r'''^ ([^:\s]+::)? # optional group or file name
2024-05-21 14:06:08 +12:00
([\w$._]+?) # module name
2024-05-17 17:54:08 +12:00
(?:\.([\w_]+))? # optional: thing name
(::[\w_]+)? # attribute
\s* $ # and nothing more
''', re.VERBOSE)
2024-05-21 14:06:08 +12:00
@dataclass
class YosysCell:
2024-05-21 18:10:20 +12:00
name: str
2024-05-21 14:06:08 +12:00
title: str
ports: str
source: str
desc: str
code: str
inputs: list[str]
outputs: list[str]
2024-09-06 15:17:51 +12:00
properties: list[str]
2026-06-23 07:24:59 +02:00
2024-05-21 18:10:20 +12:00
class YosysCellGroupDocumenter(Documenter):
objtype = 'cellgroup'
priority = 10
object: tuple[str, list[str]]
lib_key = 'groups'
2024-05-17 17:54:08 +12:00
option_spec = {
2024-05-21 18:10:20 +12:00
'caption': autodoc.annotation_option,
'members': autodoc.members_option,
2024-05-17 17:54:08 +12:00
'source': autodoc.bool_option,
'linenos': autodoc.bool_option,
}
2024-05-21 18:10:20 +12:00
__cell_lib: dict[str, list[str] | dict[str]] | None = None
2024-05-21 14:06:08 +12:00
@property
2024-05-21 18:10:20 +12:00
def cell_lib(self) -> dict[str, list[str] | dict[str]]:
2024-05-21 14:06:08 +12:00
if not self.__cell_lib:
self.__cell_lib = {}
2024-05-21 18:10:20 +12:00
cells_obj: dict[str, dict[str, list[str] | dict[str]]]
2024-05-21 14:06:08 +12:00
try:
with open(self.config.cells_json, "r") as f:
cells_obj = json.loads(f.read())
except FileNotFoundError:
logger.warning(
f"unable to find cell lib at {self.config.cells_json}",
type = 'cellref',
subtype = 'cell_lib'
)
else:
2024-05-21 18:10:20 +12:00
for (name, obj) in cells_obj.get(self.lib_key, {}).items():
self.__cell_lib[name] = obj
2024-05-21 14:06:08 +12:00
return self.__cell_lib
2026-06-23 07:24:59 +02:00
2024-05-17 17:54:08 +12:00
@classmethod
def can_document_member(
cls,
member: Any,
membername: str,
isattr: bool,
parent: Any
) -> bool:
2024-05-21 18:10:20 +12:00
return False
2024-05-17 17:54:08 +12:00
def parse_name(self) -> bool:
2024-05-21 18:10:20 +12:00
if not self.options.caption:
self.content_indent = ''
self.fullname = self.modname = self.name
2024-05-17 17:54:08 +12:00
return True
2026-06-23 07:24:59 +02:00
2024-05-17 17:54:08 +12:00
def import_object(self, raiseerror: bool = False) -> bool:
# get cell
try:
2024-05-21 18:10:20 +12:00
self.object = (self.modname, self.cell_lib[self.modname])
2024-05-17 17:54:08 +12:00
except KeyError:
2024-05-21 18:10:20 +12:00
if raiseerror:
raise
2024-05-17 17:54:08 +12:00
return False
2024-05-21 18:10:20 +12:00
self.real_modname = self.modname
2024-05-17 17:54:08 +12:00
return True
2026-06-23 07:24:59 +02:00
2024-05-17 17:54:08 +12:00
def get_sourcename(self) -> str:
2024-05-21 18:10:20 +12:00
return self.env.doc2path(self.env.docname)
2026-06-23 07:24:59 +02:00
2024-05-17 17:54:08 +12:00
def format_name(self) -> str:
2024-05-21 18:10:20 +12:00
return self.options.caption or ''
2024-05-17 17:54:08 +12:00
def format_signature(self, **kwargs: Any) -> str:
2024-05-21 18:10:20 +12:00
return self.modname
2026-06-23 07:24:59 +02:00
2024-05-17 17:54:08 +12:00
def add_directive_header(self, sig: str) -> None:
2024-05-21 18:10:20 +12:00
domain = getattr(self, 'domain', 'cell')
directive = getattr(self, 'directivetype', 'group')
2024-05-17 17:54:08 +12:00
name = self.format_name()
sourcename = self.get_sourcename()
2024-05-21 18:10:20 +12:00
cell_list = self.object
2024-05-17 17:54:08 +12:00
# cell definition
2024-05-21 18:10:20 +12:00
self.add_line(f'.. {domain}:{directive}:: {sig}', sourcename)
self.add_line(f' :caption: {name}', sourcename)
2024-05-17 17:54:08 +12:00
if self.options.noindex:
self.add_line(' :noindex:', sourcename)
2026-06-23 07:24:59 +02:00
2024-05-17 17:54:08 +12:00
def add_content(self, more_content: Any | None) -> None:
2024-05-21 18:10:20 +12:00
# groups have no native content
2024-05-17 17:54:08 +12:00
# add additional content (e.g. from document), if present
if more_content:
for line, src in zip(more_content.data, more_content.items):
self.add_line(line, src[0], src[1])
def filter_members(
self,
members: list[tuple[str, Any]],
want_all: bool
) -> list[tuple[str, Any, bool]]:
return [(x[0], x[1], False) for x in members]
def get_object_members(
self,
want_all: bool
) -> tuple[bool, list[tuple[str, Any]]]:
ret: list[tuple[str, str]] = []
2024-05-21 18:10:20 +12:00
if want_all:
for member in self.object[1]:
ret.append((member, self.modname))
else:
memberlist = self.options.members or []
for name in memberlist:
if name in self.object:
ret.append((name, self.modname))
else:
logger.warning(('unknown module mentioned in :members: option: '
f'group {self.modname}, module {name}'),
type='cellref')
2024-05-17 17:54:08 +12:00
return False, ret
def document_members(self, all_members: bool = False) -> None:
want_all = (all_members or
2024-05-21 18:10:20 +12:00
self.options.inherited_members or
self.options.members is autodoc.ALL)
2024-05-17 17:54:08 +12:00
# find out which members are documentable
members_check_module, members = self.get_object_members(want_all)
# document non-skipped members
memberdocumenters: list[tuple[Documenter, bool]] = []
for (mname, member, isattr) in self.filter_members(members, want_all):
classes = [cls for cls in self.documenters.values()
if cls.can_document_member(member, mname, isattr, self)]
if not classes:
# don't know how to document this member
continue
# prefer the documenter with the highest priority
classes.sort(key=lambda cls: cls.priority)
# give explicitly separated module name, so that members
# of inner classes can be documented
2024-05-21 18:10:20 +12:00
full_mname = self.format_signature() + '::' + mname
2024-05-17 17:54:08 +12:00
documenter = classes[-1](self.directive, full_mname, self.indent)
memberdocumenters.append((documenter, isattr))
member_order = self.options.member_order or self.config.autodoc_member_order
memberdocumenters = self.sort_members(memberdocumenters, member_order)
for documenter, isattr in memberdocumenters:
documenter.generate(
all_members=True, real_modname=self.real_modname,
check_module=members_check_module and not isattr)
def generate(
self,
more_content: Any | None = None,
real_modname: str | None = None,
check_module: bool = False,
all_members: bool = False
) -> None:
if not self.parse_name():
# need a cell lib to import from
logger.warning(
f"don't know which cell lib to import for autodocumenting {self.name}",
type = 'cellref'
)
return
2024-05-20 16:45:23 +12:00
if not self.import_object():
logger.warning(
f"unable to load {self.name}",
type = 'cellref'
)
return
2024-05-17 17:54:08 +12:00
# check __module__ of object (for members not given explicitly)
# if check_module:
# if not self.check_module():
# return
sourcename = self.get_sourcename()
self.add_line('', sourcename)
# format the object's signature, if any
try:
sig = self.format_signature()
except Exception as exc:
logger.warning(('error while formatting signature for %s: %s'),
self.fullname, exc, type='cellref')
return
# generate the directive header and options, if applicable
self.add_directive_header(sig)
self.add_line('', sourcename)
# e.g. the module directive doesn't have content
self.indent += self.content_indent
# add all content (from docstrings, attribute docs etc.)
self.add_content(more_content)
# document members, if possible
self.document_members(all_members)
2024-05-21 18:10:20 +12:00
class YosysCellDocumenter(YosysCellGroupDocumenter):
objtype = 'cell'
priority = 15
object: YosysCell
lib_key = 'cells'
@classmethod
def can_document_member(
cls,
member: Any,
membername: str,
isattr: bool,
parent: Any
) -> bool:
if membername == "__source":
return False
if not membername.startswith('$'):
return False
return isinstance(parent, YosysCellGroupDocumenter)
def parse_name(self) -> bool:
try:
matched = cell_ext_sig_re.match(self.name)
group, modname, thing, attribute = matched.groups()
except AttributeError:
logger.warning(('invalid signature for auto%s (%r)') % (self.objtype, self.name),
type='cellref')
return False
self.modname = modname
self.groupname = group or ''
self.attribute = attribute or ''
self.fullname = ((self.modname) + (thing or ''))
return True
2026-06-23 07:24:59 +02:00
2024-05-21 18:10:20 +12:00
def import_object(self, raiseerror: bool = False) -> bool:
if super().import_object(raiseerror):
self.object = YosysCell(self.modname, **self.object[1])
return True
return False
2026-06-23 07:24:59 +02:00
2024-05-21 18:10:20 +12:00
def get_sourcename(self) -> str:
return self.object.source.split(":")[0]
2026-06-23 07:24:59 +02:00
2024-05-21 18:10:20 +12:00
def format_name(self) -> str:
return self.object.name
def format_signature(self, **kwargs: Any) -> str:
return self.groupname + self.fullname + self.attribute
2026-06-23 07:24:59 +02:00
2024-05-21 18:10:20 +12:00
def add_directive_header(self, sig: str) -> None:
domain = getattr(self, 'domain', self.objtype)
directive = getattr(self, 'directivetype', 'def')
name = self.format_name()
sourcename = self.get_sourcename()
cell = self.object
# cell definition
self.add_line(f'.. {domain}:{directive}:: {sig}', sourcename)
# options
2024-09-06 15:17:51 +12:00
opt_attrs = ["title", "properties", ]
2024-05-21 18:10:20 +12:00
for attr in opt_attrs:
val = getattr(cell, attr, None)
2024-09-06 15:17:51 +12:00
if isinstance(val, list):
val = ' '.join(val)
2024-05-21 18:10:20 +12:00
if val:
self.add_line(f' :{attr}: {val}', sourcename)
2024-09-06 15:17:51 +12:00
2024-09-16 14:11:01 +12:00
self.add_line('\n', sourcename)
2024-05-21 18:10:20 +12:00
if self.options.noindex:
self.add_line(' :noindex:', sourcename)
2026-06-23 07:24:59 +02:00
2024-05-21 18:10:20 +12:00
def add_content(self, more_content: Any | None) -> None:
# set sourcename and add content from attribute documentation
sourcename = self.get_sourcename()
startline = int(self.object.source.split(":")[1])
for i, line in enumerate(self.object.desc.splitlines(), startline):
self.add_line(line, sourcename, i)
# add additional content (e.g. from document), if present
if more_content:
for line, src in zip(more_content.data, more_content.items):
self.add_line(line, src[0], src[1])
2024-09-16 14:11:01 +12:00
# fields
self.add_line('\n', sourcename)
field_attrs = ["properties", ]
for field in field_attrs:
attr = getattr(self.object, field, [])
for val in attr:
self.add_line(f':{field} {val}:', sourcename)
2024-05-21 18:10:20 +12:00
def get_object_members(
self,
want_all: bool
) -> tuple[bool, list[tuple[str, Any]]]:
ret: list[tuple[str, str]] = []
if self.options.source:
ret.append(('__source', self.real_modname))
return False, ret
2024-05-17 17:54:08 +12:00
class YosysCellSourceDocumenter(YosysCellDocumenter):
objtype = 'cellsource'
priority = 20
@classmethod
def can_document_member(
cls,
member: Any,
membername: str,
isattr: bool,
parent: Any
) -> bool:
if membername != "__source":
return False
if isinstance(parent, YosysCellDocumenter):
return True
return False
2026-06-23 07:24:59 +02:00
2024-05-17 17:54:08 +12:00
def add_directive_header(self, sig: str) -> None:
domain = getattr(self, 'domain', 'cell')
directive = getattr(self, 'directivetype', 'source')
name = self.format_name()
sourcename = self.get_sourcename()
cell = self.object
# cell definition
2024-05-21 18:10:20 +12:00
self.add_line(f'.. {domain}:{directive}:: {sig}', sourcename)
2024-05-17 17:54:08 +12:00
if self.options.linenos:
self.add_line(f' :source: {cell.source.split(":")[0]}', sourcename)
else:
self.add_line(f' :source: {cell.source}', sourcename)
self.add_line(f' :language: verilog', sourcename)
if self.options.linenos:
startline = int(self.object.source.split(":")[1])
self.add_line(f' :lineno-start: {startline}', sourcename)
if self.options.noindex:
self.add_line(' :noindex:', sourcename)
2026-06-23 07:24:59 +02:00
2024-05-17 17:54:08 +12:00
def add_content(self, more_content: Any | None) -> None:
# set sourcename and add content from attribute documentation
sourcename = self.get_sourcename()
startline = int(self.object.source.split(":")[1])
2024-05-21 14:06:08 +12:00
for i, line in enumerate(self.object.code.splitlines(), startline-1):
2024-05-17 17:54:08 +12:00
self.add_line(line, sourcename, i)
# add additional content (e.g. from document), if present
if more_content:
for line, src in zip(more_content.data, more_content.items):
self.add_line(line, src[0], src[1])
def get_object_members(
self,
want_all: bool
) -> tuple[bool, list[tuple[str, Any]]]:
return False, []
def setup(app: Sphinx) -> dict[str, Any]:
2024-05-21 14:06:08 +12:00
app.add_config_value('cells_json', False, 'html', [Path, PosixPath, WindowsPath])
2024-05-17 17:54:08 +12:00
app.setup_extension('sphinx.ext.autodoc')
app.add_autodocumenter(YosysCellDocumenter)
app.add_autodocumenter(YosysCellSourceDocumenter)
2024-05-21 18:10:20 +12:00
app.add_autodocumenter(YosysCellGroupDocumenter)
2024-05-17 17:54:08 +12:00
return {
'version': '1',
'parallel_read_safe': True,
}