In preparation for switching QAPIDoc to a more complex data structure
for keeping track of sections, convert the "all_sections" property to
a private property.

Rename it "_sections" to emphasize that there is not* some alternative
property that is somehow not all sections. This is what you get and
you're going to love it.

Implement the len() and iter() features for QAPIDoc as a replacement
for direct, public access to the all_sections property.

(*OK, currently there are some, but spoilers, they're going away in
the next commits.)

Signed-off-by: John Snow <[email protected]>
---
 docs/sphinx/qapidoc.py         |  7 ++++---
 scripts/qapi/parser.py         | 27 +++++++++++++++++----------
 tests/qapi-schema/test-qapi.py |  2 +-
 3 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/docs/sphinx/qapidoc.py b/docs/sphinx/qapidoc.py
index ae57056a393..fd666be131f 100644
--- a/docs/sphinx/qapidoc.py
+++ b/docs/sphinx/qapidoc.py
@@ -377,7 +377,7 @@ def visit_sections(self, ent: QAPISchemaDefinition) -> None:
                 )
             )
 
-        sections = ent.doc.all_sections if ent.doc else []
+        sections = list(ent.doc) if ent.doc else []
 
         # Add sections in source order:
         for section in sections:
@@ -416,8 +416,9 @@ def visit_module(self, path: str) -> None:
         self.ensure_blank_line()
 
     def visit_freeform(self, doc: QAPIDoc) -> None:
-        assert len(doc.all_sections) == 1, doc.all_sections
-        body = doc.all_sections[0]
+        sections = list(doc)
+        assert len(sections) == 1, sections
+        body = sections[0]
         self.add_lines(self.reformat_arobase(body.text), doc.info)
         self.ensure_blank_line()
 
diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
index 0dca1acba4c..bdd0f1ec3ff 100644
--- a/scripts/qapi/parser.py
+++ b/scripts/qapi/parser.py
@@ -19,6 +19,7 @@
     TYPE_CHECKING,
     Any,
     Dict,
+    Iterator,
     List,
     Mapping,
     Match,
@@ -758,7 +759,7 @@ def __init__(self, info: QAPISourceInfo, symbol: 
Optional[str] = None):
         # definition doc's symbol, None for free-form doc
         self.symbol: Optional[str] = symbol
         # the sections in textual order
-        self.all_sections: List[QAPIDoc.Section] = []
+        self._sections: List[QAPIDoc.Section] = []
         # dicts mapping parameter/feature names to their description
         self._args: Dict[str, QAPIDoc.ArgSection] = {}
         self._features: Dict[str, QAPIDoc.ArgSection] = {}
@@ -797,10 +798,16 @@ def _append(self, section: 'QAPIDoc.Section') -> None:
                 raise QAPISemError(
                     section.info, "duplicated '%s' section" % section.kind)
             self.since = section
-        self.all_sections.append(section)
+        self._sections.append(section)
+
+    def __iter__(self) -> Iterator['QAPIDoc.Section']:
+        return iter(self._sections)
+
+    def __len__(self) -> int:
+        return len(self._sections)
 
     def end(self) -> None:
-        for section in self.all_sections:
+        for section in self:
             section.text = section.text.strip('\n')
             if (
                     section.kind not in (
@@ -813,9 +820,9 @@ def end(self) -> None:
     def ensure_untagged_section(self, info: QAPISourceInfo) -> None:
         kind = QAPIDoc.Kind.DETAILS
 
-        if self.all_sections and self.all_sections[-1].kind == kind:
+        if self._sections and self._sections[-1].kind == kind:
             # extend current section
-            section = self.all_sections[-1]
+            section = self._sections[-1]
             if not section.text:
                 # Section is empty so far; update info to start *here*.
                 section.info = info
@@ -856,7 +863,7 @@ def new_feature(self, info: QAPISourceInfo, name: str) -> 
None:
         self._new_description(info, name, QAPIDoc.Kind.FEATURE, self._features)
 
     def append_line(self, line: str) -> None:
-        self.all_sections[-1].append_line(line)
+        self._sections[-1].append_line(line)
 
     def _insert_near_kind(
         self,
@@ -865,12 +872,12 @@ def _insert_near_kind(
         after: bool = False,
     ) -> bool:
         """Insert or append a new doc section at a specific point."""
-        for idx, sect in enumerate(reversed(self.all_sections)):
+        for idx, sect in enumerate(reversed(list(self))):
             if sect.kind == kind:
-                pos = len(self.all_sections) - idx - 1
+                pos = len(self) - idx - 1
                 if after:
                     pos += 1
-                self.all_sections.insert(pos, new_sect)
+                self._sections.insert(pos, new_sect)
                 return True
         return False
 
@@ -915,7 +922,7 @@ def ensure_returns(self, info: QAPISourceInfo) -> None:
         # TODO: Take advantage of the new strict section ordering and
         # simplify this algorithm.
 
-        if any(s.kind == QAPIDoc.Kind.RETURNS for s in self.all_sections):
+        if self._returns:
             return
 
         # Stub "Returns" section for undocumented returns value
diff --git a/tests/qapi-schema/test-qapi.py b/tests/qapi-schema/test-qapi.py
index 5beb96e894f..d5b4ac3a682 100755
--- a/tests/qapi-schema/test-qapi.py
+++ b/tests/qapi-schema/test-qapi.py
@@ -117,7 +117,7 @@ def test_frontend(fname):
             print('doc symbol=%s' % doc.symbol)
         else:
             print('doc freeform')
-        for section in doc.all_sections:
+        for section in doc:
             if isinstance(section, QAPIDoc.ArgSection):
                 print('    %s=%s' % (section.kind, section.name))
             else:
-- 
2.55.0


Reply via email to