mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-11-17 07:58:21 +01:00
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:
committed by
Philip Withnall
parent
07ba38449e
commit
4405fe5fc8
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user