[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 32/37] qapi/introspect.py: create a typed 'Node' data structure
From: |
John Snow |
Subject: |
[PATCH 32/37] qapi/introspect.py: create a typed 'Node' data structure |
Date: |
Tue, 15 Sep 2020 18:40:22 -0400 |
Replacing the un-typed tuple, add a typed Node that we can add typed
metadata to.
Signed-off-by: John Snow <jsnow@redhat.com>
---
scripts/qapi/introspect.py | 53 ++++++++++++++++++++------------------
1 file changed, 28 insertions(+), 25 deletions(-)
diff --git a/scripts/qapi/introspect.py b/scripts/qapi/introspect.py
index e1edd0b179..e0f5007ab7 100644
--- a/scripts/qapi/introspect.py
+++ b/scripts/qapi/introspect.py
@@ -31,11 +31,18 @@ class Extra(NamedTuple):
ifcond: Sequence[str] = tuple()
-def _make_tree(obj, ifcond,
- extra: Optional[Extra] = None):
- comment = extra.comment if extra else None
- extra = Extra(comment, ifcond)
- return (obj, extra)
+class Node:
+ """
+ Node generally contains a SchemaInfo-like type (as a dict),
+ But it also used to wrap comments/ifconds around leaf value types.
+ """
+ # Remove after 3.7 adds @dataclass:
+ # pylint: disable=too-few-public-methods
+ def __init__(self, data, ifcond: List[str],
+ extra: Optional[Extra] = None):
+ self.data = data
+ comment = extra.comment if extra else None
+ self.extra = Extra(comment, ifcond)
def _tree_to_qlit(obj, level=0, suppress_first_indent=False):
@@ -43,18 +50,15 @@ def _tree_to_qlit(obj, level=0,
suppress_first_indent=False):
def indent(level):
return level * 4 * ' '
- if isinstance(obj, tuple):
- ifobj, extra = obj
- ifcond = extra.ifcond
- comment = extra.comment
+ if isinstance(obj, Node):
ret = ''
- if comment:
- ret += indent(level) + '/* %s */\n' % comment
- if ifcond:
- ret += gen_if(ifcond)
- ret += _tree_to_qlit(ifobj, level)
- if ifcond:
- ret += '\n' + gen_endif(ifcond)
+ if obj.extra.comment:
+ ret += indent(level) + '/* %s */\n' % obj.extra.comment
+ if obj.extra.ifcond:
+ ret += gen_if(obj.extra.ifcond)
+ ret += _tree_to_qlit(obj.data, level)
+ if obj.extra.ifcond:
+ ret += '\n' + gen_endif(obj.extra.ifcond)
return ret
ret = ''
@@ -169,7 +173,7 @@ def _use_type(self, typ):
@classmethod
def _gen_features(cls, features: List[QAPISchemaFeature]):
- return [_make_tree(f.name, f.ifcond) for f in features]
+ return [Node(f.name, f.ifcond) for f in features]
def _gen_tree(self, name, mtype, obj, ifcond, features):
extra = None
@@ -183,7 +187,7 @@ def _gen_tree(self, name, mtype, obj, ifcond, features):
obj['meta-type'] = mtype
if features:
obj['features'] = self._gen_features(features)
- self._trees.append(_make_tree(obj, ifcond, extra))
+ self._trees.append(Node(obj, ifcond, extra))
def _gen_member(self, member):
obj = {'name': member.name, 'type': self._use_type(member.type)}
@@ -191,7 +195,7 @@ def _gen_member(self, member):
obj['default'] = None
if member.features:
obj['features'] = self._gen_features(member.features)
- return _make_tree(obj, member.ifcond)
+ return Node(obj, member.ifcond)
def _gen_variants(self, tag_name, variants):
return {'tag': tag_name,
@@ -199,15 +203,14 @@ def _gen_variants(self, tag_name, variants):
def _gen_variant(self, variant):
obj = {'case': variant.name, 'type': self._use_type(variant.type)}
- return _make_tree(obj, variant.ifcond)
+ return Node(obj, variant.ifcond)
def visit_builtin_type(self, name, info, json_type):
self._gen_tree(name, 'builtin', {'json-type': json_type}, [], None)
def visit_enum_type(self, name, info, ifcond, features, members, prefix):
self._gen_tree(name, 'enum',
- {'values': [_make_tree(m.name, m.ifcond, None)
- for m in members]},
+ {'values': [Node(m.name, m.ifcond) for m in members]},
ifcond, features)
def visit_array_type(self, name, info, ifcond, element_type):
@@ -227,9 +230,9 @@ def visit_object_type_flat(self, name, info, ifcond,
features,
def visit_alternate_type(self, name, info, ifcond, features, variants):
self._gen_tree(name, 'alternate',
{'members': [
- _make_tree({'type': self._use_type(m.type)},
- m.ifcond, None)
- for m in variants.variants]},
+ Node({'type': self._use_type(m.type)}, m.ifcond)
+ for m in variants.variants
+ ]},
ifcond, features)
def visit_command(self, name, info, ifcond, features,
--
2.26.2
- [PATCH 28/37] qapi/gen.py: update write() to be more idiomatic, (continued)
- [PATCH 28/37] qapi/gen.py: update write() to be more idiomatic, John Snow, 2020/09/15
- [PATCH 29/37] qapi/gen.py: delint with pylint, John Snow, 2020/09/15
- [PATCH 27/37] qapi/gen.py: Remove unused parameter, John Snow, 2020/09/15
- [PATCH 26/37] qapi/gen.py: Enable checking with mypy, John Snow, 2020/09/15
- [PATCH 30/37] qapi/introspect.py: Add a typed 'extra' structure, John Snow, 2020/09/15
- [PATCH 25/37] qapi/gen.py: add notational type hints, John Snow, 2020/09/15
- [PATCH 34/37] qapi/types.py: add notational type hints, John Snow, 2020/09/15
- [PATCH 36/37] qapi/visit.py: remove unused parameters from gen_visit_object, John Snow, 2020/09/15
- [PATCH 37/37] qapi/visit.py: add notational type hints, John Snow, 2020/09/15
- [PATCH 31/37] qapi/introspect.py: add _gen_features helper, John Snow, 2020/09/15
- [PATCH 32/37] qapi/introspect.py: create a typed 'Node' data structure,
John Snow <=
- [PATCH 35/37] qapi/types.py: remove one-letter variables, John Snow, 2020/09/15
- [PATCH 33/37] qapi/introspect.py: add notational type hints, John Snow, 2020/09/15
- Re: [PATCH 00/37] qapi: static typing conversion, pt1, John Snow, 2020/09/16
- Re: [PATCH 00/37] qapi: static typing conversion, pt1, John Snow, 2020/09/17
- Re: [PATCH 00/37] qapi: static typing conversion, pt1, Philippe Mathieu-Daudé, 2020/09/18