From nobody Wed Jan 29 00:12:34 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1734052710; cv=none; d=zohomail.com; s=zohoarc; b=JEpGc4h89rT2lBDFQQj6O5zfUfn0Soo6iba5KLlz2/W9k8a/8+AXOUxK9Ox0u7LzLJrOuEDUzSU62LwShL2odTSi1Huh/5t/gY37n2f8UbOjt0dH+ui4/iCY+XZ95AOa9LHLYnqnys6aepSeVAu1j4QKOs3B1bUhrXReBMiheQE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1734052710; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=E2uBLnCFk7cw9O+LvbpeL9YtAqyFV9HBoqcOf2eGrOU=; b=QXAWdpw9aq8b5PGZqy6q13yEKy8wPZgooHiQI+Vg19QJNuodIMbn03oGnm4okLOasgZhSpyLoJGw42/q/btpl6xoSwwRMYoCRHJ1przUE74NSvcT1Sd/tPdAVv810IRwLGw3+tBHaaFsSfMgVC3B1tXPqXJFM4u2UJzhRrYkwik= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1734052710128181.6732345621084; Thu, 12 Dec 2024 17:18:30 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tLuHd-00017z-Ue; Thu, 12 Dec 2024 20:15:58 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tLuH6-0000yW-SP for qemu-devel@nongnu.org; Thu, 12 Dec 2024 20:15:24 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tLuH0-0000vv-29 for qemu-devel@nongnu.org; Thu, 12 Dec 2024 20:15:23 -0500 Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-675-0EitUavIPSe0ikg6VNw1Qw-1; Thu, 12 Dec 2024 20:15:14 -0500 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8BA4F19560B3; Fri, 13 Dec 2024 01:15:13 +0000 (UTC) Received: from jsnow-thinkpadp16vgen1.westford.csb (unknown [10.22.88.22]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id CD2BF1956086; Fri, 13 Dec 2024 01:15:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1734052516; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=E2uBLnCFk7cw9O+LvbpeL9YtAqyFV9HBoqcOf2eGrOU=; b=Qv/3W8XVMBQW5Wea2zIvnEcWa3qzcEa0c2h4N5a+sqDsI8+mohSoj8HPUcnKkZZ1C9GGbB vzR9wFd8eXq9HvhHsvY2vKZwZOj4eBftgMuaxchAOuwG6ECxJiJjZ0oHAkUCc47rRjq7+b HUryIpyMTOzYV48/k7ogdPAVeohfiz4= X-MC-Unique: 0EitUavIPSe0ikg6VNw1Qw-1 X-Mimecast-MFC-AGG-ID: 0EitUavIPSe0ikg6VNw1Qw From: John Snow To: qemu-devel@nongnu.org Cc: Michael Roth , Markus Armbruster , Peter Maydell , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Victor Toso de Carvalho , Paolo Bonzini , John Snow Subject: [RFC PATCH v2 32/35] WIP: 3.x - XREF Date: Thu, 12 Dec 2024 20:13:01 -0500 Message-ID: <20241213011307.2942030-33-jsnow@redhat.com> In-Reply-To: <20241213011307.2942030-1-jsnow@redhat.com> References: <20241213011307.2942030-1-jsnow@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.496, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1734052711996116600 Content-Type: text/plain; charset="utf-8" Signed-off-by: John Snow --- docs/sphinx/compat.py | 114 +++++++++++++++++++- docs/sphinx/qapi-domain.py | 207 ++++++++++++------------------------- 2 files changed, 179 insertions(+), 142 deletions(-) diff --git a/docs/sphinx/compat.py b/docs/sphinx/compat.py index 28cb39161fe..657c05a81a4 100644 --- a/docs/sphinx/compat.py +++ b/docs/sphinx/compat.py @@ -2,14 +2,32 @@ Sphinx cross-version compatibility goop """ =20 -from typing import Callable +import re +from typing import ( + Any, + Callable, + Optional, + Type, +) =20 +from docutils import nodes from docutils.nodes import Element, Node, Text =20 import sphinx from sphinx import addnodes -from sphinx.util.docutils import SphinxDirective, switch_source_input +from sphinx.environment import BuildEnvironment +from sphinx.roles import XRefRole +from sphinx.util import docfields +from sphinx.util.docutils import ( + ReferenceRole, + SphinxDirective, + switch_source_input, +) from sphinx.util.nodes import nested_parse_with_titles +from sphinx.util.typing import TextlikeNode + + +MAKE_XREF_WORKAROUND =3D sphinx.version_info[:3] < (4, 1, 0) =20 =20 space_node: Callable[[str], Node] @@ -46,3 +64,95 @@ def nested_parse(directive: SphinxDirective, content_nod= e: Element) -> None: nested_parse_with_titles( directive.state, directive.content, content_node ) + + +class CompatFieldMixin: + """ + Compatibility workaround for Sphinx versions prior to 4.1.0. + + Older sphinx versions do not use the domain's XRefRole for parsing + and formatting cross-references, so we need to perform this magick + ourselves to avoid needing to write the parser/formatter in two + separate places. + + This workaround isn't brick-for-brick compatible with modern Sphinx + versions, because we do not have access to the parent directive's + state during this parsing like we do in more modern versions. + + It's no worse than what pre-Sphinx 4.1.0 does, so... oh well! + """ + + def make_xref( + self, + rolename: str, + domain: str, + target: str, + innernode: Type[TextlikeNode] =3D addnodes.literal_emphasis, + contnode: Optional[Node] =3D None, + env: Optional[BuildEnvironment] =3D None, + *args: Any, + **kwargs: Any, + ) -> Node: + print("Using compat make_xref") + + assert env + if not rolename: + return contnode or innernode(target, target) + + # Get the role instance, but don't *execute it* - we lack the + # correct state to do so. Instead, we'll just use its public + # methods to do our reference formatting, and emulate the rest. + role =3D env.get_domain(domain).roles[rolename] + assert isinstance(role, XRefRole) + + # XRefRole features not supported by this compatibility shim; + # these were not supported in Sphinx 3.x either, so nothing of + # value is really lost. + assert not target.startswith("!") + assert not re.match(ReferenceRole.explicit_title_re, target) + assert not role.lowercase + assert not role.fix_parens + + # Code below based mostly on sphinx.roles.XRefRole; run() and + # create_xref_node() + options =3D { + "refdoc": env.docname, + "refdomain": domain, + "reftype": rolename, + "refexplicit": False, + "refwarn": role.warn_dangling, + } + refnode =3D role.nodeclass(target, **options) + title, target =3D role.process_link(env, refnode, False, target, t= arget) + refnode["reftarget"] =3D target + classes =3D ["xref", domain, f"{domain}-{rolename}"] + refnode +=3D role.innernodeclass(target, title, classes=3Dclasses) + result_nodes, messages =3D role.result_nodes( + None, # FIXME - normally self.inliner.document ... + env, + refnode, + is_ref=3DTrue, + ) + return nodes.inline(target, "", *result_nodes) + + +class CompatField(CompatFieldMixin, docfields.Field): + pass + + +class CompatGroupedField(CompatFieldMixin, docfields.GroupedField): + pass + + +class CompatTypedField(CompatFieldMixin, docfields.TypedField): + pass + + +if not MAKE_XREF_WORKAROUND: + Field =3D docfields.Field + GroupedField =3D docfields.GroupedField + TypedField =3D docfields.TypedField +else: + Field =3D CompatField + GroupedField =3D CompatGroupedField + TypedField =3D CompatTypedField diff --git a/docs/sphinx/qapi-domain.py b/docs/sphinx/qapi-domain.py index ee9b1d056ff..ebdf9074391 100644 --- a/docs/sphinx/qapi-domain.py +++ b/docs/sphinx/qapi-domain.py @@ -24,7 +24,14 @@ from docutils.statemachine import StringList =20 from collapse import CollapseNode -from compat import keyword_node, nested_parse, space_node +from compat import ( + Field, + GroupedField, + TypedField, + keyword_node, + nested_parse, + space_node, +) import sphinx from sphinx import addnodes from sphinx.addnodes import desc_signature, pending_xref @@ -38,24 +45,18 @@ from sphinx.locale import _, __ from sphinx.roles import XRefRole from sphinx.util import logging -from sphinx.util.docfields import ( - DocFieldTransformer, - Field, - GroupedField, - TypedField, -) +from sphinx.util.docfields import DocFieldTransformer from sphinx.util.docutils import SphinxDirective from sphinx.util.nodes import make_id, make_refnode =20 =20 if TYPE_CHECKING: from docutils.nodes import Element, Node - from docutils.parsers.rst.states import Inliner =20 from sphinx.application import Sphinx from sphinx.builders import Builder from sphinx.environment import BuildEnvironment - from sphinx.util.typing import OptionSpec, TextlikeNode + from sphinx.util.typing import OptionSpec =20 logger =3D logging.getLogger(__name__) =20 @@ -82,111 +83,8 @@ class ObjectEntry(NamedTuple): aliased: bool =20 =20 -class QAPIXrefMixin: - def make_xref( - self, - rolename: str, - domain: str, - target: str, - innernode: type[TextlikeNode] =3D nodes.literal, - contnode: Optional[Node] =3D None, - env: Optional[BuildEnvironment] =3D None, - inliner: Optional[Inliner] =3D None, - location: Optional[Node] =3D None, - ) -> Node: - # make_xref apparently has a mode of operation where the inliner - # class argument is passed to the role object - # (e.g. QAPIXRefRole) to construct the final result; passing - # inliner =3D location =3D None forces it into its legacy mode whe= re - # it returns a pending_xref node instead. - # (This is how the built-in Python domain behaves.) - result =3D super().make_xref( # type: ignore[misc] - rolename, - domain, - target, - innernode=3Dinnernode, - contnode=3Dcontnode, - env=3Denv, - inliner=3DNone, - location=3DNone, - ) - if isinstance(result, pending_xref): - assert env is not None - # Add domain-specific context information to the pending refer= ence. - result["refspecific"] =3D True - result["qapi:module"] =3D env.ref_context.get("qapi:module") - - assert isinstance(result, nodes.Node) - return result - - def make_xrefs( - self, - rolename: str, - domain: str, - target: str, - innernode: type[TextlikeNode] =3D nodes.literal, - contnode: Optional[Node] =3D None, - env: Optional[BuildEnvironment] =3D None, - inliner: Optional[Inliner] =3D None, - location: Optional[Node] =3D None, - ) -> list[Node]: - # Note: this function is called on up to three fields of text: - # (1) The field name argument (e.g. member/arg name) - # (2) The field name type (e.g. member/arg type) - # (3) The field *body* text, for Fields that do not take arguments. - - list_type =3D False - optional =3D False - - # If the rolename is qapi:type, we know we are processing a type - # and not an arg/memb name or field body text. - if rolename =3D=3D "type": - # force the innernode class to be a literal. - innernode =3D nodes.literal - - # Type names that end with "?" are considered Optional - # arguments and should be documented as such, but it's not - # part of the xref itself. - if target.endswith("?"): - optional =3D True - target =3D target[:-1] - - # Type names wrapped in brackets denote lists. strip the - # brackets and remember to add them back later. - if target.startswith("[") and target.endswith("]"): - list_type =3D True - target =3D target[1:-1] - - # When processing Fields with bodyrolename=3D"type", contnode - # will be present, which indicates that the body has already - # been parsed into nodes. We don't want that, actually: - # we'll re-create our own nodes for it. - contnode =3D None - - results =3D [] - result =3D self.make_xref( - rolename, - domain, - target, - innernode, - contnode, - env, - inliner, - location, - ) - results.append(result) - - if list_type: - results.insert(0, nodes.literal("[", "[")) - results.append(nodes.literal("]", "]")) - if optional: - results.append(nodes.Text(", ")) - results.append(nodes.emphasis("?", "optional")) - - return results - - class QAPIXRefRole(XRefRole): + def process_link( self, env: BuildEnvironment, @@ -196,34 +94,63 @@ def process_link( target: str, ) -> tuple[str, str]: refnode["qapi:module"] =3D env.ref_context.get("qapi:module") - if not has_explicit_title: - title =3D title.lstrip(".") # only has a meaning for the targ= et - target =3D target.lstrip("~") # only has a meaning for the ti= tle - # if the first character is a tilde, don't display the module - # parts of the contents - if title[0:1] =3D=3D "~": - title =3D title[1:] - dot =3D title.rfind(".") - if dot !=3D -1: - title =3D title[dot + 1 :] - # if the first character is a dot, search more specific namespaces= first - # else search builtins first - if target[0:1] =3D=3D ".": + + # Cross-references that begin with a tilde adjust the title to + # only show the reference without a leading module, even if one + # was provided. This is a Sphinx-standard syntax; give it + # priority over QAPI-specific type markup below. + hide_module =3D False + if target.startswith("~"): + hide_module =3D True target =3D target[1:] - refnode["refspecific"] =3D True + + # Type names that end with "?" are considered optional + # arguments and should be documented as such, but it's not + # part of the xref itself. + if target.endswith("?"): + refnode["qapi:optional"] =3D True + target =3D target[:-1] + + # Type names wrapped in brackets denote lists. strip the + # brackets and remember to add them back later. + if target.startswith("[") and target.endswith("]"): + refnode["qapi:array"] =3D True + target =3D target[1:-1] + + if has_explicit_title: + # Don't mess with the title at all if it was explicitly set. + # Explicit title syntax for references is e.g. + # :qapi:type:`target ` + # and this explicit title overrides everything else here. + return title, target + + title =3D target + if hide_module: + title =3D target.split(".")[-1] + return title, target =20 + def result_nodes( + self, + document: nodes.document, + env: BuildEnvironment, + node: Element, + is_ref: bool, + ) -> Tuple[List[nodes.Node], List[nodes.system_message]]: =20 -class QAPIGroupedField(QAPIXrefMixin, GroupedField): - pass + # node here is the pending_xref node (or whatever nodeclass was + # configured at XRefRole class instantiation time). + results: List[nodes.Node] =3D [node] =20 + if node.get("qapi:array"): + results.insert(0, nodes.literal("[", "[")) + results.append(nodes.literal("]", "]")) =20 -class QAPITypedField(QAPIXrefMixin, TypedField): - pass + if node.get("qapi:optional"): + results.append(nodes.Text(", ")) + results.append(nodes.emphasis("?", "optional")) =20 - -class QAPIField(QAPIXrefMixin, Field): - pass + return results, [] =20 =20 def since_validator(param: str) -> str: @@ -633,7 +560,7 @@ class QAPICommand(QAPIObject): doc_field_types.extend( [ # :arg TypeName ArgName: descr - QAPITypedField( + TypedField( "argument", label=3D_("Arguments"), names=3D("arg",), @@ -648,7 +575,7 @@ class QAPICommand(QAPIObject): has_arg=3DFalse, ), # :returns TypeName: descr - QAPIGroupedField( + GroupedField( "returnvalue", label=3D_("Returns"), rolename=3D"type", @@ -656,7 +583,7 @@ class QAPICommand(QAPIObject): can_collapse=3DTrue, ), # :returns-nodesc: TypeName - QAPIField( + Field( "returnvalue", label=3D_("Returns"), names=3D("returns-nodesc",), @@ -691,7 +618,7 @@ class QAPIAlternate(QAPIObject): doc_field_types.extend( [ # :choice type name: descr - QAPITypedField( + TypedField( "choice", label=3D_("Choices"), names=3D("choice",), @@ -709,7 +636,7 @@ class QAPIObjectWithMembers(QAPIObject): doc_field_types.extend( [ # :member type name: descr - QAPITypedField( + TypedField( "member", label=3D_("Members"), names=3D("memb",), @@ -842,7 +769,7 @@ class Branch(SphinxDirective): doc_field_types: List[Field] =3D [ # :arg type name: descr # :memb type name: descr - QAPITypedField( + TypedField( "branch-arg-or-memb", label=3D":BRANCH:", names=3D("arg", "memb"), --=20 2.47.0