From nobody Thu Apr 3 11:25:01 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=1741665529; cv=none; d=zohomail.com; s=zohoarc; b=dKS+BGxQ8eDfVEgfw0r54FgM2EtevAp6EX0S+mCopwbm9YVdJTE92G+Z13PTR30IX1dLZBVjcD8g7nn5fITp5qf2XnTUhcDYPcQXefZQmw175IA8cO436N2LoPU/9FIBUB9p88FSNcsXrPApb4MsdVBZvj5bTmQDrOj6qi+V36g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1741665529; 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=+SF6Heuqy2ntt8GE+4CMS9RKdqSX2G/9XaTuyRM0hDo=; b=HQ7CRk1IiEX5+8GCWtYC1+PmMnXc21djR8JqBD0cN0EZAlJp7uWU2Qezn02ubkyqHtotVp4pq+e4sc+lWruF+VXg2IBaSqUK+1/BjmZN6eOorW/DIrVErpT60dH1l7Khwr0vBi5X0EKm7dmQUriTn9SkoBzBX0PW2FxTHV9h3jY= 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 1741665529772778.9859722081125; Mon, 10 Mar 2025 20:58:49 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1trqZu-0000bS-GL; Mon, 10 Mar 2025 23:46:50 -0400 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 1trqYK-0006gz-59 for qemu-devel@nongnu.org; Mon, 10 Mar 2025 23:45:13 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1trqYH-0002bw-8u for qemu-devel@nongnu.org; Mon, 10 Mar 2025 23:45:11 -0400 Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-634-hYgrjwPYPvGWCxjnKHAlZg-1; Mon, 10 Mar 2025 23:45:05 -0400 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D1F20180025E; Tue, 11 Mar 2025 03:45:03 +0000 (UTC) Received: from jsnow-thinkpadp16vgen1.westford.csb (unknown [10.22.64.49]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 46CF41800373; Tue, 11 Mar 2025 03:45:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1741664708; 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=+SF6Heuqy2ntt8GE+4CMS9RKdqSX2G/9XaTuyRM0hDo=; b=Kc5vmMKZhjFk5Ptl4luc403Jru3NBF+QL/sl2PhC7lcgvDX3gSz0VgM6L6QvNaCz01aJZU W7qKkDBfTu8bfKuBwJBqrZdUDvoYkgVe0oGgoA6GlLkAUygYZclr6z+rKj/v9ZqoelLFZJ J8rywQGZIMOHtH0EPoQUb/y7b5RD2G0= X-MC-Unique: hYgrjwPYPvGWCxjnKHAlZg-1 X-Mimecast-MFC-AGG-ID: hYgrjwPYPvGWCxjnKHAlZg_1741664703 From: John Snow To: qemu-devel@nongnu.org Cc: Peter Maydell , John Snow , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Eric Blake , Michael Roth , Thomas Huth , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Markus Armbruster Subject: [PATCH v3 33/63] qapi: clean up encoding of section kinds Date: Mon, 10 Mar 2025 23:42:31 -0400 Message-ID: <20250311034303.75779-34-jsnow@redhat.com> In-Reply-To: <20250311034303.75779-1-jsnow@redhat.com> References: <20250311034303.75779-1-jsnow@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 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.133.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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: 1741665530354019000 Content-Type: text/plain; charset="utf-8" We have several kinds of sections, and to tell them apart, we use Section attribute @tag and also the section object's Python type: type @tag untagged Section None @foo: ArgSection 'foo' Returns: Section 'Returns' Errors: Section 'Errors' Since: Section 'Since' TODO: Section 'TODO' Note: * @foo can be a member or a feature description, depending on context. * tag =3D=3D 'Since' can be a Since: section or a member or feature description. If it's a Section, it's the former, and if it's an ArgSection, it's the latter. Clean this up as follows. Move the member or feature name to new ArgSection attribute @name, and replace @tag by enum @kind like this: type kind name untagged Section PLAIN @foo: ArgSection MEMBER 'foo' if member or argument ArgSection FEATURE 'foo' if feature Returns: Section RETURNS Errors: Section ERRORS Since: Section SINCE TODO: Section TODO The qapi-schema tests are updated to account for the new section names; "TODO" becomes "Todo" and `None` becomes "Plain" there. Signed-off-by: John Snow Reviewed-by: Markus Armbruster --- docs/sphinx/qapidoc.py | 7 +-- scripts/qapi/parser.py | 97 ++++++++++++++++++++++++---------- tests/qapi-schema/doc-good.out | 10 ++-- tests/qapi-schema/test-qapi.py | 2 +- 4 files changed, 80 insertions(+), 36 deletions(-) diff --git a/docs/sphinx/qapidoc.py b/docs/sphinx/qapidoc.py index 61997fd21af..d622398f1da 100644 --- a/docs/sphinx/qapidoc.py +++ b/docs/sphinx/qapidoc.py @@ -35,6 +35,7 @@ from docutils.statemachine import ViewList from qapi.error import QAPIError, QAPISemError from qapi.gen import QAPISchemaVisitor +from qapi.parser import QAPIDoc from qapi.schema import QAPISchema =20 from sphinx import addnodes @@ -258,11 +259,11 @@ def _nodes_for_sections(self, doc): """Return list of doctree nodes for additional sections""" nodelist =3D [] for section in doc.sections: - if section.tag and section.tag =3D=3D 'TODO': + if section.kind =3D=3D QAPIDoc.Kind.TODO: # Hide TODO: sections continue =20 - if not section.tag: + if section.kind =3D=3D QAPIDoc.Kind.PLAIN: # Sphinx cannot handle sectionless titles; # Instead, just append the results to the prior section. container =3D nodes.container() @@ -270,7 +271,7 @@ def _nodes_for_sections(self, doc): nodelist +=3D container.children continue =20 - snode =3D self._make_section(section.tag) + snode =3D self._make_section(section.kind.name.title()) self._parse_text_into_node(dedent(section.text), snode) nodelist.append(snode) return nodelist diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py index 97def9f0e4f..94d5322f8af 100644 --- a/scripts/qapi/parser.py +++ b/scripts/qapi/parser.py @@ -14,6 +14,7 @@ # This work is licensed under the terms of the GNU GPL, version 2. # See the COPYING file in the top-level directory. =20 +import enum import os import re from typing import ( @@ -574,7 +575,10 @@ def get_doc(self) -> 'QAPIDoc': ) raise QAPIParseError(self, emsg) =20 - doc.new_tagged_section(self.info, match.group(1)) + doc.new_tagged_section( + self.info, + QAPIDoc.Kind.from_string(match.group(1)) + ) text =3D line[match.end():] if text: doc.append_line(text) @@ -585,7 +589,7 @@ def get_doc(self) -> 'QAPIDoc': self, "unexpected '=3D' markup in definition documentati= on") else: - # tag-less paragraph + # plain paragraph doc.ensure_untagged_section(self.info) doc.append_line(line) line =3D self.get_doc_paragraph(doc) @@ -634,14 +638,33 @@ class QAPIDoc: Free-form documentation blocks consist only of a body section. """ =20 + class Kind(enum.Enum): + PLAIN =3D 0 + MEMBER =3D 1 + FEATURE =3D 2 + RETURNS =3D 3 + ERRORS =3D 4 + SINCE =3D 5 + TODO =3D 6 + + @staticmethod + def from_string(kind: str) -> 'QAPIDoc.Kind': + return QAPIDoc.Kind[kind.upper()] + + def __str__(self) -> str: + return self.name.title() + class Section: # pylint: disable=3Dtoo-few-public-methods - def __init__(self, info: QAPISourceInfo, - tag: Optional[str] =3D None): + def __init__( + self, + info: QAPISourceInfo, + kind: 'QAPIDoc.Kind', + ): # section source info, i.e. where it begins self.info =3D info - # section tag, if any ('Returns', '@name', ...) - self.tag =3D tag + # section kind + self.kind =3D kind # section text without tag self.text =3D '' =20 @@ -649,8 +672,14 @@ def append_line(self, line: str) -> None: self.text +=3D line + '\n' =20 class ArgSection(Section): - def __init__(self, info: QAPISourceInfo, tag: str): - super().__init__(info, tag) + def __init__( + self, + info: QAPISourceInfo, + kind: 'QAPIDoc.Kind', + name: str + ): + super().__init__(info, kind) + self.name =3D name self.member: Optional['QAPISchemaMember'] =3D None =20 def connect(self, member: 'QAPISchemaMember') -> None: @@ -662,7 +691,9 @@ def __init__(self, info: QAPISourceInfo, symbol: Option= al[str] =3D None): # definition doc's symbol, None for free-form doc self.symbol: Optional[str] =3D symbol # the sections in textual order - self.all_sections: List[QAPIDoc.Section] =3D [QAPIDoc.Section(info= )] + self.all_sections: List[QAPIDoc.Section] =3D [ + QAPIDoc.Section(info, QAPIDoc.Kind.PLAIN) + ] # the body section self.body: Optional[QAPIDoc.Section] =3D self.all_sections[0] # dicts mapping parameter/feature names to their description @@ -679,12 +710,14 @@ def __init__(self, info: QAPISourceInfo, symbol: Opti= onal[str] =3D None): def end(self) -> None: for section in self.all_sections: section.text =3D section.text.strip('\n') - if section.tag is not None and section.text =3D=3D '': + if section.kind !=3D QAPIDoc.Kind.PLAIN and section.text =3D= =3D '': raise QAPISemError( - section.info, "text required after '%s:'" % section.ta= g) + section.info, "text required after '%s:'" % section.ki= nd) =20 def ensure_untagged_section(self, info: QAPISourceInfo) -> None: - if self.all_sections and not self.all_sections[-1].tag: + kind =3D QAPIDoc.Kind.PLAIN + + if self.all_sections and self.all_sections[-1].kind =3D=3D kind: # extend current section section =3D self.all_sections[-1] if not section.text: @@ -692,46 +725,56 @@ def ensure_untagged_section(self, info: QAPISourceInf= o) -> None: section.info =3D info section.text +=3D '\n' return + # start new section - section =3D self.Section(info) + section =3D self.Section(info, kind) self.sections.append(section) self.all_sections.append(section) =20 - def new_tagged_section(self, info: QAPISourceInfo, tag: str) -> None: - section =3D self.Section(info, tag) - if tag =3D=3D 'Returns': + def new_tagged_section( + self, + info: QAPISourceInfo, + kind: 'QAPIDoc.Kind', + ) -> None: + section =3D self.Section(info, kind) + if kind =3D=3D QAPIDoc.Kind.RETURNS: if self.returns: raise QAPISemError( - info, "duplicated '%s' section" % tag) + info, "duplicated '%s' section" % kind) self.returns =3D section - elif tag =3D=3D 'Errors': + elif kind =3D=3D QAPIDoc.Kind.ERRORS: if self.errors: raise QAPISemError( - info, "duplicated '%s' section" % tag) + info, "duplicated '%s' section" % kind) self.errors =3D section - elif tag =3D=3D 'Since': + elif kind =3D=3D QAPIDoc.Kind.SINCE: if self.since: raise QAPISemError( - info, "duplicated '%s' section" % tag) + info, "duplicated '%s' section" % kind) self.since =3D section self.sections.append(section) self.all_sections.append(section) =20 - def _new_description(self, info: QAPISourceInfo, name: str, - desc: Dict[str, ArgSection]) -> None: + def _new_description( + self, + info: QAPISourceInfo, + name: str, + kind: 'QAPIDoc.Kind', + desc: Dict[str, ArgSection] + ) -> None: if not name: raise QAPISemError(info, "invalid parameter name") if name in desc: raise QAPISemError(info, "'%s' parameter name duplicated" % na= me) - section =3D self.ArgSection(info, '@' + name) + section =3D self.ArgSection(info, kind, name) self.all_sections.append(section) desc[name] =3D section =20 def new_argument(self, info: QAPISourceInfo, name: str) -> None: - self._new_description(info, name, self.args) + self._new_description(info, name, QAPIDoc.Kind.MEMBER, self.args) =20 def new_feature(self, info: QAPISourceInfo, name: str) -> None: - self._new_description(info, name, self.features) + self._new_description(info, name, QAPIDoc.Kind.FEATURE, self.featu= res) =20 def append_line(self, line: str) -> None: self.all_sections[-1].append_line(line) @@ -744,7 +787,7 @@ def connect_member(self, member: 'QAPISchemaMember') ->= None: "%s '%s' lacks documentation" % (member.role, member.name)) self.args[member.name] =3D QAPIDoc.ArgSection( - self.info, '@' + member.name) + self.info, QAPIDoc.Kind.MEMBER, member.name) self.args[member.name].connect(member) =20 def connect_feature(self, feature: 'QAPISchemaFeature') -> None: diff --git a/tests/qapi-schema/doc-good.out b/tests/qapi-schema/doc-good.out index 0a9da3efdeb..5773f1dd6d6 100644 --- a/tests/qapi-schema/doc-good.out +++ b/tests/qapi-schema/doc-good.out @@ -113,7 +113,7 @@ The _one_ {and only}, description on the same line Also _one_ {and only} feature=3Denum-member-feat a member feature - section=3DNone + section=3DPlain @two is undocumented doc symbol=3DBase body=3D @@ -171,15 +171,15 @@ description starts on the same line a feature feature=3Dcmd-feat2 another feature - section=3DNone + section=3DPlain .. note:: @arg3 is undocumented section=3DReturns @Object section=3DErrors some - section=3DTODO + section=3DTodo frobnicate - section=3DNone + section=3DPlain .. admonition:: Notes =20 - Lorem ipsum dolor sit amet @@ -212,7 +212,7 @@ If you're bored enough to read this, go see a video of = boxed cats a feature feature=3Dcmd-feat2 another feature - section=3DNone + section=3DPlain .. qmp-example:: =20 -> "this example" diff --git a/tests/qapi-schema/test-qapi.py b/tests/qapi-schema/test-qapi.py index 8fe951c8803..4be930228cc 100755 --- a/tests/qapi-schema/test-qapi.py +++ b/tests/qapi-schema/test-qapi.py @@ -122,7 +122,7 @@ def test_frontend(fname): for feat, section in doc.features.items(): print(' feature=3D%s\n%s' % (feat, section.text)) for section in doc.sections: - print(' section=3D%s\n%s' % (section.tag, section.text)) + print(' section=3D%s\n%s' % (section.kind, section.text)) =20 =20 def open_test_result(dir_name, file_name, update): --=20 2.48.1