gdbus-codegen: Add an extension system which allows hooking codegen

The extension points are chosen by what libdex needs to generate future
based method calls.
This commit is contained in:
Sebastian Wick
2025-09-16 21:42:28 +02:00
committed by Philip Withnall
parent 07ba38449e
commit 4405fe5fc8
3 changed files with 106 additions and 0 deletions

View File

@@ -26,6 +26,8 @@
import argparse
import os
import sys
import importlib.util
import traceback
from contextlib import contextmanager
from . import config
@@ -38,6 +40,16 @@ from . import codegen_rst
from .utils import print_error, print_warning
def import_from_path(module_name, file_path):
spec = importlib.util.spec_from_file_location(module_name, file_path)
if not spec:
raise Exception("Not a Python file")
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
return module
def find_arg(arg_list, arg_name):
for a in arg_list:
if a.name == arg_name:
@@ -274,6 +286,12 @@ def codegen_main():
help="Additional define required for decorator specified by "
"--symbol-decorator",
)
arg_parser.add_argument(
"--extension-path",
metavar="EXTENSION_PATH",
default="",
help="Path to a gdbus-codegen Python extension file (unstable API)",
)
group = arg_parser.add_mutually_exclusive_group()
group.add_argument(
@@ -305,6 +323,20 @@ def codegen_main():
args = arg_parser.parse_args()
codegen_ext = {}
if args.extension_path:
try:
codegen_ext = import_from_path("GDBusCodegenExt", args.extension_path)
codegen_ext.init(
args,
{
"version": 1,
},
)
except Exception:
print_warning(traceback.format_exc())
print_error("Loading extension {} failed".format(args.extension_path))
if len(args.xml_files) > 0:
print_warning(
'The "--xml-files" option is deprecated; use positional arguments instead'
@@ -479,6 +511,7 @@ def codegen_main():
args.symbol_decorator,
args.symbol_decorator_header,
outfile,
codegen_ext,
)
gen.generate()
@@ -494,6 +527,7 @@ def codegen_main():
glib_min_required,
args.symbol_decorator_define,
outfile,
codegen_ext,
)
gen.generate()