qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: John Snow <jsnow@redhat.com>
To: Markus Armbruster <armbru@redhat.com>
Cc: qemu-devel@nongnu.org, Cleber Rosa <crosa@redhat.com>,
	John Snow <jsnow@redhat.com>,
	Michael Roth <mdroth@linux.vnet.ibm.com>,
	Eduardo Habkost <ehabkost@redhat.com>
Subject: [PATCH 08/14] qapi/doc.py: Add generic texi_member callback
Date: Tue, 22 Sep 2020 17:17:56 -0400	[thread overview]
Message-ID: <20200922211802.4083666-9-jsnow@redhat.com> (raw)
In-Reply-To: <20200922211802.4083666-1-jsnow@redhat.com>

We can just use the same callback and dispatch based on type, which
removes the need to type the callback, add downcast assertions, or use
less-specific types in the specialized generators.

---

This patch is optional, it's just demonstrating a different way to
handle the callback typing -- by eliminating the callback.

Signed-off-by: John Snow <jsnow@redhat.com>
---
 scripts/qapi/doc.py | 37 +++++++++++++++++++------------------
 1 file changed, 19 insertions(+), 18 deletions(-)

diff --git a/scripts/qapi/doc.py b/scripts/qapi/doc.py
index 43943575da..74d017f60e 100644
--- a/scripts/qapi/doc.py
+++ b/scripts/qapi/doc.py
@@ -5,7 +5,7 @@
 """This script produces the documentation of a qapi schema in texinfo format"""
 
 import re
-from typing import Callable, List, Optional
+from typing import List, Optional
 
 from .gen import QAPIGenDoc
 from .parser import QAPIDoc
@@ -150,24 +150,17 @@ def texi_if(ifcond: Optional[List[str]],
     return '%s@b{If:} @code{%s}%s' % (prefix, ', '.join(ifcond), suffix)
 
 
-TexiMemberCallback = Callable[[QAPISchemaMember, str, str], str]
-
-
-def texi_enum_value(value: QAPISchemaMember,
-                    desc: str,
-                    suffix: str) -> str:
+def texi_enum_value(value: QAPISchemaEnumMember,
+                    desc: str, suffix: str) -> str:
     """Format a table of members item for an enumeration value"""
-    assert isinstance(value, QAPISchemaEnumMember)
     assert suffix == '', "Ignored suffix for texi_enum_value"
     return '@item @code{%s}\n%s%s' % (
         value.name, desc, texi_if(value.ifcond, prefix='@*'))
 
 
-def texi_member(member: QAPISchemaMember,
-                desc: str,
-                suffix: str) -> str:
+def texi_object_member(member: QAPISchemaObjectTypeMember,
+                       desc: str, suffix: str) -> str:
     """Format a table of members item for an object type member"""
-    assert isinstance(member, QAPISchemaObjectTypeMember)
     typ = member.type.doc_type()
     membertype = ': ' + typ if typ else ''
     return '@item @code{%s%s}%s%s\n%s%s' % (
@@ -176,11 +169,20 @@ def texi_member(member: QAPISchemaMember,
         suffix, desc, texi_if(member.ifcond, prefix='@*'))
 
 
+def texi_member(member: QAPISchemaMember, desc: str, suffix: str) -> str:
+    """Format a table of members item for an arbitrary member type"""
+
+    if isinstance(member, QAPISchemaObjectTypeMember):
+        return texi_object_member(member, desc, suffix)
+    if isinstance(member, QAPISchemaEnumMember):
+        return texi_enum_value(member, desc, suffix)
+    raise Exception(f"Unhandled member type {type(member).__name__}")
+
+
 def texi_members(doc: QAPIDoc,
                  what: str,
                  base: Optional[QAPISchemaObjectType] = None,
-                 variants: Optional[QAPISchemaVariants] = None,
-                 member_func: TexiMemberCallback = texi_member) -> str:
+                 variants: Optional[QAPISchemaVariants] = None) -> str:
     """Format the table of members"""
     items = ''
     for section in doc.args.values():
@@ -198,7 +200,7 @@ def texi_members(doc: QAPIDoc,
         if desc is None:
             desc = 'Not documented\n'
 
-        items += member_func(section.member, desc, '')
+        items += texi_member(section.member, desc, '')
     if base:
         items += '@item The members of @code{%s}\n' % base.doc_type()
     if variants:
@@ -208,7 +210,7 @@ def texi_members(doc: QAPIDoc,
             if v.type.is_implicit():
                 assert not v.type.base and not v.type.variants
                 for m in v.type.local_members:
-                    items += member_func(m, '', when)
+                    items += texi_member(m, '', when)
             else:
                 items += '@item The members of @code{%s}%s\n' % (
                     v.type.doc_type(), when)
@@ -288,8 +290,7 @@ def visit_enum_type(self,
                         prefix: Optional[str]) -> None:
         doc = self.cur_doc
         self._gen.add(texi_type('Enum', doc, ifcond,
-                                texi_members(doc, 'Values',
-                                             member_func=texi_enum_value)))
+                                texi_members(doc, 'Values')))
 
     def visit_object_type(self,
                           name: str,
-- 
2.26.2



  parent reply	other threads:[~2020-09-22 22:11 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-22 21:17 [PATCH 00/14] qapi: static typing conversion, pt3 John Snow
2020-09-22 21:17 ` [PATCH 01/14] qapi/doc.py: stash long temporary locals in named locals John Snow
2020-09-23 20:46   ` Eduardo Habkost
2020-09-22 21:17 ` [PATCH 02/14] qapi/doc.py: avoid unnecessary keyword arguments John Snow
2020-09-23 20:46   ` Eduardo Habkost
2020-09-22 21:17 ` [PATCH 03/14] qapi/doc.py: Add assertion on section.member John Snow
2020-09-23 20:46   ` Eduardo Habkost
2020-09-22 21:17 ` [PATCH 04/14] qapi/doc.py: assert correct types in member_func callbacks John Snow
2020-09-23 20:46   ` Eduardo Habkost
2020-09-22 21:17 ` [PATCH 05/14] qapi/doc.py: Assert no suffix given for enum members John Snow
2020-09-23 20:47   ` Eduardo Habkost
2020-09-22 21:17 ` [PATCH 06/14] qapi/doc.py: Add type hint annotations John Snow
2020-09-23 20:47   ` Eduardo Habkost
2020-09-22 21:17 ` [PATCH 07/14] qapi/doc.py: enable mypy checks John Snow
2020-09-23 20:47   ` Eduardo Habkost
2020-09-22 21:17 ` John Snow [this message]
2020-09-22 21:17 ` [PATCH 09/14] qapi/doc.py: Remove one-letter variables John Snow
2020-09-23 21:00   ` Eduardo Habkost
2020-09-22 21:17 ` [PATCH 10/14] qapi/gen.py: Add __bool__ dunder method to QAPIGen John Snow
2020-09-23 21:05   ` Eduardo Habkost
2020-09-22 21:17 ` [PATCH 11/14] qapi/doc.py: Don't use private attributes of QAPIGen property John Snow
2020-09-23 21:05   ` Eduardo Habkost
2020-09-22 21:18 ` [PATCH 12/14] qapi/doc.py: Assert tag member is Enum type John Snow
2020-09-23 21:07   ` Eduardo Habkost
2020-09-22 21:18 ` [PATCH 13/14] qapi/doc.py: Assert type of object variant John Snow
2020-09-23 21:08   ` Eduardo Habkost
2020-09-22 21:18 ` [PATCH 14/14] qapi/doc.py: enable pylint checks John Snow
2020-09-23 21:09   ` Eduardo Habkost
2020-09-30  4:44 ` [PATCH 00/14] qapi: static typing conversion, pt3 John Snow

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200922211802.4083666-9-jsnow@redhat.com \
    --to=jsnow@redhat.com \
    --cc=armbru@redhat.com \
    --cc=crosa@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).