Skip to contentSkip to conversation

qapi: pluggable backend code generators

PATCH In review Daniel P. Berrangé 2 participants 5 messages Feb 24, 2025 – Mar 4, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 qapi: pluggable backend code generators

The 'qapi.backend.QAPIBackend' class defines an API contract for code generators. The current generator is put into a new class 'qapi.backend.QAPICBackend' and made to be the default impl.

A custom generator can be requested using the '-k' arg which takes a fully qualified python class name

qapi-gen.py -B the.python.module.QAPIMyBackend

This allows out of tree code to use the QAPI generator infrastructure to create new language bindings for QAPI schemas. This has the caveat that the QAPI generator APIs are not guaranteed stable, so consumers of this feature may have to update their code to be compatible with future QEMU releases.

---

In v2:

- Create QAPISchema object ahead of calling backend
- Use -B instead of -k
- Fix mypy annotations
- Add error checking when loading backend
- Hardcode import of QAPICBackend to guarantee mypy coverage

 scripts/qapi/backend.py | 63 ++++++++++++++++++++++++++++++++++
 scripts/qapi/main.py    | 75 ++++++++++++++++++++++-------------------
 2 files changed, 103 insertions(+), 35 deletions(-)
 create mode 100644 scripts/qapi/backend.py
scripts/qapi/backend.py new file +63 -0
new file mode 100644
index 0000000000..14e60aa67a
@@ -0,0 +1,63 @@
+# This work is licensed under the terms of the GNU GPL, version 2 or later.
+# See the COPYING file in the top-level directory.
+
+from abc import ABC, abstractmethod
+
+from .commands import gen_commands
+from .events import gen_events
+from .features import gen_features
+from .introspect import gen_introspect
+from .schema import QAPISchema
+from .types import gen_types
+from .visit import gen_visit
+
+
+class QAPIBackend(ABC):
+
+    @abstractmethod
+    def generate(self,
+                 schema: QAPISchema,
+                 output_dir: str,
+                 prefix: str,
+                 unmask: bool,
+                 builtins: bool,
+                 gen_tracing: bool) -> None:
+        """
+        Generate code for the given schema into the target directory.
+
+        :param schema: The primary QAPI schema object.
+        :param output_dir: The output directory to store generated code.
+        :param prefix: Optional C-code prefix for symbol names.
+        :param unmask: Expose non-ABI names through introspection?
+        :param builtins: Generate code for built-in types?
+
+        :raise QAPIError: On failures.
+        """
+
+
+class QAPICBackend(QAPIBackend):
+
+    def generate(self,
+                 schema: QAPISchema,
+                 output_dir: str,
+                 prefix: str,
+                 unmask: bool,
+                 builtins: bool,
+                 gen_tracing: bool) -> None:
+        """
+        Generate C code for the given schema into the target directory.
+
+        :param schema_file: The primary QAPI schema file.
+        :param output_dir: The output directory to store generated code.
+        :param prefix: Optional C-code prefix for symbol names.
+        :param unmask: Expose non-ABI names through introspection?
+        :param builtins: Generate code for built-in types?
+
+        :raise QAPIError: On failures.
+        """
+        gen_types(schema, output_dir, prefix, builtins)
+        gen_features(schema, output_dir, prefix)
+        gen_visit(schema, output_dir, prefix, builtins)
+        gen_commands(schema, output_dir, prefix, gen_tracing)
+        gen_events(schema, output_dir, prefix)
+        gen_introspect(schema, output_dir, prefix, unmask)
scripts/qapi/main.py +40 -35

108 lines not on this page · raw message

Signature · 1 line
2.47.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 5 lines · Daniel P. Berrangé

The 'qapi.backend.QAPIBackend' class defines an API contract for code generators. The current generator is put into a new class 'qapi.backend.QAPICBackend' and made to be the default impl.

A custom generator can be requested using the '-k' arg which takes a

Missed an instance of -k. Can fix this myself.

Quoted 139 lines

fully qualified python class name

qapi-gen.py -B the.python.module.QAPIMyBackend

This allows out of tree code to use the QAPI generator infrastructure to create new language bindings for QAPI schemas. This has the caveat that the QAPI generator APIs are not guaranteed stable, so consumers of this feature may have to update their code to be compatible with future QEMU releases.

---

In v2:

- Create QAPISchema object ahead of calling backend
- Use -B instead of -k
- Fix mypy annotations
- Add error checking when loading backend
- Hardcode import of QAPICBackend to guarantee mypy coverage

 scripts/qapi/backend.py | 63 ++++++++++++++++++++++++++++++++++
 scripts/qapi/main.py    | 75 ++++++++++++++++++++++-------------------
 2 files changed, 103 insertions(+), 35 deletions(-)
 create mode 100644 scripts/qapi/backend.py
scripts/qapi/backend.py new file +63 -0
new file mode 100644
index 0000000000..14e60aa67a
@@ -0,0 +1,63 @@
+# This work is licensed under the terms of the GNU GPL, version 2 or later.
+# See the COPYING file in the top-level directory.
+
+from abc import ABC, abstractmethod
+
+from .commands import gen_commands
+from .events import gen_events
+from .features import gen_features
+from .introspect import gen_introspect
+from .schema import QAPISchema
+from .types import gen_types
+from .visit import gen_visit
+
+
+class QAPIBackend(ABC):
+
+    @abstractmethod
+    def generate(self,
+                 schema: QAPISchema,
+                 output_dir: str,
+                 prefix: str,
+                 unmask: bool,
+                 builtins: bool,
+                 gen_tracing: bool) -> None:
+        """
+        Generate code for the given schema into the target directory.
+
+        :param schema: The primary QAPI schema object.
+        :param output_dir: The output directory to store generated code.
+        :param prefix: Optional C-code prefix for symbol names.
+        :param unmask: Expose non-ABI names through introspection?
+        :param builtins: Generate code for built-in types?
+
+        :raise QAPIError: On failures.
+        """
+
+
+class QAPICBackend(QAPIBackend):
+
+    def generate(self,
+                 schema: QAPISchema,
+                 output_dir: str,
+                 prefix: str,
+                 unmask: bool,
+                 builtins: bool,
+                 gen_tracing: bool) -> None:
+        """
+        Generate C code for the given schema into the target directory.
+
+        :param schema_file: The primary QAPI schema file.
+        :param output_dir: The output directory to store generated code.
+        :param prefix: Optional C-code prefix for symbol names.
+        :param unmask: Expose non-ABI names through introspection?
+        :param builtins: Generate code for built-in types?
+
+        :raise QAPIError: On failures.
+        """
+        gen_types(schema, output_dir, prefix, builtins)
+        gen_features(schema, output_dir, prefix)
+        gen_visit(schema, output_dir, prefix, builtins)
+        gen_commands(schema, output_dir, prefix, gen_tracing)
+        gen_events(schema, output_dir, prefix)
+        gen_introspect(schema, output_dir, prefix, unmask)
scripts/qapi/main.py +5 -17
index 324081b9fc..8a8b1e0121 100644
@@ -8,18 +8,14 @@
 """
 
 import argparse
+from importlib import import_module
 import sys
 from typing import Optional
 
-from .commands import gen_commands
+from .backend import QAPIBackend, QAPICBackend
 from .common import must_match
 from .error import QAPIError
-from .events import gen_events
-from .features import gen_features
-from .introspect import gen_introspect
 from .schema import QAPISchema
-from .types import gen_types
-from .visit import gen_visit
 
 
 def invalid_prefix_char(prefix: str) -> Optional[str]:
@@ -29,32 +25,37 @@ def invalid_prefix_char(prefix: str) -> Optional[str]:
     return None
 
 
-def generate(schema_file: str,
-             output_dir: str,
-             prefix: str,
-             unmask: bool = False,
-             builtins: bool = False,
-             gen_tracing: bool = False) -> None:
-    """
-    Generate C code for the given schema into the target directory.
+def create_backend(path: str) -> QAPIBackend:
+    if path is None:
+        return QAPICBackend()
 
-    :param schema_file: The primary QAPI schema file.
-    :param output_dir: The output directory to store generated code.
-    :param prefix: Optional C-code prefix for symbol names.

scripts/qapi/main.py def invalid_prefix_char(prefix: str) -> Optional[str]: · line 36

Quoted 10 lines
scripts/qapi/main.py +4 -5
-    :param unmask: Expose non-ABI names through introspection?
-    :param builtins: Generate code for built-in types?
+    if "." not in path:
+        print(f"Missing qualified module path in '{path}'", file=sys.stderr)
+        sys.exit(1)
 
-    :raise QAPIError: On failures.
-    """
-    assert invalid_prefix_char(prefix) is None
+    module_path, _, class_name = path.rpartition('.')

I'd like to tweak this to

module_path, dot, class_name = path.rpartition('.') if not dot:
print(f"argument of -B must be of the form MODULE.CLASS",
file=sys.stderr)
sys.exit(1)

scripts/qapi/main.py def invalid_prefix_char(prefix: str) -> Optional[str]: · line 39

Quoted 3 lines
scripts/qapi/main.py +3 -0
+    try:
+        mod = import_module(module_path)
+    except Exception as ex:

pylint complains:

scripts/qapi/main.py:39:11: W0718: Catching too general exception Exception (broad-exception-caught)

I can't see offhand what exception(s) we're supposed to catch here, so let's not worry about this now.

scripts/qapi/main.py def invalid_prefix_char(prefix: str) -> Optional[str]: · line 44

Quoted 5 lines
scripts/qapi/main.py +5 -0
+        print(f"Unable to import '{module_path}': {ex}", file=sys.stderr)
+        sys.exit(1)
+
+    if not hasattr(mod, class_name):
+        print(f"Module '{module_path}' has no class '{class_name}'", file=sys.stderr)

pycodestyle-3 complains:

    scripts/qapi/main.py:44:80: E501 line too long (85 > 79 characters)

Let's break the line after the comma, and also start the error message in lower case for consistency with error messages elsewhere.

scripts/qapi/main.py def invalid_prefix_char(prefix: str) -> Optional[str]: · line 46

Quoted 2 lines
scripts/qapi/main.py +2 -0
+        sys.exit(1)
+    klass = getattr(mod, class_name)

Alternatively

try:
klass = getattr(mod, class_name)
except AttributeError:
print(f"module '{module_path}' has no class '{class_name}'",
file=sys.stderr)
sys.exit(1)

Admittedly a matter of taste. I tend to avoid the

if frobnicate would fail:
error out
frobnicate

pattern when practical.

scripts/qapi/main.py def invalid_prefix_char(prefix: str) -> Optional[str]:

Quoted 7 lines
scripts/qapi/main.py +6 -0
+
+    try:
+        backend = klass()
 
+
+        if not isinstance(backend, QAPIBackend):
+            print(f"Backend '{path}' must be an instance of QAPIBackend", file=sys.stderr)

Likewise.

scripts/qapi/main.py def invalid_prefix_char(prefix: str) -> Optional[str]: · line 56

Quoted 11 lines
scripts/qapi/main.py +3 -7
+            sys.exit(1)
 
-    schema = QAPISchema(schema_file)
-    gen_types(schema, output_dir, prefix, builtins)
-    gen_features(schema, output_dir, prefix)
-    gen_visit(schema, output_dir, prefix, builtins)
-    gen_commands(schema, output_dir, prefix, gen_tracing)
-    gen_events(schema, output_dir, prefix)
-    gen_introspect(schema, output_dir, prefix, unmask)
+        return backend
+    except Exception as ex:

Likewise too general exception.

I'd like to shrink the try block and reduce the nesting:

try:
backend = klass()
except Exception as ex:
print(f"Backend '{path}' cannot be instantiated: {ex}", file=sys.stderr) sys.exit(1)

if not isinstance(backend, QAPIBackend):
print(f"Backend '{path}' must be an instance of QAPIBackend", file=sys.stderr) sys.exit(1)

return backend

scripts/qapi/main.py def invalid_prefix_char(prefix: str) -> Optional[str]: · line 57

Quoted 1 line
scripts/qapi/main.py +1 -0
+        print(f"Backend '{path}' cannot be instantiated: {ex}", file=sys.stderr)

Likewise line too long.

Quoted 24 lines
scripts/qapi/main.py +1 -0
+        sys.exit(1)
 
 

def main() -> int:

scripts/qapi/main.py +3 -6
@@ -77,6 +78,8 @@ def main() -> int:
     parser.add_argument('-u', '--unmask-non-abi-names', action='store_true',
                         dest='unmask',
                         help="expose non-ABI names in introspection")
+    parser.add_argument('-B', '--backend', default=None,
+                        help="Python module name for code generator")
 
     # Option --suppress-tracing exists so we can avoid solving build system
     # problems.  TODO Drop it when we no longer need it.
@@ -93,12 +96,14 @@ def main() -> int:
         return 1
 
     try:
-        generate(args.schema,
-                 output_dir=args.output_dir,
-                 prefix=args.prefix,
-                 unmask=args.unmask,
-                 builtins=args.builtins,
-                 gen_tracing=not args.suppress_tracing)
+        schema = QAPISchema(args.schema)

scripts/qapi/main.py def main() -> int: · line 109

Quoted 10 lines
scripts/qapi/main.py +7 -0
+        backend = create_backend(args.backend)
+        backend.generate(schema,
+                         output_dir=args.output_dir,
+                         prefix=args.prefix,
+                         unmask=args.unmask,
+                         builtins=args.builtins,
+                         gen_tracing=not args.suppress_tracing)
     except QAPIError as err:
         print(err, file=sys.stderr)
         return 1

Error reporting test cases:

$ python3 scripts/qapi-gen.py -o /tmp/$$ -b -B nonexistent qapi/qapi-schema.json 
argument of -B must be of the form MODULE.CLASS
$ python3 scripts/qapi-gen.py -o /tmp/$$ -b -B qapi.backend.foo qapi/qapi-schema.json 
module 'qapi.backend' has no class 'foo'
$ python3 scripts/qapi-gen.py -o /tmp/$$ -b -B nonexistent qapi/qapi-schema.json 
$ python3 scripts/qapi-gen.py -o /tmp/$$ -b -B nonexistent qapi/qapi-schema.json 
argument of -B must be of the form MODULE.CLASS
$ python3 scripts/qapi-gen.py -o /tmp/$$ -b -B nonexistent.foo qapi/qapi-schema.json 
unable to import 'nonexistent': No module named 'nonexistent'
$ python3 scripts/qapi-gen.py -o /tmp/$$ -b -B qapi.backend.foo qapi/qapi-schema.json 
module 'qapi.backend' has no class 'foo'
$ python3 scripts/qapi-gen.py -o /tmp/$$ -b -B qapi.backend.QAPIBackend qapi/qapi-schema.json 
backend 'qapi.backend.QAPIBackend' cannot be instantiated: Can't instantiate abstract class QAPIBackend without an implementation for abstract method 'generate'
$ python3 scripts/qapi-gen.py -o /tmp/$$ -b -B qapi.common.Indentation qapi/qapi-schema.json 
backend 'qapi.common.Indentation' must be an instance of QAPIBackend

Good enough.

No need to respin, I can make the tweaks I suggested myself. Feel free to challenge my suggestions, of course.

#2 lore raw
Quoted 51 lines · Markus Armbruster
Quoted 5 lines · Daniel P. Berrangé

The 'qapi.backend.QAPIBackend' class defines an API contract for code generators. The current generator is put into a new class 'qapi.backend.QAPICBackend' and made to be the default impl.

A custom generator can be requested using the '-k' arg which takes a

Missed an instance of -k. Can fix this myself.

Quoted 28 lines

fully qualified python class name

qapi-gen.py -B the.python.module.QAPIMyBackend

This allows out of tree code to use the QAPI generator infrastructure to create new language bindings for QAPI schemas. This has the caveat that the QAPI generator APIs are not guaranteed stable, so consumers of this feature may have to update their code to be compatible with future QEMU releases.

---

scripts/qapi/main.py +3 -11
-def generate(schema_file: str,
-             output_dir: str,
-             prefix: str,
-             unmask: bool = False,
-             builtins: bool = False,
-             gen_tracing: bool = False) -> None:
-    """
-    Generate C code for the given schema into the target directory.
+def create_backend(path: str) -> QAPIBackend:
+    if path is None:
+        return QAPICBackend()
 
-    :param schema_file: The primary QAPI schema file.
-    :param output_dir: The output directory to store generated code.
-    :param prefix: Optional C-code prefix for symbol names.

scripts/qapi/main.py def invalid_prefix_char(prefix: str) -> Optional[str]: · line 36

Quoted 10 lines
scripts/qapi/main.py +4 -5
-    :param unmask: Expose non-ABI names through introspection?
-    :param builtins: Generate code for built-in types?
+    if "." not in path:
+        print(f"Missing qualified module path in '{path}'", file=sys.stderr)
+        sys.exit(1)
 
-    :raise QAPIError: On failures.
-    """
-    assert invalid_prefix_char(prefix) is None
+    module_path, _, class_name = path.rpartition('.')

I'd like to tweak this to

Quoted 5 lines

module_path, dot, class_name = path.rpartition('.') if not dot:
print(f"argument of -B must be of the form MODULE.CLASS",
file=sys.stderr)
sys.exit(1)

Yep, sure thing.

Quoted 11 lines

scripts/qapi/main.py def invalid_prefix_char(prefix: str) -> Optional[str]: · line 39

Quoted 4 lines
scripts/qapi/main.py +3 -0
+    try:
+        mod = import_module(module_path)
+    except Exception as ex:

pylint complains:

scripts/qapi/main.py:39:11: W0718: Catching too general exception Exception (broad-exception-caught)

I can't see offhand what exception(s) we're supposed to catch here, so let's not worry about this now.

Yeah, I was concerned that by putting specialized exception classes here, we would miss some possible scenarios, and IMHO the completeness of catching Exception is better than the technical purity of pylint's complaint.

Quoted 22 lines

scripts/qapi/main.py def invalid_prefix_char(prefix: str) -> Optional[str]: · line 44

Quoted 2 lines
scripts/qapi/main.py +2 -0
+    if not hasattr(mod, class_name):
+        print(f"Module '{module_path}' has no class '{class_name}'", file=sys.stderr)

pycodestyle-3 complains:

    scripts/qapi/main.py:44:80: E501 line too long (85 > 79 characters)

Let's break the line after the comma, and also start the error message in lower case for consistency with error messages elsewhere.

Review

Quoted 2 lines
(quoted diff) +2 -0
+        sys.exit(1)
+    klass = getattr(mod, class_name)

Alternatively

try:
klass = getattr(mod, class_name)
except AttributeError:
print(f"module '{module_path}' has no class '{class_name}'",
file=sys.stderr)
sys.exit(1)

Quoted 7 lines

Admittedly a matter of taste. I tend to avoid the

if frobnicate would fail:
error out
frobnicate

pattern when practical.

I guess I tend to avoid using exception catching for such flow control, but I don't mind that much either way.

Quoted 20 lines

scripts/qapi/main.py def invalid_prefix_char(prefix: str) -> Optional[str]: · line 56

Quoted 9 lines
scripts/qapi/main.py +2 -7
-    schema = QAPISchema(schema_file)
-    gen_types(schema, output_dir, prefix, builtins)
-    gen_features(schema, output_dir, prefix)
-    gen_visit(schema, output_dir, prefix, builtins)
-    gen_commands(schema, output_dir, prefix, gen_tracing)
-    gen_events(schema, output_dir, prefix)
-    gen_introspect(schema, output_dir, prefix, unmask)
+        return backend
+    except Exception as ex:

Likewise too general exception.

I'd like to shrink the try block and reduce the nesting:

try:
backend = klass()
except Exception as ex:
print(f"Backend '{path}' cannot be instantiated: {ex}", file=sys.stderr) sys.exit(1)

Quoted 5 lines

if not isinstance(backend, QAPIBackend):
print(f"Backend '{path}' must be an instance of QAPIBackend", file=sys.stderr) sys.exit(1)

return backend

Sure, fine with me.

Quoted 6 lines

Good enough.

No need to respin, I can make the tweaks I suggested myself. Feel free to challenge my suggestions, of course.

Thank you, it is all fine.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw
Quoted 69 lines · Daniel P. Berrangé
Quoted 51 lines · Markus Armbruster
Quoted 5 lines · Daniel P. Berrangé

The 'qapi.backend.QAPIBackend' class defines an API contract for code generators. The current generator is put into a new class 'qapi.backend.QAPICBackend' and made to be the default impl.

A custom generator can be requested using the '-k' arg which takes a

Missed an instance of -k. Can fix this myself.

Quoted 28 lines

fully qualified python class name

qapi-gen.py -B the.python.module.QAPIMyBackend

This allows out of tree code to use the QAPI generator infrastructure to create new language bindings for QAPI schemas. This has the caveat that the QAPI generator APIs are not guaranteed stable, so consumers of this feature may have to update their code to be compatible with future QEMU releases.

---

scripts/qapi/main.py +3 -11
-def generate(schema_file: str,
-             output_dir: str,
-             prefix: str,
-             unmask: bool = False,
-             builtins: bool = False,
-             gen_tracing: bool = False) -> None:
-    """
-    Generate C code for the given schema into the target directory.
+def create_backend(path: str) -> QAPIBackend:
+    if path is None:
+        return QAPICBackend()
 
-    :param schema_file: The primary QAPI schema file.
-    :param output_dir: The output directory to store generated code.
-    :param prefix: Optional C-code prefix for symbol names.

scripts/qapi/main.py def invalid_prefix_char(prefix: str) -> Optional[str]: · line 36

Quoted 10 lines
scripts/qapi/main.py +4 -5
-    :param unmask: Expose non-ABI names through introspection?
-    :param builtins: Generate code for built-in types?
+    if "." not in path:
+        print(f"Missing qualified module path in '{path}'", file=sys.stderr)
+        sys.exit(1)
 
-    :raise QAPIError: On failures.
-    """
-    assert invalid_prefix_char(prefix) is None
+    module_path, _, class_name = path.rpartition('.')

I'd like to tweak this to

Quoted 5 lines

module_path, dot, class_name = path.rpartition('.') if not dot:
print(f"argument of -B must be of the form MODULE.CLASS",
file=sys.stderr)
sys.exit(1)

Yep, sure thing.

Quoted 9 lines

scripts/qapi/main.py def invalid_prefix_char(prefix: str) -> Optional[str]: · line 39

Quoted 4 lines
scripts/qapi/main.py +3 -0
+    try:
+        mod = import_module(module_path)
+    except Exception as ex:

pylint complains:

scripts/qapi/main.py:39:11: W0718: Catching too general exception Exception (broad-exception-caught)

Quoted 7 lines
Quoted 2 lines

I can't see offhand what exception(s) we're supposed to catch here, so let's not worry about this now.

Yeah, I was concerned that by putting specialized exception classes here, we would miss some possible scenarios, and IMHO the completeness of catching Exception is better than the technical purity of pylint's complaint.

Cleaner code would require a stronger contract.

We'll be just fine.

Quoted 24 lines
Quoted 24 lines

scripts/qapi/main.py def invalid_prefix_char(prefix: str) -> Optional[str]: · line 44

Quoted 2 lines
scripts/qapi/main.py +2 -0
+    if not hasattr(mod, class_name):
+        print(f"Module '{module_path}' has no class '{class_name}'", file=sys.stderr)

pycodestyle-3 complains:

    scripts/qapi/main.py:44:80: E501 line too long (85 > 79 characters)

Let's break the line after the comma, and also start the error message in lower case for consistency with error messages elsewhere.

Review

Quoted 2 lines
(quoted diff) +2 -0
+        sys.exit(1)
+    klass = getattr(mod, class_name)

Alternatively

try:
klass = getattr(mod, class_name)
except AttributeError:
print(f"module '{module_path}' has no class '{class_name}'",
file=sys.stderr)
sys.exit(1)

Admittedly a matter of taste. I tend to avoid the

Quoted 8 lines
Quoted 5 lines

if frobnicate would fail:
error out
frobnicate

pattern when practical.

I guess I tend to avoid using exception catching for such flow control, but I don't mind that much either way.

I'm not a fan of using exceptions for mundane failures, but it's how Python rolls.

Quoted 29 lines
Quoted 20 lines

scripts/qapi/main.py def invalid_prefix_char(prefix: str) -> Optional[str]: · line 56

Quoted 9 lines
scripts/qapi/main.py +2 -7
-    schema = QAPISchema(schema_file)
-    gen_types(schema, output_dir, prefix, builtins)
-    gen_features(schema, output_dir, prefix)
-    gen_visit(schema, output_dir, prefix, builtins)
-    gen_commands(schema, output_dir, prefix, gen_tracing)
-    gen_events(schema, output_dir, prefix)
-    gen_introspect(schema, output_dir, prefix, unmask)
+        return backend
+    except Exception as ex:

Likewise too general exception.

I'd like to shrink the try block and reduce the nesting:

try:
backend = klass()
except Exception as ex:
print(f"Backend '{path}' cannot be instantiated: {ex}", file=sys.stderr) sys.exit(1)

Quoted 5 lines

if not isinstance(backend, QAPIBackend):
print(f"Backend '{path}' must be an instance of QAPIBackend", file=sys.stderr) sys.exit(1)

return backend

Sure, fine with me.

Quoted 8 lines
Quoted 6 lines

Good enough.

No need to respin, I can make the tweaks I suggested myself. Feel free to challenge my suggestions, of course.

Thank you, it is all fine.

Cool, expect it in my next pull request. Thanks!

#4 lore raw
In reply to Markus Armbruster
Quoted 21 lines · Markus Armbruster
Quoted 5 lines · Daniel P. Berrangé

The 'qapi.backend.QAPIBackend' class defines an API contract for code generators. The current generator is put into a new class 'qapi.backend.QAPICBackend' and made to be the default impl.

A custom generator can be requested using the '-k' arg which takes a

Missed an instance of -k. Can fix this myself.

Quoted 11 lines

fully qualified python class name

qapi-gen.py -B the.python.module.QAPIMyBackend

This allows out of tree code to use the QAPI generator infrastructure to create new language bindings for QAPI schemas. This has the caveat that the QAPI generator APIs are not guaranteed stable, so consumers of this feature may have to update their code to be compatible with future QEMU releases.

[...]

Quoted 4 lines
Quoted 4 lines
scripts/qapi/main.py +0 -0
index 324081b9fc..8a8b1e0121 100644

[...]

Quoted 32 lines
Quoted 19 lines
scripts/qapi/main.py +3 -11
@@ -29,32 +25,37 @@ def invalid_prefix_char(prefix: str) -> Optional[str]:
     return None
 
 
-def generate(schema_file: str,
-             output_dir: str,
-             prefix: str,
-             unmask: bool = False,
-             builtins: bool = False,
-             gen_tracing: bool = False) -> None:
-    """
-    Generate C code for the given schema into the target directory.
+def create_backend(path: str) -> QAPIBackend:
+    if path is None:
+        return QAPICBackend()
 
-    :param schema_file: The primary QAPI schema file.
-    :param output_dir: The output directory to store generated code.
-    :param prefix: Optional C-code prefix for symbol names.

scripts/qapi/main.py def invalid_prefix_char(prefix: str) -> Optional[str]: · line 36

Quoted 10 lines
scripts/qapi/main.py +4 -5
-    :param unmask: Expose non-ABI names through introspection?
-    :param builtins: Generate code for built-in types?
+    if "." not in path:
+        print(f"Missing qualified module path in '{path}'", file=sys.stderr)
+        sys.exit(1)
 
-    :raise QAPIError: On failures.
-    """
-    assert invalid_prefix_char(prefix) is None
+    module_path, _, class_name = path.rpartition('.')

I'd like to tweak this to

Quoted 4 lines

module_path, dot, class_name = path.rpartition('.') if not dot:
print(f"argument of -B must be of the form MODULE.CLASS",
file=sys.stderr)

This bothers flake8:

scripts/qapi/main.py:34:15: F541 f-string is missing placeholders

I'll make it a plain string instead.

Quoted 2 lines
           sys.exit(1)

[...]